org.eclipse.wst.jsdt.core.dom

## Class FunctionDeclaration

- java.lang.Object
- - org.eclipse.wst.jsdt.core.dom.ASTNode
  - - org.eclipse.wst.jsdt.core.dom.ProgramElement
    - - org.eclipse.wst.jsdt.core.dom.BodyDeclaration
      - - org.eclipse.wst.jsdt.core.dom.FunctionDeclaration

    public class FunctionDeclaration
    extends BodyDeclaration

Method declaration AST node type. A method declaration is the union of a method declaration and a constructor declaration. For JLS2:

     FunctionDeclaration:
        [ jsdoc ] { Modifier } ( Type | void ) Identifier (
            [ FormalParameter
                         { , FormalParameter } ] ) {[ ] }
            [ throws TypeName { , TypeName } ] ( Block | ; )
     ConstructorDeclaration:
        [ jsdoc ] { Modifier } Identifier (
                      [ FormalParameter
                             { , FormalParameter } ] )
            [throws TypeName { , TypeName } ] Block

For JLS3, type parameters and reified modifiers were added:

     FunctionDeclaration:
        [ jsdoc ] { ExtendedModifier }
                      [ < TypeParameter { , TypeParameter } > ]
            ( Type | void ) Identifier (
            [ FormalParameter
                         { , FormalParameter } ] ) {[ ] }
            [ throws TypeName { , TypeName } ] ( Block | ; )
     ConstructorDeclaration:
        [ jsdoc ] { ExtendedModifier }
                      [ < TypeParameter { , TypeParameter } > ]
            Identifier (
                      [ FormalParameter
                             { , FormalParameter } ] )
            [throws TypeName { , TypeName } ] Block

When a jsdoc comment is present, the source range begins with the first character of the "/**" comment delimiter. When there is no jsdoc comment, the source range begins with the first character of the first modifier keyword (if modifiers), or the first character of the "<" token (method, no modifiers, type parameters), or the first character of the return type (method, no modifiers, no type parameters), or the first character of the identifier (constructor, no modifiers). The source range extends through the last character of the ";" token (if no body), or the last character of the block (if body).

Provisional API: This class/interface is part of an interim API that is still under development and expected to change significantly before reaching stability. It is being made available at this early stage to solicit feedback from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken (repeatedly) as the API evolves.

### Field Summary

Fields
`static ChildPropertyDescriptor` `BODY_PROPERTY` The "body" structural property of this node type.
`static SimplePropertyDescriptor` `CONSTRUCTOR_PROPERTY` The "constructor" structural property of this node type.
`static SimplePropertyDescriptor` `EXTRA_DIMENSIONS_PROPERTY` The "extraDimensions" structural property of this node type.
`static SimplePropertyDescriptor` `GENERATOR_PROPERTY` The "generator" property
`static ChildPropertyDescriptor` `JAVADOC_PROPERTY` The "javadoc" structural property of this node type.
`static ChildPropertyDescriptor` `METHOD_NAME_PROPERTY` The "methodName" structural property of this node type.
`static SimplePropertyDescriptor` `MODIFIERS_PROPERTY` The "modifiers" structural property of this node type (JLS2 API only).
`static ChildListPropertyDescriptor` `MODIFIERS2_PROPERTY` The "modifiers" structural property of this node type (added in JLS3 API).
`static ChildPropertyDescriptor` `NAME_PROPERTY` Deprecated.
`static ChildListPropertyDescriptor` `PARAMETERS_PROPERTY` The "parameters" structural property of this node type).
`static ChildPropertyDescriptor` `RETURN_TYPE_PROPERTY` The "returnType" structural property of this node type (JLS2 API only).
`static ChildPropertyDescriptor` `RETURN_TYPE2_PROPERTY` The "returnType2" structural property of this node type (added in JLS3 API).
`static ChildListPropertyDescriptor` `THROWN_EXCEPTIONS_PROPERTY` The "thrownExceptions" structural property of this node type).

