Package: org.codehaus.groovy.ast

## [Java] Class MethodNode

- org.codehaus.groovy.ast.MethodNode

-     public class MethodNode
      extends AnnotatedNode

  Represents a method declaration.

- - ### Constructor Summary

    Constructors
    Constructor and 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.

  - ### Methods Summary

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

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class AnnotatedNode` | `addAnnotation, addAnnotation, addAnnotations, getAnnotations, getAnnotations, getDeclaringClass, getGroovydoc, getInstance, hasNoRealSourcePosition, isSynthetic, setDeclaringClass, setHasNoRealSourcePosition, setSynthetic`
    `class ASTNode` | `copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, getText, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMap, setSourcePosition, visit`

- - ### Constructor Detail

    - #### protected 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 Detail

    - #### 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

    - #### 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

    - #### 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

    - #### 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

    - #### 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

    - #### public String getName()

      Returns the method name. Special names: "<init>" for constructors, "<clinit>" for static initializers.

      Returns:
        the method's identifier

    - #### 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

    - #### public ClassNode getReturnType()

      Returns the method's return type as a ClassNode.

      Returns:
        the return type (defaults to ClassHelper.OBJECT_TYPE if not set)

    - #### public String getText()

    - #### 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.

    - #### @Deprecated
      public String getTypeDescriptor(boolean pretty)

      deprecated:
        use MethodNodeUtils.methodDescriptor

    - #### public VariableScope getVariableScope()

      Returns the variable scope for this method. The scope tracks declared variables and parameters.

      Returns:
        the VariableScope for this method

    - #### public boolean hasAnnotationDefault()

      Returns:
        `true` if annotation method has a default value

    - #### 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

    - #### 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

    - #### public boolean isConstructor()

      Checks whether this method is a constructor (<init>).

      Returns:
        true if this method's name is "<init>"
      Since:
        4.0.0

    - #### 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

    - #### 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

    - #### 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

    - #### public boolean isPackageScope()

      Checks whether this method has package scope (no explicit visibility modifier).

      Returns:
        true if no public/private/protected modifier is set

    - #### public boolean isPrivate()

      Checks whether this method is declared as private.

      Returns:
        true if this method has the private modifier

    - #### public boolean isProtected()

      Checks whether this method is declared as protected.

      Returns:
        true if this method has the protected modifier

    - #### public boolean isPublic()

      Checks whether this method is declared as public.

      Returns:
        true if this method has the public modifier

    - #### 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

    - #### public boolean isStatic()

      Checks whether this method is declared as static.

      Returns:
        true if this method has the static modifier

    - #### public boolean isStaticConstructor()

      Checks whether this method is a static initializer (<clinit>).

      Returns:
        true if this method's name is "<clinit>"

    - #### 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

    - #### public boolean isVoidMethod()

      Checks whether this method returns void.

      Returns:
        true if the return type is primitive void

    - #### 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

    - #### 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

    - #### 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

    - #### public void setIsScriptBody()

      Sets the flag for this method to indicate it is a script body implementation.

      See Also:
        ModuleNode.createStatementsClass

    - #### 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

    - #### 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

    - #### 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

    - #### public void setSyntheticPublic(boolean syntheticPublic)

      Marks this method as having synthetic public visibility.

      Parameters:
        `syntheticPublic` - true to mark as synthetically public

    - #### 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

    - #### public String toString()