Eclipse JDT
2.0

org.eclipse.jdt.core.dom
Class MethodDeclaration

java.lang.Object
  |
  +--org.eclipse.jdt.core.dom.ASTNode
        |
        +--org.eclipse.jdt.core.dom.BodyDeclaration
              |
              +--org.eclipse.jdt.core.dom.MethodDeclaration

public class MethodDeclaration
extends BodyDeclaration

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

 MethodDeclaration:
    [ Javadoc ] { Modifier } ( Type | void ) Identifier (
        [ FormalParameter 
 		     { , FormalParameter } ] ) {[ ] }
        [ throws TypeName { , TypeName } ] ( Block | ; )
 ConstructorDeclaration:
    [ Javadoc ] { Modifier } Identifier (
 		  [ FormalParameter
 			 { , FormalParameter } ] )
        [throws TypeName { , TypeName } ] MethodBody
 
Normal form:
 MethodDeclaration:
    [ Javadoc ] { Modifier } ( Type | void ) Identifier
        ( [ FormalParamter { , FormalParameter } ] )
        [ throws TypeName { , TypeName } ]
        ( Block | ; )
 ConstructorDeclaration:
    [ Javadoc ] { Modifier } Identifier
        ( [ FormalParameter { , FormalParameter } ] )
        [ throws TypeName { , TypeName } ]
        Block
 

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 return type (method, no modifiers), 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).

Since:
2.0

Field Summary
 
Fields inherited from class org.eclipse.jdt.core.dom.ASTNode
ANONYMOUS_CLASS_DECLARATION, ARRAY_ACCESS, ARRAY_CREATION, ARRAY_INITIALIZER, ARRAY_TYPE, ASSERT_STATEMENT, ASSIGNMENT, BLOCK, BOOLEAN_LITERAL, BREAK_STATEMENT, CAST_EXPRESSION, CATCH_CLAUSE, CHARACTER_LITERAL, CLASS_INSTANCE_CREATION, COMPILATION_UNIT, CONDITIONAL_EXPRESSION, CONSTRUCTOR_INVOCATION, CONTINUE_STATEMENT, DO_STATEMENT, EMPTY_STATEMENT, EXPRESSION_STATEMENT, FIELD_ACCESS, FIELD_DECLARATION, FOR_STATEMENT, IF_STATEMENT, IMPORT_DECLARATION, INFIX_EXPRESSION, INITIALIZER, INSTANCEOF_EXPRESSION, JAVADOC, LABELED_STATEMENT, MALFORMED, METHOD_DECLARATION, METHOD_INVOCATION, NULL_LITERAL, NUMBER_LITERAL, PACKAGE_DECLARATION, PARENTHESIZED_EXPRESSION, POSTFIX_EXPRESSION, PREFIX_EXPRESSION, PRIMITIVE_TYPE, QUALIFIED_NAME, RETURN_STATEMENT, SIMPLE_NAME, SIMPLE_TYPE, SINGLE_VARIABLE_DECLARATION, STRING_LITERAL, SUPER_CONSTRUCTOR_INVOCATION, SUPER_FIELD_ACCESS, SUPER_METHOD_INVOCATION, SWITCH_CASE, SWITCH_STATEMENT, SYNCHRONIZED_STATEMENT, THIS_EXPRESSION, THROW_STATEMENT, TRY_STATEMENT, TYPE_DECLARATION, TYPE_DECLARATION_STATEMENT, TYPE_LITERAL, VARIABLE_DECLARATION_EXPRESSION, VARIABLE_DECLARATION_FRAGMENT, VARIABLE_DECLARATION_STATEMENT, WHILE_STATEMENT
 
Method Summary
 Block getBody()
          Returns the body of this method declaration, or null if this method has no body.
 int getModifiers()
          Returns the modifiers explicitly specified on this declaration.
 SimpleName getName()
          Returns the name of the method declared in this method declaration.
 int getNodeType()
          Returns an integer value identifying the type of this concrete AST node.
 Type getReturnType()
          Returns the return type of the method declared in this method declaration.
 boolean isConstructor()
          Returns whether this declaration declares a constructor or a method.
 List parameters()
          Returns the live ordered list of method parameter declarations for this method declaration.
 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 setModifiers(int modifiers)
          Sets the modifiers explicitly specified on this declaration.
 void setName(SimpleName methodName)
          Sets the name of the method declared in this method declaration to the given name.
 void setReturnType(Type type)
          Sets the return type of the method declared in this method declaration to the given type.
 boolean subtreeMatch(ASTMatcher matcher, Object other)
          Returns whether the subtree rooted at the given node matches the given other object as decided by the given matcher.
 List thrownExceptions()
          Returns the live ordered list of thrown exception names in this method declaration.
 
Methods inherited from class org.eclipse.jdt.core.dom.BodyDeclaration
getJavadoc, setJavadoc
 
Methods inherited from class org.eclipse.jdt.core.dom.ASTNode
accept, copySubtree, copySubtrees, equals, getAST, getFlags, getLength, getParent, getProperty, getRoot, getStartPosition, properties, setFlags, setProperty, setSourceRange, subtreeBytes, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

getNodeType

public int getNodeType()
Description copied from class: ASTNode
Returns an integer value identifying the type of this concrete AST node. The values are small positive integers, suitable for use in switch statements.

For each concrete node type there is a unique node type constant (name and value). The unique node type constant for a concrete node type such as CastExpression is ASTNode.CAST_EXPRESSION.

Specified by:
getNodeType in class ASTNode
Returns:
one of the node type constants

subtreeMatch

public boolean subtreeMatch(ASTMatcher matcher,
                            Object other)
Description copied from class: ASTNode
Returns whether the subtree rooted at the given node matches the given other object as decided by the given matcher.

Specified by:
subtreeMatch in class ASTNode
Parameters:
matcher - the matcher
other - the other object, or null
Returns:
true if the subtree matches, or false if they do not match

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

getModifiers

public int getModifiers()
Returns the modifiers explicitly specified on this declaration.

Note that deprecated is not included.

Returns:
the bit-wise or of Modifier constants
See Also:
Modifier

setModifiers

public void setModifiers(int modifiers)
Sets the modifiers explicitly specified on this declaration.

The following modifiers are valid for methods: public, private, protected, static, final, synchronized, native, abstract, and strictfp. For constructors, only public, private, and protected are meaningful.

Parameters:
modifiers - the bit-wise or of Modifier constants
Throws:
IllegalArgumentException - if the modifiers are illegal
See Also:
Modifier

getName

public SimpleName 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

public void setName(SimpleName methodName)
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

parameters

public 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)

thrownExceptions

public 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

public Type getReturnType()
Returns the return type of the method declared in this method declaration. 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).

Returns:
the return type, possibly the void primitive type

setReturnType

public void setReturnType(Type type)
Sets the return type of the method declared in this method declaration to the given type. 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).

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

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:
IllegalArgumentException - if:
  • the node belongs to a different AST
  • the node already has a parent
  • a cycle in would be created

resolveBinding

public IMethodBinding 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

Eclipse JDT
2.0

Copyright (c) IBM Corp. and others 2000, 2002. All Rights Reserved.