Class NodeUtils


  • public class NodeUtils
    extends Object
    General utility methods to deal with ASTNode instances
    Since:
    0.1.4
    See Also:
    Utils
    • Constructor Detail

      • NodeUtils

        public NodeUtils()
    • Method Detail

      • addCheckTo

        public void addCheckTo​(MethodNode methodNode)
        Adds checks to the method node passed as parameter
        Parameters:
        methodNode - the node we want to add the checks to
        Since:
        0.3.0
      • getFirstNodeAs

        public <T> T getFirstNodeAs​(ASTNode[] nodes,
                                    Class<T> clazz)
        Returns the first node of the list passed as parameter with the expected type T
        Type Parameters:
        T - the expected type
        Parameters:
        nodes - a list of nodes
        clazz - the type of the expected value
        Returns:
        a node with type T
        Since:
        0.3.0
      • getLastNodeAs

        public <T> T getLastNodeAs​(ASTNode[] nodes,
                                   Class<T> clazz)
        Returns the last node of the list passed as parameter with the expected type T
        Type Parameters:
        T - the expected type
        Parameters:
        nodes - a list of nodes
        clazz - the type of the expected value
        Returns:
        a node with type T
        Since:
        0.3.0
      • getStringValue

        public String getStringValue​(AnnotationNode annotationNode)
        Returns the String representation of the member "value" of the annotation passed as parameter
        Parameters:
        annotationNode - The node we want the value from
        Returns:
        the value of the member "value" as a String
        Since:
        0.3.0
      • get

        public <T> T get​(AnnotationNode annotationNode,
                         Class<T> clazz)
        Returns the value of a given annotation member. The value is casted to the type passed as second argument.
        Type Parameters:
        T - The expected return type
        Parameters:
        annotationNode - The node we want the member value from
        clazz - the type of value expected
        Returns:
        an instance of type T
        Since:
        0.3.0
      • get

        public <T> T get​(AnnotationNode annotationNode,
                         String name,
                         Class<T> clazz)
        Returns the value of a member of the AnnotationNode passed as first parameter with the name passed as second parameter. The third parameter is the expected type for the member. There are a set of rules before trying to convert values to the Class passed as third parameter: Then there will be a cast to the specific Class
        Parameters:
        annotationNode - the annotation we want a member value from
        name - the name of the member we want the value from
        clazz - the clazz of the expected value
        Since:
        0.3.0
      • addGeneratedFieldIfNotPresent

        public void addGeneratedFieldIfNotPresent​(ClassNode classNode,
                                                  FieldNode fieldNode)
        Adds a FieldNode annotated with Generated to the ClassNode passed as argument ONLY if it wasn't added before
        Parameters:
        classNode - ClassNode to add the field to
        fieldNode - FieldNode to add to the class node
        Since:
        0.4.3
      • addProperty

        public void addProperty​(ClassNode classNode,
                                PropertyNode propertyNode)
        Adds the property to the class node passed as first argument
        Parameters:
        classNode - the class we want to add the property to
        propertyNode - the property we want to add
        Since:
        0.3.0
      • addPropertyIfNotPresent

        public void addPropertyIfNotPresent​(ClassNode classNode,
                                            PropertyNode propertyNode)
        Adds a property to the class node passed as first argument only if it wasn't present in the first place
        Parameters:
        classNode - the class we want to add the property to
        propertyNode - the property we want to add
        Since:
        0.3.0
      • addMethod

        public void addMethod​(ClassNode classNode,
                              MethodNode methodNode)
        Adds the method to the class node passed as first argument
        Parameters:
        classNode - the class we want to add the method to
        methodNode - the method we want to add
        Since:
        0.3.0
      • addGeneratedMethod

        public void addGeneratedMethod​(ClassNode classNode,
                                       MethodNode methodNode)
        Adds the method marked as Generated to the class node passed as first argument
        Parameters:
        classNode - the class we want to add the method to
        methodNode - the method we want to add
        Since:
        0.4.3
      • addMethodIfNotPresent

        public void addMethodIfNotPresent​(ClassNode classNode,
                                          MethodNode methodNode)
        Adds a method to the class node passed as first argument only if it wasn't present in the first place
        Parameters:
        classNode - the class we want to add the method to
        methodNode - the method we want to add
        Since:
        0.3.0
      • addGeneratedMethodIfNotPresent

        public void addGeneratedMethodIfNotPresent​(ClassNode classNode,
                                                   MethodNode methodNode)
        Adds a method marked as Generated to the class node passed as first argument only if it wasn't present in the first place
        Parameters:
        classNode - the class we want to add the method to
        methodNode - the method we want to add
        Since:
        0.4.3
      • addInterfaces

        public void addInterfaces​(ClassNode classNode,
                                  Class... interfaces)
        Makes the ClassNode to implement the interfaces passed as arguments

        IMPORTANT: Dont use this method at any CompilePhase before SEMANTIC_ANALYSIS. Classes may have not been set at this point.
        Parameters:
        classNode - The class we want to add the interfaces to
        interfaces - the interfaces we want the class node to be implementing
        Since:
        0.3.0
      • addInterfaces

        public void addInterfaces​(ClassNode classNode,
                                  ClassNode... interfaces)
        Makes the ClassNode to implement the interfaces passed as arguments.
        Parameters:
        classNode - the ClassNode we want to implement certain interfaces
        interfaces - the interfaces we want our ClassNode to implement
        Since:
        0.3.0
      • getInstancePropertyFields

        public List<FieldNode> getInstancePropertyFields​(ClassNode classNode)
        Returns all properties from a given ClassNode passed as argument
        Parameters:
        classNode - the ClassNode we want its properties from
        Returns:
        a list of the properties (FieldNode) of a given ClassNode
        Since:
        0.3.0
      • getAnnotationFrom

        public AnnotationNode getAnnotationFrom​(ClassNode classNode,
                                                ClassNode annotationType)
        Gets a given annotation node from the ClassNode passed as first argument.
        Parameters:
        classNode - the class node annotated with the annotation we're looking for
        annotationType - the annotation class node
        Returns:
        the annotation type if found, null otherwise
        Since:
        0.3.0
      • getAnnotationFrom

        public AnnotationNode getAnnotationFrom​(ClassNode classNode,
                                                String simpleName)
        Retrieves an annotation with a specific simple name. A simple name stands for the qualified name of the type minus the package name, e.g if we had a type "groovy.transform.ToString" the simple name would be "ToString".
        Parameters:
        classNode - the class node annotated with the annotation we're looking for
        simpleName - the annotation type simple name
        Returns:
        the annotation node if found, null otherwise
        Since:
        0.3.0
      • hasField

        public Boolean hasField​(ClassNode node,
                                String fieldName)
        Checks whether the ClassNode passed as a parameter has a FieldNode with the name passed as second argument
        Parameters:
        node - ClassNode to look the field from
        fieldName - the name of the field we're looking for
        Returns:
        true if the ClassNode has a field with the name
        Since:
        0.4.3
      • findFieldByName

        public FieldNode findFieldByName​(ClassNode node,
                                         String name)
        Looks for a FieldNode with a name as the one passed as parameter. Returns null if the field is not found.
        Parameters:
        node - the class where the field is supposed to be
        name - the name of the field
        Returns:
        if found, and instance of FieldNode null otherwise
        Since:
        0.4.3
      • hasFieldOfType

        public Boolean hasFieldOfType​(ClassNode node,
                                      String qualifiedName)
        Returns whether if the ClassNode passed as first parameter has any field with the type matching the qualified name passed as second parameter.
        Parameters:
        node - The node under test
        qualifiedName - The qualified name of the type
        Returns:
        true if there is any field with the given type, false otherwise
        Since:
        0.3.0
      • isOrImplements

        public Boolean isOrImplements​(Class child,
                                      Class parent)
        Returns true if the classNode passed as first argument is of type `clazz` or that class implements `parent`, false otherwise
        Parameters:
        child - the Class we are checking
        parent - the Class we are testing against
        Returns:
        true if the classNode is of type `clazz`
        Since:
        0.3.0
      • isOrImplements

        public Boolean isOrImplements​(ClassNode child,
                                      Class parent)
        Returns true if the classNode passed as first argument is of type `clazz` or that class implements `parent`, false otherwise
        Parameters:
        child - the ClassNode we are checking
        parent - the Class we are testing against
        Returns:
        true if the classNode is of type `parent`
        Since:
        0.3.0
      • isOrImplements

        public Boolean isOrImplements​(ClassNode child,
                                      String parent)
        Returns true if the classNode passed as first argument is of type `clazz` or that class implements `parent`, false otherwise
        Parameters:
        child - the ClassNode we are checking
        parent - the qualified name of the Class we are testing against
        Returns:
        true if the classNode is of type `clazz`
        Since:
        0.3.0
      • isOrExtends

        public Boolean isOrExtends​(ClassNode child,
                                   Class parent)
        Returns true if the classNode passed as first argument is of type `clazz` or that class extends the other class, false otherwise
        Parameters:
        child - the ClassNode we are checking
        parent - the Class we are testing against
        Returns:
        true if the classNode is of type `parent`
        Since:
        0.3.0
      • isOrExtends

        public Boolean isOrExtends​(ClassNode child,
                                   ClassNode parent)
        Returns true if the classNode passed as first argument is of type `clazz` or that class extends the other class, false otherwise
        Parameters:
        child - the ClassNode we are checking
        parent - the ClassNode we are testing against
        Returns:
        true if the classNode is of type `parent`
        Since:
        0.3.0
      • isOrExtends

        public Boolean isOrExtends​(ClassNode child,
                                   String parent)
        Returns true if the classNode passed as first argument is of type `clazz` or that class extends the other class, false otherwise
        Parameters:
        child - the ClassNode we are checking
        parent - the qualified name of the class we are testing against
        Returns:
        true if the classNode is of type `parent`
        Since:
        0.3.0
      • isOrExtendsUnsafe

        public Boolean isOrExtendsUnsafe​(ClassNode child,
                                         ClassNode parent)
        Returns true if the classNode passed as first argument is of type `clazz` or that class extends the other class, false otherwise. It's unsafe because it tries to compare also the super class simple name. So two classes having the same name even in different packages will return true.
        Parameters:
        child - the ClassNode we are checking
        parent - the ClassNode we are testing against
        Returns:
        true if the classNode is of type `parent`
        Since:
        0.3.0
      • findMethodByName

        public MethodNode findMethodByName​(ClassNode classNode,
                                           String methodName)
        Returns the first MethodNode found with a given name in a specific ClassNode
        Parameters:
        classNode - the ClassNode the method should be found
        methodName - the method name
        Returns:
        an instance of MethodNode if found
        Since:
        0.3.0
      • addImport

        public void addImport​(ClassNode classNode,
                              Class clazz)
        Adds an import to the ModuleNode containing the ClassNode passed as first argument.
        Parameters:
        classNode - the ClassNode where the import will be added
        clazz - the type Class of the import
        Since:
        0.3.0
      • addImport

        public void addImport​(ClassNode classNode,
                              String clazz)
        Adds an import to the ModuleNode containing the ClassNode passed as first argument.
        Parameters:
        classNode - the ClassNode where the import will be added
        clazz - the string representing the qualified class of the import
        Since:
        0.3.0
      • addImport

        public void addImport​(ClassNode classNode,
                              Class clazz,
                              String alias)
        Adds an import to the ModuleNode containing the ClassNode passed as first argument.
        Parameters:
        classNode - the ClassNode where the import will be added
        clazz - the type Class of the import
        alias - an alias to avoid class collisions
        Since:
        0.3.0
      • addImport

        public void addImport​(ClassNode classNode,
                              String clazz,
                              String alias)
        Adds an import to the ModuleNode containing the ClassNode passed as first argument.
        Parameters:
        classNode - the ClassNode where the import will be added
        clazz - the string representing the qualified class of the import
        alias - an alias to avoid class collisions
        Since:
        0.3.0
      • getCodeBlock

        public BlockStatement getCodeBlock​(MethodNode methodNode)
        Utility method to make your transformation code more compile static compliant when getting a method node code block. Many times you may want to deal with it as if it were a BlockStatement.
        Parameters:
        methodNode - the method we want the code from
        Returns:
        the method code as a BlockStatement
      • addBeforeCodeBlock

        public void addBeforeCodeBlock​(MethodNode methodNode,
                                       Statement statement)
        Adds the statement passed as argument before the current method code
        Parameters:
        statement - the statement to be executed before current code
        Since:
        0.3.0
      • addAfterCodeBlock

        public void addAfterCodeBlock​(MethodNode methodNode,
                                      Statement statement)
        Adds the statement passed as argument after the current method code
        Parameters:
        statement - the statement to be executed after current code
        Since:
        0.3.0
      • addAroundCodeBlock

        public void addAroundCodeBlock​(MethodNode methodNode,
                                       Statement before,
                                       Statement after)
        Adds two different statements to be executed before and after the current method code
        Parameters:
        before - to be executed before
        after - to be executed after
        Since:
        0.3.0