Package org.codehaus.groovy.ast
Class ModifierNode
java.lang.Object
org.codehaus.groovy.ast.ASTNode
org.codehaus.groovy.ast.ModifierNode
- All Implemented Interfaces:
NodeMetaDataHandler
Represents a modifier keyword or annotation in Groovy source code.
ModifierNode wraps language modifiers (public, private, static, final, etc.)
and treats annotations as pseudo-modifiers for consistent AST handling.
Each modifier maps to an ASM opcode for bytecode generation.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intPseudo-modifier type constant for annotations.Maps modifier types (parser token types) to their corresponding ASM opcodes. -
Constructor Summary
ConstructorsConstructorDescriptionModifierNode(Integer type) Creates a modifier node for the specified modifier type.ModifierNode(Integer type, String text) Creates a modifier node with a specific text representation.ModifierNode(AnnotationNode annotationNode, String text) Creates a modifier node wrapping an annotation. -
Method Summary
Modifier and TypeMethodDescriptionbooleanCompares this modifier with another object for equality.Returns the wrapped annotation node if this modifier represents an annotation.Returns the ASM opcode corresponding to this modifier.getText()Returns the source text representation of this modifier.getType()Returns the modifier type constant (parser token type).inthashCode()Returns the hash code for this modifier.booleanChecks whether this node represents an annotation (pseudo-modifier).booleanisDef()Checks whether this node represents a property or variable declaration modifier (def, val, or var).booleanChecks whether this node represents a true modifier (not annotation or def).booleanChecks whether this is a non-visibility modifier (static, final, abstract, etc.).booleanChecks whether this modifier supports repetition (stacking).booleanisVal()Checks whether this node specifically represents the 'val' modifier.booleanChecks whether this modifier controls visibility (public, protected, private).toString()Returns the string representation of this modifier.Methods 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, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.codehaus.groovy.ast.NodeMetaDataHandler
copyNodeMetaData, getNodeMetaData, getNodeMetaData, getNodeMetaData, newMetaDataMap, putNodeMetaData, removeNodeMetaData, setNodeMetaData
-
Field Details
-
ANNOTATION_TYPE
public static final int ANNOTATION_TYPEPseudo-modifier type constant for annotations. Annotations are treated as modifiers in the AST for unified processing, distinguished by this sentinel type.- See Also:
-
MODIFIER_OPCODE_MAP
Maps modifier types (parser token types) to their corresponding ASM opcodes. Enables conversion from source-level modifiers to bytecode-level access flags. Some modifiers (SEALED, NON_SEALED, DEFAULT, DEF, VAL, VAR) have no direct bytecode equivalent and map to 0.
-
-
Constructor Details
-
ModifierNode
Creates a modifier node for the specified modifier type. The type corresponds to a parser token type and is mapped to an ASM opcode viaMODIFIER_OPCODE_MAP.- Parameters:
type- the modifier type (parser token type)- Throws:
IllegalArgumentException- if the type has no valid ASM opcode mapping
-
ModifierNode
Creates a modifier node with a specific text representation. Useful for preserving the exact source text of the modifier.- Parameters:
type- the modifier type (parser token type)text- text of the ast node (source representation)- Throws:
IllegalArgumentException- if the type has no valid ASM opcode mapping
-
ModifierNode
Creates a modifier node wrapping an annotation. Treats annotations as pseudo-modifiers for unified AST processing.- Parameters:
annotationNode- the annotation node to wraptext- text of the ast node (source representation)- Throws:
IllegalArgumentException- if annotationNode is null
-
-
Method Details
-
isModifier
public boolean isModifier()Checks whether this node represents a true modifier (not annotation or def). Distinguishes real modifiers (public, static, final, etc.) from pseudo-modifiers (annotations, def, val, var) that are treated as modifiers in the AST.- Returns:
- true if this is a real modifier, false for annotations or def declarations
-
isVisibilityModifier
public boolean isVisibilityModifier()Checks whether this modifier controls visibility (public, protected, private). Visibility modifiers are mutually exclusive and determine class/member access scope.- Returns:
- true if this is a visibility modifier
-
isNonVisibilityModifier
public boolean isNonVisibilityModifier()Checks whether this is a non-visibility modifier (static, final, abstract, etc.). These modifiers can coexist with visibility modifiers.- Returns:
- true if this is a non-visibility modifier
-
isAnnotation
public boolean isAnnotation()Checks whether this node represents an annotation (pseudo-modifier).- Returns:
- true if this node wraps an annotation
-
isDef
public boolean isDef()Checks whether this node represents a property or variable declaration modifier (def, val, or var). These are Groovy-specific pseudo-modifiers.- Returns:
- true if this is a def/val/var declaration modifier
-
isVal
public boolean isVal()Checks whether this node specifically represents the 'val' modifier. In Groovy, 'val' declares a final variable (shorthand for final var).- Returns:
- true if this is the 'val' modifier
- Since:
- 6.0.0
-
getType
Returns the modifier type constant (parser token type).- Returns:
- the type identifier for this modifier
-
getOpcode
Returns the ASM opcode corresponding to this modifier. Used during bytecode generation to set appropriate access flags. Returns 0 for modifiers without direct bytecode representation.- Returns:
- the ASM Opcodes constant for this modifier
-
isRepeatable
public boolean isRepeatable()Checks whether this modifier supports repetition (stacking). Only annotations are repeatable in Groovy; modifiers are mutually exclusive.- Returns:
- true if this modifier can appear multiple times (only for annotations)
-
getText
Returns the source text representation of this modifier. Preserves the exact text as it appeared in the source code. -
getAnnotationNode
Returns the wrapped annotation node if this modifier represents an annotation. Only non-null for pseudo-modifiers created viaModifierNode(AnnotationNode, String).- Returns:
- the
AnnotationNodewrapped by this modifier, or null if this is a true modifier
-
equals
Compares this modifier with another object for equality. Two modifiers are equal if they have the same type, text representation, and wrapped annotation. Used for deduplicating modifier nodes during AST processing. -
hashCode
public int hashCode()Returns the hash code for this modifier. Consistent with equals(): identical modifiers have identical hash codes. Used for efficient storage in hash-based collections. -
toString
Returns the string representation of this modifier. Returns the source text if available, otherwise delegates toObject.toString().
-