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:
- [email protected] (Alan Malloy)
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
AbstractMockChecker.MockForbidder
A policy for determining what classes should not be mocked.static class
AbstractMockChecker.Reason
An explanation of what type should not be mocked, and the reason why.static interface
AbstractMockChecker.TypeExtractor<T extends 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<A extends Object,B extends Object>, BugChecker.SwitchTreeMatcher, BugChecker.SynchronizedTreeMatcher, BugChecker.ThrowTreeMatcher, BugChecker.TryTreeMatcher, BugChecker.TypeCastTreeMatcher, BugChecker.TypeParameterTreeMatcher, BugChecker.UnaryTreeMatcher, BugChecker.UnionTypeTreeMatcher, BugChecker.VariableTreeMatcher, BugChecker.WhileLoopTreeMatcher, BugChecker.WildcardTreeMatcher
-
-
Field Summary
Fields Modifier and Type Field Description static AbstractMockChecker.TypeExtractor<MethodInvocationTree>
MOCKING_METHOD
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
Constructors Constructor Description AbstractMockChecker(AbstractMockChecker.TypeExtractor<VariableTree> varExtractor, AbstractMockChecker.TypeExtractor<MethodInvocationTree> methodExtractor, Class<T> annotationClass, Function<T,String> getValueFunction)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static AbstractMockChecker.TypeExtractor<MethodInvocationTree>
extractClassArg(Matcher<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<MethodInvocationTree>
extractFirstArg(Matcher<MethodInvocationTree> m)
Produces an extractor which, if the tree matches, extracts the type of the first argument to the method invocation.static <T extends 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<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
forbidder()
Produce a MockForbidder to use when looking for disallowed mocks, in addition to the built-in checks for Annotations of typeT
.Description
matchMethodInvocation(MethodInvocationTree tree, VisitorState state)
Description
matchVariable(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, 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 Detail
-
MOCKING_METHOD
public static final AbstractMockChecker.TypeExtractor<MethodInvocationTree> MOCKING_METHOD
A TypeExtractor for method invocations that create a mock using Mockito.mock, Mockito.spy, or EasyMock.create[...]Mock, extracting the type being mocked.
-
-
Constructor Detail
-
AbstractMockChecker
public AbstractMockChecker(AbstractMockChecker.TypeExtractor<VariableTree> varExtractor, AbstractMockChecker.TypeExtractor<MethodInvocationTree> methodExtractor, Class<T> annotationClass, Function<T,String> getValueFunction)
-
-
Method Detail
-
forbidder
protected AbstractMockChecker.MockForbidder 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 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<MethodInvocationTree> extractFirstArg(Matcher<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<MethodInvocationTree> extractClassArg(Matcher<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<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(MethodInvocationTree tree, VisitorState state)
- Specified by:
matchMethodInvocation
in interfaceBugChecker.MethodInvocationTreeMatcher
-
matchVariable
public final Description matchVariable(VariableTree tree, VisitorState state)
- Specified by:
matchVariable
in interfaceBugChecker.VariableTreeMatcher
-
-