### Fields inherited from class org.eclipse.wst.jsdt.core.dom.ASTNode

`ANONYMOUS_CLASS_DECLARATION, ARRAY_ACCESS, ARRAY_CREATION, ARRAY_INITIALIZER, ARRAY_NAME, ARRAY_TYPE, ARROW_FUNCTION_EXPRESSION, ASSIGNMENT, ASSIGNMENT_NAME, BLOCK, BLOCK_COMMENT, BOOLEAN_LITERAL, BREAK_STATEMENT, CATCH_CLAUSE, CHARACTER_LITERAL, CLASS_INSTANCE_CREATION, CONDITIONAL_EXPRESSION, CONSTRUCTOR_INVOCATION, CONTINUE_STATEMENT, DEBUGGER_STATEMENT, DO_STATEMENT, EMPTY_EXPRESSION, EMPTY_STATEMENT, ENHANCED_FOR_STATEMENT, EXPORT_DECLARATION, EXPRESSION_STATEMENT, FIELD_ACCESS, FIELD_DECLARATION, FOR_IN_STATEMENT, FOR_OF_STATEMENT, FOR_STATEMENT, FUNCTION_DECLARATION, FUNCTION_DECLARATION_STATEMENT, FUNCTION_EXPRESSION, FUNCTION_INVOCATION, FUNCTION_REF, FUNCTION_REF_PARAMETER, IF_STATEMENT, IMPORT_DECLARATION, INFERRED_TYPE, INFIX_EXPRESSION, INITIALIZER, INSTANCEOF_EXPRESSION, JAVASCRIPT_UNIT, JSDOC, LABELED_STATEMENT, LINE_COMMENT, LIST_EXPRESSION, MALFORMED, MEMBER_REF, META_PROPERTY, MODIFIER, MODULE_SPECIFIER, NULL_LITERAL, NUMBER_LITERAL, OBJECT_LITERAL, OBJECT_LITERAL_FIELD, OBJECT_NAME, ORIGINAL, PACKAGE_DECLARATION, PARENTHESIZED_EXPRESSION, POSTFIX_EXPRESSION, PREFIX_EXPRESSION, PRIMITIVE_TYPE, PROTECT, QUALIFIED_NAME, QUALIFIED_TYPE, RECOVERED, REGULAR_EXPRESSION_LITERAL, REST_ELEMENT_NAME, RETURN_STATEMENT, SIMPLE_NAME, SIMPLE_TYPE, SINGLE_VARIABLE_DECLARATION, SPREAD_ELEMENT, STRING_LITERAL, SUPER_CONSTRUCTOR_INVOCATION, SUPER_FIELD_ACCESS, SUPER_METHOD_INVOCATION, SWITCH_CASE, SWITCH_STATEMENT, TAG_ELEMENT, TEMPLATE_ELEMENT, TEMPLATE_LITERAL, TEXT_ELEMENT, THIS_EXPRESSION, THROW_STATEMENT, TRY_STATEMENT, TYPE_DECLARATION, TYPE_DECLARATION_EXPRESSION, TYPE_DECLARATION_STATEMENT, TYPE_LITERAL, UNDEFINED_LITERAL, VARIABLE_DECLARATION_EXPRESSION, VARIABLE_DECLARATION_FRAGMENT, VARIABLE_DECLARATION_STATEMENT, WHILE_STATEMENT, WITH_STATEMENT, YIELD_EXPRESSION`

### Method Summary

