Class ClassNode

  • All Implemented Interfaces:
    org.objectweb.asm.Opcodes
    Direct Known Subclasses:
    CompileUnit.ConstructedOuterNestedClassNode, DecompiledClassNode, InnerClassNode, MixinNode, WideningCategories.LowestUpperBoundClassNode

    public class ClassNode
    extends AnnotatedNode
    implements org.objectweb.asm.Opcodes
    Represents a class in the AST.

    A ClassNode should be created using the methods in ClassHelper. This ClassNode may be used to represent a class declaration or any other type. This class uses a proxy mechanism allowing to create a class for a plain name at AST creation time. In another phase of the compiler the real ClassNode for the plain name may be found. To avoid the need of exchanging this ClassNode with an instance of the correct ClassNode the correct ClassNode is set as redirect. Most method calls are then redirected to that ClassNode.

    There are three types of ClassNodes:

    1. Primary ClassNodes:
      A primary ClassNode is one where we have a source representation which is to be compiled by Groovy and which we have an AST for. The groovy compiler will output one class for each such ClassNode that passes through AsmBytecodeGenerator... not more, not less. That means for example Closures become such ClassNodes too at some point.
    2. ClassNodes create through different sources (typically created from a java.lang.reflect.Class object):
      The compiler will not output classes from these, the methods usually do not contain bodies. These kind of ClassNodes will be used in different checks, but not checks that work on the method bodies. For example if such a ClassNode is a super class to a primary ClassNode, then the abstract method test and others will be done with data based on these. Theoretically it is also possible to mix both (1 and 2) kind of classes in a hierarchy, but this probably works only in the newest Groovy versions. Such ClassNodes normally have to isResolved() returning true without having a redirect.In the Groovy compiler the only version of this, that exists, is a ClassNode created through a Class instance
    3. Labels:
      ClassNodes created through ClassHelper.makeWithoutCaching. They are place holders, its redirect points to the real structure, which can be a label too, but following all redirects it should end with a ClassNode from one of the other two categories. If ResolveVisitor finds such a node, it tries to set the redirects. Any such label created after ResolveVisitor has done its work needs to have a redirect pointing to case 1 or 2. If not the compiler may react strange... this can be considered as a kind of dangling pointer.
    Note: the redirect mechanism is only allowed for classes that are not primary ClassNodes. Typically this is done for classes created by name only. The redirect itself can be any type of ClassNode.

    To describe generic type signature see getGenericsTypes() and setGenericsTypes(GenericsType[]). These methods are not proxied, they describe the type signature used at the point of declaration or the type signatures provided by the class. If the type signatures provided by the class are needed, then a call to redirect() will help.

    See Also:
    ClassHelper
    • Field Detail

      • EMPTY_ARRAY

        public static final ClassNode[] EMPTY_ARRAY
      • isPrimaryNode

        protected boolean isPrimaryNode
      • lazyInitLock

        protected final java.lang.Object lazyInitLock
      • clazz

        protected java.lang.Class clazz
    • Constructor Detail

      • ClassNode

        public ClassNode​(java.lang.Class c)
        Creates a non-primary ClassNode from a real class.
      • ClassNode

        public ClassNode​(java.lang.String name,
                         int modifiers,
                         ClassNode superClass)
        Parameters:
        name - the fully-qualified name of the class
        modifiers - the modifiers; see Opcodes
        superClass - the base class; use "java.lang.Object" if no direct base class
      • ClassNode

        public ClassNode​(java.lang.String name,
                         int modifiers,
                         ClassNode superClass,
                         ClassNode[] interfaces,
                         MixinNode[] mixins)
        Parameters:
        name - the fully-qualified name of the class
        modifiers - the modifiers; see Opcodes
        superClass - the base class; use "java.lang.Object" if no direct base class
        interfaces - the interfaces for this class
        mixins - the mixins for this class
        See Also:
        Opcodes
    • Method Detail

      • redirect

        public ClassNode redirect()
        Returns the ClassNode this node is a proxy for or the node itself.
      • isRedirectNode

        public boolean isRedirectNode()
      • setRedirect

        public void setRedirect​(ClassNode cn)
        Sets this instance as proxy for the given ClassNode.
        Parameters:
        node - the class to redirect to; if null the redirect is removed
      • makeArray

        public ClassNode makeArray()
        Returns a ClassNode representing an array of the type represented by this.
      • isPrimaryClassNode

        public boolean isPrimaryClassNode()
        Returns:
        true if this instance is a primary ClassNode
      • getEnclosingMethod

        public MethodNode getEnclosingMethod()
      • setEnclosingMethod

        public void setEnclosingMethod​(MethodNode enclosingMethod)
      • isSyntheticPublic

        public boolean isSyntheticPublic()
        Indicates that this class has been "promoted" to public by Groovy when in fact there was no public modifier explicitly in the source code. That is, it remembers that it has applied Groovy's "public classes by default" rule. This property is typically only of interest to AST transform writers.
        Returns:
        true if node is public but had no explicit public modifier
      • setSyntheticPublic

        public void setSyntheticPublic​(boolean syntheticPublic)
      • setSuperClass

        public void setSuperClass​(ClassNode superClass)
        Sets the superclass of this ClassNode.
      • getFields

        public java.util.List<FieldNode> getFields()
        Returns:
        the fields associated with this ClassNode
      • getInterfaces

        public ClassNode[] getInterfaces()
        Returns:
        the array of interfaces which this ClassNode implements
      • setInterfaces

        public void setInterfaces​(ClassNode[] interfaces)
      • getMixins

        public MixinNode[] getMixins()
        Returns:
        the mixins associated with this ClassNode
      • getMethods

        public java.util.List<MethodNode> getMethods()
        Returns:
        the methods associated with this ClassNode
      • getAbstractMethods

        public java.util.List<MethodNode> getAbstractMethods()
        Returns:
        the abstract methods associated with this ClassNode
      • getAllDeclaredMethods

        public java.util.List<MethodNode> getAllDeclaredMethods()
      • getAllInterfaces

        public java.util.Set<ClassNode> getAllInterfaces()
      • getDeclaredMethodsMap

        public java.util.Map<java.lang.String,​MethodNode> getDeclaredMethodsMap()
      • getName

        public java.lang.String getName()
      • getUnresolvedName

        public java.lang.String getUnresolvedName()
      • setName

        public java.lang.String setName​(java.lang.String name)
      • getModifiers

        public int getModifiers()
      • setModifiers

        public void setModifiers​(int modifiers)
      • getProperties

        public java.util.List<PropertyNode> getProperties()
      • getDeclaredConstructors

        public java.util.List<ConstructorNode> getDeclaredConstructors()
      • getDeclaredConstructor

        public ConstructorNode getDeclaredConstructor​(Parameter[] parameters)
        Returns:
        the constructor matching the given parameters or null
      • removeConstructor

        public void removeConstructor​(ConstructorNode node)
      • setModule

        public void setModule​(ModuleNode module)
      • addField

        public void addField​(FieldNode node)
      • addFieldFirst

        public void addFieldFirst​(FieldNode node)
      • getFieldIndex

        public java.util.Map<java.lang.String,​FieldNode> getFieldIndex()
      • addProperty

        public void addProperty​(PropertyNode node)
      • hasProperty

        public boolean hasProperty​(java.lang.String name)
      • getProperty

        public PropertyNode getProperty​(java.lang.String name)
      • addMethod

        public void addMethod​(MethodNode node)
      • removeMethod

        public void removeMethod​(MethodNode node)
      • addMethod

        public MethodNode addMethod​(java.lang.String name,
                                    int modifiers,
                                    ClassNode returnType,
                                    Parameter[] parameters,
                                    ClassNode[] exceptions,
                                    Statement code)
        If a method with the given name and parameters is already defined then it is returned otherwise the given method is added to this node. This method is useful for default method adding like getProperty() or invokeMethod() where there may already be a method defined in a class and so the default implementations should not be added if already present.
      • addSyntheticMethod

        public MethodNode addSyntheticMethod​(java.lang.String name,
                                             int modifiers,
                                             ClassNode returnType,
                                             Parameter[] parameters,
                                             ClassNode[] exceptions,
                                             Statement code)
        Adds a synthetic method as part of the compilation process.
      • addInterface

        public void addInterface​(ClassNode type)
      • equals

        public boolean equals​(java.lang.Object that)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • addMixin

        public void addMixin​(MixinNode mixin)
      • getDeclaredField

        public FieldNode getDeclaredField​(java.lang.String name)
        Finds a field matching the given name in this class.
        Parameters:
        name - the name of the field of interest
        Returns:
        the method matching the given name and parameters or null
      • getField

        public FieldNode getField​(java.lang.String name)
        Finds a field matching the given name in this class or a parent class.
        Parameters:
        name - the name of the field of interest
        Returns:
        the method matching the given name and parameters or null
      • getOuterField

        public FieldNode getOuterField​(java.lang.String name)
        Returns:
        the field on the outer class or null if this is not an inner class
      • getOuterClass

        public ClassNode getOuterClass()
      • getOuterClasses

        public java.util.List<ClassNode> getOuterClasses()
      • addObjectInitializerStatements

        public void addObjectInitializerStatements​(Statement statements)
        Adds a statement to the object initializer.
        Parameters:
        statements - the statement to be added
      • getObjectInitializerStatements

        public java.util.List<Statement> getObjectInitializerStatements()
      • addStaticInitializerStatements

        public void addStaticInitializerStatements​(java.util.List<Statement> staticStatements,
                                                   boolean fieldInit)
      • positionStmtsAfterEnumInitStmts

        public void positionStmtsAfterEnumInitStmts​(java.util.List<Statement> staticFieldStatements)
      • getDeclaredMethods

        public java.util.List<MethodNode> getDeclaredMethods​(java.lang.String name)
        This methods returns a list of all methods of the given name defined in the current class
        Returns:
        the method list
        See Also:
        getMethods(String)
      • getMethods

        public java.util.List<MethodNode> getMethods​(java.lang.String name)
        This methods creates a list of all methods with this name of the current class and of all super classes
        Returns:
        the methods list
        See Also:
        getDeclaredMethods(String)
      • getDeclaredMethod

        public MethodNode getDeclaredMethod​(java.lang.String name,
                                            Parameter[] parameters)
        Finds a method matching the given name and parameters in this class.
        Returns:
        the method matching the given name and parameters or null
      • getMethod

        public MethodNode getMethod​(java.lang.String name,
                                    Parameter[] parameters)
        Finds a method matching the given name and parameters in this class or any parent class.
        Returns:
        the method matching the given name and parameters or null
      • isDerivedFrom

        public boolean isDerivedFrom​(ClassNode type)
        Parameters:
        type - the ClassNode of interest
        Returns:
        true if this node is derived from the given ClassNode
      • isDerivedFromGroovyObject

        public boolean isDerivedFromGroovyObject()
        Returns:
        true if this type implements GroovyObject
      • implementsInterface

        public boolean implementsInterface​(ClassNode classNode)
        Parameters:
        classNode - the class node for the interface
        Returns:
        true if this type implements the given interface
      • declaresInterface

        public boolean declaresInterface​(ClassNode classNode)
        Parameters:
        classNode - the class node for the interface
        Returns:
        true if this class declares that it implements the given interface or if one of its interfaces extends directly/indirectly the interface NOTE: Doesn't consider an interface to implement itself. I think this is intended to be called on ClassNodes representing classes, not interfaces.
      • getSuperClass

        public ClassNode getSuperClass()
        Returns:
        the ClassNode of the super class of this type
      • getUnresolvedSuperClass

        public ClassNode getUnresolvedSuperClass()
      • getUnresolvedSuperClass

        public ClassNode getUnresolvedSuperClass​(boolean useRedirect)
      • setUnresolvedSuperClass

        public void setUnresolvedSuperClass​(ClassNode sn)
      • getUnresolvedInterfaces

        public ClassNode[] getUnresolvedInterfaces()
      • getUnresolvedInterfaces

        public ClassNode[] getUnresolvedInterfaces​(boolean useRedirect)
      • setCompileUnit

        protected void setCompileUnit​(CompileUnit cu)
      • parametersEqual

        @Deprecated
        protected boolean parametersEqual​(Parameter[] a,
                                          Parameter[] b)
        Deprecated.
        Returns:
        true if the two arrays are of the same size and have the same contents
      • getPackageName

        public java.lang.String getPackageName()
      • getNameWithoutPackage

        public java.lang.String getNameWithoutPackage()
      • getGetterMethod

        public MethodNode getGetterMethod​(java.lang.String getterName)
      • getGetterMethod

        public MethodNode getGetterMethod​(java.lang.String getterName,
                                          boolean searchSuperClasses)
      • getSetterMethod

        public MethodNode getSetterMethod​(java.lang.String setterName)
      • getSetterMethod

        public MethodNode getSetterMethod​(java.lang.String setterName,
                                          boolean voidOnly)
      • isStaticClass

        public boolean isStaticClass()
        Is this class declared in a static method (such as a closure / inner class declared in a static method)
      • setStaticClass

        public void setStaticClass​(boolean staticClass)
      • isScriptBody

        public boolean isScriptBody()
        Returns:
        true if this inner class or closure was declared inside a script body
      • setScriptBody

        public void setScriptBody​(boolean scriptBody)
      • isScript

        public boolean isScript()
      • setScript

        public void setScript​(boolean script)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • toString

        public java.lang.String toString​(boolean showRedirect)
      • hasPossibleMethod

        public boolean hasPossibleMethod​(java.lang.String name,
                                         Expression arguments)
        Determines if the type has a possibly-matching instance method with the given name and arguments.
        Parameters:
        name - the name of the method of interest
        arguments - the arguments to match against
        Returns:
        true if a matching method was found
      • tryFindPossibleMethod

        public MethodNode tryFindPossibleMethod​(java.lang.String name,
                                                Expression arguments)
      • hasPossibleStaticMethod

        public boolean hasPossibleStaticMethod​(java.lang.String name,
                                               Expression arguments)
        Checks if the given method has a possibly matching static method with the given name and arguments.
        Parameters:
        name - the name of the method of interest
        arguments - the arguments to match against
        Returns:
        true if a matching method was found
      • isInterface

        public boolean isInterface()
      • isAbstract

        public boolean isAbstract()
      • isResolved

        public boolean isResolved()
      • isArray

        public boolean isArray()
      • getComponentType

        public ClassNode getComponentType()
      • getTypeClass

        public java.lang.Class getTypeClass()
        Returns the concrete class this classnode relates to. However, this method is inherently unsafe as it may return null depending on the compile phase you are using. AST transformations should never use this method directly, but rather obtain a new class node using getPlainNodeReference().
        Returns:
        the class this classnode relates to. May return null.
      • hasPackageName

        public boolean hasPackageName()
      • setAnnotated

        public void setAnnotated​(boolean flag)
        Marks if the current class uses annotations or not.
      • isAnnotated

        public boolean isAnnotated()
      • getGenericsTypes

        public GenericsType[] getGenericsTypes()
      • setGenericsTypes

        public void setGenericsTypes​(GenericsType[] genericsTypes)
      • setGenericsPlaceHolder

        public void setGenericsPlaceHolder​(boolean b)
      • isGenericsPlaceHolder

        public boolean isGenericsPlaceHolder()
      • isUsingGenerics

        public boolean isUsingGenerics()
      • setUsingGenerics

        public void setUsingGenerics​(boolean b)
      • getPlainNodeReference

        public ClassNode getPlainNodeReference()
      • isAnnotationDefinition

        public boolean isAnnotationDefinition()
      • renameField

        public void renameField​(java.lang.String oldName,
                                java.lang.String newName)
      • removeField

        public void removeField​(java.lang.String oldName)
      • isEnum

        public boolean isEnum()
      • getInnerClasses

        public java.util.Iterator<InnerClassNode> getInnerClasses()
        Returns:
        iterator of inner classes defined inside this one
      • getText

        public java.lang.String getText()
        Overrides:
        getText in class ASTNode