Package org.codehaus.groovy.ast
Class FieldNode
java.lang.Object
org.codehaus.groovy.ast.ASTNode
org.codehaus.groovy.ast.AnnotatedNode
org.codehaus.groovy.ast.FieldNode
- All Implemented Interfaces:
GroovydocHolder<AnnotatedNode>,NodeMetaDataHandler,Variable
Represents a field (member variable)
-
Field Summary
Fields inherited from interface groovy.lang.groovydoc.GroovydocHolder
DOC_COMMENT -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturns the initial value expression for this field.Returns the initial value expression for this field.intReturns the ASM modifier flags for this field.getName()Returns the field name.Returns the original declared type of this field before any transformations.getOwner()Returns the class that declares this field.getType()Returns the declared type of this field as aClassNode.booleanChecks whether this field has an initial value expression.booleanChecks whether this field's type is dynamically typed (unresolved type).booleanisEnum()Checks whether this field is an enum constant.booleanisHolder()Checks whether this field is a holder field.booleanChecks whether this field is in a static context.static FieldNodeCreates a new FieldNode wrapping a reflection Field from a Java class.voidRenames this field within its declaring class.voidsetHolder(boolean holder) Marks this field as a holder field for internal compilation purposes.voidsetInitialValueExpression(Expression initialValueExpression) Sets the initial value expression for this field.voidsetModifiers(int modifiers) Sets the ASM modifier flags for this field.voidSets the original type information for this field.voidSets the class that declares this field.voidSets the field's type.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, getText, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMap, setSourcePosition, visitMethods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.codehaus.groovy.ast.NodeMetaDataHandler
copyNodeMetaData, getNodeMetaData, getNodeMetaData, getNodeMetaData, newMetaDataMap, putNodeMetaData, removeNodeMetaData, setNodeMetaDataMethods inherited from interface org.codehaus.groovy.ast.Variable
isClosureSharedVariable, isFinal, isPrivate, isProtected, isPublic, isStatic, isVolatile, setClosureSharedVariable
-
Constructor Details
-
FieldNode
public FieldNode(String name, int modifiers, ClassNode type, ClassNode owner, Expression initialValueExpression) Creates a field node representing a class member variable. The field's type can be dynamically determined based on the provided ClassNode. -
FieldNode
protected FieldNode()
-
-
Method Details
-
newStatic
public static FieldNode newStatic(Class theClass, String name) throws SecurityException, NoSuchFieldException Creates a new FieldNode wrapping a reflection Field from a Java class. Extracts type information from the reflected field and creates an AST representation with public static access modifiers matching the source field.- Parameters:
theClass- the class containing the reflected fieldname- the field name- Returns:
- a new FieldNode representing the reflected field
- Throws:
SecurityException- if field access is deniedNoSuchFieldException- if the field does not exist
-
getInitialExpression
Returns the initial value expression for this field. The expression is evaluated when the field is initialized. Returns null if no initializer is present.- Specified by:
getInitialExpressionin interfaceVariable- Returns:
- the
Expressionproviding the initial value, or null
-
getName
Returns the field name. -
getType
Returns the declared type of this field as aClassNode. The type may be dynamically determined or may reference generics. -
setType
Sets the field's type. Updates both the current type and tracks the origin type. If the type is dynamic, marks this field as dynamically typed.- Parameters:
type- the field'sClassNodetype
-
getOwner
Returns the class that declares this field.- Returns:
- the declaring
ClassNode, or null if not yet set
-
isHolder
public boolean isHolder()Checks whether this field is a holder field. Holder fields are used internally for certain compilation strategies.- Returns:
- true if this field is marked as a holder
-
setHolder
public void setHolder(boolean holder) Marks this field as a holder field for internal compilation purposes.- Parameters:
holder- true to mark as a holder field
-
isDynamicTyped
public boolean isDynamicTyped()Checks whether this field's type is dynamically typed (unresolved type). Dynamic typing occurs when the exact type cannot be determined at compile time.- Specified by:
isDynamicTypedin interfaceVariable- Returns:
- true if this field uses dynamic typing
-
getModifiers
public int getModifiers()Returns the ASM modifier flags for this field. Flags include visibility (public/protected/private), static, final, transient, volatile, etc.- Specified by:
getModifiersin interfaceVariable- Returns:
- ASM opcode flags representing this field's modifiers
- See Also:
-
Opcodes
-
setModifiers
public void setModifiers(int modifiers) Sets the ASM modifier flags for this field. Allows updating visibility or other modifier flags during compilation.- Parameters:
modifiers- ASM opcode flags to set
-
hasInitialExpression
public boolean hasInitialExpression()Checks whether this field has an initial value expression.- Specified by:
hasInitialExpressionin interfaceVariable- Returns:
- true if an initializer expression is present
-
isInStaticContext
public boolean isInStaticContext()Checks whether this field is in a static context. A field is in a static context if it has the static modifier.- Specified by:
isInStaticContextin interfaceVariable- Returns:
- true if this field is static
-
isEnum
public boolean isEnum()Checks whether this field is an enum constant. Enum constants are fields with the ACC_ENUM access flag.- Returns:
- true if this field is an enum constant
-
setOwner
Sets the class that declares this field. Used during AST construction to establish the ownership relationship.- Parameters:
owner- the declaringClassNode
-
getInitialValueExpression
Returns the initial value expression for this field. Provides direct access to the initializer expression if one is defined.- Returns:
- the
Expressionproviding initial value, or null if absent
-
setInitialValueExpression
Sets the initial value expression for this field. The expression will be evaluated during field initialization.- Parameters:
initialValueExpression- theExpressionfor initialization, or null
-
equals
-
getOriginType
Returns the original declared type of this field before any transformations. Useful for preserving type information through compilation phases that may modify types.- Specified by:
getOriginTypein interfaceVariable- Returns:
- the
ClassNoderepresenting the original type
-
setOriginType
Sets the original type information for this field. Typically set during type transformation to preserve original type metadata.- Parameters:
cn- the originalClassNodetype
-
rename
Renames this field within its declaring class. Updates both the AST node and the declaring class's field registry.- Parameters:
name- the new field name
-