Package org.revapi.java.spi
Class CheckBase
- java.lang.Object
-
- org.revapi.java.spi.CheckBase
-
- All Implemented Interfaces:
org.revapi.configuration.Configurable
,Check
public abstract class CheckBase extends Object implements Check
A basic implementation of theCheck
interface. This class easies the matching of thevisit*()
methods and their correspondingvisitEnd()
by keeping track of the "depth" individual calls (see the recursive nature of thecall order
).This class also contains a couple of utility methods for checking the accessibility of elements, etc.
- Since:
- 0.1
- Author:
- Lukas Krejci
- See Also:
pushActive(JavaElement, JavaElement, Object...)
,popIfActive()
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
CheckBase.ActiveElements<T extends JavaElement>
Represents the elements that have beenpushed
onto the active elements stack.-
Nested classes/interfaces inherited from interface org.revapi.java.spi.Check
Check.Type
-
-
Constructor Summary
Constructors Constructor Description CheckBase()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected org.revapi.Difference
createDifference(Code code, LinkedHashMap<String,String> attachments)
protected org.revapi.Difference
createDifferenceWithExplicitParams(Code code, LinkedHashMap<String,String> attachments, String... params)
protected List<org.revapi.Difference>
doEnd()
protected List<org.revapi.Difference>
doVisitAnnotation(JavaAnnotationElement oldAnnotation, JavaAnnotationElement newAnnotation)
protected void
doVisitClass(JavaTypeElement oldType, JavaTypeElement newType)
protected void
doVisitField(JavaFieldElement oldField, JavaFieldElement newField)
protected void
doVisitMethod(JavaMethodElement oldMethod, JavaMethodElement newMethod)
protected void
doVisitMethodParameter(JavaMethodParameterElement oldParameter, JavaMethodParameterElement newParameter)
org.revapi.AnalysisContext
getAnalysisContext()
String
getExtensionId()
Reader
getJSONSchema()
TypeEnvironment
getNewTypeEnvironment()
TypeEnvironment
getOldTypeEnvironment()
void
initialize(org.revapi.AnalysisContext analysisContext)
boolean
isAccessible(JavaModelElement e)
This method checks that the provided element and all its parent elements are accessible (public or protected).boolean
isBothAccessible(JavaModelElement a, JavaModelElement b)
Checks whether both provided elements are public or protected.boolean
isBothPrivate(JavaModelElement a, JavaModelElement b)
Checks whether both provided elements are (package) private.boolean
isDescendingOnNonExisting()
The default implementation returns false, because that is the right decision most of the time.boolean
isMissing(Element e)
The element is deemed missing if its type kind (TypeMirror.getKind()
) isTypeKind.ERROR
.protected CheckBase.ActiveElements<?>
peekLastActive()
protected <T extends JavaElement>
CheckBase.ActiveElements<T>popIfActive()
Pops the top of the stack of active elements if the current position in the call stack corresponds to the one that pushed the active elements.protected <T extends JavaElement>
voidpushActive(T oldElement, T newElement, Object... context)
If called in one of thedoVisit*()
methods, this method will push the elements along with some contextual data onto an internal stack.void
setNewTypeEnvironment(TypeEnvironment env)
The environment containing the new version of the classes.void
setOldTypeEnvironment(TypeEnvironment env)
The environment containing the old version of the classes.List<org.revapi.Difference>
visitAnnotation(JavaAnnotationElement oldAnnotation, JavaAnnotationElement newAnnotation)
Please override thedoVisitAnnotation(JavaAnnotationElement, JavaAnnotationElement)
instead.void
visitClass(JavaTypeElement oldType, JavaTypeElement newType)
Please override thedoVisitClass(JavaTypeElement, JavaTypeElement)
List<org.revapi.Difference>
visitEnd()
Please override thedoEnd()
method instead.void
visitField(JavaFieldElement oldField, JavaFieldElement newField)
Please override thedoVisitField(JavaFieldElement, JavaFieldElement)
instead.void
visitMethod(JavaMethodElement oldMethod, JavaMethodElement newMethod)
Please override thedoVisitMethod(JavaMethodElement, JavaMethodElement)
instead.void
visitMethodParameter(JavaMethodParameterElement oldParameter, JavaMethodParameterElement newParameter)
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.revapi.java.spi.Check
getInterest
-
-
-
-
Method Detail
-
isDescendingOnNonExisting
public boolean isDescendingOnNonExisting()
The default implementation returns false, because that is the right decision most of the time.- Specified by:
isDescendingOnNonExisting
in interfaceCheck
- Returns:
- false
-
isBothPrivate
public boolean isBothPrivate(@Nullable JavaModelElement a, @Nullable JavaModelElement b)
Checks whether both provided elements are (package) private. If one of them is null, the fact cannot be determined and therefore this method would return false.- Parameters:
a
- first elementb
- second element- Returns:
- true if both elements are not null and are private or package private
-
isBothAccessible
public boolean isBothAccessible(@Nullable JavaModelElement a, @Nullable JavaModelElement b)
Checks whether both provided elements are public or protected. If one at least one of them is null, the method returns false, because the accessibility cannot be truthfully detected in that case.- Parameters:
a
- first elementb
- second element- Returns:
- true if both elements are not null and accessible (i.e. public or protected)
-
isAccessible
public boolean isAccessible(@Nonnull JavaModelElement e)
This method checks that the provided element and all its parent elements are accessible (public or protected). Additionally, if the provided element is a type, it must be in API or, if it is not a type, its nearest enclosing type must be in API.- Parameters:
e
- the element to check- Returns:
- true if the provided element is accessible and in API, false otherwise.
-
isMissing
public boolean isMissing(@Nonnull Element e)
The element is deemed missing if its type kind (TypeMirror.getKind()
) isTypeKind.ERROR
.- Parameters:
e
- the element- Returns:
- true if the element is missing, false otherwise
-
createDifference
@Nonnull protected org.revapi.Difference createDifference(@Nonnull Code code, LinkedHashMap<String,String> attachments)
-
createDifferenceWithExplicitParams
@Nonnull protected org.revapi.Difference createDifferenceWithExplicitParams(@Nonnull Code code, LinkedHashMap<String,String> attachments, String... params)
-
getOldTypeEnvironment
@Nonnull public TypeEnvironment getOldTypeEnvironment()
-
getNewTypeEnvironment
@Nonnull public TypeEnvironment getNewTypeEnvironment()
-
getAnalysisContext
@Nonnull public org.revapi.AnalysisContext getAnalysisContext()
-
getExtensionId
@Nullable public String getExtensionId()
- Specified by:
getExtensionId
in interfaceorg.revapi.configuration.Configurable
-
getJSONSchema
@Nullable public Reader getJSONSchema()
- Specified by:
getJSONSchema
in interfaceorg.revapi.configuration.Configurable
-
initialize
public void initialize(@Nonnull org.revapi.AnalysisContext analysisContext)
- Specified by:
initialize
in interfaceorg.revapi.configuration.Configurable
-
setOldTypeEnvironment
public void setOldTypeEnvironment(@Nonnull TypeEnvironment env)
Description copied from interface:Check
The environment containing the old version of the classes. This can be used to reason about the classes when doing the checks.Called once after the check has been instantiated.
- Specified by:
setOldTypeEnvironment
in interfaceCheck
- Parameters:
env
- the environment to obtain the helper objects using which one can navigate and examine types
-
setNewTypeEnvironment
public void setNewTypeEnvironment(@Nonnull TypeEnvironment env)
Description copied from interface:Check
The environment containing the new version of the classes. This can be used to reason about the classes when doing the checks.Called once after the check has been instantiated.
- Specified by:
setNewTypeEnvironment
in interfaceCheck
- Parameters:
env
- the environment to obtain the helper objects using which one can navigate and examine types
-
visitEnd
@Nullable public final List<org.revapi.Difference> visitEnd()
Please override thedoEnd()
method instead.- Specified by:
visitEnd
in interfaceCheck
- Returns:
- the list of found differences between corresponding elements or null if no differences found (null is considered equivalent to returning an empty collection).
- See Also:
Check.visitEnd()
-
visitClass
public final void visitClass(@Nullable JavaTypeElement oldType, @Nullable JavaTypeElement newType)
Please override thedoVisitClass(JavaTypeElement, JavaTypeElement)
- Specified by:
visitClass
in interfaceCheck
- See Also:
Check.visitClass(JavaTypeElement, JavaTypeElement)
-
doVisitClass
protected void doVisitClass(@Nullable JavaTypeElement oldType, @Nullable JavaTypeElement newType)
-
visitMethod
public final void visitMethod(@Nullable JavaMethodElement oldMethod, @Nullable JavaMethodElement newMethod)
Please override thedoVisitMethod(JavaMethodElement, JavaMethodElement)
instead.- Specified by:
visitMethod
in interfaceCheck
- See Also:
Check.visitMethod(JavaMethodElement, JavaMethodElement)
-
doVisitMethod
protected void doVisitMethod(@Nullable JavaMethodElement oldMethod, @Nullable JavaMethodElement newMethod)
-
visitMethodParameter
public final void visitMethodParameter(@Nullable JavaMethodParameterElement oldParameter, @Nullable JavaMethodParameterElement newParameter)
- Specified by:
visitMethodParameter
in interfaceCheck
-
doVisitMethodParameter
protected void doVisitMethodParameter(@Nullable JavaMethodParameterElement oldParameter, @Nullable JavaMethodParameterElement newParameter)
-
visitField
public final void visitField(@Nullable JavaFieldElement oldField, @Nullable JavaFieldElement newField)
Please override thedoVisitField(JavaFieldElement, JavaFieldElement)
instead.- Specified by:
visitField
in interfaceCheck
- See Also:
Check.visitField(JavaFieldElement, JavaFieldElement)
-
doVisitField
protected void doVisitField(@Nullable JavaFieldElement oldField, @Nullable JavaFieldElement newField)
-
visitAnnotation
@Nullable public final List<org.revapi.Difference> visitAnnotation(@Nullable JavaAnnotationElement oldAnnotation, @Nullable JavaAnnotationElement newAnnotation)
Please override thedoVisitAnnotation(JavaAnnotationElement, JavaAnnotationElement)
instead.- Specified by:
visitAnnotation
in interfaceCheck
- Parameters:
oldAnnotation
- the annotation in the old APInewAnnotation
- the annotation in the new API- Returns:
- the list of differences between the two annotations
- See Also:
Check.visitAnnotation(JavaAnnotationElement, JavaAnnotationElement)
-
doVisitAnnotation
@Nullable protected List<org.revapi.Difference> doVisitAnnotation(@Nullable JavaAnnotationElement oldAnnotation, @Nullable JavaAnnotationElement newAnnotation)
-
pushActive
protected final <T extends JavaElement> void pushActive(@Nullable T oldElement, @Nullable T newElement, Object... context)
If called in one of thedoVisit*()
methods, this method will push the elements along with some contextual data onto an internal stack.You can then retrieve the contents on the top of the stack in your
doEnd()
override by calling thepopIfActive()
method.- Type Parameters:
T
- the type of the elements- Parameters:
oldElement
- the old API elementnewElement
- the new API elementcontext
- optional contextual data
-
popIfActive
@Nullable protected <T extends JavaElement> CheckBase.ActiveElements<T> popIfActive()
Pops the top of the stack of active elements if the current position in the call stack corresponds to the one that pushed the active elements.This method does not do any type checks, so take care to retrieve the elements with the same types used to push to them onto the stack.
- Type Parameters:
T
- the type of the elements- Returns:
- the active elements or null if the current call stack did not push any active elements onto the stack
-
peekLastActive
@Nullable protected CheckBase.ActiveElements<?> peekLastActive()
- Returns:
- the last activation. This can be called at any point and can refer to any of the enclosing elements of the currently processed element pair, depending on how this check activated them.
-
-