All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods
`Block` `getBody()` Returns the body of this method declaration, or `null` if this method has no body.
`int` `getExtraDimensions()` Returns the number of extra array dimensions over and above the explicitly-specified return type.
`Expression` `getMethodName()`
`SimpleName` `getName()` Deprecated. use `getMethodName()`
`Type` `getReturnType()` Deprecated. In the JLS3 API, this method is replaced by `getReturnType2()`, which may return `null`.
`Type` `getReturnType2()` Returns the return type of the method declared in this method declaration, exclusive of any extra array dimensions (added in JLS3 API).
`boolean` `isConstructor()` Returns whether this declaration declares a constructor or a method.
`boolean` `isGenerator()`
`boolean` `isVarargs()` Returns whether this method declaration declares a variable arity method (added in JLS3 API).
`java.util.List` `parameters()` Returns the live ordered list of method parameter declarations for this method declaration.
`static java.util.List` `propertyDescriptors(int apiLevel)` Returns a list of structural property descriptors for this node type.
`IFunctionBinding` `resolveBinding()` Resolves and returns the binding for the method or constructor declared in this method or constructor declaration.
`void` `setBody(Block body)` Sets or clears the body of this method declaration.
`void` `setConstructor(boolean isConstructor)` Sets whether this declaration declares a constructor or a method.
`void` `setExtraDimensions(int dimensions)` Sets the number of extra array dimensions over and above the explicitly-specified return type.
`void` `setGenerator(boolean isGenerator)`
`void` `setMethodName(Expression methodName)`
`void` `setName(SimpleName methodName)` Deprecated. use `setMethodName(Expression)`
`void` `setReturnType(Type type)` Deprecated. In the JLS3 API, this method is replaced by `setReturnType2(Type)`, which accepts `null`.
`void` `setReturnType2(Type type)` Sets the return type of the method declared in this method declaration to the given type, exclusive of any extra array dimensions (added in JLS3 API).
`java.util.List` `thrownExceptions()` Returns the live ordered list of thrown exception names in this method declaration.

### Methods inherited from class org.eclipse.wst.jsdt.core.dom.BodyDeclaration

`getJavadoc, getJavadocProperty, getModifiers, getModifiersProperty, modifiers, setJavadoc, setModifiers`

### Methods inherited from class org.eclipse.wst.jsdt.core.dom.ASTNode

`accept, copySubtree, copySubtrees, delete, equals, getAST, getBodyChild, getFlags, getLength, getLocationInParent, getNodeType, getParent, getProperty, getRoot, getStartPosition, getStructuralProperty, hashCode, nodeClassForType, properties, setFlags, setProperty, setSourceRange, setStructuralProperty, structuralPropertiesForType, subtreeBytes, subtreeMatch, toString`

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

### Field Detail

#### JAVADOC_PROPERTY

    public static final ChildPropertyDescriptor JAVADOC_PROPERTY

The "javadoc" structural property of this node type.

#### MODIFIERS_PROPERTY

    public static final SimplePropertyDescriptor MODIFIERS_PROPERTY

The "modifiers" structural property of this node type (JLS2 API only).

#### MODIFIERS2_PROPERTY

    public static final ChildListPropertyDescriptor MODIFIERS2_PROPERTY

The "modifiers" structural property of this node type (added in JLS3 API).

#### CONSTRUCTOR_PROPERTY

    public static final SimplePropertyDescriptor CONSTRUCTOR_PROPERTY

The "constructor" structural property of this node type.

#### NAME_PROPERTY

    @Deprecated
    public static final ChildPropertyDescriptor NAME_PROPERTY

Deprecated.
The "name" structural property of this node type.

#### METHOD_NAME_PROPERTY

    public static final ChildPropertyDescriptor METHOD_NAME_PROPERTY

The "methodName" structural property of this node type.

Since:
  2.0

#### RETURN_TYPE_PROPERTY

    public static final ChildPropertyDescriptor RETURN_TYPE_PROPERTY

The "returnType" structural property of this node type (JLS2 API only).

#### RETURN_TYPE2_PROPERTY

    public static final ChildPropertyDescriptor RETURN_TYPE2_PROPERTY

The "returnType2" structural property of this node type (added in JLS3 API).

#### EXTRA_DIMENSIONS_PROPERTY

    public static final SimplePropertyDescriptor EXTRA_DIMENSIONS_PROPERTY

