Class Matchers

java.lang.Object
com.google.errorprone.matchers.Matchers

public class Matchers extends Object
Static factory methods which make the DSL read more fluently. Since matchers are run in a tight loop during compilation, performance is important. When assembling a matcher from the DSL, it's best to construct it only once, by saving the resulting matcher as a static variable for example.
Author:
alexeagle@google.com (Alex Eagle)
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Matcher<com.sun.source.tree.Tree>
     
    static final Matcher<com.sun.source.tree.MethodTree>
     
    static final Matcher<com.sun.source.tree.MethodTree>
    Method signature of serialization methods.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    allOf(Matcher<? super T>... matchers)
    Compose several matchers together, such that the composite matches an AST node iff all the given matchers do.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    allOf(Iterable<? extends Matcher<? super T>> matchers)
    Compose several matchers together, such that the composite matches an AST node iff all the given matchers do.
    static <T extends com.sun.source.tree.Tree>
    MultiMatcher<T,com.sun.source.tree.AnnotationTree>
    annotations(ChildMultiMatcher.MatchType matchType, Matcher<com.sun.source.tree.AnnotationTree> annotationMatcher)
    Matches if the given annotation matcher matches all of or any of the annotations on this tree node.
    Matches a static or instance method.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    anyOf(Matcher<? super T>... matchers)
     
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    anyOf(Iterable<? extends Matcher<? super T>> matchers)
    Compose several matchers together, such that the composite matches an AST node if any of the given matchers do.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    A matcher that matches any AST node.
    static Matcher<com.sun.source.tree.MethodInvocationTree>
    argument(int position, Matcher<com.sun.source.tree.ExpressionTree> argumentMatcher)
     
    static Matcher<com.sun.source.tree.MethodInvocationTree>
    argumentCount(int argumentCount)
     
    static Matcher<com.sun.source.tree.ExpressionTree>
    Matches calls to the method org.junit.Assert#assertEquals and corresponding methods in JUnit 3.x.
    static Matcher<com.sun.source.tree.AssertTree>
    assertionWithCondition(Matcher<com.sun.source.tree.ExpressionTree> conditionMatcher)
    Matches an assertion AST node if the given matcher matches its condition.
    static Matcher<com.sun.source.tree.ExpressionTree>
    Matches calls to the method org.junit.Assert#assertNotEquals and corresponding methods in JUnit 3.x.
    static Matcher<com.sun.source.tree.StatementTree>
    assertStatement(Matcher<com.sun.source.tree.ExpressionTree> conditionMatcher)
    Matches an assert statement where the condition is matched by the passed conditionMatcher.
    static Matcher<com.sun.source.tree.AssignmentTree>
    assignment(Matcher<com.sun.source.tree.ExpressionTree> variableMatcher, Matcher<? super com.sun.source.tree.ExpressionTree> expressionMatcher)
    Matches an assignment operator AST node if both of the given matchers match.
    static Matcher<com.sun.source.tree.BinaryTree>
    binaryTree(Matcher<com.sun.source.tree.ExpressionTree> matcher1, Matcher<com.sun.source.tree.ExpressionTree> matcher2)
    Matches a binary tree if the given matchers match the operands in either order.
    static Matcher<com.sun.source.tree.ExpressionTree>
    booleanConstant(boolean value)
    Matches the boolean constant (Boolean.TRUE or Boolean.FALSE) corresponding to the given value.
    static Matcher<com.sun.source.tree.ExpressionTree>
    booleanLiteral(boolean value)
     
    static Matcher<com.sun.source.tree.ExpressionTree>
    classLiteral(Matcher<? super com.sun.source.tree.ExpressionTree> classMatcher)
     
    static Matcher<com.sun.source.tree.MethodTree>
    Matches compareTo method declaration.
    compoundAssignment(com.sun.source.tree.Tree.Kind operator, Matcher<com.sun.source.tree.ExpressionTree> leftOperandMatcher, Matcher<com.sun.source.tree.ExpressionTree> rightOperandMatcher)
    Matches a compound assignment operator AST node which matches a given left-operand matcher, a given right-operand matcher, and a specific compound assignment operator.
    compoundAssignment(Set<com.sun.source.tree.Tree.Kind> operators, Matcher<com.sun.source.tree.ExpressionTree> receiverMatcher, Matcher<com.sun.source.tree.ExpressionTree> expressionMatcher)
    Matches a compound assignment operator AST node which matches a given left-operand matcher, a given right-operand matcher, and is one of a set of compound assignment operators.
    Matches a constructor.
    static MultiMatcher<com.sun.source.tree.ClassTree,com.sun.source.tree.MethodTree>
    constructor(ChildMultiMatcher.MatchType matchType, Matcher<com.sun.source.tree.MethodTree> constructorMatcher)
    Matches a class in which any of/all of its constructors match the given constructorMatcher.
    static Matcher<com.sun.source.tree.MethodTree>
    Matches a constructor declaration in a specific enclosing class.
    static Matcher<com.sun.source.tree.Tree>
    contains(Matcher<com.sun.source.tree.Tree> treeMatcher)
    Applies the given matcher recursively to all descendants of an AST node, and matches if any matching descendant node is found.
    static <T extends com.sun.source.tree.Tree, V extends com.sun.source.tree.Tree>
    Matcher<T>
    contains(Class<? extends V> clazz, Matcher<? super V> treeMatcher)
    Applies the given matcher recursively to all descendants of an AST node, and matches if any matching descendant node is found.
    static Matcher<com.sun.source.tree.StatementTree>
    Matches a continue statement.
    static Matcher<com.sun.source.tree.AnnotationTree>
    Matches an Annotation AST node if an argument to the annotation does not exist.
    static <T extends com.sun.source.tree.Tree>
    Enclosing.Block<T>
    enclosingBlock(Matcher<com.sun.source.tree.BlockTree> matcher)
    Matches an AST node which is enclosed by a block node that matches the given matcher.
    static <T extends com.sun.source.tree.Tree>
    Enclosing.Class<T>
    enclosingClass(Matcher<com.sun.source.tree.ClassTree> matcher)
    Matches an AST node which is enclosed by a class node that matches the given matcher.
    static <T extends com.sun.source.tree.Tree>
    Enclosing.Method<T>
    enclosingMethod(Matcher<com.sun.source.tree.MethodTree> matcher)
    Matches an AST node which is enclosed by a method node that matches the given matcher.
    static Matcher<com.sun.source.tree.Tree>
    enclosingNode(Matcher<com.sun.source.tree.Tree> matcher)
    Matches an AST node that is enclosed by some node that matches the given matcher.
    static Matcher<com.sun.source.tree.EnhancedForLoopTree>
    enhancedForLoop(Matcher<com.sun.source.tree.VariableTree> variableMatcher, Matcher<com.sun.source.tree.ExpressionTree> expressionMatcher, Matcher<com.sun.source.tree.StatementTree> statementMatcher)
    Matches an enhanced for loop if all the given matchers match.
    static Matcher<com.sun.source.tree.MethodTree>
    Matches Object.equals(java.lang.Object) method declaration.
    static Matcher<com.sun.source.tree.StatementTree>
    expressionStatement(Matcher<com.sun.source.tree.ExpressionTree> matcher)
    Matches an ExpressionStatementTree based on its ExpressionTree.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    hasAnnotation(Class<? extends Annotation> inputClass)
    Determines whether an expression has an annotation of the given class.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    hasAnnotation(String annotationClass)
    Determines whether an expression has an annotation of the given type.
    static Matcher<com.sun.source.tree.Tree>
    hasAnnotation(TypeMirror annotationMirror)
    Determines if an expression has an annotation referred to by the given mirror.
    static Matcher<com.sun.source.tree.MethodTree>
    Matches if a method or any method it overrides has an annotation of the given type.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    Determines whether an expression has an annotation with the given simple name.
    static Matcher<com.sun.source.tree.Tree>
    hasAnyAnnotation(Class<? extends Annotation>... annotations)
     
    static Matcher<com.sun.source.tree.Tree>
    hasAnyAnnotation(List<? extends TypeMirror> mirrors)
     
    static MultiMatcher<com.sun.source.tree.MethodInvocationTree,com.sun.source.tree.ExpressionTree>
    hasArguments(ChildMultiMatcher.MatchType matchType, Matcher<com.sun.source.tree.ExpressionTree> argumentMatcher)
    Matches if the given matcher matches all of/any of the arguments to this method invocation.
    static Matcher<com.sun.source.tree.AnnotationTree>
    hasArgumentWithValue(String argumentName, Matcher<com.sun.source.tree.ExpressionTree> valueMatcher)
    Matches an Annotation AST node if the argument to the annotation with the given name has a value which matches the given matcher.
    static Matcher<com.sun.source.tree.MethodTree>
    Matches hashCode method declaration.
    static Matcher<com.sun.source.tree.Tree>
    hasIdentifier(Matcher<com.sun.source.tree.IdentifierTree> nodeMatcher)
    Matches any AST that contains an identifier with a certain property.
    static Matcher<com.sun.source.tree.ClassTree>
    hasMethod(Matcher<com.sun.source.tree.MethodTree> methodMatcher)
    Matches a class in which at least one method matches the given methodMatcher.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    Returns true if the Tree node has the expected Modifier.
    static Matcher<com.sun.source.tree.ExpressionTree>
    ignoreParens(Matcher<com.sun.source.tree.ExpressionTree> innerMatcher)
    Ignores any number of parenthesis wrapping an expression and then applies the passed matcher to that expression.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    Matches if the given tree is inside a loop.
    static <T extends com.sun.source.tree.ExpressionTree>
    Matcher<T>
    Matches calls to the method Object.equals(Object) or any override of that method.
    static Matcher<com.sun.source.tree.ExpressionTree>
    Matches calls to the method Object.hashCode() or any override of that method.
    Matches an instance method.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    Matches if this Tree is enclosed by either a synchronized block or a synchronized method.
    static Matcher<com.sun.source.tree.ExpressionTree>
    intLiteral(int value)
     
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    Matches an AST node if its type is an array type.
    static Matcher<com.sun.source.tree.ExpressionTree>
    Matches an AST node if its type is a boxed primitive type.
    static Matcher<com.sun.source.tree.ClassTree>
    Matches any node that is directly an implementation, but not extension, of the given Class.
    static Matcher<com.sun.source.tree.ClassTree>
    Matches if a ClassTree is an enum declaration.
    static Matcher<com.sun.source.tree.ClassTree>
    Matches any node that is a direct extension of the given class.
    static Matcher<com.sun.source.tree.VariableTree>
    Matches if a VariableTree is a field declaration, as opposed to a local variable, enum constant, parameter to a method, etc.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    isInstance(Class<?> klass)
    Matches if an AST node is an instance of the given class.
    static Matcher<com.sun.source.tree.ExpressionTree>
    Matches an AST node that represents a non-static field.
    static Matcher<com.sun.source.tree.StatementTree>
    Matches a statement AST node if the statement is the last statement in the block.
    static Matcher<com.sun.source.tree.ExpressionTree>
    Deprecated.
    static Matcher<com.sun.source.tree.ExpressionTree>
    Matches if the expression is provably non-null.
    static Matcher<com.sun.source.tree.ExpressionTree>
    Deprecated.
    static Matcher<com.sun.source.tree.ExpressionTree>
    Matches if the expression is provably null.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    Matches an AST node if its type is a primitive array type.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    Matches an AST node if its type is a primitive type, or a boxed version of a primitive type.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    Matches an AST node if its type is either a primitive type or a void type.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    Matches an AST node if its type is a primitive type.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    isSame(com.sun.source.tree.Tree t)
    Matches an AST node which is the same object reference as the given node.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    isSameType(Supplier<com.sun.tools.javac.code.Type> type)
    Matches an AST node if it has the same erased type as the given type.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    isSameType(Class<?> clazz)
    Matches an AST node if it has the same erased type as the given class.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    isSameType(String typeString)
    Matches an AST node if it has the same erased type as the given type.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    Matches an AST node that is static.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    isSubtypeOf(Supplier<com.sun.tools.javac.code.Type> type)
    Matches an AST node if its type is a subtype of the given type.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    isSubtypeOf(Class<?> clazz)
    Matches an AST node if its type is a subtype of the given type.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    Matches an AST node if its type is a subtype of the given type.
    static boolean
    isThrowingFunctionalInterface(com.sun.tools.javac.code.Type clazzType, VisitorState state)
     
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    Matches an AST node that is transient.
    static Matcher<com.sun.source.tree.AnnotationTree>
    isType(String annotationClassName)
     
    static Matcher<com.sun.source.tree.ExpressionTree>
    Matches an AST node that represents a local variable or parameter.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    Matches an AST node if its type is a void type.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    kindAnyOf(Set<com.sun.source.tree.Tree.Kind> kinds)
    Matches an AST node of a given kind, for example, an Annotation or a switch block.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    kindIs(com.sun.source.tree.Tree.Kind kind)
    Matches an AST node of a given kind, for example, an Annotation or a switch block.
    static Matcher<com.sun.source.tree.StatementTree>
    matchExpressionReturn(Matcher<com.sun.source.tree.ExpressionTree> expressionTreeMatcher)
    Matches the Tree if it returns an expression matching expressionTreeMatcher.
    static Matcher<com.sun.source.tree.BlockTree>
    matchSingleStatementBlock(Matcher<com.sun.source.tree.StatementTree> statementMatcher)
    Matches a BlockTree if it single statement block with statement matching statementMatcher.
    static boolean
     
    static Matcher<com.sun.source.tree.MethodTree>
    methodHasArity(int arity)
    Matches if the method accepts the given number of arguments.
    static Matcher<com.sun.source.tree.MethodTree>
    Matches an AST node that represents a method declaration with no parameters.
    static MultiMatcher<com.sun.source.tree.MethodTree,com.sun.source.tree.VariableTree>
    methodHasParameters(ChildMultiMatcher.MatchType matchType, Matcher<com.sun.source.tree.VariableTree> parameterMatcher)
    Matches if the given matcher matches all of/any of the parameters to this method.
    static Matcher<com.sun.source.tree.MethodTree>
    methodHasParameters(Matcher<com.sun.source.tree.VariableTree>... variableMatcher)
    Matches an AST node that represents a method declaration, based on the list of variableMatchers.
    static Matcher<com.sun.source.tree.MethodTree>
    methodHasParameters(List<Matcher<com.sun.source.tree.VariableTree>> variableMatcher)
    Matches an AST node that represents a method declaration, based on the list of variableMatchers.
    static Matcher<com.sun.source.tree.MethodTree>
     
    static Matcher<com.sun.source.tree.ExpressionTree>
    methodInvocation(Matcher<com.sun.source.tree.ExpressionTree> methodSelectMatcher)
    Matches an AST node if it is a method invocation and the method select matches methodSelectMatcher.
    static Matcher<com.sun.source.tree.ExpressionTree>
    methodInvocation(Matcher<com.sun.source.tree.ExpressionTree> methodSelectMatcher, ChildMultiMatcher.MatchType matchType, Matcher<com.sun.source.tree.ExpressionTree> methodArgumentMatcher)
    Matches an AST node if it is a method invocation and the given matchers match.
    static Matcher<com.sun.source.tree.MethodTree>
     
    static Matcher<com.sun.source.tree.MethodTree>
    methodIsNamed(String methodName)
    Match a method declaration with a specific name.
    static Matcher<com.sun.source.tree.MethodTree>
    Match a method declaration that starts with a given string.
    static Matcher<com.sun.source.tree.MethodTree>
    methodReturns(Matcher<? super com.sun.source.tree.Tree> returnTypeMatcher)
     
    static Matcher<com.sun.source.tree.MethodTree>
    methodReturns(Supplier<com.sun.tools.javac.code.Type> returnType)
     
    static Matcher<com.sun.source.tree.ExpressionTree>
    Matches a method invocation that is known to never return null.
    static Matcher<com.sun.source.tree.MethodTree>
    Match a method that returns a non-primitive type.
    static Matcher<com.sun.source.tree.MethodTree>
    methodWithClassAndName(String className, String methodName)
    Match a method declaration with a specific enclosing class and method name.
    static Matcher<com.sun.source.tree.ClassTree>
    Matches an class based on whether it is nested in another class or method.
    static <T extends com.sun.source.tree.StatementTree>
    Matcher<T>
    nextStatement(Matcher<com.sun.source.tree.StatementTree> matcher)
    Matches a statement AST node if the following statement in the enclosing block matches the given matcher.
    static Matcher<com.sun.source.tree.ExpressionTree>
    Matches an AST node if it is a literal other than null.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    not(Matcher<T> matcher)
    Matches an AST node iff it does not match the given matcher.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    A matcher that matches no AST node.
    static Matcher<com.sun.source.tree.ExpressionTree>
    Matches an AST node if it is a null literal.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    Matches an AST node whose compilation unit's package name matches the given predicate.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    Matches an AST node whose compilation unit's package name matches the given pattern.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    Matches an AST node whose compilation unit starts with this prefix.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    parentNode(Matcher<com.sun.source.tree.Tree> treeMatcher)
    Matches an AST node if its parent node is matched by the given matcher.
    static <T extends com.sun.source.tree.StatementTree>
    Matcher<T>
    previousStatement(Matcher<com.sun.source.tree.StatementTree> matcher)
    Matches a statement AST node if the previous statement in the enclosing block matches the given matcher.
    static Matcher<com.sun.source.tree.MethodInvocationTree>
    receiverOfInvocation(Matcher<com.sun.source.tree.ExpressionTree> expressionTreeMatcher)
     
    static Matcher<? super com.sun.source.tree.MethodInvocationTree>
    Matches when the receiver of an instance method is the same reference as a particular argument to the method.
    static Matcher<com.sun.source.tree.StatementTree>
    returnStatement(Matcher<? super com.sun.source.tree.ExpressionTree> returnedMatcher)
    Matches a return statement where the returned expression is matched by the passed returnedMatcher.
    static Matcher<? super com.sun.source.tree.MethodInvocationTree>
    sameArgument(int index1, int index2)
    Matches a MethodInvocation when the arguments at the two given indices are both the same variable, as determined by ASTHelpers.sameVariable(com.sun.source.tree.ExpressionTree, com.sun.source.tree.ExpressionTree).
    static Matcher<com.sun.source.tree.ExpressionTree>
    sameVariable(com.sun.source.tree.ExpressionTree expr)
    Matches if this ExpressionTree refers to the same variable as the one passed into the matcher.
    static Matcher<com.sun.source.tree.MethodTree>
    singleStatementReturnMatcher(Matcher<com.sun.source.tree.ExpressionTree> expressionTreeMatcher)
    Returns a matcher for MethodTree whose implementation contains a single return statement with expression matching the passed expressionTreeMatcher.
    static <T extends com.sun.source.tree.ExpressionTree>
    Matcher<T>
    Matches an invocation of a recognized static object equality method such as Objects.equals(java.lang.Object, java.lang.Object).
    static Matcher<com.sun.source.tree.ExpressionTree>
    Matches an AST node which is an expression yielding the indicated static field access.
    Matches a static method.
    static Matcher<com.sun.source.tree.ExpressionTree>
    Matches a Literal AST node if it is a string literal with the given value.
    static Matcher<com.sun.source.tree.ExpressionTree>
    Matches a Literal AST node if it is a string literal which matches the given Pattern.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    symbolHasAnnotation(Class<? extends Annotation> inputClass)
    Determines whether an expression refers to a symbol that has an annotation of the given type.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    symbolHasAnnotation(String annotationClass)
    Determines whether an expression refers to a symbol that has an annotation of the given type.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    symbolMatcher(BiPredicate<com.sun.tools.javac.code.Symbol,VisitorState> pred)
    Match a Tree based solely on the Symbol produced by ASTHelpers.getSymbol(Tree).
    static Matcher<com.sun.source.tree.StatementTree>
    throwStatement(Matcher<? super com.sun.source.tree.ExpressionTree> thrownMatcher)
    Matches a throw statement where the thrown item is matched by the passed thrownMatcher.
    static Matcher<com.sun.source.tree.MethodTree>
    Matches Object.toString() method declaration.
    static <S extends T, T extends com.sun.source.tree.Tree>
    Matcher<T>
    toType(Class<S> type, Matcher<? super S> matcher)
    Converts the given matcher to one that can be applied to any tree but is only executed when run on a tree of type and returns false for all other tree types.
    static Matcher<com.sun.source.tree.TypeCastTree>
    typeCast(Matcher<com.sun.source.tree.Tree> typeMatcher, Matcher<com.sun.source.tree.ExpressionTree> expressionMatcher)
    Matches a type cast AST node if both of the given matchers match.
    static <T extends com.sun.source.tree.Tree>
    Matcher<T>
    Match a Tree based solely on the type produced by ASTHelpers.getType(Tree).
    static Matcher<com.sun.source.tree.VariableTree>
    variableInitializer(Matcher<com.sun.source.tree.ExpressionTree> expressionTreeMatcher)
    Matches on the initializer of a VariableTree AST node.
    static Matcher<com.sun.source.tree.VariableTree>
    variableType(Matcher<com.sun.source.tree.Tree> treeMatcher)
    Matches on the type of a VariableTree AST node.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MAIN_METHOD

      public static final Matcher<com.sun.source.tree.MethodTree> MAIN_METHOD
    • SERIALIZATION_METHODS

      public static final Matcher<com.sun.source.tree.MethodTree> SERIALIZATION_METHODS
      Method signature of serialization methods.
    • IS_INTERFACE

      public static final Matcher<com.sun.source.tree.Tree> IS_INTERFACE
  • Method Details

    • anything

      public static <T extends com.sun.source.tree.Tree> Matcher<T> anything()
      A matcher that matches any AST node.
    • nothing

      public static <T extends com.sun.source.tree.Tree> Matcher<T> nothing()
      A matcher that matches no AST node.
    • not

      public static <T extends com.sun.source.tree.Tree> Matcher<T> not(Matcher<T> matcher)
      Matches an AST node iff it does not match the given matcher.
    • allOf

      @SafeVarargs public static <T extends com.sun.source.tree.Tree> Matcher<T> allOf(Matcher<? super T>... matchers)
      Compose several matchers together, such that the composite matches an AST node iff all the given matchers do.
    • allOf

      public static <T extends com.sun.source.tree.Tree> Matcher<T> allOf(Iterable<? extends Matcher<? super T>> matchers)
      Compose several matchers together, such that the composite matches an AST node iff all the given matchers do.
    • anyOf

      public static <T extends com.sun.source.tree.Tree> Matcher<T> anyOf(Iterable<? extends Matcher<? super T>> matchers)
      Compose several matchers together, such that the composite matches an AST node if any of the given matchers do.
    • anyOf

      @SafeVarargs public static <T extends com.sun.source.tree.Tree> Matcher<T> anyOf(Matcher<? super T>... matchers)
    • isInstance

      public static <T extends com.sun.source.tree.Tree> Matcher<T> isInstance(Class<?> klass)
      Matches if an AST node is an instance of the given class.
    • kindIs

      public static <T extends com.sun.source.tree.Tree> Matcher<T> kindIs(com.sun.source.tree.Tree.Kind kind)
      Matches an AST node of a given kind, for example, an Annotation or a switch block.
    • kindAnyOf

      public static <T extends com.sun.source.tree.Tree> Matcher<T> kindAnyOf(Set<com.sun.source.tree.Tree.Kind> kinds)
      Matches an AST node of a given kind, for example, an Annotation or a switch block.
    • isSame

      public static <T extends com.sun.source.tree.Tree> Matcher<T> isSame(com.sun.source.tree.Tree t)
      Matches an AST node which is the same object reference as the given node.
    • staticMethod

      public static MethodMatchers.StaticMethodMatcher staticMethod()
      Matches a static method.
    • instanceMethod

      public static MethodMatchers.InstanceMethodMatcher instanceMethod()
      Matches an instance method.
    • anyMethod

      public static MethodMatchers.AnyMethodMatcher anyMethod()
      Matches a static or instance method.
    • constructor

      public static MethodMatchers.ConstructorMatcher constructor()
      Matches a constructor.
    • symbolMatcher

      public static <T extends com.sun.source.tree.Tree> Matcher<T> symbolMatcher(BiPredicate<com.sun.tools.javac.code.Symbol,VisitorState> pred)
      Match a Tree based solely on the Symbol produced by ASTHelpers.getSymbol(Tree).

      If getSymbol returns null, the matcher returns false instead of calling pred.

    • isInstanceField

      public static Matcher<com.sun.source.tree.ExpressionTree> isInstanceField()
      Matches an AST node that represents a non-static field.
    • isVariable

      public static Matcher<com.sun.source.tree.ExpressionTree> isVariable()
      Matches an AST node that represents a local variable or parameter.
    • compoundAssignment

      public static CompoundAssignment compoundAssignment(com.sun.source.tree.Tree.Kind operator, Matcher<com.sun.source.tree.ExpressionTree> leftOperandMatcher, Matcher<com.sun.source.tree.ExpressionTree> rightOperandMatcher)
      Matches a compound assignment operator AST node which matches a given left-operand matcher, a given right-operand matcher, and a specific compound assignment operator.
      Parameters:
      operator - Which compound assignment operator to match against.
      leftOperandMatcher - The matcher to apply to the left operand.
      rightOperandMatcher - The matcher to apply to the right operand.
    • compoundAssignment

      public static CompoundAssignment compoundAssignment(Set<com.sun.source.tree.Tree.Kind> operators, Matcher<com.sun.source.tree.ExpressionTree> receiverMatcher, Matcher<com.sun.source.tree.ExpressionTree> expressionMatcher)
      Matches a compound assignment operator AST node which matches a given left-operand matcher, a given right-operand matcher, and is one of a set of compound assignment operators. Does not match compound assignment operators.
      Parameters:
      operators - Which compound assignment operators to match against.
      receiverMatcher - The matcher to apply to the receiver.
      expressionMatcher - The matcher to apply to the expression.
    • receiverSameAsArgument

      public static Matcher<? super com.sun.source.tree.MethodInvocationTree> receiverSameAsArgument(int argNum)
      Matches when the receiver of an instance method is the same reference as a particular argument to the method. For example, receiverSameAsArgument(1) would match obj.method("", obj)
      Parameters:
      argNum - The number of the argument to compare against (zero-based.
    • receiverOfInvocation

      public static Matcher<com.sun.source.tree.MethodInvocationTree> receiverOfInvocation(Matcher<com.sun.source.tree.ExpressionTree> expressionTreeMatcher)
    • annotations

      public static <T extends com.sun.source.tree.Tree> MultiMatcher<T,com.sun.source.tree.AnnotationTree> annotations(ChildMultiMatcher.MatchType matchType, Matcher<com.sun.source.tree.AnnotationTree> annotationMatcher)
      Matches if the given annotation matcher matches all of or any of the annotations on this tree node.
      Parameters:
      matchType - Whether to match if the matchers match any of or all of the annotations on this tree.
      annotationMatcher - The annotation matcher to use.
    • constructor

      public static MultiMatcher<com.sun.source.tree.ClassTree,com.sun.source.tree.MethodTree> constructor(ChildMultiMatcher.MatchType matchType, Matcher<com.sun.source.tree.MethodTree> constructorMatcher)
      Matches a class in which any of/all of its constructors match the given constructorMatcher.
    • argument

      public static Matcher<com.sun.source.tree.MethodInvocationTree> argument(int position, Matcher<com.sun.source.tree.ExpressionTree> argumentMatcher)
    • hasArguments

      public static MultiMatcher<com.sun.source.tree.MethodInvocationTree,com.sun.source.tree.ExpressionTree> hasArguments(ChildMultiMatcher.MatchType matchType, Matcher<com.sun.source.tree.ExpressionTree> argumentMatcher)
      Matches if the given matcher matches all of/any of the arguments to this method invocation.
    • methodInvocation

      public static Matcher<com.sun.source.tree.ExpressionTree> methodInvocation(Matcher<com.sun.source.tree.ExpressionTree> methodSelectMatcher, ChildMultiMatcher.MatchType matchType, Matcher<com.sun.source.tree.ExpressionTree> methodArgumentMatcher)
      Matches an AST node if it is a method invocation and the given matchers match.
      Parameters:
      methodSelectMatcher - matcher identifying the method being called
      matchType - how to match method arguments with methodArgumentMatcher
      methodArgumentMatcher - matcher applied to each method argument
    • methodInvocation

      public static Matcher<com.sun.source.tree.ExpressionTree> methodInvocation(Matcher<com.sun.source.tree.ExpressionTree> methodSelectMatcher)
      Matches an AST node if it is a method invocation and the method select matches methodSelectMatcher. Ignores any arguments.
    • argumentCount

      public static Matcher<com.sun.source.tree.MethodInvocationTree> argumentCount(int argumentCount)
    • parentNode

      public static <T extends com.sun.source.tree.Tree> Matcher<T> parentNode(Matcher<com.sun.source.tree.Tree> treeMatcher)
      Matches an AST node if its parent node is matched by the given matcher. For example, parentNode(kindIs(Kind.RETURN)) would match the this expression in return this;
    • isSubtypeOf

      public static <T extends com.sun.source.tree.Tree> Matcher<T> isSubtypeOf(String typeStr)
      Matches an AST node if its type is a subtype of the given type.
      Parameters:
      typeStr - a string representation of the type, e.g., "java.util.AbstractList"
    • isSubtypeOf

      public static <T extends com.sun.source.tree.Tree> Matcher<T> isSubtypeOf(Supplier<com.sun.tools.javac.code.Type> type)
      Matches an AST node if its type is a subtype of the given type.
      Parameters:
      type - the type to check against
    • isSubtypeOf

      public static <T extends com.sun.source.tree.Tree> Matcher<T> isSubtypeOf(Class<?> clazz)
      Matches an AST node if its type is a subtype of the given type.
      Parameters:
      clazz - a class representation of the type, e.g., Action.class.
    • isSameType

      public static <T extends com.sun.source.tree.Tree> Matcher<T> isSameType(Supplier<com.sun.tools.javac.code.Type> type)
      Matches an AST node if it has the same erased type as the given type.
    • isSameType

      public static <T extends com.sun.source.tree.Tree> Matcher<T> isSameType(String typeString)
      Matches an AST node if it has the same erased type as the given type.
    • isSameType

      public static <T extends com.sun.source.tree.Tree> Matcher<T> isSameType(Class<?> clazz)
      Matches an AST node if it has the same erased type as the given class.
    • typePredicateMatcher

      public static <T extends com.sun.source.tree.Tree> Matcher<T> typePredicateMatcher(TypePredicate pred)
      Match a Tree based solely on the type produced by ASTHelpers.getType(Tree).

      If getType returns null, the matcher returns false instead of calling pred.

    • isArrayType

      public static <T extends com.sun.source.tree.Tree> Matcher<T> isArrayType()
      Matches an AST node if its type is an array type.
    • isPrimitiveArrayType

      public static <T extends com.sun.source.tree.Tree> Matcher<T> isPrimitiveArrayType()
      Matches an AST node if its type is a primitive array type.
    • isPrimitiveType

      public static <T extends com.sun.source.tree.Tree> Matcher<T> isPrimitiveType()
      Matches an AST node if its type is a primitive type.
    • isPrimitiveOrVoidType

      public static <T extends com.sun.source.tree.Tree> Matcher<T> isPrimitiveOrVoidType()
      Matches an AST node if its type is either a primitive type or a void type.
    • isVoidType

      public static <T extends com.sun.source.tree.Tree> Matcher<T> isVoidType()
      Matches an AST node if its type is a void type.
    • isPrimitiveOrBoxedPrimitiveType

      public static <T extends com.sun.source.tree.Tree> Matcher<T> isPrimitiveOrBoxedPrimitiveType()
      Matches an AST node if its type is a primitive type, or a boxed version of a primitive type.
    • isBoxedPrimitiveType

      public static Matcher<com.sun.source.tree.ExpressionTree> isBoxedPrimitiveType()
      Matches an AST node if its type is a boxed primitive type.
    • enclosingBlock

      public static <T extends com.sun.source.tree.Tree> Enclosing.Block<T> enclosingBlock(Matcher<com.sun.source.tree.BlockTree> matcher)
      Matches an AST node which is enclosed by a block node that matches the given matcher.
    • enclosingClass

      public static <T extends com.sun.source.tree.Tree> Enclosing.Class<T> enclosingClass(Matcher<com.sun.source.tree.ClassTree> matcher)
      Matches an AST node which is enclosed by a class node that matches the given matcher.
    • enclosingMethod

      public static <T extends com.sun.source.tree.Tree> Enclosing.Method<T> enclosingMethod(Matcher<com.sun.source.tree.MethodTree> matcher)
      Matches an AST node which is enclosed by a method node that matches the given matcher.
    • enclosingNode

      public static Matcher<com.sun.source.tree.Tree> enclosingNode(Matcher<com.sun.source.tree.Tree> matcher)
      Matches an AST node that is enclosed by some node that matches the given matcher.

      TODO(eaftan): This could be used instead of enclosingBlock and enclosingClass.

    • nextStatement

      public static <T extends com.sun.source.tree.StatementTree> Matcher<T> nextStatement(Matcher<com.sun.source.tree.StatementTree> matcher)
      Matches a statement AST node if the following statement in the enclosing block matches the given matcher.
    • previousStatement

      public static <T extends com.sun.source.tree.StatementTree> Matcher<T> previousStatement(Matcher<com.sun.source.tree.StatementTree> matcher)
      Matches a statement AST node if the previous statement in the enclosing block matches the given matcher.
    • isLastStatementInBlock

      public static Matcher<com.sun.source.tree.StatementTree> isLastStatementInBlock()
      Matches a statement AST node if the statement is the last statement in the block.
    • nullLiteral

      public static Matcher<com.sun.source.tree.ExpressionTree> nullLiteral()
      Matches an AST node if it is a null literal.
    • nonNullLiteral

      public static Matcher<com.sun.source.tree.ExpressionTree> nonNullLiteral()
      Matches an AST node if it is a literal other than null.
    • stringLiteral

      public static Matcher<com.sun.source.tree.ExpressionTree> stringLiteral(String value)
      Matches a Literal AST node if it is a string literal with the given value. For example, stringLiteral("thing") matches the literal "thing"
    • stringLiteral

      public static Matcher<com.sun.source.tree.ExpressionTree> stringLiteral(Pattern pattern)
      Matches a Literal AST node if it is a string literal which matches the given Pattern.
      See Also:
    • booleanLiteral

      public static Matcher<com.sun.source.tree.ExpressionTree> booleanLiteral(boolean value)
    • booleanConstant

      public static Matcher<com.sun.source.tree.ExpressionTree> booleanConstant(boolean value)
      Matches the boolean constant (Boolean.TRUE or Boolean.FALSE) corresponding to the given value.
    • ignoreParens

      public static Matcher<com.sun.source.tree.ExpressionTree> ignoreParens(Matcher<com.sun.source.tree.ExpressionTree> innerMatcher)
      Ignores any number of parenthesis wrapping an expression and then applies the passed matcher to that expression. For example, the passed matcher would be applied to value in (((value))).
    • intLiteral

      public static Matcher<com.sun.source.tree.ExpressionTree> intLiteral(int value)
    • classLiteral

      public static Matcher<com.sun.source.tree.ExpressionTree> classLiteral(Matcher<? super com.sun.source.tree.ExpressionTree> classMatcher)
    • hasArgumentWithValue

      public static Matcher<com.sun.source.tree.AnnotationTree> hasArgumentWithValue(String argumentName, Matcher<com.sun.source.tree.ExpressionTree> valueMatcher)
      Matches an Annotation AST node if the argument to the annotation with the given name has a value which matches the given matcher. For example, hasArgumentWithValue("value", stringLiteral("one")) matches @Thing("one") or @Thing({"one", "two"}) or @Thing(value = "one")
    • doesNotHaveArgument

      public static Matcher<com.sun.source.tree.AnnotationTree> doesNotHaveArgument(String argumentName)
      Matches an Annotation AST node if an argument to the annotation does not exist.
    • isType

      public static Matcher<com.sun.source.tree.AnnotationTree> isType(String annotationClassName)
    • sameArgument

      public static Matcher<? super com.sun.source.tree.MethodInvocationTree> sameArgument(int index1, int index2)
      Matches a MethodInvocation when the arguments at the two given indices are both the same variable, as determined by ASTHelpers.sameVariable(com.sun.source.tree.ExpressionTree, com.sun.source.tree.ExpressionTree).
      Parameters:
      index1 - the index of the first actual parameter to test
      index2 - the index of the second actual parameter to test
      Throws:
      IndexOutOfBoundsException - if the given indices are invalid
    • hasAnnotation

      public static <T extends com.sun.source.tree.Tree> Matcher<T> hasAnnotation(String annotationClass)
      Determines whether an expression has an annotation of the given type. This includes annotations inherited from superclasses due to @Inherited.
      Parameters:
      annotationClass - the binary class name of the annotation (e.g. "javax.annotation.Nullable", or "some.package.OuterClassName$InnerClassName")
    • hasAnnotation

      public static Matcher<com.sun.source.tree.Tree> hasAnnotation(TypeMirror annotationMirror)
      Determines if an expression has an annotation referred to by the given mirror. Accounts for binary names and annotations inherited due to @Inherited.
      Parameters:
      annotationMirror - mirror referring to the annotation type
    • hasAnnotationWithSimpleName

      public static <T extends com.sun.source.tree.Tree> Matcher<T> hasAnnotationWithSimpleName(String simpleName)
      Determines whether an expression has an annotation with the given simple name. This does not include annotations inherited from superclasses due to @Inherited.
      Parameters:
      simpleName - the simple name of the annotation (e.g. "Nullable")
    • symbolHasAnnotation

      public static <T extends com.sun.source.tree.Tree> Matcher<T> symbolHasAnnotation(String annotationClass)
      Determines whether an expression refers to a symbol that has an annotation of the given type. This includes annotations inherited from superclasses due to @Inherited.
      Parameters:
      annotationClass - the binary class name of the annotation (e.g. "javax.annotation.Nullable", or "some.package.OuterClassName$InnerClassName")
    • hasAnnotation

      public static <T extends com.sun.source.tree.Tree> Matcher<T> hasAnnotation(Class<? extends Annotation> inputClass)
      Determines whether an expression has an annotation of the given class. This includes annotations inherited from superclasses due to @Inherited.
      Parameters:
      inputClass - The class of the annotation to look for (e.g, Produces.class).
    • symbolHasAnnotation

      public static <T extends com.sun.source.tree.Tree> Matcher<T> symbolHasAnnotation(Class<? extends Annotation> inputClass)
      Determines whether an expression refers to a symbol that has an annotation of the given type. This includes annotations inherited from superclasses due to @Inherited.
      Parameters:
      inputClass - The class of the annotation to look for (e.g, Produces.class).
    • hasAnnotationOnAnyOverriddenMethod

      public static Matcher<com.sun.source.tree.MethodTree> hasAnnotationOnAnyOverriddenMethod(String annotationClass)
      Matches if a method or any method it overrides has an annotation of the given type. JUnit 4's @Test, @Before, and @After annotations behave this way.
      Parameters:
      annotationClass - the binary class name of the annotation (e.g. "javax.annotation.Nullable", or "some.package.OuterClassName$InnerClassName")
    • methodReturnsNonNull

      public static Matcher<com.sun.source.tree.ExpressionTree> methodReturnsNonNull()
      Matches a method invocation that is known to never return null.
    • methodReturns

      public static Matcher<com.sun.source.tree.MethodTree> methodReturns(Matcher<? super com.sun.source.tree.Tree> returnTypeMatcher)
    • methodReturns

      public static Matcher<com.sun.source.tree.MethodTree> methodReturns(Supplier<com.sun.tools.javac.code.Type> returnType)
    • methodReturnsNonPrimitiveType

      public static Matcher<com.sun.source.tree.MethodTree> methodReturnsNonPrimitiveType()
      Match a method that returns a non-primitive type.
    • methodIsNamed

      public static Matcher<com.sun.source.tree.MethodTree> methodIsNamed(String methodName)
      Match a method declaration with a specific name.
      Parameters:
      methodName - The name of the method to match, e.g., "equals"
    • methodNameStartsWith

      public static Matcher<com.sun.source.tree.MethodTree> methodNameStartsWith(String prefix)
      Match a method declaration that starts with a given string.
      Parameters:
      prefix - The prefix.
    • methodWithClassAndName

      public static Matcher<com.sun.source.tree.MethodTree> methodWithClassAndName(String className, String methodName)
      Match a method declaration with a specific enclosing class and method name.
      Parameters:
      className - The fully-qualified name of the enclosing class, e.g. "com.google.common.base.Preconditions"
      methodName - The name of the method to match, e.g., "checkNotNull"
    • methodHasParameters

      @SafeVarargs public static Matcher<com.sun.source.tree.MethodTree> methodHasParameters(Matcher<com.sun.source.tree.VariableTree>... variableMatcher)
      Matches an AST node that represents a method declaration, based on the list of variableMatchers. Applies the variableMatcher at index n to the parameter at index n and returns true iff they all match. Returns false if the number of variableMatchers provided does not match the number of parameters.

      If you pass no variableMatchers, this will match methods with no parameters.

      Parameters:
      variableMatcher - an array of matchers to apply to the parameters of the method
    • methodHasNoParameters

      public static Matcher<com.sun.source.tree.MethodTree> methodHasNoParameters()
      Matches an AST node that represents a method declaration with no parameters.
    • methodHasParameters

      public static Matcher<com.sun.source.tree.MethodTree> methodHasParameters(List<Matcher<com.sun.source.tree.VariableTree>> variableMatcher)
      Matches an AST node that represents a method declaration, based on the list of variableMatchers. Applies the variableMatcher at index n to the parameter at index n and returns true iff they all match. Returns false if the number of variableMatchers provided does not match the number of parameters.

      If you pass no variableMatchers, this will match methods with no parameters.

      Parameters:
      variableMatcher - a list of matchers to apply to the parameters of the method
    • methodHasParameters

      public static MultiMatcher<com.sun.source.tree.MethodTree,com.sun.source.tree.VariableTree> methodHasParameters(ChildMultiMatcher.MatchType matchType, Matcher<com.sun.source.tree.VariableTree> parameterMatcher)
      Matches if the given matcher matches all of/any of the parameters to this method.
    • methodHasVisibility

      public static Matcher<com.sun.source.tree.MethodTree> methodHasVisibility(MethodVisibility.Visibility visibility)
    • methodIsConstructor

      public static Matcher<com.sun.source.tree.MethodTree> methodIsConstructor()
    • constructorOfClass

      public static Matcher<com.sun.source.tree.MethodTree> constructorOfClass(String className)
      Matches a constructor declaration in a specific enclosing class.
      Parameters:
      className - The fully-qualified name of the enclosing class, e.g. "com.google.common.base.Preconditions"
    • hasMethod

      public static Matcher<com.sun.source.tree.ClassTree> hasMethod(Matcher<com.sun.source.tree.MethodTree> methodMatcher)
      Matches a class in which at least one method matches the given methodMatcher.
      Parameters:
      methodMatcher - A matcher on MethodTrees to run against all methods in this class.
      Returns:
      True if some method in the class matches the given methodMatcher.
    • variableType

      public static Matcher<com.sun.source.tree.VariableTree> variableType(Matcher<com.sun.source.tree.Tree> treeMatcher)
      Matches on the type of a VariableTree AST node.
      Parameters:
      treeMatcher - A matcher on the type of the variable.
    • variableInitializer

      public static Matcher<com.sun.source.tree.VariableTree> variableInitializer(Matcher<com.sun.source.tree.ExpressionTree> expressionTreeMatcher)
      Matches on the initializer of a VariableTree AST node.
      Parameters:
      expressionTreeMatcher - A matcher on the initializer of the variable.
    • isField

      public static Matcher<com.sun.source.tree.VariableTree> isField()
      Matches if a VariableTree is a field declaration, as opposed to a local variable, enum constant, parameter to a method, etc.
    • isEnum

      public static Matcher<com.sun.source.tree.ClassTree> isEnum()
      Matches if a ClassTree is an enum declaration.
    • nestingKind

      public static Matcher<com.sun.source.tree.ClassTree> nestingKind(NestingKind kind)
      Matches an class based on whether it is nested in another class or method.
      Parameters:
      kind - The kind of nesting to match, eg ANONYMOUS, LOCAL, MEMBER, TOP_LEVEL
    • binaryTree

      public static Matcher<com.sun.source.tree.BinaryTree> binaryTree(Matcher<com.sun.source.tree.ExpressionTree> matcher1, Matcher<com.sun.source.tree.ExpressionTree> matcher2)
      Matches a binary tree if the given matchers match the operands in either order. That is, returns true if either: matcher1 matches the left operand and matcher2 matches the right operand or matcher2 matches the left operand and matcher1 matches the right operand
    • hasIdentifier

      public static Matcher<com.sun.source.tree.Tree> hasIdentifier(Matcher<com.sun.source.tree.IdentifierTree> nodeMatcher)
      Matches any AST that contains an identifier with a certain property. This matcher can be used, for instance, to locate identifiers with a certain name or which is defined in a certain class.
      Parameters:
      nodeMatcher - Which identifiers to look for
    • hasModifier

      public static <T extends com.sun.source.tree.Tree> Matcher<T> hasModifier(Modifier modifier)
      Returns true if the Tree node has the expected Modifier.
    • staticFieldAccess

      public static Matcher<com.sun.source.tree.ExpressionTree> staticFieldAccess()
      Matches an AST node which is an expression yielding the indicated static field access.
    • isStatic

      public static <T extends com.sun.source.tree.Tree> Matcher<T> isStatic()
      Matches an AST node that is static.
    • isTransient

      public static <T extends com.sun.source.tree.Tree> Matcher<T> isTransient()
      Matches an AST node that is transient.
    • throwStatement

      public static Matcher<com.sun.source.tree.StatementTree> throwStatement(Matcher<? super com.sun.source.tree.ExpressionTree> thrownMatcher)
      Matches a throw statement where the thrown item is matched by the passed thrownMatcher.
    • returnStatement

      public static Matcher<com.sun.source.tree.StatementTree> returnStatement(Matcher<? super com.sun.source.tree.ExpressionTree> returnedMatcher)
      Matches a return statement where the returned expression is matched by the passed returnedMatcher.
    • assertStatement

      public static Matcher<com.sun.source.tree.StatementTree> assertStatement(Matcher<com.sun.source.tree.ExpressionTree> conditionMatcher)
      Matches an assert statement where the condition is matched by the passed conditionMatcher.
    • continueStatement

      public static Matcher<com.sun.source.tree.StatementTree> continueStatement()
      Matches a continue statement.
    • expressionStatement

      public static Matcher<com.sun.source.tree.StatementTree> expressionStatement(Matcher<com.sun.source.tree.ExpressionTree> matcher)
      Matches an ExpressionStatementTree based on its ExpressionTree.
    • toType

      public static <S extends T, T extends com.sun.source.tree.Tree> Matcher<T> toType(Class<S> type, Matcher<? super S> matcher)
      Converts the given matcher to one that can be applied to any tree but is only executed when run on a tree of type and returns false for all other tree types.
    • inSynchronized

      public static <T extends com.sun.source.tree.Tree> Matcher<T> inSynchronized()
      Matches if this Tree is enclosed by either a synchronized block or a synchronized method.
    • sameVariable

      public static Matcher<com.sun.source.tree.ExpressionTree> sameVariable(com.sun.source.tree.ExpressionTree expr)
      Matches if this ExpressionTree refers to the same variable as the one passed into the matcher.
    • isNonNullUsingDataflow

      public static Matcher<com.sun.source.tree.ExpressionTree> isNonNullUsingDataflow()
      Matches if the expression is provably non-null.

      Uses the Checker Framework dataflow implementation to determine nullness. Be careful about using this in performance-critical code.

    • isNonNull

      @Deprecated public static Matcher<com.sun.source.tree.ExpressionTree> isNonNull()
      Deprecated.
    • isNullUsingDataflow

      public static Matcher<com.sun.source.tree.ExpressionTree> isNullUsingDataflow()
      Matches if the expression is provably null.

      Uses the Checker Framework dataflow implementation to determine nullness. Be careful about using this in performance-critical code.

      Prefer kindIs(NULL_LITERAL) unless you really need dataflow.

    • isNull

      @Deprecated public static Matcher<com.sun.source.tree.ExpressionTree> isNull()
      Deprecated.
    • enhancedForLoop

      public static Matcher<com.sun.source.tree.EnhancedForLoopTree> enhancedForLoop(Matcher<com.sun.source.tree.VariableTree> variableMatcher, Matcher<com.sun.source.tree.ExpressionTree> expressionMatcher, Matcher<com.sun.source.tree.StatementTree> statementMatcher)
      Matches an enhanced for loop if all the given matchers match.
      Parameters:
      variableMatcher - The matcher to apply to the variable.
      expressionMatcher - The matcher to apply to the expression.
      statementMatcher - The matcher to apply to the statement.
    • inLoop

      public static <T extends com.sun.source.tree.Tree> Matcher<T> inLoop()
      Matches if the given tree is inside a loop.
    • assignment

      public static Matcher<com.sun.source.tree.AssignmentTree> assignment(Matcher<com.sun.source.tree.ExpressionTree> variableMatcher, Matcher<? super com.sun.source.tree.ExpressionTree> expressionMatcher)
      Matches an assignment operator AST node if both of the given matchers match.
      Parameters:
      variableMatcher - The matcher to apply to the variable.
      expressionMatcher - The matcher to apply to the expression.
    • typeCast

      public static Matcher<com.sun.source.tree.TypeCastTree> typeCast(Matcher<com.sun.source.tree.Tree> typeMatcher, Matcher<com.sun.source.tree.ExpressionTree> expressionMatcher)
      Matches a type cast AST node if both of the given matchers match.
      Parameters:
      typeMatcher - The matcher to apply to the type.
      expressionMatcher - The matcher to apply to the expression.
    • assertionWithCondition

      public static Matcher<com.sun.source.tree.AssertTree> assertionWithCondition(Matcher<com.sun.source.tree.ExpressionTree> conditionMatcher)
      Matches an assertion AST node if the given matcher matches its condition.
      Parameters:
      conditionMatcher - The matcher to apply to the condition in the assertion, e.g. in "assert false", the "false" part of the statement
    • contains

      public static Matcher<com.sun.source.tree.Tree> contains(Matcher<com.sun.source.tree.Tree> treeMatcher)
      Applies the given matcher recursively to all descendants of an AST node, and matches if any matching descendant node is found.
      Parameters:
      treeMatcher - The matcher to apply recursively to the tree.
    • contains

      public static <T extends com.sun.source.tree.Tree, V extends com.sun.source.tree.Tree> Matcher<T> contains(Class<? extends V> clazz, Matcher<? super V> treeMatcher)
      Applies the given matcher recursively to all descendants of an AST node, and matches if any matching descendant node is found.
      Parameters:
      clazz - The type of node to be matched.
      treeMatcher - The matcher to apply recursively to the tree.
    • methodHasArity

      public static Matcher<com.sun.source.tree.MethodTree> methodHasArity(int arity)
      Matches if the method accepts the given number of arguments.
      Parameters:
      arity - the number of arguments the method should accept
    • isDirectImplementationOf

      public static Matcher<com.sun.source.tree.ClassTree> isDirectImplementationOf(String clazz)
      Matches any node that is directly an implementation, but not extension, of the given Class.

      E.x. class C implements I will match, but class C extends A will not.

      Additionally, this will only match direct implementations of interfaces. E.g. the following will not match:

      interface I1 {} interface I2 extends I1 {} class C implements I2 {} ... isDirectImplementationOf(I1).match(\/*class tree for C*\/); // will not match

    • isExtensionOf

      public static Matcher<com.sun.source.tree.ClassTree> isExtensionOf(String clazz)
      Matches any node that is a direct extension of the given class.
    • hasAnyAnnotation

      @SafeVarargs public static Matcher<com.sun.source.tree.Tree> hasAnyAnnotation(Class<? extends Annotation>... annotations)
    • hasAnyAnnotation

      public static Matcher<com.sun.source.tree.Tree> hasAnyAnnotation(List<? extends TypeMirror> mirrors)
    • methodCallInDeclarationOfThrowingRunnable

      public static boolean methodCallInDeclarationOfThrowingRunnable(VisitorState state)
    • isThrowingFunctionalInterface

      public static boolean isThrowingFunctionalInterface(com.sun.tools.javac.code.Type clazzType, VisitorState state)
    • packageMatches

      public static <T extends com.sun.source.tree.Tree> Matcher<T> packageMatches(Predicate<String> predicate)
      Matches an AST node whose compilation unit's package name matches the given predicate.
    • packageMatches

      public static <T extends com.sun.source.tree.Tree> Matcher<T> packageMatches(Pattern pattern)
      Matches an AST node whose compilation unit's package name matches the given pattern.
    • packageStartsWith

      public static <T extends com.sun.source.tree.Tree> Matcher<T> packageStartsWith(String prefix)
      Matches an AST node whose compilation unit starts with this prefix.
    • staticEqualsInvocation

      public static <T extends com.sun.source.tree.ExpressionTree> Matcher<T> staticEqualsInvocation()
      Matches an invocation of a recognized static object equality method such as Objects.equals(java.lang.Object, java.lang.Object). These are simple facades to Object.equals(java.lang.Object) that accept null for either argument.
    • instanceEqualsInvocation

      public static <T extends com.sun.source.tree.ExpressionTree> Matcher<T> instanceEqualsInvocation()
      Matches calls to the method Object.equals(Object) or any override of that method.
    • instanceHashCodeInvocation

      public static Matcher<com.sun.source.tree.ExpressionTree> instanceHashCodeInvocation()
      Matches calls to the method Object.hashCode() or any override of that method.
    • assertEqualsInvocation

      public static Matcher<com.sun.source.tree.ExpressionTree> assertEqualsInvocation()
      Matches calls to the method org.junit.Assert#assertEquals and corresponding methods in JUnit 3.x.
    • assertNotEqualsInvocation

      public static Matcher<com.sun.source.tree.ExpressionTree> assertNotEqualsInvocation()
      Matches calls to the method org.junit.Assert#assertNotEquals and corresponding methods in JUnit 3.x.
    • equalsMethodDeclaration

      public static Matcher<com.sun.source.tree.MethodTree> equalsMethodDeclaration()
      Matches Object.equals(java.lang.Object) method declaration.
    • toStringMethodDeclaration

      public static Matcher<com.sun.source.tree.MethodTree> toStringMethodDeclaration()
      Matches Object.toString() method declaration.
    • hashCodeMethodDeclaration

      public static Matcher<com.sun.source.tree.MethodTree> hashCodeMethodDeclaration()
      Matches hashCode method declaration.
    • compareToMethodDeclaration

      public static Matcher<com.sun.source.tree.MethodTree> compareToMethodDeclaration()
      Matches compareTo method declaration.
    • matchExpressionReturn

      public static Matcher<com.sun.source.tree.StatementTree> matchExpressionReturn(Matcher<com.sun.source.tree.ExpressionTree> expressionTreeMatcher)
      Matches the Tree if it returns an expression matching expressionTreeMatcher.
    • matchSingleStatementBlock

      public static Matcher<com.sun.source.tree.BlockTree> matchSingleStatementBlock(Matcher<com.sun.source.tree.StatementTree> statementMatcher)
      Matches a BlockTree if it single statement block with statement matching statementMatcher.
    • singleStatementReturnMatcher

      public static Matcher<com.sun.source.tree.MethodTree> singleStatementReturnMatcher(Matcher<com.sun.source.tree.ExpressionTree> expressionTreeMatcher)
      Returns a matcher for MethodTree whose implementation contains a single return statement with expression matching the passed expressionTreeMatcher.