Class MethodDeclaration
MethodDeclaration: [ Javadoc ] { ExtendedModifier } [ < TypeParameter { , TypeParameter } > ] ( Type | void ) Identifier ( [ ReceiverParameter , ] [ FormalParameter { , FormalParameter } ] ) { Dimension } [ throws Type { , Type } ] ( Block | ; ) ConstructorDeclaration: [ Javadoc ] { ExtendedModifier } [ < TypeParameter { , TypeParameter } > ] Identifier ( [ ReceiverParameter , ] [ FormalParameter { , FormalParameter } ] ) { Dimension } [ throws Type { , Type } ] ( Block | ; ) CompactConstructorDeclaration: [ Javadoc ] ExtendedModifier { ExtendedModifier} Identifier ( Block | ; )
The ReceiverParameter is represented as: Type [ SimpleName . ] this
The FormalParameter is represented by a SingleVariableDeclaration
.
When a Javadoc comment is present, the source range begins with the first character of the "/**" comment delimiter. When there is no Javadoc 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). The compact constructor must be declared public. (jls-8.10.5)
- Since:
- 2.0
- Restriction:
- This class is not intended to be instantiated by clients.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ChildPropertyDescriptor
The "body" structural property of this node type (child type:Block
).static final SimplePropertyDescriptor
The "compact constructor" structural property of record node type (type:Boolean
).static final SimplePropertyDescriptor
The "constructor" structural property of this node type (type:Boolean
).static final SimplePropertyDescriptor
Deprecated.static final ChildListPropertyDescriptor
The "extraDimensions2" structural property of this node type (element type:Dimension
) (added in JLS8 API).static final ChildPropertyDescriptor
The "javadoc" structural property of this node type (child type:Javadoc
).static final SimplePropertyDescriptor
Deprecated.In the JLS3 API, this property is replaced byMODIFIERS2_PROPERTY
.static final ChildListPropertyDescriptor
The "modifiers" structural property of this node type (element type:IExtendedModifier
) (added in JLS3 API).static final ChildPropertyDescriptor
The "name" structural property of this node type (child type:SimpleName
).static final ChildListPropertyDescriptor
The "parameters" structural property of this node type (element type:SingleVariableDeclaration
).static final ChildPropertyDescriptor
The "receiverQualifier" structural property of this node type (child type:SimpleName
) (added in JLS8 API).static final ChildPropertyDescriptor
The "receiverType" structural property of this node type (child type:Type
) (added in JLS8 API).static final ChildPropertyDescriptor
Deprecated.In the JLS3 API, this property is replaced byRETURN_TYPE2_PROPERTY
.static final ChildPropertyDescriptor
The "returnType2" structural property of this node type (child type:Type
) (added in JLS3 API).static final ChildListPropertyDescriptor
The "thrownExceptionTypes" structural property of this node type (element type:Type
) (added in JLS8 API).static final ChildListPropertyDescriptor
Deprecated.In JLS8 and later, useTHROWN_EXCEPTION_TYPES_PROPERTY
instead.static final ChildListPropertyDescriptor
The "typeParameters" structural property of this node type (element type:TypeParameter
) (added in JLS3 API).Fields inherited from class org.eclipse.jdt.core.dom.ASTNode
ANNOTATION_TYPE_DECLARATION, ANNOTATION_TYPE_MEMBER_DECLARATION, ANONYMOUS_CLASS_DECLARATION, ARRAY_ACCESS, ARRAY_CREATION, ARRAY_INITIALIZER, ARRAY_TYPE, ASSERT_STATEMENT, ASSIGNMENT, BLOCK, BLOCK_COMMENT, BOOLEAN_LITERAL, BREAK_STATEMENT, CASE_DEFAULT_EXPRESSION, CAST_EXPRESSION, CATCH_CLAUSE, CHARACTER_LITERAL, CLASS_INSTANCE_CREATION, COMPILATION_UNIT, CONDITIONAL_EXPRESSION, CONSTRUCTOR_INVOCATION, CONTINUE_STATEMENT, CREATION_REFERENCE, DIMENSION, DO_STATEMENT, EitherOr_MultiPattern, EMPTY_STATEMENT, ENHANCED_FOR_STATEMENT, ENUM_CONSTANT_DECLARATION, ENUM_DECLARATION, EXPORTS_DIRECTIVE, EXPRESSION_METHOD_REFERENCE, EXPRESSION_STATEMENT, FIELD_ACCESS, FIELD_DECLARATION, FOR_STATEMENT, GUARDED_PATTERN, IF_STATEMENT, IMPORT_DECLARATION, INFIX_EXPRESSION, INITIALIZER, INSTANCEOF_EXPRESSION, INTERSECTION_TYPE, JAVADOC, JAVADOC_REGION, JAVADOC_TEXT_ELEMENT, LABELED_STATEMENT, LAMBDA_EXPRESSION, LINE_COMMENT, MALFORMED, MARKER_ANNOTATION, MEMBER_REF, MEMBER_VALUE_PAIR, METHOD_DECLARATION, METHOD_INVOCATION, METHOD_REF, METHOD_REF_PARAMETER, MODIFIER, MODULE_DECLARATION, MODULE_MODIFIER, MODULE_QUALIFIED_NAME, NAME_QUALIFIED_TYPE, NORMAL_ANNOTATION, NULL_LITERAL, NULL_PATTERN, NUMBER_LITERAL, OPENS_DIRECTIVE, ORIGINAL, PACKAGE_DECLARATION, PARAMETERIZED_TYPE, PARENTHESIZED_EXPRESSION, PATTERN_INSTANCEOF_EXPRESSION, POSTFIX_EXPRESSION, PREFIX_EXPRESSION, PRIMITIVE_TYPE, PROTECT, PROVIDES_DIRECTIVE, QUALIFIED_NAME, QUALIFIED_TYPE, RECORD_DECLARATION, RECORD_PATTERN, RECOVERED, REQUIRES_DIRECTIVE, RETURN_STATEMENT, SIMPLE_NAME, SIMPLE_TYPE, SINGLE_MEMBER_ANNOTATION, SINGLE_VARIABLE_DECLARATION, STRING_LITERAL, SUPER_CONSTRUCTOR_INVOCATION, SUPER_FIELD_ACCESS, SUPER_METHOD_INVOCATION, SUPER_METHOD_REFERENCE, SWITCH_CASE, SWITCH_EXPRESSION, SWITCH_STATEMENT, SYNCHRONIZED_STATEMENT, TAG_ELEMENT, TAG_PROPERTY, TEXT_BLOCK, TEXT_ELEMENT, THIS_EXPRESSION, THROW_STATEMENT, TRY_STATEMENT, TYPE_DECLARATION, TYPE_DECLARATION_STATEMENT, TYPE_LITERAL, TYPE_METHOD_REFERENCE, TYPE_PARAMETER, TYPE_PATTERN, UNION_TYPE, UNNAMED_CLASS, USES_DIRECTIVE, VARIABLE_DECLARATION_EXPRESSION, VARIABLE_DECLARATION_FRAGMENT, VARIABLE_DECLARATION_STATEMENT, WHILE_STATEMENT, WILDCARD_TYPE, YIELD_STATEMENT
-
Method Summary
Modifier and TypeMethodDescriptionReturns the live ordered list of extra dimensions with optional annotations (added in JLS8 API).getBody()
Returns the body of this method declaration, ornull
if this method has no body.int
Returns the number of extra array dimensions over and above the explicitly-specified return type.getName()
Returns the name of the method declared in this method declaration.Returns the qualifying name, if any, for the explicit receiver ornull
if not used (added in JLS8 API).Returns the receiver type explicitly declared in the method or constructor declaration (added in JLS8 API).Deprecated.In the JLS3 API, this method is replaced bygetReturnType2()
, which may returnnull
.Returns the return type of the method declared in this method declaration, exclusive of any extra array dimensions (added in JLS3 API).boolean
Returns whether this declaration declares a constructor or a method.boolean
Returns whether this declaration declares a constructor or a method.boolean
Returns whether this method declaration declares a variable arity method (added in JLS3 API).Returns the live ordered list of method parameter declarations for this method declaration.static List
propertyDescriptors
(int apiLevel) Returns a list of structural property descriptors for this node type.Resolves and returns the binding for the method or constructor declared in this method or constructor declaration.void
Sets or clears the body of this method declaration.void
setCompactConstructor
(boolean isCompactConstructor) Sets whether this declaration declares a compact constructor in a record or a method.void
setConstructor
(boolean isConstructor) Sets whether this declaration declares a constructor or a method.void
setExtraDimensions
(int dimensions) Deprecated.In the JLS8 API, this method is replaced byextraDimensions()
which contains a list ofDimension
nodes.void
setName
(SimpleName methodName) Sets the name of the method declared in this method declaration to the given name.void
setReceiverQualifier
(SimpleName receiverQualifier) Sets the given simple name as the qualifier for the receiver (added in JLS8 API).void
setReceiverType
(Type receiverType) Sets or clears the given type as the type of explicit receiver parameter (added in JLS8 API).void
setReturnType
(Type type) Deprecated.In the JLS3 API, this method is replaced bysetReturnType2(Type)
, which acceptsnull
.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).Deprecated.In the JLS8 API, this method is replaced bythrownExceptionTypes()
.Returns the live ordered list of thrown exception types in this method declaration.Returns the live ordered list of type parameters of this method declaration (added in JLS3 API).Methods inherited from class org.eclipse.jdt.core.dom.BodyDeclaration
getJavadoc, getJavadocProperty, getModifiers, getModifiersProperty, modifiers, setJavadoc, setModifiers
Methods inherited from class org.eclipse.jdt.core.dom.ASTNode
accept, copySubtree, copySubtrees, delete, equals, getAST, getFlags, getLength, getLocationInParent, getNodeType, getParent, getProperty, getRoot, getStartPosition, getStructuralProperty, hashCode, nodeClassForType, properties, setFlags, setProperty, setSourceRange, setStructuralProperty, structuralPropertiesForType, subtreeBytes, subtreeMatch, toString
-
Field Details
-
JAVADOC_PROPERTY
The "javadoc" structural property of this node type (child type:Javadoc
).- Since:
- 3.0
-
MODIFIERS_PROPERTY
Deprecated.In the JLS3 API, this property is replaced byMODIFIERS2_PROPERTY
.The "modifiers" structural property of this node type (type:Integer
) (JLS2 API only).- Since:
- 3.0
-
MODIFIERS2_PROPERTY
The "modifiers" structural property of this node type (element type:IExtendedModifier
) (added in JLS3 API).- Since:
- 3.1
-
CONSTRUCTOR_PROPERTY
The "constructor" structural property of this node type (type:Boolean
).- Since:
- 3.0
-
COMPACT_CONSTRUCTOR_PROPERTY
The "compact constructor" structural property of record node type (type:Boolean
).- Since:
- 3.22
-
NAME_PROPERTY
The "name" structural property of this node type (child type:SimpleName
).- Since:
- 3.0
-
RETURN_TYPE_PROPERTY
Deprecated.In the JLS3 API, this property is replaced byRETURN_TYPE2_PROPERTY
.The "returnType" structural property of this node type (child type:Type
) (JLS2 API only).- Since:
- 3.0
-
RETURN_TYPE2_PROPERTY
The "returnType2" structural property of this node type (child type:Type
) (added in JLS3 API).- Since:
- 3.1
-
EXTRA_DIMENSIONS_PROPERTY
Deprecated.In JLS8 and later, useEXTRA_DIMENSIONS2_PROPERTY
instead.The "extraDimensions" structural property of this node type (type:Integer
) (below JLS8 only).- Since:
- 3.0
-
EXTRA_DIMENSIONS2_PROPERTY
The "extraDimensions2" structural property of this node type (element type:Dimension
) (added in JLS8 API).- Since:
- 3.10
-
TYPE_PARAMETERS_PROPERTY
The "typeParameters" structural property of this node type (element type:TypeParameter
) (added in JLS3 API).- Since:
- 3.1
-
PARAMETERS_PROPERTY
The "parameters" structural property of this node type (element type:SingleVariableDeclaration
).- Since:
- 3.0
-
RECEIVER_TYPE_PROPERTY
The "receiverType" structural property of this node type (child type:Type
) (added in JLS8 API).- Since:
- 3.10
-
RECEIVER_QUALIFIER_PROPERTY
The "receiverQualifier" structural property of this node type (child type:SimpleName
) (added in JLS8 API).- Since:
- 3.10
-
THROWN_EXCEPTIONS_PROPERTY
Deprecated.In JLS8 and later, useTHROWN_EXCEPTION_TYPES_PROPERTY
instead.The "thrownExceptions" structural property of this node type (element type:Name
) (before JLS8 only).- Since:
- 3.0
-
THROWN_EXCEPTION_TYPES_PROPERTY
The "thrownExceptionTypes" structural property of this node type (element type:Type
) (added in JLS8 API).- Since:
- 3.10
-
BODY_PROPERTY
The "body" structural property of this node type (child type:Block
).- Since:
- 3.0
-
-
Method Details
-
propertyDescriptors
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
) - Since:
- 3.0
-
isConstructor
public boolean isConstructor()Returns whether this declaration declares a constructor or a method.- Returns:
true
if this is a constructor declaration, andfalse
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, andfalse
for a method declaration
-
isCompactConstructor
public boolean isCompactConstructor()Returns whether this declaration declares a constructor or a method.- Returns:
true
if this is a compact constructor declaration in a record, andfalse
if this is a method declaration- Throws:
UnsupportedOperationException
- if this operation is used below JLS16- Since:
- 3.26
-
setCompactConstructor
public void setCompactConstructor(boolean isCompactConstructor) Sets whether this declaration declares a compact constructor in a record or a method.- Parameters:
isCompactConstructor
-true
for a constructor declaration, andfalse
for a method declaration- Throws:
UnsupportedOperationException
- if this operation is used below JLS16- Since:
- 3.26
-
typeParameters
Returns the live ordered list of type parameters of this method declaration (added in JLS3 API). This list is non-empty for parameterized methods.- Returns:
- the live list of type parameters
(element type:
TypeParameter
) - Throws:
UnsupportedOperationException
- if this operation is used in a JLS2 AST- Since:
- 3.1
-
getName
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
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:
methodName
- the new method name- Throws:
IllegalArgumentException
- if:- the node belongs to a different AST
- the node already has a parent
-
getReceiverType
Returns the receiver type explicitly declared in the method or constructor declaration (added in JLS8 API). If the receiver is not explicitly declared in the method or constructor declaration,null
is returned.- Returns:
- the receiver type or
null
if receiver is not declared explicitly - Throws:
UnsupportedOperationException
- if this operation is used below JLS8- Since:
- 3.10
-
setReceiverType
Sets or clears the given type as the type of explicit receiver parameter (added in JLS8 API).A receiver type is only legal in Java code if it appears on an instance method or on a constructor of an inner class.
- Parameters:
receiverType
- type of the explicit receiver parameter, ornull
if there is none- Throws:
UnsupportedOperationException
- if this operation is used below JLS8- Since:
- 3.10
-
getReceiverQualifier
Returns the qualifying name, if any, for the explicit receiver ornull
if not used (added in JLS8 API).A receiver qualifier is only legal in Java code if it appears on a constructor of an inner class.
- Returns:
- the qualifying name or
null
if a qualifier was not specified - Throws:
UnsupportedOperationException
- if this operation is used below JLS8- Since:
- 3.10
-
setReceiverQualifier
Sets the given simple name as the qualifier for the receiver (added in JLS8 API).- Parameters:
receiverQualifier
- explicit receiver parameter to be added to the method declaration- Throws:
UnsupportedOperationException
- if this operation is used below JLS8- Since:
- 3.10
-
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, andfalse
otherwise- Throws:
UnsupportedOperationException
- if this operation is used in a JLS2 AST- Since:
- 3.1
- See Also:
-
thrownExceptions
Deprecated.In the JLS8 API, this method is replaced bythrownExceptionTypes()
.Returns the live ordered list of thrown exception names in this method declaration (below JLS8 API only).- Returns:
- the live list of exception names
(element type:
Name
) - Throws:
UnsupportedOperationException
- if this operation is used in a JLS8 or later AST
-
thrownExceptionTypes
Returns the live ordered list of thrown exception types in this method declaration.- Returns:
- the live list of exception types
(element type:
Type
) - Throws:
UnsupportedOperationException
- if this operation is used in a JLS2, JLS3 or JLS4 AST- Since:
- 3.10
-
getReturnType
Deprecated.In the JLS3 API, this method is replaced bygetReturnType2()
, which may returnnull
.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:
UnsupportedOperationException
- if this operation is used in an AST later than JLS2
-
setReturnType
Deprecated.In the JLS3 API, this method is replaced bysetReturnType2(Type)
, which acceptsnull
.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:
IllegalArgumentException
- if:- the node belongs to a different AST
- the node already has a parent
UnsupportedOperationException
- if this operation is used in an AST later than JLS2
-
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:
UnsupportedOperationException
- if this operation is used in a JLS2 AST- Since:
- 3.1
-
setReturnType2
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, ornull
if none- Throws:
UnsupportedOperationException
- if this operation is used in a JLS2 ASTIllegalArgumentException
- if:- the node belongs to a different AST
- the node already has a parent
- Since:
- 3.1
-
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 ofint
and two extra array dimensions;int[][] foo()
has a return type ofint[][]
and zero extra array dimensions. The two constructs have different ASTs, even though there are really syntactic variants of the same method declaration.In the JLS8 API, this method is a convenience method that counts
extraDimensions()
.- Returns:
- the number of extra array dimensions
- Since:
- 2.1
-
setExtraDimensions
public void setExtraDimensions(int dimensions) Deprecated.In the JLS8 API, this method is replaced byextraDimensions()
which contains a list ofDimension
nodes.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 ofint
with two extra array dimensions;int[][] foo()
is rendered as a return type ofint[][]
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:
IllegalArgumentException
- if the number of dimensions is negativeUnsupportedOperationException
- if this operation is used in a JLS8 or later AST- Since:
- 2.1
-
extraDimensions
Returns the live ordered list of extra dimensions with optional annotations (added in JLS8 API).- Returns:
- the live list of extra dimensions with optional annotations (element type:
Dimension
) - Throws:
UnsupportedOperationException
- if this operation is used below JLS8- Since:
- 3.10
-
getBody
Returns the body of this method declaration, ornull
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
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, ornull
if there is none- Throws:
IllegalArgumentException
- if:- the node belongs to a different AST
- the node already has a parent
- a cycle in would be created
-
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
-
EXTRA_DIMENSIONS2_PROPERTY
instead.