public class MethodDeclaration extends BodyDeclaration
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 | ; )
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).
Modifier and Type | Field and Description |
---|---|
static ChildPropertyDescriptor |
BODY_PROPERTY
The "body" structural property of this node type (child type:
Block ). |
static SimplePropertyDescriptor |
CONSTRUCTOR_PROPERTY
The "constructor" structural property of this node type (type:
Boolean ). |
static SimplePropertyDescriptor |
EXTRA_DIMENSIONS_PROPERTY
Deprecated.
In JLS8 and later, use
EXTRA_DIMENSIONS2_PROPERTY instead. |
static ChildListPropertyDescriptor |
EXTRA_DIMENSIONS2_PROPERTY
The "extraDimensions2" structural property of this node type (element type:
Dimension ) (added in JLS8 API). |
static ChildPropertyDescriptor |
JAVADOC_PROPERTY
The "javadoc" structural property of this node type (child type:
Javadoc ). |
static SimplePropertyDescriptor |
MODIFIERS_PROPERTY
Deprecated.
In the JLS3 API, this property is replaced by
MODIFIERS2_PROPERTY . |
static ChildListPropertyDescriptor |
MODIFIERS2_PROPERTY
The "modifiers" structural property of this node type (element type:
IExtendedModifier ) (added in JLS3 API). |
static ChildPropertyDescriptor |
NAME_PROPERTY
The "name" structural property of this node type (child type:
SimpleName ). |
static ChildListPropertyDescriptor |
PARAMETERS_PROPERTY
The "parameters" structural property of this node type (element type:
SingleVariableDeclaration ). |
static ChildPropertyDescriptor |
RECEIVER_QUALIFIER_PROPERTY
The "receiverQualifier" structural property of this node type (child type:
SimpleName ) (added in JLS8 API). |
static ChildPropertyDescriptor |
RECEIVER_TYPE_PROPERTY
The "receiverType" structural property of this node type (child type:
Type ) (added in JLS8 API). |
static ChildPropertyDescriptor |
RETURN_TYPE_PROPERTY
Deprecated.
In the JLS3 API, this property is replaced by
RETURN_TYPE2_PROPERTY . |
static ChildPropertyDescriptor |
RETURN_TYPE2_PROPERTY
The "returnType2" structural property of this node type (child type:
Type ) (added in JLS3 API). |
static ChildListPropertyDescriptor |
THROWN_EXCEPTION_TYPES_PROPERTY
The "thrownExceptionTypes" structural property of this node type (element type:
Type ) (added in JLS8 API). |
static ChildListPropertyDescriptor |
THROWN_EXCEPTIONS_PROPERTY
Deprecated.
In JLS8 and later, use
THROWN_EXCEPTION_TYPES_PROPERTY instead. |
static ChildListPropertyDescriptor |
TYPE_PARAMETERS_PROPERTY
The "typeParameters" structural property of this node type (element type:
TypeParameter ) (added in JLS3 API). |
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, CAST_EXPRESSION, CATCH_CLAUSE, CHARACTER_LITERAL, CLASS_INSTANCE_CREATION, COMPILATION_UNIT, CONDITIONAL_EXPRESSION, CONSTRUCTOR_INVOCATION, CONTINUE_STATEMENT, CREATION_REFERENCE, DIMENSION, DO_STATEMENT, EMPTY_STATEMENT, ENHANCED_FOR_STATEMENT, ENUM_CONSTANT_DECLARATION, ENUM_DECLARATION, EXPRESSION_METHOD_REFERENCE, EXPRESSION_STATEMENT, FIELD_ACCESS, FIELD_DECLARATION, FOR_STATEMENT, IF_STATEMENT, IMPORT_DECLARATION, INFIX_EXPRESSION, INITIALIZER, INSTANCEOF_EXPRESSION, INTERSECTION_TYPE, JAVADOC, LABELED_STATEMENT, LAMBDA_EXPRESSION, LINE_COMMENT, MALFORMED, MARKER_ANNOTATION, MEMBER_REF, MEMBER_VALUE_PAIR, METHOD_DECLARATION, METHOD_INVOCATION, METHOD_REF, METHOD_REF_PARAMETER, MODIFIER, NAME_QUALIFIED_TYPE, NORMAL_ANNOTATION, NULL_LITERAL, NUMBER_LITERAL, ORIGINAL, PACKAGE_DECLARATION, PARAMETERIZED_TYPE, PARENTHESIZED_EXPRESSION, POSTFIX_EXPRESSION, PREFIX_EXPRESSION, PRIMITIVE_TYPE, PROTECT, QUALIFIED_NAME, QUALIFIED_TYPE, RECOVERED, 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_STATEMENT, SYNCHRONIZED_STATEMENT, TAG_ELEMENT, TEXT_ELEMENT, THIS_EXPRESSION, THROW_STATEMENT, TRY_STATEMENT, TYPE_DECLARATION, TYPE_DECLARATION_STATEMENT, TYPE_LITERAL, TYPE_METHOD_REFERENCE, TYPE_PARAMETER, UNION_TYPE, VARIABLE_DECLARATION_EXPRESSION, VARIABLE_DECLARATION_FRAGMENT, VARIABLE_DECLARATION_STATEMENT, WHILE_STATEMENT, WILDCARD_TYPE
Modifier and Type | Method and Description |
---|---|
List |
extraDimensions()
Returns the live ordered list of extra dimensions with optional annotations (added in JLS8 API).
|
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.
|
SimpleName |
getName()
Returns the name of the method declared in this method declaration.
|
SimpleName |
getReceiverQualifier()
Returns the qualifying name, if any, for the explicit receiver or
null if not used (added in JLS8 API). |
Type |
getReceiverType()
Returns the receiver type explicitly declared in the method or constructor
declaration (added in JLS8 API).
|
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 |
isVarargs()
Returns whether this method declaration declares a
variable arity method (added in JLS3 API).
|
List |
parameters()
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.
|
IMethodBinding |
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)
Deprecated.
In the JLS8 API, this method is replaced by
extraDimensions() which contains a list of Dimension 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 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).
|
List |
thrownExceptions()
Deprecated.
In the JLS8 API, this method is replaced by
thrownExceptionTypes() . |
List |
thrownExceptionTypes()
Returns the live ordered list of thrown exception types in this method
declaration.
|
List |
typeParameters()
Returns the live ordered list of type parameters of this method
declaration (added in JLS3 API).
|
getJavadoc, getJavadocProperty, getModifiers, getModifiersProperty, modifiers, setJavadoc, setModifiers
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
public static final ChildPropertyDescriptor JAVADOC_PROPERTY
Javadoc
).public static final SimplePropertyDescriptor MODIFIERS_PROPERTY
MODIFIERS2_PROPERTY
.Integer
) (JLS2 API only).public static final ChildListPropertyDescriptor MODIFIERS2_PROPERTY
IExtendedModifier
) (added in JLS3 API).public static final SimplePropertyDescriptor CONSTRUCTOR_PROPERTY
Boolean
).public static final ChildPropertyDescriptor NAME_PROPERTY
SimpleName
).public static final ChildPropertyDescriptor RETURN_TYPE_PROPERTY
RETURN_TYPE2_PROPERTY
.Type
) (JLS2 API only).public static final ChildPropertyDescriptor RETURN_TYPE2_PROPERTY
Type
) (added in JLS3 API).public static final SimplePropertyDescriptor EXTRA_DIMENSIONS_PROPERTY
EXTRA_DIMENSIONS2_PROPERTY
instead.Integer
) (below JLS8 only).public static final ChildListPropertyDescriptor EXTRA_DIMENSIONS2_PROPERTY
Dimension
) (added in JLS8 API).public static final ChildListPropertyDescriptor TYPE_PARAMETERS_PROPERTY
TypeParameter
) (added in JLS3 API).public static final ChildListPropertyDescriptor PARAMETERS_PROPERTY
SingleVariableDeclaration
).public static final ChildPropertyDescriptor RECEIVER_TYPE_PROPERTY
Type
) (added in JLS8 API).public static final ChildPropertyDescriptor RECEIVER_QUALIFIER_PROPERTY
SimpleName
) (added in JLS8 API).public static final ChildListPropertyDescriptor THROWN_EXCEPTIONS_PROPERTY
THROWN_EXCEPTION_TYPES_PROPERTY
instead.Name
) (before JLS8 only).public static final ChildListPropertyDescriptor THROWN_EXCEPTION_TYPES_PROPERTY
Type
) (added in JLS8 API).public static final ChildPropertyDescriptor BODY_PROPERTY
Block
).public static List propertyDescriptors(int apiLevel)
apiLevel
- the API level; one of the AST.JLS* constantsStructuralPropertyDescriptor
)public boolean isConstructor()
true
if this is a constructor declaration,
and false
if this is a method declarationpublic void setConstructor(boolean isConstructor)
isConstructor
- true
for a constructor declaration,
and false
for a method declarationpublic List typeParameters()
TypeParameter
)UnsupportedOperationException
- if this operation is used in
a JLS2 ASTpublic SimpleName getName()
public void setName(SimpleName methodName)
methodName
- the new method nameIllegalArgumentException
- if:
public Type getReceiverType()
null
is returned.null
if receiver is not declared explicitlyUnsupportedOperationException
- if this operation is used below JLS8public void setReceiverType(Type receiverType)
A receiver type is only legal in Java code if it appears on an instance method or on a constructor of an inner class.
receiverType
- type of the explicit receiver parameter, or null
if there is noneUnsupportedOperationException
- if this operation is used below JLS8public SimpleName getReceiverQualifier()
null
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.
null
if a qualifier was not specifiedUnsupportedOperationException
- if this operation is used below JLS8public void setReceiverQualifier(SimpleName receiverQualifier)
receiverQualifier
- explicit receiver parameter to be added to the method declarationUnsupportedOperationException
- if this operation is used below JLS8public List parameters()
SingleVariableDeclaration
)public boolean isVarargs()
true
if this is a variable arity method declaration,
and false
otherwiseUnsupportedOperationException
- if this operation is used in
a JLS2 ASTSingleVariableDeclaration.isVarargs()
public List thrownExceptions()
thrownExceptionTypes()
.Name
)UnsupportedOperationException
- if this operation is used in
a JLS8 or later ASTpublic List thrownExceptionTypes()
Type
)UnsupportedOperationException
- if this operation is used
in a JLS2, JLS3 or JLS4 ASTpublic Type getReturnType()
getReturnType2()
,
which may return null
.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.
UnsupportedOperationException
- if this operation is used in
an AST later than JLS2public void setReturnType(Type type)
setReturnType2(Type)
, which accepts null
.Note that this child is not relevant for constructor declarations (although it does still figure in subtree equality comparisons and visits).
type
- the new return type, possibly the void primitive typeIllegalArgumentException
- if:
UnsupportedOperationException
- if this operation is used in
an AST later than JLS2public Type getReturnType2()
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.
null
if noneUnsupportedOperationException
- if this operation is used in
a JLS2 ASTpublic void setReturnType2(Type type)
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.
type
- the new return type, possibly the void primitive type,
or null
if noneUnsupportedOperationException
- if this operation is used in
a JLS2 ASTIllegalArgumentException
- if:
public int getExtraDimensions()
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.
In the JLS8 API, this method is a convenience method that
counts extraDimensions()
.
public void setExtraDimensions(int dimensions)
extraDimensions()
which contains a list of Dimension
nodes.
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.
dimensions
- the number of array dimensionsIllegalArgumentException
- if the number of dimensions is
negativeUnsupportedOperationException
- if this operation is used in
a JLS8 or later ASTpublic List extraDimensions()
Dimension
)UnsupportedOperationException
- if this operation is used below JLS8public Block getBody()
null
if
this method has no body.
Note that there is a subtle difference between having no body and having an empty body ("{}").
null
if this method has no
bodypublic void setBody(Block body)
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.
body
- the block node, or null
if
there is noneIllegalArgumentException
- if:
public IMethodBinding resolveBinding()
Note that bindings are generally unavailable unless requested when the AST is being built.
null
if the binding cannot be
resolved
Copyright (c) 2000, 2015 Eclipse Contributors and others. All rights reserved.Guidelines for using Eclipse APIs.