Package org.codehaus.groovy.ast

# Class MethodNode

java.lang.Object
org.codehaus.groovy.ast.ASTNode
org.codehaus.groovy.ast.AnnotatedNode
org.codehaus.groovy.ast.MethodNode

All Implemented Interfaces:
  `GroovydocHolder<AnnotatedNode>`, `NodeMetaDataHandler`

Direct Known Subclasses:
  `ConstructorNode`, `ExtensionMethodNode`

public class MethodNode extends AnnotatedNode
Represents a method declaration.

- ## Field Summary

  ### Fields inherited from interface groovy.lang.groovydoc.GroovydocHolder

  `DOC_COMMENT`

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `MethodNode()`
  ``
  `MethodNode(String name, int modifiers, ClassNode returnType, Parameter[] parameters, ClassNode[] exceptions, Statement code)`
  Creates a method node representing a method or constructor declaration.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `Statement`
  `getCode()`
  Returns the method body as a Statement.
  `ClassNode[]`
  `getExceptions()`
  Returns the list of checked exceptions declared in the method's throws clause.
  `Statement`
  `getFirstStatement()`
  Returns the first statement in this method's code block.
  `GenericsType[]`
  `getGenericsTypes()`
  Returns the generic type parameters for this method.
  `int`
  `getModifiers()`
  Returns the ASM modifier flags for this method.
  `String`
  `getName()`
  Returns the method name.
  `Parameter[]`
  `getParameters()`
  Returns the parameter list for this method.
  `ClassNode`
  `getReturnType()`
  Returns the method's return type as a `ClassNode`.
  `String`
  `getText()`
  Returns a human-readable text representation of this AST node.
  `String`
  `getTypeDescriptor()`
  The type descriptor for a method node is a string containing the name of the method, its return type, and its parameter types in a canonical form.
  `String`
  `getTypeDescriptor(boolean pretty)`
  Deprecated.
  use `MethodNodeUtils.methodDescriptor(MethodNode, boolean)`
  `VariableScope`
  `getVariableScope()`
  Returns the variable scope for this method.
  `boolean`
  `hasAnnotationDefault()`
  `boolean`
  `hasDefaultValue()`
  Checks whether any parameter of this method has a default value.
  `boolean`
  `isAbstract()`
  Checks whether this method is declared as abstract.
  `boolean`
  `isConstructor()`
  Checks whether this method is a constructor (<init>).
  `boolean`
  `isDefault()`
  Checks whether this method is a default method.
  `boolean`
  `isDynamicReturnType()`
  Checks whether this method has a dynamically typed return type.
  `boolean`
  `isFinal()`
  Checks whether this method is declared as final.
  `boolean`
  `isPackageScope()`
  Checks whether this method has package scope (no explicit visibility modifier).
  `boolean`
  `isPrivate()`
  Checks whether this method is declared as private.
  `boolean`
  `isProtected()`
  Checks whether this method is declared as protected.
  `boolean`
  `isPublic()`
  Checks whether this method is declared as public.
  `boolean`
  `isScriptBody()`
  Checks whether this method is the run method from a script execution.
  `boolean`
  `isStatic()`
  Checks whether this method is declared as static.
  `boolean`
  `isStaticConstructor()`
  Checks whether this method is a static initializer (<clinit>).
  `boolean`
  `isSyntheticPublic()`
  Checks whether this method was synthesized as public by Groovy's default visibility rule.
  `boolean`
  `isVoidMethod()`
  Checks whether this method returns void.
  `void`
  `setAnnotationDefault(boolean hasDefaultValue)`
  Sets the annotation default flag for this method.
  `void`
  `setCode(Statement code)`
  Sets the method body statement.
  `void`
  `setGenericsTypes(GenericsType[] genericsTypes)`
  Sets the generic type parameters for this method.
  `void`
  `setIsScriptBody()`
  Sets the flag for this method to indicate it is a script body implementation.
  `void`
  `setModifiers(int modifiers)`
  Sets the ASM modifier flags for this method.
  `void`
  `setParameters(Parameter[] parameters)`
  Sets the method's parameter list.
  `void`
  `setReturnType(ClassNode returnType)`
  Sets the method's return type.
  `void`
  `setSyntheticPublic(boolean syntheticPublic)`
  Marks this method as having synthetic public visibility.
  `void`
  `setVariableScope(VariableScope variableScope)`
  Sets the variable scope for this method.
  `String`
  `toString()`

  ### Methods inherited from class org.codehaus.groovy.ast.AnnotatedNode

  `addAnnotation, addAnnotation, addAnnotations, getAnnotations, getAnnotations, getDeclaringClass, getGroovydoc, getInstance, hasNoRealSourcePosition, isSynthetic, setDeclaringClass, setHasNoRealSourcePosition, setSynthetic`

  ### Methods inherited from class org.codehaus.groovy.ast.ASTNode

  `copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMap, setSourcePosition, visit`

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

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

  ### Methods inherited from interface org.codehaus.groovy.ast.NodeMetaDataHandler

  `copyNodeMetaData, getNodeMetaData, getNodeMetaData, getNodeMetaData, newMetaDataMap, putNodeMetaData, removeNodeMetaData, setNodeMetaData`

