Class AbstractMockChecker<T extends Annotation>
java.lang.Object
com.google.errorprone.bugpatterns.BugChecker
com.google.errorprone.bugpatterns.AbstractMockChecker<T>
- All Implemented Interfaces:
BugChecker.MethodInvocationTreeMatcher
,BugChecker.VariableTreeMatcher
,Suppressible
,Serializable
- Direct Known Subclasses:
DoNotMockAutoValue
,DoNotMockChecker
public abstract class AbstractMockChecker<T extends Annotation>
extends BugChecker
implements BugChecker.MethodInvocationTreeMatcher, BugChecker.VariableTreeMatcher
Helper for enforcing Annotations that disallow mocking.
- Author:
- amalloy@google.com (Alan Malloy)
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
A policy for determining what classes should not be mocked.static class
An explanation of what type should not be mocked, and the reason why.static interface
AbstractMockChecker.TypeExtractor<T extends com.sun.source.tree.Tree>
An extension ofMatcher
to return, not just a boolean `matches`, but also extract some type information about the Tree of interest.Nested classes/interfaces inherited from class com.google.errorprone.bugpatterns.BugChecker
BugChecker.AnnotatedTypeTreeMatcher, BugChecker.AnnotationTreeMatcher, BugChecker.ArrayAccessTreeMatcher, BugChecker.ArrayTypeTreeMatcher, BugChecker.AssertTreeMatcher, BugChecker.AssignmentTreeMatcher, BugChecker.BinaryTreeMatcher, BugChecker.BlockTreeMatcher, BugChecker.BreakTreeMatcher, BugChecker.CaseTreeMatcher, BugChecker.CatchTreeMatcher, BugChecker.ClassTreeMatcher, BugChecker.CompilationUnitTreeMatcher, BugChecker.CompoundAssignmentTreeMatcher, BugChecker.ConditionalExpressionTreeMatcher, BugChecker.ContinueTreeMatcher, BugChecker.DoWhileLoopTreeMatcher, BugChecker.EmptyStatementTreeMatcher, BugChecker.EnhancedForLoopTreeMatcher, BugChecker.ExpressionStatementTreeMatcher, BugChecker.ForLoopTreeMatcher, BugChecker.IdentifierTreeMatcher, BugChecker.IfTreeMatcher, BugChecker.ImportTreeMatcher, BugChecker.InstanceOfTreeMatcher, BugChecker.IntersectionTypeTreeMatcher, BugChecker.LabeledStatementTreeMatcher, BugChecker.LambdaExpressionTreeMatcher, BugChecker.LiteralTreeMatcher, BugChecker.MemberReferenceTreeMatcher, BugChecker.MemberSelectTreeMatcher, BugChecker.MethodInvocationTreeMatcher, BugChecker.MethodTreeMatcher, BugChecker.ModifiersTreeMatcher, BugChecker.NewArrayTreeMatcher, BugChecker.NewClassTreeMatcher, BugChecker.ParameterizedTypeTreeMatcher, BugChecker.ParenthesizedTreeMatcher, BugChecker.PrimitiveTypeTreeMatcher, BugChecker.ReturnTreeMatcher, BugChecker.SuppressibleTreePathScanner<R,
P>, BugChecker.SwitchTreeMatcher, BugChecker.SynchronizedTreeMatcher, BugChecker.ThrowTreeMatcher, BugChecker.TryTreeMatcher, BugChecker.TypeCastTreeMatcher, BugChecker.TypeParameterTreeMatcher, BugChecker.UnaryTreeMatcher, BugChecker.UnionTypeTreeMatcher, BugChecker.VariableTreeMatcher, BugChecker.WhileLoopTreeMatcher, BugChecker.WildcardTreeMatcher -
Field Summary
Modifier and TypeFieldDescriptionstatic final AbstractMockChecker.TypeExtractor
<com.sun.source.tree.MethodInvocationTree> A TypeExtractor for method invocations that create a mock using Mockito.mock, Mockito.spy, or EasyMock.create[...]Mock, extracting the type being mocked. -
Constructor Summary
ConstructorDescriptionAbstractMockChecker
(AbstractMockChecker.TypeExtractor<com.sun.source.tree.VariableTree> varExtractor, AbstractMockChecker.TypeExtractor<com.sun.source.tree.MethodInvocationTree> methodExtractor, Class<T> annotationClass, Function<T, String> getValueFunction) -
Method Summary
Modifier and TypeMethodDescriptionstatic AbstractMockChecker.TypeExtractor
<com.sun.source.tree.MethodInvocationTree> extractClassArg
(Matcher<com.sun.source.tree.MethodInvocationTree> m) Produces an extractor which, if the tree matches, extracts the type of the first argument whose type isClass
(preserving its<T>
type parameter, if it has one}.static AbstractMockChecker.TypeExtractor
<com.sun.source.tree.MethodInvocationTree> extractFirstArg
(Matcher<com.sun.source.tree.MethodInvocationTree> m) Produces an extractor which, if the tree matches, extracts the type of the first argument to the method invocation.static <T extends com.sun.source.tree.Tree>
AbstractMockChecker.TypeExtractor<T> extractType
(Matcher<T> m) Produces an extractor which, if the tree matches, extracts the type of that tree, as given byASTHelpers.getType(Tree)
.static AbstractMockChecker.TypeExtractor
<com.sun.source.tree.VariableTree> fieldAnnotatedWithOneOf
(Stream<String> annotationClasses) Creates a TypeExtractor that extracts the type of a class field if that field is annotated with any one of the given annotations.protected AbstractMockChecker.MockForbidder
Produce a MockForbidder to use when looking for disallowed mocks, in addition to the built-in checks for Annotations of typeT
.final Description
matchMethodInvocation
(com.sun.source.tree.MethodInvocationTree tree, VisitorState state) final Description
matchVariable
(com.sun.source.tree.VariableTree tree, VisitorState state) Methods inherited from class com.google.errorprone.bugpatterns.BugChecker
allNames, buildDescription, buildDescription, buildDescription, canonicalName, customSuppressionAnnotations, defaultSeverity, describeMatch, describeMatch, describeMatch, describeMatch, describeMatch, describeMatch, disableable, equals, hashCode, isSuppressed, isSuppressed, isSuppressed, isSuppressed, linkUrl, message, supportsSuppressWarnings, suppressedByAnyOf, suppressedRegions
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.google.errorprone.matchers.Suppressible
allNames, canonicalName, customSuppressionAnnotations, supportsSuppressWarnings, suppressedByAnyOf
-
Field Details
-
MOCKING_METHOD
public static final AbstractMockChecker.TypeExtractor<com.sun.source.tree.MethodInvocationTree> MOCKING_METHODA TypeExtractor for method invocations that create a mock using Mockito.mock, Mockito.spy, or EasyMock.create[...]Mock, extracting the type being mocked.
-
-
Constructor Details
-
AbstractMockChecker
public AbstractMockChecker(AbstractMockChecker.TypeExtractor<com.sun.source.tree.VariableTree> varExtractor, AbstractMockChecker.TypeExtractor<com.sun.source.tree.MethodInvocationTree> methodExtractor, Class<T> annotationClass, Function<T, String> getValueFunction)
-
-
Method Details
-
forbidder
Produce a MockForbidder to use when looking for disallowed mocks, in addition to the built-in checks for Annotations of typeT
.This method will be called at most once for each instance of AbstractMockChecker, but of course the returned object's
forbidReason
method may be called many times.The default implementation forbids nothing.
-
extractType
public static <T extends com.sun.source.tree.Tree> AbstractMockChecker.TypeExtractor<T> extractType(Matcher<T> m) Produces an extractor which, if the tree matches, extracts the type of that tree, as given byASTHelpers.getType(Tree)
. -
extractFirstArg
public static AbstractMockChecker.TypeExtractor<com.sun.source.tree.MethodInvocationTree> extractFirstArg(Matcher<com.sun.source.tree.MethodInvocationTree> m) Produces an extractor which, if the tree matches, extracts the type of the first argument to the method invocation. -
extractClassArg
public static AbstractMockChecker.TypeExtractor<com.sun.source.tree.MethodInvocationTree> extractClassArg(Matcher<com.sun.source.tree.MethodInvocationTree> m) Produces an extractor which, if the tree matches, extracts the type of the first argument whose type isClass
(preserving its<T>
type parameter, if it has one}.- Parameters:
m
- the matcher to use. It is an error for this matcher to succeed on any Tree that does not include at least one argument of typeClass
; if such a matcher is provided, the behavior of the returned Extractor is undefined.
-
fieldAnnotatedWithOneOf
public static AbstractMockChecker.TypeExtractor<com.sun.source.tree.VariableTree> fieldAnnotatedWithOneOf(Stream<String> annotationClasses) Creates a TypeExtractor that extracts the type of a class field if that field is annotated with any one of the given annotations. -
matchMethodInvocation
public final Description matchMethodInvocation(com.sun.source.tree.MethodInvocationTree tree, VisitorState state) - Specified by:
matchMethodInvocation
in interfaceBugChecker.MethodInvocationTreeMatcher
-
matchVariable
- Specified by:
matchVariable
in interfaceBugChecker.VariableTreeMatcher
-