Package asteroid

Class 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 from A.CRITERIA or 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
    
     class AddPropertyToInnerClass extends AbstractClassNodeTransformer {
         AddPropertyToInnerClass(final SourceUnit sourceUnit) {
             super(sourceUnit,
                   A.CRITERIA.byClassNodeNameContains('AddTransformerSpecExample$Input'))
         }
     }
     
    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.

    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
    
     List finders = classNode
     .getMethods()
     .findAll(and(byMethodNodeNameStartsWith('find'),
                  byMethodNodeNameContains('By')))
     
    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.
    Since:
    0.2.4
    • Constructor Detail

      • Criterias

        public Criterias()
    • Method Detail

      • byAnnotation

        public static <T extends AnnotatedNodeClosure<Boolean> byAnnotation​(Class annotationClazz)
        Criteria to find those annotated nodes with an annotation with a Class.

        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 AnnotatedNodeClosure<Boolean> byAnnotationSimpleName​(String annotationName)
        Criteria to find those annotated nodes with an annotation with a Class with a name as the passed argument. This name should be the same as using Class.getSimpleName()

        This method doesn't use a Class as argument cause the package (type information) won't be available for earlier CompilePhase
        Parameters:
        annotationName - the simple name of the Class of the annotation used as markero
        Returns:
        a criteria to look for annotated nodes annotated with a given type
        Since:
        0.2.4
      • byMethodNodeNameContains

        public static <T extends MethodNodeClosure<Boolean> byMethodNodeNameContains​(String term)
        Criteria to find those methods with a name containing the term passed as parameter
        Parameters:
        term - the term contained in the MethodNode name
        Returns:
        a criteria that can be used in a AbstractMethodNodeTransformer constructor
        Since:
        0.2.4
      • byMethodNodeNameEndsWith

        public static <T extends MethodNodeClosure<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 the MethodNode name
        Returns:
        a criteria that can be used in a AbstractMethodNodeTransformer constructor
        Since:
        0.2.4
      • byMethodNodeNameStartsWith

        public static <T extends MethodNodeClosure<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 the MethodNode name
        Returns:
        a criteria that can be used in a AbstractMethodNodeTransformer constructor
        Since:
        0.2.4
      • byClassNodeNameContains

        public static <T extends ClassNodeClosure<Boolean> byClassNodeNameContains​(String term)
        Criteria to find those classes with a name containing the term passed as parameter
        Parameters:
        term - the term contained in the ClassNode name
        Returns:
        a criteria that can be used in a AbstractClassNodeTransformer constructor
        Since:
        0.2.4
      • byClassNodeNameEndsWith

        public static <T extends ClassNodeClosure<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 the ClassNode name
        Returns:
        a criteria that can be used in a AbstractClassNodeTransformer constructor
        Since:
        0.2.4
      • byClassNodeNameStartsWith

        public static <T extends ClassNodeClosure<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 the ClassNode name
        Returns:
        a criteria that can be used in a AbstractClassNodeTransformer constructor
        Since:
        0.2.4
      • byExprMethodCallByName

        public static <T extends ExpressionClosure<Boolean> byExprMethodCallByName​(String name)
        This method returns a criteria to look for MethodCallExpression with 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 for MethodCallExpression with 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 ExpressionClosure<Boolean> byExprAny()
        This criteria will make the transformer to process every Expression
        Returns:
        a criteria to process everything
        Since:
        0.2.4
      • byExprBinaryUsingToken

        public static <T extends ExpressionClosure<Boolean> byExprBinaryUsingToken​(int tokenType)
        Checks that a given BinaryExpression uses a specific token type. The token type is an `int` value. You can use Types where all token types are declared.
        Parameters:
        tokenType - Check Types for more info
        Returns:
        a Closure used as criteria
        Since:
        0.2.4
        See Also:
        Types
      • byStmtByType

        public static <T extends StatementClosure<Boolean> byStmtByType​(Class<T> stmtClass)
        This method returns a criteria to look for Statement with a specific type
        Parameters:
        stmtClass - the type Class
        Returns:
        a search criteria
        Since:
        0.2.4
      • or

        public static Closure<Boolean> or​(Closure<Boolean>... fns)
        Combines two Closure expressions 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. AST
        def 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 two Closure expressions 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. AST
        def 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