Class ExprMatcher


  • public class ExprMatcher
    extends Object
    Composable predicate over expressions, used to select edit targets in the AST.
    • Method Detail

      • matches

        public boolean matches​(Expression expr)
        Applies this matcher's predicate.
        Parameters:
        expr - the expression to test, may be null
        Returns:
        true if the predicate accepts it; false if it is null
      • methodCall

        public static ExprMatcher methodCall​(String methodName)
        Matches method calls by method name, whatever the owner.
        Parameters:
        methodName - the method name to require
        Returns:
        the matcher
      • methodCall

        public static ExprMatcher methodCall​(String ownerClass,
                                             String methodName)
        Matches method calls by owner class and method name.
        Parameters:
        ownerClass - the declaring class, in either slashed or dotted form
        methodName - the method name to require
        Returns:
        the matcher
      • methodCall

        public static ExprMatcher methodCall​(String ownerClass,
                                             String methodName,
                                             int argCount)
        Matches method calls by owner class, method name and argument count.
        Parameters:
        ownerClass - the declaring class, in either slashed or dotted form
        methodName - the method name to require
        argCount - the exact number of arguments to require
        Returns:
        the matcher
      • fieldAccess

        public static ExprMatcher fieldAccess​(String fieldName)
        Matches field accesses by field name, whatever the owner.
        Parameters:
        fieldName - the field name to require
        Returns:
        the matcher
      • fieldAccess

        public static ExprMatcher fieldAccess​(String ownerClass,
                                              String fieldName)
        Matches field accesses by owner class and field name.
        Parameters:
        ownerClass - the declaring class, in either slashed or dotted form
        fieldName - the field name to require
        Returns:
        the matcher
      • newExpr

        public static ExprMatcher newExpr​(String className)
        Matches allocations of a specific class.
        Parameters:
        className - the class name, in either slashed or dotted form
        Returns:
        the matcher
      • newArray

        public static ExprMatcher newArray()
        Matches new array expressions.
        Returns:
        the matcher
      • cast

        public static ExprMatcher cast​(String targetType)
        Matches cast expressions to a specific type.
        Parameters:
        targetType - the cast target, in either slashed or dotted form
        Returns:
        the matcher
      • anyCast

        public static ExprMatcher anyCast()
        Matches any cast expression.
        Returns:
        the matcher
      • instanceOf

        public static ExprMatcher instanceOf​(String checkedType)
        Matches instanceof expressions checking a specific type.
        Parameters:
        checkedType - the type name, in either slashed or dotted form
        Returns:
        the matcher
      • anyInstanceOf

        public static ExprMatcher anyInstanceOf()
        Matches any instanceof expression.
        Returns:
        the matcher
      • ofType

        public static ExprMatcher ofType​(Class<? extends Expression> type)
        Matches any expression of a specific node type.
        Parameters:
        type - the expression class to require
        Returns:
        the matcher
      • anyMethodCall

        public static ExprMatcher anyMethodCall()
        Matches all method call expressions.
        Returns:
        the matcher
      • anyFieldAccess

        public static ExprMatcher anyFieldAccess()
        Matches all field access expressions.
        Returns:
        the matcher
      • anyBinary

        public static ExprMatcher anyBinary()
        Matches all binary expressions.
        Returns:
        the matcher
      • anyUnary

        public static ExprMatcher anyUnary()
        Matches all unary expressions.
        Returns:
        the matcher
      • anyLiteral

        public static ExprMatcher anyLiteral()
        Matches all literal expressions.
        Returns:
        the matcher
      • anyArrayAccess

        public static ExprMatcher anyArrayAccess()
        Matches all array access expressions.
        Returns:
        the matcher
      • binaryOp

        public static ExprMatcher binaryOp​(BinaryOperator op)
        Matches binary expressions with a specific operator.
        Parameters:
        op - the operator to require
        Returns:
        the matcher
      • assignment

        public static ExprMatcher assignment()
        Matches binary expressions whose operator assigns.
        Returns:
        the matcher
      • comparison

        public static ExprMatcher comparison()
        Matches binary expressions whose operator is a comparison.
        Returns:
        the matcher
      • unaryOp

        public static ExprMatcher unaryOp​(UnaryOperator op)
        Matches unary expressions with a specific operator.
        Parameters:
        op - the operator to require
        Returns:
        the matcher
      • custom

        public static ExprMatcher custom​(Predicate<Expression> predicate)
        Creates a matcher from a custom predicate.
        Parameters:
        predicate - the test applied to each expression
        Returns:
        the matcher
      • custom

        public static ExprMatcher custom​(Predicate<Expression> predicate,
                                         String description)
        Creates a matcher from a custom predicate with a description.
        Parameters:
        predicate - the test applied to each expression
        description - the text used by toString()
        Returns:
        the matcher
      • any

        public static ExprMatcher any()
        Matches all expressions.
        Returns:
        the matcher
      • none

        public static ExprMatcher none()
        Matches no expressions.
        Returns:
        the matcher
      • and

        public ExprMatcher and​(ExprMatcher other)
        Combines this matcher with another using AND logic.
        Parameters:
        other - the matcher to combine with
        Returns:
        a matcher accepting only what both accept
      • or

        public ExprMatcher or​(ExprMatcher other)
        Combines this matcher with another using OR logic.
        Parameters:
        other - the matcher to combine with
        Returns:
        a matcher accepting what either one accepts
      • not

        public ExprMatcher not()
        Negates this matcher.
        Returns:
        a matcher accepting everything this one rejects