Package asteroid.utils
Class NodeUtils
- java.lang.Object
-
- asteroid.utils.NodeUtils
-
-
Field Summary
Fields Modifier and Type Field Description static StringANNOTATION_VALUE
-
Constructor Summary
Constructors Constructor Description NodeUtils()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddAfterCodeBlock(MethodNode methodNode, Statement statement)Adds the statement passed as argument after the current method codevoidaddAroundCodeBlock(MethodNode methodNode, Statement before, Statement after)Adds two different statements to be executed before and after the current method codevoidaddBeforeCodeBlock(MethodNode methodNode, Statement statement)Adds the statement passed as argument before the current method codevoidaddCheckTo(MethodNode methodNode)Adds checks to the method node passed as parametervoidaddGeneratedField(ClassNode classNode, FieldNode fieldNode)voidaddGeneratedFieldIfNotPresent(ClassNode classNode, FieldNode fieldNode)voidaddGeneratedMethod(ClassNode classNode, MethodNode methodNode)Adds the method marked asGeneratedto the class node passed as first argumentvoidaddGeneratedMethodIfNotPresent(ClassNode classNode, MethodNode methodNode)Adds a method marked asGeneratedto the class node passed as first argument only if it wasn't present in the first placevoidaddImport(ClassNode classNode, Class clazz)Adds an import to theModuleNodecontaining theClassNodepassed as first argument.voidaddImport(ClassNode classNode, Class clazz, String alias)Adds an import to theModuleNodecontaining theClassNodepassed as first argument.voidaddImport(ClassNode classNode, String clazz)Adds an import to theModuleNodecontaining theClassNodepassed as first argument.voidaddImport(ClassNode classNode, String clazz, String alias)Adds an import to theModuleNodecontaining theClassNodepassed as first argument.voidaddInterfaces(ClassNode classNode, Class... interfaces)Makes theClassNodeto implement the interfaces passed as arguments
IMPORTANT: Dont use this method at anyCompilePhasebefore SEMANTIC_ANALYSIS.voidaddInterfaces(ClassNode classNode, ClassNode... interfaces)Makes theClassNodeto implement the interfaces passed as arguments.voidaddMethod(ClassNode classNode, MethodNode methodNode)Adds the method to the class node passed as first argumentvoidaddMethodIfNotPresent(ClassNode classNode, MethodNode methodNode)Adds a method to the class node passed as first argument only if it wasn't present in the first placevoidaddProperty(ClassNode classNode, PropertyNode propertyNode)Adds the property to the class node passed as first argumentvoidaddPropertyIfNotPresent(ClassNode classNode, PropertyNode propertyNode)Adds a property to the class node passed as first argument only if it wasn't present in the first placeList<MethodNode>findAllMethodByName(ClassNode classNode, String methodName)Returns allMethodNodefound with a given name in a specificClassNodeFieldNodefindFieldByName(ClassNode node, String name)Looks for aFieldNodewith a name as the one passed as parameter.MethodNodefindMethodByName(ClassNode classNode, String methodName)Returns the firstMethodNodefound with a given name in a specificClassNode<T> Tget(AnnotationNode annotationNode, Class<T> clazz)Returns the value of a given annotation member.<T> Tget(AnnotationNode annotationNode, String name, Class<T> clazz)Returns the value of a member of theAnnotationNodepassed as first parameter with the name passed as second parameter.AnnotationNodegetAnnotationFrom(ClassNode classNode, String simpleName)Retrieves an annotation with a specific simple name.AnnotationNodegetAnnotationFrom(ClassNode classNode, ClassNode annotationType)Gets a given annotation node from theClassNodepassed as first argument.BlockStatementgetCodeBlock(MethodNode methodNode)Utility method to make your transformation code more compile static compliant when getting a method node code block.<T> TgetFirstNodeAs(ASTNode[] nodes, Class<T> clazz)Returns the first node of the list passed as parameter with the expected type TList<FieldNode>getInstancePropertyFields(ClassNode classNode)Returns all properties from a givenClassNodepassed as argument<T> TgetLastNodeAs(ASTNode[] nodes, Class<T> clazz)Returns the last node of the list passed as parameter with the expected type TStringgetStringValue(AnnotationNode annotationNode)Returns theStringrepresentation of the member "value" of the annotation passed as parameterBooleanhasField(ClassNode node, String fieldName)BooleanhasFieldOfType(ClassNode node, String qualifiedName)Returns whether if theClassNodepassed as first parameter has any field with the type matching the qualified name passed as second parameter.BooleanisOrExtends(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 otherwiseBooleanisOrExtends(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 otherwiseBooleanisOrExtends(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 otherwiseBooleanisOrExtendsUnsafe(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.BooleanisOrImplements(Class child, Class parent)Returns true if the classNode passed as first argument is of type `clazz` or that class implements `parent`, false otherwiseBooleanisOrImplements(ClassNode child, Class parent)Returns true if the classNode passed as first argument is of type `clazz` or that class implements `parent`, false otherwiseBooleanisOrImplements(ClassNode child, String parent)Returns true if the classNode passed as first argument is of type `clazz` or that class implements `parent`, false otherwisevoidremoveAnnotation(AnnotatedNode annotated, AnnotationNode annotation)It removes theAnnotationNodefrom a givenAnnotatedNode
-
-
-
Field Detail
-
ANNOTATION_VALUE
public static final String ANNOTATION_VALUE
- See Also:
- Constant Field Values
-
-
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 nodesclazz- 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 nodesclazz- the type of the expected value- Returns:
- a node with type T
- Since:
- 0.3.0
-
getStringValue
public String getStringValue(AnnotationNode annotationNode)
Returns theStringrepresentation 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 fromclazz- 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 theAnnotationNodepassed 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 theClasspassed as third parameter:- If instance of
ClassExpressionaStringwith the qualified name will be returned - If instance of
ConstantExpressionthe raw value will be returned - If isntance of
PropertyExpressionthe value will be converted to String
Class- Parameters:
annotationNode- the annotation we want a member value fromname- the name of the member we want the value fromclazz- the clazz of the expected value- Since:
- 0.3.0
- If instance of
-
addGeneratedFieldIfNotPresent
public void addGeneratedFieldIfNotPresent(ClassNode classNode, FieldNode fieldNode)
-
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 topropertyNode- 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 topropertyNode- 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 tomethodNode- the method we want to add- Since:
- 0.3.0
-
addGeneratedMethod
public void addGeneratedMethod(ClassNode classNode, MethodNode methodNode)
Adds the method marked asGeneratedto the class node passed as first argument- Parameters:
classNode- the class we want to add the method tomethodNode- 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 tomethodNode- the method we want to add- Since:
- 0.3.0
-
addGeneratedMethodIfNotPresent
public void addGeneratedMethodIfNotPresent(ClassNode classNode, MethodNode methodNode)
Adds a method marked asGeneratedto 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 tomethodNode- the method we want to add- Since:
- 0.4.3
-
addInterfaces
public void addInterfaces(ClassNode classNode, Class... interfaces)
Makes theClassNodeto implement the interfaces passed as arguments
IMPORTANT: Dont use this method at anyCompilePhasebefore SEMANTIC_ANALYSIS. Classes may have not been set at this point.- Parameters:
classNode- The class we want to add the interfaces tointerfaces- the interfaces we want the class node to be implementing- Since:
- 0.3.0
-
addInterfaces
public void addInterfaces(ClassNode classNode, ClassNode... interfaces)
Makes theClassNodeto implement the interfaces passed as arguments.
-
getInstancePropertyFields
public List<FieldNode> getInstancePropertyFields(ClassNode classNode)
Returns all properties from a givenClassNodepassed as argument
-
getAnnotationFrom
public AnnotationNode getAnnotationFrom(ClassNode classNode, ClassNode annotationType)
Gets a given annotation node from theClassNodepassed as first argument.- Parameters:
classNode- the class node annotated with the annotation we're looking forannotationType- 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 forsimpleName- the annotation type simple name- Returns:
- the annotation node if found, null otherwise
- Since:
- 0.3.0
-
removeAnnotation
public void removeAnnotation(AnnotatedNode annotated, AnnotationNode annotation)
It removes theAnnotationNodefrom a givenAnnotatedNode- Parameters:
annotated- theAnnotatedNodeto remove the annotation fromannotation- theAnnotationNodeyou want to remove- Since:
- 0.3.0
-
findFieldByName
public FieldNode findFieldByName(ClassNode node, String name)
Looks for aFieldNodewith 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 bename- the name of the field- Returns:
- if found, and instance of
FieldNodenull otherwise - Since:
- 0.4.3
-
hasFieldOfType
public Boolean hasFieldOfType(ClassNode node, String qualifiedName)
Returns whether if theClassNodepassed as first parameter has any field with the type matching the qualified name passed as second parameter.- Parameters:
node- The node under testqualifiedName- 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
-
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
-
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
-
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
-
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
-
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- theClassNodewe are checkingparent- 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.
-
findMethodByName
public MethodNode findMethodByName(ClassNode classNode, String methodName)
Returns the firstMethodNodefound with a given name in a specificClassNode- Parameters:
classNode- theClassNodethe method should be foundmethodName- the method name- Returns:
- an instance of
MethodNodeif found - Since:
- 0.3.0
-
findAllMethodByName
public List<MethodNode> findAllMethodByName(ClassNode classNode, String methodName)
Returns allMethodNodefound with a given name in a specificClassNode- Parameters:
classNode- theClassNodethe method should be foundmethodName- the method name- Returns:
- an instance of
MethodNodeif found - Since:
- 0.3.0
-
addImport
public void addImport(ClassNode classNode, Class clazz)
Adds an import to theModuleNodecontaining theClassNodepassed as first argument.
-
addImport
public void addImport(ClassNode classNode, String clazz)
Adds an import to theModuleNodecontaining theClassNodepassed as first argument.- Parameters:
classNode- theClassNodewhere the import will be addedclazz- 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 theModuleNodecontaining theClassNodepassed as first argument.
-
addImport
public void addImport(ClassNode classNode, String clazz, String alias)
Adds an import to theModuleNodecontaining theClassNodepassed as first argument.- Parameters:
classNode- theClassNodewhere the import will be addedclazz- the string representing the qualified class of the importalias- 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 aBlockStatement.- 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 beforeafter- to be executed after- Since:
- 0.3.0
-
-