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
Represents a method declaration.
-
Field Summary
Fields inherited from interface groovy.lang.groovydoc.GroovydocHolder
DOC_COMMENT -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongetCode()Returns the method body as a Statement.Returns the list of checked exceptions declared in the method's throws clause.Returns the first statement in this method's code block.Returns the generic type parameters for this method.intReturns the ASM modifier flags for this method.getName()Returns the method name.Returns the parameter list for this method.Returns the method's return type as aClassNode.getText()Returns a human-readable text representation of this AST node.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.getTypeDescriptor(boolean pretty) Deprecated.Returns the variable scope for this method.booleanbooleanChecks whether any parameter of this method has a default value.booleanChecks whether this method is declared as abstract.booleanChecks whether this method is a constructor (<init>).booleanChecks whether this method is a default method.booleanChecks whether this method has a dynamically typed return type.booleanisFinal()Checks whether this method is declared as final.booleanChecks whether this method has package scope (no explicit visibility modifier).booleanChecks whether this method is declared as private.booleanChecks whether this method is declared as protected.booleanisPublic()Checks whether this method is declared as public.booleanChecks whether this method is the run method from a script execution.booleanisStatic()Checks whether this method is declared as static.booleanChecks whether this method is a static initializer (<clinit>).booleanChecks whether this method was synthesized as public by Groovy's default visibility rule.booleanChecks whether this method returns void.voidsetAnnotationDefault(boolean hasDefaultValue) Sets the annotation default flag for this method.voidSets the method body statement.voidsetGenericsTypes(GenericsType[] genericsTypes) Sets the generic type parameters for this method.voidSets the flag for this method to indicate it is a script body implementation.voidsetModifiers(int modifiers) Sets the ASM modifier flags for this method.voidsetParameters(Parameter[] parameters) Sets the method's parameter list.voidsetReturnType(ClassNode returnType) Sets the method's return type.voidsetSyntheticPublic(boolean syntheticPublic) Marks this method as having synthetic public visibility.voidsetVariableScope(VariableScope variableScope) Sets the variable scope for this method.toString()Methods inherited from class org.codehaus.groovy.ast.AnnotatedNode
addAnnotation, addAnnotation, addAnnotations, getAnnotations, getAnnotations, getDeclaringClass, getGroovydoc, getInstance, hasNoRealSourcePosition, isSynthetic, setDeclaringClass, setHasNoRealSourcePosition, setSyntheticMethods inherited from class org.codehaus.groovy.ast.ASTNode
copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMap, setSourcePosition, visitMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods 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 aClassNodeparameters- the method parameters as an array ofParameterexceptions- the checked exception types thrown by this methodcode- the method body as aStatement(typically aBlockStatement)
-
-
Method Details
-
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. -
getCode
Returns the method body as a Statement. Typically aBlockStatementcontaining 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
Sets the method body statement. Typically aBlockStatement. 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
Returns the method name. Special names: "<init>" for constructors, "<clinit>" for static initializers.- Returns:
- the method's identifier
-
getParameters
Returns the parameter list for this method. EachParametermay have an initial value expression for default parameters.- Returns:
- array of parameters, or empty array if no parameters
-
setParameters
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 ofParameterobjects
-
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
Returns the method's return type as aClassNode.- Returns:
- the return type (defaults to
ClassHelper.OBJECT_TYPEif not set)
-
setReturnType
Sets the method's return type. If the type is null, defaults toClassHelper.OBJECT_TYPE. Marks the method as having dynamic return type if appropriate.- Parameters:
returnType- the returnClassNode
-
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
Returns the variable scope for this method. The scope tracks declared variables and parameters.- Returns:
- the
VariableScopefor this method
-
setVariableScope
Sets the variable scope for this method. The scope is updated to match the method's static context.- Parameters:
variableScope- theVariableScopeto 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
Returns the list of checked exceptions declared in the method's throws clause.- Returns:
- array of exception
ClassNodetypes, or empty array if no exceptions declared
-
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
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
Sets the generic type parameters for this method. Invalidates cached data like the type descriptor.- Parameters:
genericsTypes- array ofGenericsTypefor this method
-
hasAnnotationDefault
public boolean hasAnnotationDefault()- Returns:
trueif 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:
-
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
Description copied from class:ASTNodeReturns 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. -
toString
-
MethodNodeUtils.methodDescriptor(MethodNode, boolean)