Package asteroid
Class Criterias
- java.lang.Object
-
- asteroid.Criterias
-
public final class Criterias extends Object
This class contains out-of-the-box criterias, they are used to help out finding specific nodes. They can be used in transformer constructors or when filtering lists of nodes.
ACCESS
You can access directly criterias fromA.CRITERIAor by importing this class directly.
USE CASES
Because transformers traverse a given AST tree, they need to know which nodes they're interested in and leave the rest, and criterias are exactly built to do that.
Transformers
In this case the constructor receives as second argument a criteria to apply the transformation only over a specific inner class. Although criterias were targeted for transformers they could be also used in any other transformation.class AddPropertyToInnerClass extends AbstractClassNodeTransformer { AddPropertyToInnerClass(final SourceUnit sourceUnit) { super(sourceUnit, A.CRITERIA.byClassNodeNameContains('AddTransformerSpecExample$Input')) } }
But the way criterias were designed, make them also suitable for using them to filter a list of especific nodes, expressions or statements in any other situation.
Filtering
This last example could be a normal case in a local transformation where you are interested in checking the existence of a certain node in order to take a decision during the transformation.Listfinders = classNode .getMethods() .findAll(and(byMethodNodeNameStartsWith('find'), byMethodNodeNameContains('By'))) - Since:
- 0.2.4
-
-
Constructor Summary
Constructors Constructor Description Criterias()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Closure<Boolean>and(Closure<Boolean>... fns)Combines twoClosureexpressions returning a boolean.static <T extends AnnotatedNode>
Closure<Boolean>byAnnotation(Class annotationClazz)Criteria to find those annotated nodes with an annotation with aClass.static <T extends AnnotatedNode>
Closure<Boolean>byAnnotationSimpleName(String annotationName)Criteria to find those annotated nodes with an annotation with aClasswith a name as the passed argument.static <T extends ClassNode>
Closure<Boolean>byClassNodeNameContains(String term)Criteria to find those classes with a name containing the term passed as parameterstatic <T extends ClassNode>
Closure<Boolean>byClassNodeNameEndsWith(String term)Criteria to find those classes with a name containing the term passed as parameter at the end.static <T extends ClassNode>
Closure<Boolean>byClassNodeNameStartsWith(String term)Criteria to find those classes with a name containing the term passed as parameter at the beginning.static <T extends Expression>
Closure<Boolean>byExprAny()This criteria will make the transformer to process everyExpressionstatic <T extends Expression>
Closure<Boolean>byExprBinaryUsingToken(int tokenType)Checks that a givenBinaryExpressionuses a specific token type.static Closure<Boolean>byExprMethodCallByArgs(Class... argTypes)This method returns a criteria to look forMethodCallExpressionwith arguments with the types specified as parametersstatic <T extends Expression>
Closure<Boolean>byExprMethodCallByName(String name)This method returns a criteria to look forMethodCallExpressionwith a name equals to the name passed as parameterstatic <T extends MethodNode>
Closure<Boolean>byMethodNodeName(String methodName)Criteria to find those methods with a specific namestatic <T extends MethodNode>
Closure<Boolean>byMethodNodeNameContains(String term)Criteria to find those methods with a name containing the term passed as parameterstatic <T extends MethodNode>
Closure<Boolean>byMethodNodeNameEndsWith(String suffix)Criteria to find those methods with a name containing the term passed as parameter at the end.static <T extends MethodNode>
Closure<Boolean>byMethodNodeNameStartsWith(String prefix)Criteria to find those methods with a name containing the term passed as parameter at the beginning.static <T extends Statement>
Closure<Boolean>byStmtByType(Class<T> stmtClass)This method returns a criteria to look forStatementwith a specific typestatic Closure<Boolean>or(Closure<Boolean>... fns)Combines twoClosureexpressions returning a boolean.
-
-
-
Method Detail
-
byAnnotation
public static <T extends AnnotatedNode> Closure<Boolean> byAnnotation(Class annotationClazz)
Criteria to find those annotated nodes with an annotation with aClass.
ONLY use in a compilation phase where type information is available (from SEMANTIC_ANALYSIS forwards)- Parameters:
annotationClazz- the type of the annotation- Returns:
- a criteria to look for annotated nodes annotated with a given type
- Since:
- 0.2.4
-
byAnnotationSimpleName
public static <T extends AnnotatedNode> Closure<Boolean> byAnnotationSimpleName(String annotationName)
Criteria to find those annotated nodes with an annotation with aClasswith a name as the passed argument. This name should be the same as usingClass.getSimpleName()
This method doesn't use aClassas argument cause the package (type information) won't be available for earlierCompilePhase- Parameters:
annotationName- the simple name of theClassof the annotation used as markero- Returns:
- a criteria to look for annotated nodes annotated with a given type
- Since:
- 0.2.4
-
byMethodNodeName
public static <T extends MethodNode> Closure<Boolean> byMethodNodeName(String methodName)
Criteria to find those methods with a specific name- Parameters:
methodName- the name returned byMethodNode.getName()- Returns:
- a criteria that can be used in a
AbstractMethodNodeTransformerconstructor - Since:
- 0.2.4
-
byMethodNodeNameContains
public static <T extends MethodNode> Closure<Boolean> byMethodNodeNameContains(String term)
Criteria to find those methods with a name containing the term passed as parameter- Parameters:
term- the term contained in theMethodNodename- Returns:
- a criteria that can be used in a
AbstractMethodNodeTransformerconstructor - Since:
- 0.2.4
-
byMethodNodeNameEndsWith
public static <T extends MethodNode> Closure<Boolean> byMethodNodeNameEndsWith(String suffix)
Criteria to find those methods with a name containing the term passed as parameter at the end.- Parameters:
suffix- the term at the end of theMethodNodename- Returns:
- a criteria that can be used in a
AbstractMethodNodeTransformerconstructor - Since:
- 0.2.4
-
byMethodNodeNameStartsWith
public static <T extends MethodNode> Closure<Boolean> byMethodNodeNameStartsWith(String prefix)
Criteria to find those methods with a name containing the term passed as parameter at the beginning.- Parameters:
prefix- at the beginning of theMethodNodename- Returns:
- a criteria that can be used in a
AbstractMethodNodeTransformerconstructor - Since:
- 0.2.4
-
byClassNodeNameContains
public static <T extends ClassNode> Closure<Boolean> byClassNodeNameContains(String term)
Criteria to find those classes with a name containing the term passed as parameter- Parameters:
term- the term contained in theClassNodename- Returns:
- a criteria that can be used in a
AbstractClassNodeTransformerconstructor - Since:
- 0.2.4
-
byClassNodeNameEndsWith
public static <T extends ClassNode> Closure<Boolean> byClassNodeNameEndsWith(String term)
Criteria to find those classes with a name containing the term passed as parameter at the end.- Parameters:
term- the term at the end of theClassNodename- Returns:
- a criteria that can be used in a
AbstractClassNodeTransformerconstructor - Since:
- 0.2.4
-
byClassNodeNameStartsWith
public static <T extends ClassNode> Closure<Boolean> byClassNodeNameStartsWith(String term)
Criteria to find those classes with a name containing the term passed as parameter at the beginning.- Parameters:
term- at the beginning of theClassNodename- Returns:
- a criteria that can be used in a
AbstractClassNodeTransformerconstructor - Since:
- 0.2.4
-
byExprMethodCallByName
public static <T extends Expression> Closure<Boolean> byExprMethodCallByName(String name)
This method returns a criteria to look forMethodCallExpressionwith a name equals to the name passed as parameter- Parameters:
name- the method name- Returns:
- a search criteria
- Since:
- 0.2.4
-
byExprMethodCallByArgs
public static Closure<Boolean> byExprMethodCallByArgs(Class... argTypes)
This method returns a criteria to look forMethodCallExpressionwith arguments with the types specified as parameters- Parameters:
argTypes- the classes of every parameter- Returns:
- a search criteria
- Since:
- 0.2.9
-
byExprAny
public static <T extends Expression> Closure<Boolean> byExprAny()
This criteria will make the transformer to process everyExpression- Returns:
- a criteria to process everything
- Since:
- 0.2.4
-
byExprBinaryUsingToken
public static <T extends Expression> Closure<Boolean> byExprBinaryUsingToken(int tokenType)
Checks that a givenBinaryExpressionuses a specific token type. The token type is an `int` value. You can useTypeswhere all token types are declared.
-
byStmtByType
public static <T extends Statement> Closure<Boolean> byStmtByType(Class<T> stmtClass)
This method returns a criteria to look forStatementwith a specific type- Parameters:
stmtClass- the typeClass- Returns:
- a search criteria
- Since:
- 0.2.4
-
or
public static Closure<Boolean> or(Closure<Boolean>... fns)
Combines twoClosureexpressions returning a boolean. The result will be a function that returns true if the parameter passed makes any of the former functions to return true. ASTdef even = { x -> x % 2 == 0 } def positive = { y -> y > 0 } def evenOrPositive = or(even, positive)- Parameters:
fns- functions to combine- Returns:
- a combined
Closure - Since:
- 0.2.4
-
and
public static Closure<Boolean> and(Closure<Boolean>... fns)
Combines twoClosureexpressions returning a boolean. The result will be a function that returns true only if the parameter passed makes all of the former functions to return true. ASTdef even = { x -> x % 2 == 0 } def positive = { y -> y > 0 } def evenAndPositive = and(even, positive)- Parameters:
fns- functions to combine- Returns:
- a combined
Closure - Since:
- 0.2.4
-
-