Class Matchers
- Author:
- alexeagle@google.com (Alex Eagle)
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends com.sun.source.tree.Tree>
Matcher<T> 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> 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> static <T extends com.sun.source.tree.Tree>
Matcher<T> 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> anything()
A matcher that matches any AST node.static Matcher
<com.sun.source.tree.MethodInvocationTree> static Matcher
<com.sun.source.tree.MethodInvocationTree> argumentCount
(int argumentCount) static Matcher
<com.sun.source.tree.ExpressionTree> Matches calls to the methodorg.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 methodorg.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 anassert
statement where the condition is matched by the passedconditionMatcher
.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
orBoolean.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> MatchescompareTo
method declaration.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.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.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> constructorOfClass
(String className) Matches a constructor declaration in a specific enclosing class.static Matcher
<com.sun.source.tree.Tree> 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> 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 acontinue
statement.static Matcher
<com.sun.source.tree.AnnotationTree> doesNotHaveArgument
(String argumentName) 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> MatchesObject.equals(java.lang.Object)
method declaration.static Matcher
<com.sun.source.tree.StatementTree> expressionStatement
(Matcher<com.sun.source.tree.ExpressionTree> matcher) Matches anExpressionStatementTree
based on itsExpressionTree
.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> hasAnnotationOnAnyOverriddenMethod
(String annotationClass) 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> hasAnnotationWithSimpleName
(String simpleName) 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> MatcheshashCode
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> Matches a class in which at least one method matches the given methodMatcher.static <T extends com.sun.source.tree.Tree>
Matcher<T> hasModifier
(Modifier modifier) Returns true if the Tree node has the expectedModifier
.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> inLoop()
Matches if the given tree is inside a loop.static <T extends com.sun.source.tree.ExpressionTree>
Matcher<T> Matches calls to the methodObject.equals(Object)
or any override of that method.static Matcher
<com.sun.source.tree.ExpressionTree> Matches calls to the methodObject.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> isDirectImplementationOf
(String clazz) Matches any node that is directly an implementation, but not extension, of the given Class.static Matcher
<com.sun.source.tree.ClassTree> isEnum()
Matches if aClassTree
is an enum declaration.static Matcher
<com.sun.source.tree.ClassTree> isExtensionOf
(String clazz) Matches any node that is a direct extension of the given class.static Matcher
<com.sun.source.tree.VariableTree> isField()
Matches if aVariableTree
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> isNull()
Deprecated.useisNullUsingDataflow()
instead.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 avoid
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> isStatic()
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> isSubtypeOf
(String typeStr) 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> 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 avoid
type.static <T extends com.sun.source.tree.Tree>
Matcher<T> 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 theTree
if it returns an expression matchingexpressionTreeMatcher
.static Matcher
<com.sun.source.tree.BlockTree> matchSingleStatementBlock
(Matcher<com.sun.source.tree.StatementTree> statementMatcher) Matches aBlockTree
if it single statement block with statement matchingstatementMatcher
.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> methodHasVisibility
(MethodVisibility.Visibility visibility) 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 matchesmethodSelectMatcher
.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> methodNameStartsWith
(String prefix) 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> nestingKind
(NestingKind kind) 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> Matches an AST node iff it does not match the given matcher.static <T extends com.sun.source.tree.Tree>
Matcher<T> nothing()
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> packageMatches
(Predicate<String> predicate) Matches an AST node whose compilation unit's package name matches the given predicate.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.static <T extends com.sun.source.tree.Tree>
Matcher<T> packageStartsWith
(String prefix) 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> receiverSameAsArgument
(int argNum) 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 areturn
statement where the returned expression is matched by the passedreturnedMatcher
.static Matcher
<? super com.sun.source.tree.MethodInvocationTree> sameArgument
(int index1, int index2) Matches aMethodInvocation
when the arguments at the two given indices are both the same variable, as determined byASTHelpers.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 forMethodTree
whose implementation contains a single return statement with expression matching the passedexpressionTreeMatcher
.static <T extends com.sun.source.tree.ExpressionTree>
Matcher<T> Matches an invocation of a recognized static object equality method such asObjects.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> stringLiteral
(String value) Matches a Literal AST node if it is a string literal with the given value.static Matcher
<com.sun.source.tree.ExpressionTree> stringLiteral
(Pattern pattern) Matches a Literal AST node if it is a string literal which matches the givenPattern
.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 byASTHelpers.getSymbol(Tree)
.static Matcher
<com.sun.source.tree.StatementTree> throwStatement
(Matcher<? super com.sun.source.tree.ExpressionTree> thrownMatcher) Matches athrow
statement where the thrown item is matched by the passedthrownMatcher
.static Matcher
<com.sun.source.tree.MethodTree> MatchesObject.toString()
method declaration.static <S extends T,
T extends com.sun.source.tree.Tree>
Matcher<T> Converts the given matcher to one that can be applied to any tree but is only executed when run on a tree oftype
and returnsfalse
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 byASTHelpers.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.
-
Field Details
-
MAIN_METHOD
-
SERIALIZATION_METHODS
Method signature of serialization methods. -
IS_INTERFACE
-
-
Method Details
-
anything
A matcher that matches any AST node. -
nothing
A matcher that matches no AST node. -
not
-
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
-
anyOf
-
anyOf
@SafeVarargs public static <T extends com.sun.source.tree.Tree> Matcher<T> anyOf(Matcher<? super T>... matchers) -
isInstance
-
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
-
isSame
Matches an AST node which is the same object reference as the given node. -
staticMethod
Matches a static method. -
instanceMethod
Matches an instance method. -
anyMethod
Matches a static or instance method. -
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 byASTHelpers.getSymbol(Tree)
.If
getSymbol
returnsnull
, the matcher returns false instead of callingpred
. -
isInstanceField
Matches an AST node that represents a non-static field. -
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 matchobj.method("", obj)
- Parameters:
argNum
- The number of the argument to compare against (zero-based.
-
receiverOfInvocation
-
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
-
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 calledmatchType
- how to match method arguments withmethodArgumentMatcher
methodArgumentMatcher
- matcher applied to each method argument
-
methodInvocation
-
argumentCount
-
parentNode
-
isSubtypeOf
-
isSubtypeOf
-
isSubtypeOf
-
isSameType
-
isSameType
-
isSameType
-
typePredicateMatcher
public static <T extends com.sun.source.tree.Tree> Matcher<T> typePredicateMatcher(TypePredicate pred) Match a Tree based solely on the type produced byASTHelpers.getType(Tree)
.If
getType
returnsnull
, the matcher returns false instead of callingpred
. -
isArrayType
Matches an AST node if its type is an array type. -
isPrimitiveArrayType
Matches an AST node if its type is a primitive array type. -
isPrimitiveType
Matches an AST node if its type is a primitive type. -
isPrimitiveOrVoidType
Matches an AST node if its type is either a primitive type or avoid
type. -
isVoidType
Matches an AST node if its type is avoid
type. -
isPrimitiveOrBoxedPrimitiveType
Matches an AST node if its type is a primitive type, or a boxed version of a primitive type. -
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
-
nextStatement
-
previousStatement
-
isLastStatementInBlock
Matches a statement AST node if the statement is the last statement in the block. -
nullLiteral
Matches an AST node if it is a null literal. -
nonNullLiteral
Matches an AST node if it is a literal other than null. -
stringLiteral
-
stringLiteral
-
booleanLiteral
-
booleanConstant
Matches the boolean constant (Boolean.TRUE
orBoolean.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 tovalue
in(((value)))
. -
intLiteral
-
classLiteral
-
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
-
isType
-
sameArgument
public static Matcher<? super com.sun.source.tree.MethodInvocationTree> sameArgument(int index1, int index2) Matches aMethodInvocation
when the arguments at the two given indices are both the same variable, as determined byASTHelpers.sameVariable(com.sun.source.tree.ExpressionTree, com.sun.source.tree.ExpressionTree)
.- Parameters:
index1
- the index of the first actual parameter to testindex2
- the index of the second actual parameter to test- Throws:
IndexOutOfBoundsException
- if the given indices are invalid
-
hasAnnotation
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
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
Matches a method invocation that is known to never return null. -
methodReturns
-
methodReturns
-
methodReturnsNonPrimitiveType
Match a method that returns a non-primitive type. -
methodIsNamed
-
methodNameStartsWith
-
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
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
-
constructorOfClass
-
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
-
variableInitializer
-
isField
Matches if aVariableTree
is a field declaration, as opposed to a local variable, enum constant, parameter to a method, etc. -
isEnum
Matches if aClassTree
is an enum declaration. -
nestingKind
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
-
staticFieldAccess
Matches an AST node which is an expression yielding the indicated static field access. -
isStatic
Matches an AST node that is static. -
isTransient
Matches an AST node that is transient. -
throwStatement
-
returnStatement
-
assertStatement
-
continueStatement
Matches acontinue
statement. -
expressionStatement
-
toType
-
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
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.useisNonNullUsingDataflow()
instead. -
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.useisNullUsingDataflow()
instead. -
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
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
Matches if the method accepts the given number of arguments.- Parameters:
arity
- the number of arguments the method should accept
-
isDirectImplementationOf
Matches any node that is directly an implementation, but not extension, of the given Class.E.x.
class C implements I
will match, butclass 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
-
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
-
isThrowingFunctionalInterface
public static boolean isThrowingFunctionalInterface(com.sun.tools.javac.code.Type clazzType, VisitorState state) -
packageMatches
-
packageMatches
-
packageStartsWith
-
staticEqualsInvocation
Matches an invocation of a recognized static object equality method such asObjects.equals(java.lang.Object, java.lang.Object)
. These are simple facades toObject.equals(java.lang.Object)
that accept null for either argument. -
instanceEqualsInvocation
Matches calls to the methodObject.equals(Object)
or any override of that method. -
instanceHashCodeInvocation
Matches calls to the methodObject.hashCode()
or any override of that method. -
assertEqualsInvocation
Matches calls to the methodorg.junit.Assert#assertEquals
and corresponding methods in JUnit 3.x. -
assertNotEqualsInvocation
Matches calls to the methodorg.junit.Assert#assertNotEquals
and corresponding methods in JUnit 3.x. -
equalsMethodDeclaration
MatchesObject.equals(java.lang.Object)
method declaration. -
toStringMethodDeclaration
MatchesObject.toString()
method declaration. -
hashCodeMethodDeclaration
MatcheshashCode
method declaration. -
compareToMethodDeclaration
MatchescompareTo
method declaration. -
matchExpressionReturn
-
matchSingleStatementBlock
-
singleStatementReturnMatcher
-
isNonNullUsingDataflow()
instead.