- ## Constructor Details

  - ### MethodNode

    protected MethodNode()

  - ### MethodNode

    public MethodNode(String name, int modifiers, ClassNode returnType, Parameter[] parameters, ClassNode[] exceptions, Statement code)
    Creates a method node representing a method or constructor declaration. The parameters array defines the method signature and may include default values. The exceptions array lists checked exceptions declared in the method's throws clause.
    Parameters:
      `name` - the method name (use "<init>" for constructors, "<clinit>" for static initializers)
      `modifiers` - ASM modifier flags (public, static, abstract, final, etc.)
      `returnType` - the return type as a `ClassNode`
      `parameters` - the method parameters as an array of `Parameter`
      `exceptions` - the checked exception types thrown by this method
      `code` - the method body as a `Statement` (typically a `BlockStatement`)

- ## Method Details

  - ### getTypeDescriptor

    public String getTypeDescriptor()
    The type descriptor for a method node is a string containing the name of the method, its return type, and its parameter types in a canonical form. For simplicity, we use the format of a Java declaration without parameter names or generics.

  - ### getTypeDescriptor

    @Deprecated public String getTypeDescriptor(boolean pretty)
    Deprecated.
    use `MethodNodeUtils.methodDescriptor(MethodNode, boolean)`

  - ### getCode

    public Statement getCode()
    Returns the method body as a Statement. Typically a `BlockStatement` containing the method's instructions. Returns null for abstract methods or interface methods with no implementation.
    Returns:
      the method body statement, or null for abstract methods

  - ### setCode

    public void setCode(Statement code)
    Sets the method body statement. Typically a `BlockStatement`. Setting null indicates an abstract method.
    Parameters:
      `code` - the statement representing the method body

  - ### getModifiers

    public int getModifiers()
    Returns the ASM modifier flags for this method. Flags include visibility (public/protected/private), static, abstract, final, synchronized, etc.
    Returns:
      ASM opcode flags representing this method's modifiers

  - ### setModifiers

    public void setModifiers(int modifiers)
    Sets the ASM modifier flags for this method. Invalidates cached data (like type descriptor) when modifiers change. Updates the variable scope's static context to match the new modifiers.
    Parameters:
      `modifiers` - ASM opcode flags to set

  - ### getName

    public String getName()
    Returns the method name. Special names: "<init>" for constructors, "<clinit>" for static initializers.
    Returns:
      the method's identifier

  - ### getParameters

    public Parameter[] getParameters()
    Returns the parameter list for this method. Each `Parameter` may have an initial value expression for default parameters.
    Returns:
      array of parameters, or empty array if no parameters

  - ### setParameters

    public void setParameters(Parameter[] parameters)
    Sets the method's parameter list. Updates variable scope to register all parameters and detects default values. Invalidates cached data like the type descriptor.
    Parameters:
      `parameters` - array of `Parameter` objects

  - ### hasDefaultValue

    public boolean hasDefaultValue()
    Checks whether any parameter of this method has a default value. A default value is an initializer expression for a parameter.
    Returns:
      true if at least one parameter has an initial expression

  - ### getReturnType

    public ClassNode getReturnType()
    Returns the method's return type as a `ClassNode`.
    Returns:
      the return type (defaults to `ClassHelper.OBJECT_TYPE` if not set)

  - ### setReturnType

    public void setReturnType(ClassNode returnType)
    Sets the method's return type. If the type is null, defaults to `ClassHelper.OBJECT_TYPE`. Marks the method as having dynamic return type if appropriate.
    Parameters:
      `returnType` - the return `ClassNode`

  - ### isDynamicReturnType

    public boolean isDynamicReturnType()
    Checks whether this method has a dynamically typed return type. Dynamic typing occurs when the exact return type cannot be determined at compile time.
    Returns:
      true if this method's return type is dynamically typed

  - ### isVoidMethod

    public boolean isVoidMethod()
    Checks whether this method returns void.
    Returns:
      true if the return type is primitive void

  - ### getVariableScope

    public VariableScope getVariableScope()
    Returns the variable scope for this method. The scope tracks declared variables and parameters.
    Returns:
      the `VariableScope` for this method

  - ### setVariableScope

    public void setVariableScope(VariableScope variableScope)
    Sets the variable scope for this method. The scope is updated to match the method's static context.
    Parameters:
      `variableScope` - the `VariableScope` to set

  - ### isAbstract

    public boolean isAbstract()
    Checks whether this method is declared as abstract. Abstract methods have no implementation and must be overridden in subclasses.
    Returns:
      true if this method has the abstract modifier

  - ### isDefault

    public boolean isDefault()
    Checks whether this method is a default method. Default methods are public methods in interfaces with no abstract modifier.
    Returns:
      true if this method is a default interface method

  - ### isFinal

    public boolean isFinal()
    Checks whether this method is declared as final. Final methods cannot be overridden.
    Returns:
      true if this method has the final modifier

  - ### isStatic

    public boolean isStatic()
    Checks whether this method is declared as static.
    Returns:
      true if this method has the static modifier

  - ### isPublic

    public boolean isPublic()
    Checks whether this method is declared as public.
    Returns:
      true if this method has the public modifier

  - ### isPrivate

    public boolean isPrivate()
    Checks whether this method is declared as private.
    Returns:
      true if this method has the private modifier

  - ### isProtected

    public boolean isProtected()
    Checks whether this method is declared as protected.
    Returns:
      true if this method has the protected modifier

  - ### isPackageScope

    public boolean isPackageScope()
    Checks whether this method has package scope (no explicit visibility modifier).
    Returns:
      true if no public/private/protected modifier is set

  - ### getExceptions

    public ClassNode[] getExceptions()
    Returns the list of checked exceptions declared in the method's throws clause.
    Returns:
      array of exception `ClassNode` types, or empty array if no exceptions declared

  - ### getFirstStatement

    public Statement getFirstStatement()
    Returns the first statement in this method's code block. Recursively unwraps nested BlockStatements to find the first actual statement. Returns null if the code is null or consists only of empty blocks.
    Returns:
      the first statement, or null if no statements exist

  - ### getGenericsTypes

    public GenericsType[] getGenericsTypes()
    Returns the generic type parameters for this method. Used for generic method declarations (e.g., <T> T getValue()).
    Returns:
      array of `GenericsType`, or null if no generics declared

  - ### setGenericsTypes

    public void setGenericsTypes(GenericsType[] genericsTypes)
    Sets the generic type parameters for this method. Invalidates cached data like the type descriptor.
    Parameters:
      `genericsTypes` - array of `GenericsType` for this method

  - ### hasAnnotationDefault

    public boolean hasAnnotationDefault()
    Returns:
      `true` if annotation method has a default value

  - ### setAnnotationDefault

    public void setAnnotationDefault(boolean hasDefaultValue)
    Sets the annotation default flag for this method. Used when this method is part of an annotation interface.
    Parameters:
      `hasDefaultValue` - true if this annotation method has a default value

  - ### isScriptBody

    public boolean isScriptBody()
    Checks whether this method is the run method from a script execution. Scripts have their module-level statements compiled into a synthetic run() method.
    Returns:
      true if this method represents script body code

  - ### setIsScriptBody

    public void setIsScriptBody()
    Sets the flag for this method to indicate it is a script body implementation.
    See Also:
      - `ModuleNode.createStatementsClass()`

  - ### isStaticConstructor

    public boolean isStaticConstructor()
    Checks whether this method is a static initializer (<clinit>).
    Returns:
      true if this method's name is "<clinit>"

  - ### isConstructor

    public boolean isConstructor()
    Checks whether this method is a constructor (<init>).
    Returns:
      true if this method's name is "<init>"
    Since:
      4.0.0

  - ### isSyntheticPublic

    public boolean isSyntheticPublic()
    Checks whether this method was synthesized as public by Groovy's default visibility rule. Groovy makes methods public by default, even if no public modifier was explicitly written. This flag tracks whether the public modifier was synthetic versus explicit. This information is primarily of interest to AST transform writers.
    Returns:
      true if this method is public due to Groovy's default visibility, not an explicit modifier

  - ### setSyntheticPublic

    public void setSyntheticPublic(boolean syntheticPublic)
    Marks this method as having synthetic public visibility.
    Parameters:
      `syntheticPublic` - true to mark as synthetically public

  - ### getText

    public String getText()
    Description copied from class: `ASTNode`
    Returns a human-readable text representation of this AST node. Used for debugging and error messages. Default implementation returns a message indicating the representation is not yet implemented for this node type.
    Overrides:
      `getText` in class `ASTNode`
    Returns:
      text representation of this node, or placeholder for unimplemented types

  - ### toString

    public String toString()
    Overrides:
      `toString` in class `Object`