The "extraDimensions" structural property of this node type.

#### PARAMETERS_PROPERTY

    public static final ChildListPropertyDescriptor PARAMETERS_PROPERTY

The "parameters" structural property of this node type).

#### THROWN_EXCEPTIONS_PROPERTY

    public static final ChildListPropertyDescriptor THROWN_EXCEPTIONS_PROPERTY

The "thrownExceptions" structural property of this node type).

#### BODY_PROPERTY

    public static final ChildPropertyDescriptor BODY_PROPERTY

The "body" structural property of this node type.

#### GENERATOR_PROPERTY

    public static final SimplePropertyDescriptor GENERATOR_PROPERTY

The "generator" property

Since:
  2.0

### Method Detail

#### propertyDescriptors

    public static java.util.List propertyDescriptors(int apiLevel)

Returns a list of structural property descriptors for this node type. Clients must not modify the result.

Parameters:
  `apiLevel` - the API level; one of the AST.JLS* constants
Returns:
  a list of property descriptors (element type: `StructuralPropertyDescriptor`)

#### isConstructor

    public boolean isConstructor()

Returns whether this declaration declares a constructor or a method.

Returns:
  `true` if this is a constructor declaration, and `false` if this is a method declaration

#### setConstructor

    public void setConstructor(boolean isConstructor)

Sets whether this declaration declares a constructor or a method.

Parameters:
  `isConstructor` - `true` for a constructor declaration, and `false` for a method declaration

#### isGenerator

    public boolean isGenerator()

Since:
  2.0

#### setGenerator

    public void setGenerator(boolean isGenerator)

Since:
  2.0

#### getName

    @Deprecated
    public SimpleName getName()

Deprecated. use `getMethodName()`
Returns the name of the method declared in this method declaration. For a constructor declaration, this should be the same as the name of the class.

Returns:
  the method name node

#### setName

    @Deprecated
    public void setName(SimpleName methodName)

Deprecated. use `setMethodName(Expression)`
Sets the name of the method declared in this method declaration to the given name. For a constructor declaration, this should be the same as the name of the class.

Parameters:
  `simpleName` - the new method name
Throws:
  `java.lang.IllegalArgumentException` - if:
  - the node belongs to a different AST
  - the node already has a parent

#### getMethodName

    public Expression getMethodName()

Since:
  2.0

#### setMethodName

    public void setMethodName(Expression methodName)

Since:
  2.0

#### parameters

    public java.util.List parameters()

Returns the live ordered list of method parameter declarations for this method declaration.

Returns:
  the live list of method parameter declarations (element type: `SingleVariableDeclaration`)

#### isVarargs

    public boolean isVarargs()

Returns whether this method declaration declares a variable arity method (added in JLS3 API). The convenience method checks whether the last parameter is so marked.

Returns:
  `true` if this is a variable arity method declaration, and `false` otherwise
Throws:
  `java.lang.UnsupportedOperationException` - if this operation is used in a JLS2 AST
See Also:
  `SingleVariableDeclaration.isVarargs()`

#### thrownExceptions

    public java.util.List thrownExceptions()

Returns the live ordered list of thrown exception names in this method declaration.

Returns:
  the live list of exception names (element type: `Name`)

#### getReturnType

    @Deprecated
    public Type getReturnType()

Deprecated. In the JLS3 API, this method is replaced by `getReturnType2()`, which may return `null`.
Returns the return type of the method declared in this method declaration, exclusive of any extra array dimensions (JLS2 API only). This is one of the few places where the void type is meaningful.

Note that this child is not relevant for constructor declarations (although, it does still figure in subtree equality comparisons and visits), and is devoid of the binding information ordinarily available.

Returns:
  the return type, possibly the void primitive type
Throws:
  `java.lang.UnsupportedOperationException` - if this operation is used in an AST later than JLS2

#### setReturnType

    @Deprecated
    public void setReturnType(Type type)

Deprecated. In the JLS3 API, this method is replaced by `setReturnType2(Type)`, which accepts `null`.
Sets the return type of the method declared in this method declaration to the given type, exclusive of any extra array dimensions (JLS2 API only). This is one of the few places where the void type is meaningful.

Note that this child is not relevant for constructor declarations (although it does still figure in subtree equality comparisons and visits).

Parameters:
  `type` - the new return type, possibly the void primitive type
Throws:
  `java.lang.IllegalArgumentException` - if:
  - the node belongs to a different AST
  - the node already has a parent
  `java.lang.UnsupportedOperationException` - if this operation is used in an AST later than JLS2

#### getReturnType2

    public Type getReturnType2()

Returns the return type of the method declared in this method declaration, exclusive of any extra array dimensions (added in JLS3 API). This is one of the few places where the void type is meaningful.

Note that this child is not relevant for constructor declarations (although, if present, it does still figure in subtree equality comparisons and visits), and is devoid of the binding information ordinarily available. In the JLS2 API, the return type is mandatory. In the JLS3 API, the return type is optional.

Returns:
  the return type, possibly the void primitive type, or `null` if none
Throws:
  `java.lang.UnsupportedOperationException` - if this operation is used in a JLS2 AST

#### setReturnType2

    public void setReturnType2(Type type)

Sets the return type of the method declared in this method declaration to the given type, exclusive of any extra array dimensions (added in JLS3 API). This is one of the few places where the void type is meaningful.

Note that this child is not relevant for constructor declarations (although it does still figure in subtree equality comparisons and visits). In the JLS2 API, the return type is mandatory. In the JLS3 API, the return type is optional.

Parameters:
  `type` - the new return type, possibly the void primitive type, or `null` if none
Throws:
  `java.lang.UnsupportedOperationException` - if this operation is used in a JLS2 AST
  `java.lang.IllegalArgumentException` - if:
  - the node belongs to a different AST
  - the node already has a parent

#### getExtraDimensions

    public int getExtraDimensions()

Returns the number of extra array dimensions over and above the explicitly-specified return type.

For example, `int foo()[][]` has a return type of `int` and two extra array dimensions; `int[][] foo()` has a return type of `int[][]` and zero extra array dimensions. The two constructs have different ASTs, even though there are really syntactic variants of the same method declaration.

Returns:
  the number of extra array dimensions

#### setExtraDimensions

    public void setExtraDimensions(int dimensions)

Sets the number of extra array dimensions over and above the explicitly-specified return type.

For example, `int foo()[][]` is rendered as a return type of `int` with two extra array dimensions; `int[][] foo()` is rendered as a return type of `int[][]` with zero extra array dimensions. The two constructs have different ASTs, even though there are really syntactic variants of the same method declaration.

Parameters:
  `dimensions` - the number of array dimensions
Throws:
  `java.lang.IllegalArgumentException` - if the number of dimensions is negative

#### getBody

    public Block getBody()

Returns the body of this method declaration, or `null` if this method has no body.

Note that there is a subtle difference between having no body and having an empty body ("{}").

Returns:
  the method body, or `null` if this method has no body

#### setBody

    public void setBody(Block body)

Sets or clears the body of this method declaration.

Note that there is a subtle difference between having no body (as in `"void foo();"`) and having an empty body (as in "void foo() {}"). Abstract methods, and methods declared in interfaces, have no body. Non-abstract methods, and all constructors, have a body.

Parameters:
  `body` - the block node, or `null` if there is none
Throws:
  `java.lang.IllegalArgumentException` - if:
  - the node belongs to a different AST
  - the node already has a parent
  - a cycle in would be created

#### resolveBinding

    public IFunctionBinding resolveBinding()

Resolves and returns the binding for the method or constructor declared in this method or constructor declaration.

Note that bindings are generally unavailable unless requested when the AST is being built.

Returns:
  the binding, or `null` if the binding cannot be resolved