Class BaseTypeVisitor<Factory extends GenericAnnotatedTypeFactory<?,?,?,?>>
- All Implemented Interfaces:
TreeVisitor<Void,
Void>
- Direct Known Subclasses:
AccumulationVisitor
,AliasingVisitor
,ClassValVisitor
,FenumVisitor
,FormatterVisitor
,GuiEffectVisitor
,I18nFormatterVisitor
,InitializationVisitor
,InterningVisitor
,LessThanVisitor
,LockVisitor
,LowerBoundVisitor
,MethodValVisitor
,MustCallVisitor
,OptionalVisitor
,RegexVisitor
,ReportVisitor
,ReturnsReceiverVisitor
,SameLenVisitor
,SignednessVisitor
,TaintingVisitor
,TypeOutputtingChecker.Visitor
,UnitsVisitor
,UpperBoundVisitor
,ValueVisitor
SourceVisitor
that performs assignment and pseudo-assignment checking, method
invocation checking, and assignability checking. The visitor visits every construct in a program,
not just types.
This implementation uses the AnnotatedTypeFactory
implementation provided by an
associated BaseTypeChecker
; its visitor methods will invoke this factory on parts of the
AST to determine the "annotated type" of an expression. Then, the visitor methods will check the
types in assignments and pseudo-assignments using commonAssignmentCheck(com.sun.source.tree.Tree, com.sun.source.tree.ExpressionTree, java.lang.String, java.lang.Object...)
, which
ultimately calls the TypeHierarchy.isSubtype(org.checkerframework.framework.type.AnnotatedTypeMirror, org.checkerframework.framework.type.AnnotatedTypeMirror)
method and reports errors that violate
Java's rules of assignment.
Note that since this implementation only performs assignment and pseudo-assignment checking,
other rules for custom type systems must be added in subclasses (e.g., dereference checking in
the NullnessChecker
is implemented in the NullnessChecker
's TreeScanner.visitMemberSelect(com.sun.source.tree.MemberSelectTree, P)
method).
This implementation does the following checks:
- Assignment and Pseudo-Assignment Check: It verifies that any assignment type-checks,
using the
TypeHierarchy.isSubtype(org.checkerframework.framework.type.AnnotatedTypeMirror, org.checkerframework.framework.type.AnnotatedTypeMirror)
method. This includes method invocation and method overriding checks. - Type Validity Check: It verifies that any user-supplied type is a valid type, using
one of the
isValidUse
methods. - (Re-)Assignability Check: It verifies that any assignment is valid, using
Checker.isAssignable
method.
-
Nested Class Summary
Modifier and TypeClassDescriptionclass
Class to perform method override and method reference checks. -
Field Summary
Modifier and TypeFieldDescriptionfinal boolean
True if "-AassumePureGetters" was passed on the command line.protected final Factory
The factory to use for obtaining "parsed" version of annotations.protected final BaseTypeChecker
TheBaseTypeChecker
for error reporting.protected final AnnotationMirror
The @Deterministic
annotation.protected final AnnotationMirror
The @Impure
annotation.protected @Nullable MethodTree
The tree of the enclosing method that is currently being visited, if any.protected final SourcePositions
For obtaining line numbers in-Ashowchecks
debugging output.protected final AnnotationMirror
The @Pure
annotation.protected final QualifierHierarchy
The qualifier hierarchy.protected final boolean
True if "-Ashowchecks" was passed on the command line.protected final AnnotationMirror
The @SideEffectFree
annotation.protected final AnnotationMirror
The @java.lang.annotation.Target annotation.protected final ExecutableElement
Thevalue
element/field of the @java.lang.annotation.Target annotation.protected final TypeHierarchy
The Annotated Type Hierarchy.protected final TypeValidator
protected final ExecutableElement
Thewhen
element/field of the @Unused annotation.Fields inherited from class org.checkerframework.framework.source.SourceVisitor
elements, root, trees, treesWithSuppressWarnings, types
-
Constructor Summary
ModifierConstructorDescriptionBaseTypeVisitor
(BaseTypeChecker checker) protected
BaseTypeVisitor
(BaseTypeChecker checker, @Nullable Factory typeFactory) -
Method Summary
Modifier and TypeMethodDescriptionprotected void
checkAccess
(IdentifierTree identifierTree, Void p) Issues an error if access not allowed, based on an @Unused annotation.protected void
checkAccessAllowed
(Element field, @Nullable AnnotatedTypeMirror receiverType, ExpressionTree accessTree) Issues an error if access not allowed, based on an @Unused annotation.protected void
checkArguments
(List<? extends AnnotatedTypeMirror> requiredTypes, List<? extends ExpressionTree> passedArgs, CharSequence executableName, List<?> paramNames) A helper method to check that each passed argument is a subtype of the corresponding required argument.protected boolean
checkArrayInitialization
(AnnotatedTypeMirror type, List<? extends ExpressionTree> initializers) Checks that the array initializers are consistent with the array type.protected void
checkConditionalPostcondition
(MethodTree methodTree, AnnotationMirror annotation, JavaExpression expression, boolean result) Check that the expression's type is annotated withannotation
at every regular exit that returnsresult
.protected void
checkConstructorInvocation
(AnnotatedTypeMirror.AnnotatedDeclaredType invocation, AnnotatedTypeMirror.AnnotatedExecutableType constructor, NewClassTree newClassTree) Check that the (explicit) annotations on a new class tree are comparable to the result type of the constructor.protected void
checkConstructorResult
(AnnotatedTypeMirror.AnnotatedExecutableType constructorType, ExecutableElement constructorElement) Issue a warning if the result type of the constructor declaration is not top.protected boolean
checkContract
(JavaExpression expr, AnnotationMirror necessaryAnnotation, AnnotationMirror inferredAnnotation, CFAbstractStore<?, ?> store) Returns true if and only ifinferredAnnotation
is valid for a given expression to match thenecessaryAnnotation
.protected void
Check the default constructor.protected void
Issue error if the exception parameter is not a supertype of the annotation specified bygetExceptionParameterLowerBoundAnnotations()
, which is top by default.protected void
checkExplicitAnnotationsOnIntersectionBounds
(AnnotatedTypeMirror.AnnotatedIntersectionType intersection, List<? extends Tree> boundTrees) Issues "explicit.annotation.ignored" warning if any explicit annotation on an intersection bound is not the same as the primary annotation of the given intersection type.protected void
checkExtendsAndImplements
(ClassTree classTree) In@A class X extends @B Y implements @C Z {}
, enforce that@A
must be a subtype of@B
and@C
.protected void
checkExtendsOrImplements
(Tree superClause, AnnotationMirrorSet classBounds, TypeMirror classType, boolean isExtends) Helper forcheckExtendsAndImplements(com.sun.source.tree.ClassTree)
that checks one extends or implements clause.protected void
checkFieldInvariantDeclarations
(ClassTree classTree) Check that the field invariant declaration annotations meet the following requirements: If the superclass ofclassTree
has a field invariant, then the field invariant forclassTree
must include all the fields in the superclass invariant and those fields' annotations must be a subtype (or equal) to the annotations for those fields in the superclass.protected void
checkMethodInvocability
(AnnotatedTypeMirror.AnnotatedExecutableType method, MethodInvocationTree tree) Tests whether the method can be invoked using the receiver of the 'tree' method invocation, and issues a "method.invocation" if the invocation is invalid.protected boolean
checkMethodReferenceAsOverride
(MemberReferenceTree memberReferenceTree, Void p) Check that a method reference is allowed.protected boolean
checkOverride
(MethodTree overriderTree, AnnotatedTypeMirror.AnnotatedDeclaredType overriderType, AnnotatedTypeMirror.AnnotatedExecutableType overriddenMethodType, AnnotatedTypeMirror.AnnotatedDeclaredType overriddenType) Type checks that a method may override another method.protected boolean
checkOverride
(MethodTree overriderTree, AnnotatedTypeMirror.AnnotatedExecutableType overriderMethodType, AnnotatedTypeMirror.AnnotatedDeclaredType overriderType, AnnotatedTypeMirror.AnnotatedExecutableType overriddenMethodType, AnnotatedTypeMirror.AnnotatedDeclaredType overriddenType) Type checks that a method may override another method.protected void
checkPostcondition
(MethodTree methodTree, AnnotationMirror annotation, JavaExpression expression) Check that the expression's type is annotated withannotation
at the regular exit store.protected void
checkPreconditions
(MethodInvocationTree tree, Set<Contract.Precondition> preconditions) Checks that all the givenpreconditions
hold true immediately prior to the method invocation or variable access attree
.protected void
Check method purity if needed.protected void
checkQualifierParameter
(ClassTree classTree) Issues an error ifclassTree
has polymorphic fields but is not annotated with@HasQualifierParameter
.protected void
checkSuperConstructorCall
(MethodInvocationTree superCall) Checks that the following rule is satisfied: The type on a constructor declaration must be a supertype of the return type of "super()" invocation within that constructor.protected void
checkThisConstructorCall
(MethodInvocationTree thisCall) Checks that the following rule is satisfied: The type on a constructor declaration must be a supertype of the return type of "this()" invocation within that constructor.protected void
checkThisOrSuperConstructorCall
(MethodInvocationTree call, @CompilerMessageKey String errorKey) Checks that the following rule is satisfied: The type on a constructor declaration must be a supertype of the return type of "this()" or "super()" invocation within that constructor.protected void
Checks the type of the thrown expression.protected void
checkTypeArguments
(Tree toptree, List<? extends AnnotatedTypeParameterBounds> paramBounds, List<? extends AnnotatedTypeMirror> typeargs, List<? extends Tree> typeargTrees, CharSequence typeOrMethodName, List<?> paramNames) Checks that the annotations on the type arguments supplied to a type or a method invocation are within the bounds of the type variables as declared, and issues the "type.argument" error if they are not.protected void
checkTypecastRedundancy
(TypeCastTree typeCastTree) If the lint option "cast:redundant" is set, this method issues a warning if the cast is redundant.protected void
checkTypecastSafety
(TypeCastTree typeCastTree) Issues a warning if the given explicitly-written typecast is unsafe.protected void
checkVarargs
(AnnotatedTypeMirror.AnnotatedExecutableType invokedMethod, Tree tree) If the given invocation is a varargs invocation, check that the array type of actual varargs is a subtype of the corresponding formal parameter; issues "argument" error if not.protected boolean
commonAssignmentCheck
(Tree varTree, ExpressionTree valueExpTree, @CompilerMessageKey String errorKey, Object... extraArgs) Checks the validity of an assignment (or pseudo-assignment) from a value to a variable and emits an error message (through the compiler's messaging interface) if it is not valid.protected boolean
commonAssignmentCheck
(AnnotatedTypeMirror varType, ExpressionTree valueExpTree, @CompilerMessageKey String errorKey, Object... extraArgs) Checks the validity of an assignment (or pseudo-assignment) from a value to a variable and emits an error message (through the compiler's messaging interface) if it is not valid.protected boolean
commonAssignmentCheck
(AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, Tree valueExpTree, @CompilerMessageKey String errorKey, Object... extraArgs) Checks the validity of an assignment (or pseudo-assignment) from a value to a variable and emits an error message (through the compiler's messaging interface) if it is not valid.protected final void
commonAssignmentCheckEndDiagnostic
(boolean success, @Nullable String extraMessage, AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, Tree valueExpTree) Prints a diagnostic about exitingcommonAssignmentCheck()
, if the showchecks option was set.protected final void
commonAssignmentCheckEndDiagnostic
(String message, AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, Tree valueExpTree) Helper method for printing a diagnostic about exitingcommonAssignmentCheck()
, if the showchecks option was set.protected final void
commonAssignmentCheckStartDiagnostic
(AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, Tree valueExpTree) Prints a diagnostic about enteringcommonAssignmentCheck()
, if the showchecks option was set.protected String
contractExpressionAndType
(String expression, @Nullable AnnotationMirror qualifier) Returns a string representation of an expression and type qualifier.protected BaseTypeVisitor<Factory>.OverrideChecker
createOverrideChecker
(Tree overriderTree, AnnotatedTypeMirror.AnnotatedExecutableType overriderMethodType, AnnotatedTypeMirror overriderType, AnnotatedTypeMirror overriderReturnType, AnnotatedTypeMirror.AnnotatedExecutableType overriddenMethodType, AnnotatedTypeMirror.AnnotatedDeclaredType overriddenType, AnnotatedTypeMirror overriddenReturnType) Create an OverrideChecker.protected Factory
Constructs an instance of the appropriate type factory for the implemented type system.A public variant ofcreateTypeFactory()
.protected TypeValidator
protected @Nullable MemberSelectTree
Call this only when the current path is an identifier.enclosingStatement
(Tree tree) Returns the statement that encloses the given one.protected AnnotationMirrorSet
Returns a set of AnnotationMirrors that is a lower bound for exception parameters.protected AnnotationMirrorSet
Returns a set of AnnotationMirrors that is a upper bound for thrown exceptions.final Factory
protected boolean
isTypeCastSafe
(AnnotatedTypeMirror castType, AnnotatedTypeMirror exprType) Returns true if the cast is safe.boolean
isValidUse
(AnnotatedTypeMirror.AnnotatedArrayType type, Tree tree) Tests that the qualifiers present on the array type are valid.boolean
isValidUse
(AnnotatedTypeMirror.AnnotatedDeclaredType declarationType, AnnotatedTypeMirror.AnnotatedDeclaredType useType, Tree tree) Tests that the qualifiers present onuseType
are valid qualifiers, given the qualifiers on the declaration of the type,declarationType
.boolean
isValidUse
(AnnotatedTypeMirror.AnnotatedPrimitiveType type, Tree tree) Tests that the qualifiers present on the primitive type are valid.void
processClassTree
(ClassTree classTree) Type-check classTree.protected void
reportMethodInvocabilityError
(MethodInvocationTree tree, AnnotatedTypeMirror found, AnnotatedTypeMirror expected) Report a method invocability error.protected void
reportPurityErrors
(PurityChecker.PurityResult result, MethodTree tree, EnumSet<Pure.Kind> expectedKinds) Reports errors found during purity checking.void
setRoot
(CompilationUnitTree root) Set the CompilationUnitTree to be used during any visits.protected boolean
Should Whole Program Inference attempt to infer contract annotations? Typically, the answer is "yes" whenever WPI is enabled, but this method exists to allow subclasses to customize that behavior.protected final boolean
shouldSkipUses
(ExpressionTree exprTree) Tests whether the expression should not be checked because of the tree referring to unannotated classes, as specified in thechecker.skipUses
property.protected boolean
Returns true if the checker should issue warnings about irrelevant java types.protected boolean
skipReceiverSubtypeCheck
(MethodInvocationTree tree, AnnotatedTypeMirror methodDefinitionReceiver, AnnotatedTypeMirror methodCallReceiver) Indicates whether to skip subtype checks on the receiver when checking method invocability.protected void
TestsInsertAjavaAnnotations
if the checker has the "ajavaChecks" option.protected void
TestJointJavacJavaParserVisitor
if the checker has the "ajavaChecks" option.protected boolean
testTypevarContainment
(AnnotatedTypeMirror inner, AnnotatedTypeMirror outer) Returns true if both types are type variables and outer contains inner.protected void
typeCheckVectorCopyIntoArgument
(MethodInvocationTree tree, List<? extends AnnotatedTypeMirror> params) Type checks the method arguments ofVector.copyInto()
.protected boolean
validateType
(Tree tree, AnnotatedTypeMirror type) Tests whether the type and corresponding type tree is a valid type, and emits an error if that is not the case (e.g.boolean
validateTypeOf
(Tree tree) Tests whether the tree expressed by the passed type tree is a valid type, and emits an error if that is not the case (e.g.void
visitAnnotatedType
(@Nullable List<? extends AnnotationTree> annoTrees, Tree typeTree) Checks an annotated type.visitAnnotatedType
(AnnotatedTypeTree tree, Void p) Rather than overriding this method, clients should often overridevisitAnnotatedType(List,Tree)
.visitAnnotation
(AnnotationTree tree, Void p) Ensure that the annotation arguments comply to their declarations.visitAssignment
(AssignmentTree tree, Void p) Performs two checks: subtyping and assignability checks, usingcommonAssignmentCheck(Tree, ExpressionTree, String, Object[])
.visitCatch
(CatchTree tree, Void p) Checks the type of the exception parameter.final Void
visitClass
(ClassTree classTree, Void p) Type-check classTree and skips classes specified by the skipDef option.visitCompilationUnit
(CompilationUnitTree identifierTree, Void p) Override Compilation Unit so we won't visit package names or imports.Performs assignability check.If the computation of the type of the ConditionalExpressionTree in org.checkerframework.framework.type.TypeFromTree.TypeFromExpression.visitConditionalExpression(ConditionalExpressionTree, AnnotatedTypeFactory) is correct, the following checks are redundant.visitEnhancedForLoop
(EnhancedForLoopTree tree, Void p) Performs a subtype check, to test whether the tree expression iterable type is a subtype of the variable type in the enhanced for loop.visitIdentifier
(IdentifierTree tree, Void p) visitInstanceOf
(InstanceOfTree tree, Void p) visitMemberReference
(MemberReferenceTree tree, Void p) visitMethod
(MethodTree tree, Void p) Checks that the method or constructor obeys override and subtype rules to all overridden methods.Performs a method invocation check.visitNewArray
(NewArrayTree tree, Void p) visitNewClass
(NewClassTree tree, Void p) Performs a new class invocation check.visitReturn
(ReturnTree tree, Void p) Checks that the type of the return expression is a subtype of the enclosing method required return type.void
visitSwitchExpression17
(Tree switchExpressionTree) This method validates the type of the switch expression.visitThrow
(ThrowTree tree, Void p) Checks the type of a thrown exception.visitTypeCast
(TypeCastTree tree, Void p) visitTypeParameter
(TypeParameterTree tree, Void p) visitUnary
(UnaryTree tree, Void p) Performs assignability check.visitVariable
(VariableTree tree, Void p) void
warnAboutIrrelevantJavaTypes
(@Nullable List<? extends AnnotationTree> annoTrees, Tree typeTree) Warns if a type annotation is written on a Java type that is not listed in the @RelevantJavaTypes annotation.protected void
warnInvalidPolymorphicQualifier
(ClassTree classTree) Issues an "invalid.polymorphic.qualifier" error for all polymorphic annotations written on the class declaration.protected void
warnInvalidPolymorphicQualifier
(List<? extends TypeParameterTree> typeParameterTrees) Issues an "invalid.polymorphic.qualifier" error for all polymorphic annotations written on the type parameters declaration.protected void
warnRedundantAnnotations
(Tree tree, AnnotatedTypeMirror type) Issues a "redundant.anno" warning if the annotation written on the type is the same as the default annotation for this type and location.Methods inherited from class org.checkerframework.framework.source.SourceVisitor
visit
Methods inherited from class com.sun.source.util.TreePathScanner
getCurrentPath, scan
Methods inherited from class com.sun.source.util.TreeScanner
reduce, scan, visitArrayAccess, visitArrayType, visitAssert, visitBinary, visitBindingPattern, visitBlock, visitBreak, visitCase, visitContinue, visitDefaultCaseLabel, visitDoWhileLoop, visitEmptyStatement, visitErroneous, visitExports, visitExpressionStatement, visitForLoop, visitGuardedPattern, visitIf, visitImport, visitIntersectionType, visitLabeledStatement, visitLiteral, visitMemberSelect, visitModifiers, visitModule, visitOpens, visitOther, visitPackage, visitParameterizedType, visitParenthesized, visitParenthesizedPattern, visitPrimitiveType, visitProvides, visitRequires, visitSwitch, visitSwitchExpression, visitSynchronized, visitTry, visitUnionType, visitUses, visitWhileLoop, visitWildcard, visitYield
-
Field Details
-
checker
TheBaseTypeChecker
for error reporting. -
atypeFactory
The factory to use for obtaining "parsed" version of annotations. -
qualHierarchy
The qualifier hierarchy. -
typeHierarchy
The Annotated Type Hierarchy. -
positions
For obtaining line numbers in-Ashowchecks
debugging output. -
TARGET
The @java.lang.annotation.Target annotation. -
DETERMINISTIC
The @Deterministic
annotation. -
SIDE_EFFECT_FREE
The @SideEffectFree
annotation. -
PURE
The @Pure
annotation. -
IMPURE
The @Impure
annotation. -
targetValueElement
Thevalue
element/field of the @java.lang.annotation.Target annotation. -
unusedWhenElement
Thewhen
element/field of the @Unused annotation. -
showchecks
protected final boolean showchecksTrue if "-Ashowchecks" was passed on the command line. -
assumePureGetters
public final boolean assumePureGettersTrue if "-AassumePureGetters" was passed on the command line. -
methodTree
The tree of the enclosing method that is currently being visited, if any. -
typeValidator
-
-
Constructor Details
-
BaseTypeVisitor
- Parameters:
checker
- the type-checker associated with this visitor (for callbacks toTypeHierarchy.isSubtype(org.checkerframework.framework.type.AnnotatedTypeMirror, org.checkerframework.framework.type.AnnotatedTypeMirror)
)
-
BaseTypeVisitor
- Parameters:
checker
- the type-checker associated with this visitortypeFactory
- the type factory, or null. If null, this callscreateTypeFactory()
.
-
-
Method Details
-
createTypeFactory
Constructs an instance of the appropriate type factory for the implemented type system.The default implementation uses the checker naming convention to create the appropriate type factory. If no factory is found, it returns
BaseAnnotatedTypeFactory
. It reflectively invokes the constructor that accepts this checker and compilation unit tree (in that order) as arguments.Subclasses have to override this method to create the appropriate visitor if they do not follow the checker naming convention.
- Returns:
- the appropriate type factory
-
getTypeFactory
-
createTypeFactoryPublic
A public variant ofcreateTypeFactory()
. Only use this if you know what you are doing.- Returns:
- the appropriate type factory
-
setRoot
Description copied from class:SourceVisitor
Set the CompilationUnitTree to be used during any visits. For any later calls ofcom.sun.source.util.TreePathScanner.scan(TreePath, P)
, the CompilationUnitTree of the TreePath has to be equal toroot
.- Overrides:
setRoot
in classSourceVisitor<Void,
Void>
-
scan
- Overrides:
scan
in classSourceVisitor<Void,
Void>
-
testJointJavacJavaParserVisitor
protected void testJointJavacJavaParserVisitor()TestJointJavacJavaParserVisitor
if the checker has the "ajavaChecks" option.Parse the current source file with JavaParser and check that the AST can be matched with the Tree produced by javac. Crash if not.
Subclasses may override this method to disable the test if even the option is provided.
-
testAnnotationInsertion
protected void testAnnotationInsertion()TestsInsertAjavaAnnotations
if the checker has the "ajavaChecks" option.- Parses the current file with JavaParser.
- Removes all annotations.
- Reinserts the annotations.
- Throws an exception if the ASTs are not the same.
Subclasses may override this method to disable the test even if the option is provided.
-
visitClass
Type-check classTree and skips classes specified by the skipDef option. Subclasses should overrideprocessClassTree(ClassTree)
instead of this method.- Specified by:
visitClass
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitClass
in classSourceVisitor<Void,
Void> - Parameters:
classTree
- class to checkp
- null- Returns:
- null
-
processClassTree
Type-check classTree. Subclasses should override this method instead ofvisitClass(ClassTree, Void)
.- Parameters:
classTree
- class to check
-
warnInvalidPolymorphicQualifier
Issues an "invalid.polymorphic.qualifier" error for all polymorphic annotations written on the class declaration.- Parameters:
classTree
- the class to check
-
warnInvalidPolymorphicQualifier
protected void warnInvalidPolymorphicQualifier(List<? extends TypeParameterTree> typeParameterTrees) Issues an "invalid.polymorphic.qualifier" error for all polymorphic annotations written on the type parameters declaration.- Parameters:
typeParameterTrees
- the type parameters to check
-
checkQualifierParameter
Issues an error ifclassTree
has polymorphic fields but is not annotated with@HasQualifierParameter
. Always issue a warning if the type of a static field is annotated with a polymorphic qualifier.Issues an error if
classTree
extends or implements a class/interface that has a qualifier parameter, but this class does not.- Parameters:
classTree
- the ClassTree to check for polymorphic fields
-
checkExtendsAndImplements
In@A class X extends @B Y implements @C Z {}
, enforce that@A
must be a subtype of@B
and@C
.Also validate the types of the extends and implements clauses.
- Parameters:
classTree
- class tree to check
-
checkExtendsOrImplements
protected void checkExtendsOrImplements(Tree superClause, AnnotationMirrorSet classBounds, TypeMirror classType, boolean isExtends) Helper forcheckExtendsAndImplements(com.sun.source.tree.ClassTree)
that checks one extends or implements clause.- Parameters:
superClause
- an extends or implements clauseclassBounds
- the type declarations bounds to check for consistency withsuperClause
classType
- the type being declaredisExtends
- true for an extends clause, false for an implements clause
-
checkFieldInvariantDeclarations
Check that the field invariant declaration annotations meet the following requirements:- If the superclass of
classTree
has a field invariant, then the field invariant forclassTree
must include all the fields in the superclass invariant and those fields' annotations must be a subtype (or equal) to the annotations for those fields in the superclass. - The fields in the invariant must be a.) final and b.) declared in a superclass
of
classTree
. - The qualifier for each field must be a subtype of the annotation on the declaration of that field.
- The field invariant has an equal number of fields and qualifiers, or it has one qualifier and at least one field.
- Parameters:
classTree
- class that might have a field invariant- See the Checker Framework Manual:
- Field invariants
- If the superclass of
-
checkDefaultConstructor
Check the default constructor.- Parameters:
tree
- a class declaration
-
visitMethod
Checks that the method or constructor obeys override and subtype rules to all overridden methods. (Uses the pseudo-assignment logic to do so.)The override rule specifies that a method, m1, may override a method m2 only if:
- m1 return type is a subtype of m2
- m1 receiver type is a supertype of m2
- m1 parameters are supertypes of corresponding m2 parameters
- Specified by:
visitMethod
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitMethod
in classSourceVisitor<Void,
Void>
-
shouldPerformContractInference
protected boolean shouldPerformContractInference()Should Whole Program Inference attempt to infer contract annotations? Typically, the answer is "yes" whenever WPI is enabled, but this method exists to allow subclasses to customize that behavior.- Returns:
- true if contract inference should be performed, false if it should be disabled (even when WPI is enabled)
-
checkPurityAnnotations
Check method purity if needed. Note that overriding rules are checked as part ofcheckOverride(MethodTree, AnnotatedTypeMirror.AnnotatedExecutableType, AnnotatedTypeMirror.AnnotatedDeclaredType, AnnotatedTypeMirror.AnnotatedExecutableType, AnnotatedTypeMirror.AnnotatedDeclaredType)
.- Parameters:
tree
- the method tree to check
-
checkConstructorResult
protected void checkConstructorResult(AnnotatedTypeMirror.AnnotatedExecutableType constructorType, ExecutableElement constructorElement) Issue a warning if the result type of the constructor declaration is not top. If it is a supertype of the class, then a conflicting.annos error will also be issued byisValidUse(AnnotatedTypeMirror.AnnotatedDeclaredType,AnnotatedTypeMirror.AnnotatedDeclaredType,Tree)
.- Parameters:
constructorType
- the AnnotatedExecutableType for the constructorconstructorElement
- the element that declares the constructor
-
reportPurityErrors
protected void reportPurityErrors(PurityChecker.PurityResult result, MethodTree tree, EnumSet<Pure.Kind> expectedKinds) Reports errors found during purity checking.- Parameters:
result
- whether the method is deterministic and/or side-effect-freetree
- the methodexpectedKinds
- the expected purity for the method
-
checkPostcondition
protected void checkPostcondition(MethodTree methodTree, AnnotationMirror annotation, JavaExpression expression) Check that the expression's type is annotated withannotation
at the regular exit store.- Parameters:
methodTree
- declaration of the methodannotation
- expression's type must have this annotationexpression
- the expression that must have an annotation
-
contractExpressionAndType
Returns a string representation of an expression and type qualifier.- Parameters:
expression
- a Java expressionqualifier
- the expression's type, or null if no information is available- Returns:
- a string representation of the expression and type qualifier
-
checkConditionalPostcondition
protected void checkConditionalPostcondition(MethodTree methodTree, AnnotationMirror annotation, JavaExpression expression, boolean result) Check that the expression's type is annotated withannotation
at every regular exit that returnsresult
.- Parameters:
methodTree
- tree of method with the postconditionannotation
- expression's type must have this annotationexpression
- the expression that the postcondition concernsresult
- result for which the postcondition is valid
-
visitTypeParameter
- Specified by:
visitTypeParameter
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitTypeParameter
in classTreeScanner<Void,
Void>
-
checkExplicitAnnotationsOnIntersectionBounds
protected void checkExplicitAnnotationsOnIntersectionBounds(AnnotatedTypeMirror.AnnotatedIntersectionType intersection, List<? extends Tree> boundTrees) Issues "explicit.annotation.ignored" warning if any explicit annotation on an intersection bound is not the same as the primary annotation of the given intersection type.- Parameters:
intersection
- type to useboundTrees
- trees ofintersection
bounds
-
visitVariable
- Specified by:
visitVariable
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitVariable
in classSourceVisitor<Void,
Void>
-
warnRedundantAnnotations
Issues a "redundant.anno" warning if the annotation written on the type is the same as the default annotation for this type and location.- Parameters:
tree
- an AST nodetype
- get the explicit annotation on this type and compare it with the default one for this type and location.
-
visitAssignment
Performs two checks: subtyping and assignability checks, usingcommonAssignmentCheck(Tree, ExpressionTree, String, Object[])
.If the subtype check fails, it issues an "assignment" error.
- Specified by:
visitAssignment
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitAssignment
in classTreeScanner<Void,
Void>
-
visitEnhancedForLoop
Performs a subtype check, to test whether the tree expression iterable type is a subtype of the variable type in the enhanced for loop.If the subtype check fails, it issues a "enhancedfor" error.
- Specified by:
visitEnhancedForLoop
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitEnhancedForLoop
in classTreeScanner<Void,
Void>
-
visitMethodInvocation
Performs a method invocation check.An invocation of a method, m, on the receiver, r is valid only if:
- passed arguments are subtypes of corresponding m parameters
- r is a subtype of m receiver type
- if m is generic, passed type arguments are subtypes of m type variables
- Specified by:
visitMethodInvocation
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitMethodInvocation
in classTreeScanner<Void,
Void>
-
checkThisConstructorCall
Checks that the following rule is satisfied: The type on a constructor declaration must be a supertype of the return type of "this()" invocation within that constructor.Subclasses can override this method to change the behavior for just "this" constructor class. Or override
checkThisOrSuperConstructorCall(MethodInvocationTree, String)
to change the behavior for "this" and "super" constructor calls.- Parameters:
thisCall
- the AST node for the constructor call
-
checkSuperConstructorCall
Checks that the following rule is satisfied: The type on a constructor declaration must be a supertype of the return type of "super()" invocation within that constructor.Subclasses can override this method to change the behavior for just "super" constructor class. Or override
checkThisOrSuperConstructorCall(MethodInvocationTree, String)
to change the behavior for "this" and "super" constructor calls.- Parameters:
superCall
- the AST node for the super constructor call
-
checkThisOrSuperConstructorCall
protected void checkThisOrSuperConstructorCall(MethodInvocationTree call, @CompilerMessageKey String errorKey) Checks that the following rule is satisfied: The type on a constructor declaration must be a supertype of the return type of "this()" or "super()" invocation within that constructor.- Parameters:
call
- the AST node for the constructor callerrorKey
- the error message key to use if the check fails
-
checkVarargs
If the given invocation is a varargs invocation, check that the array type of actual varargs is a subtype of the corresponding formal parameter; issues "argument" error if not.The caller must type-check for each element in varargs before or after calling this method.
- Parameters:
invokedMethod
- the method type to be invokedtree
- method or constructor invocation tree- See Also:
-
checkPreconditions
protected void checkPreconditions(MethodInvocationTree tree, Set<Contract.Precondition> preconditions) Checks that all the givenpreconditions
hold true immediately prior to the method invocation or variable access attree
.- Parameters:
tree
- the method invocation; immediately prior to it, the preconditions must hold truepreconditions
- the preconditions to be checked
-
checkContract
protected boolean checkContract(JavaExpression expr, AnnotationMirror necessaryAnnotation, AnnotationMirror inferredAnnotation, CFAbstractStore<?, ?> store) Returns true if and only ifinferredAnnotation
is valid for a given expression to match thenecessaryAnnotation
.By default,
inferredAnnotation
must be a subtype ofnecessaryAnnotation
, but subclasses might override this behavior. -
typeCheckVectorCopyIntoArgument
protected void typeCheckVectorCopyIntoArgument(MethodInvocationTree tree, List<? extends AnnotatedTypeMirror> params) Type checks the method arguments ofVector.copyInto()
.The Checker Framework special-cases the method invocation, as its type safety cannot be expressed by Java's type system.
For a Vector
v
of typeVector<E>
, the method invocationv.copyInto(arr)
is type-safe iffarr
is an array of typeT[]
, whereT
is a subtype ofE
.In other words, this method checks that the type argument of the receiver method is a subtype of the component type of the passed array argument.
- Parameters:
tree
- a method invocation ofVector.copyInto()
params
- the types of the parameters ofVectory.copyInto()
-
visitNewClass
Performs a new class invocation check.An invocation of a constructor, c, is valid only if:
- passed arguments are subtypes of corresponding c parameters
- if c is generic, passed type arguments are subtypes of c type variables
- Specified by:
visitNewClass
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitNewClass
in classTreeScanner<Void,
Void>
-
visitLambdaExpression
- Specified by:
visitLambdaExpression
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitLambdaExpression
in classTreeScanner<Void,
Void>
-
visitMemberReference
- Specified by:
visitMemberReference
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitMemberReference
in classTreeScanner<Void,
Void>
-
visitReturn
Checks that the type of the return expression is a subtype of the enclosing method required return type. If not, it issues a "return" error.- Specified by:
visitReturn
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitReturn
in classTreeScanner<Void,
Void>
-
visitAnnotation
Ensure that the annotation arguments comply to their declarations. This needs some special casing, as annotation arguments form special trees.- Specified by:
visitAnnotation
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitAnnotation
in classTreeScanner<Void,
Void>
-
visitConditionalExpression
If the computation of the type of the ConditionalExpressionTree in org.checkerframework.framework.type.TypeFromTree.TypeFromExpression.visitConditionalExpression(ConditionalExpressionTree, AnnotatedTypeFactory) is correct, the following checks are redundant. However, let's add another failsafe guard and do the checks.- Specified by:
visitConditionalExpression
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitConditionalExpression
in classTreeScanner<Void,
Void>
-
visitSwitchExpression17
This method validates the type of the switch expression. It issues an error if the type of a value that the switch expression can result is not a subtype of the switch type.If a subclass overrides this method, it must call
super.scan(switchExpressionTree, null)
so that the blocks and statements in the cases are checked.- Parameters:
switchExpressionTree
- aSwitchExpressionTree
-
visitUnary
Performs assignability check.- Specified by:
visitUnary
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitUnary
in classTreeScanner<Void,
Void>
-
visitCompoundAssignment
Performs assignability check.- Specified by:
visitCompoundAssignment
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitCompoundAssignment
in classTreeScanner<Void,
Void>
-
visitNewArray
- Specified by:
visitNewArray
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitNewArray
in classTreeScanner<Void,
Void>
-
checkTypecastRedundancy
If the lint option "cast:redundant" is set, this method issues a warning if the cast is redundant. -
checkTypecastSafety
Issues a warning if the given explicitly-written typecast is unsafe. Does nothing if the lint option "cast:unsafe" is not set. Only primary qualifiers are checked unless the command line option "checkCastElementType" is supplied.- Parameters:
typeCastTree
- an explicitly-written typecast
-
isTypeCastSafe
Returns true if the cast is safe.Only primary qualifiers are checked unless the command line option "checkCastElementType" is supplied.
- Parameters:
castType
- annotated type of the castexprType
- annotated type of the casted expression- Returns:
- true if the type cast is safe, false otherwise
-
visitTypeCast
- Specified by:
visitTypeCast
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitTypeCast
in classTreeScanner<Void,
Void>
-
visitInstanceOf
- Specified by:
visitInstanceOf
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitInstanceOf
in classTreeScanner<Void,
Void>
-
visitCatch
Checks the type of the exception parameter. Subclasses should overridecheckExceptionParameter(com.sun.source.tree.CatchTree)
rather than this method to change the behavior of this check.- Specified by:
visitCatch
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitCatch
in classTreeScanner<Void,
Void>
-
visitThrow
Checks the type of a thrown exception. Subclasses should override checkThrownExpression(ThrowTree tree) rather than this method to change the behavior of this check.- Specified by:
visitThrow
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitThrow
in classTreeScanner<Void,
Void>
-
visitAnnotatedType
Rather than overriding this method, clients should often overridevisitAnnotatedType(List,Tree)
. That method also handles the case of annotations at the beginning of a variable or method declaration. javac parses all those annotations as being on the variable or method declaration, even though the ones that are type annotations logically belong to the variable type or method return type.- Specified by:
visitAnnotatedType
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitAnnotatedType
in classTreeScanner<Void,
Void>
-
visitAnnotatedType
Checks an annotated type. Invoked byvisitAnnotatedType(AnnotatedTypeTree, Void)
,visitVariable(com.sun.source.tree.VariableTree, java.lang.Void)
, andvisitMethod(com.sun.source.tree.MethodTree, java.lang.Void)
. Exists to prevent code duplication among the three. Checking invisitVariable
andvisitMethod
is needed because there isn't an AnnotatedTypeTree within a variable declaration or for a method return type -- all the annotations are attached to the VariableTree or MethodTree, respectively.- Parameters:
annoTrees
- annotations written before a variable/method declaration, if this type is from one; null otherwise. This might contain type annotations that the Java parser attached to the declaration rather than to the type.typeTree
- the type that any type annotations in annoTrees apply to
-
warnAboutIrrelevantJavaTypes
public void warnAboutIrrelevantJavaTypes(@Nullable List<? extends AnnotationTree> annoTrees, Tree typeTree) Warns if a type annotation is written on a Java type that is not listed in the @RelevantJavaTypes annotation.- Parameters:
annoTrees
- annotations written before a variable/method declaration, if this type is from one; null otherwise. This might contain type annotations that the Java parser attached to the declaration rather than to the type.typeTree
- the type that any type annotations in annoTrees apply to
-
shouldWarnAboutIrrelevantJavaTypes
protected boolean shouldWarnAboutIrrelevantJavaTypes()Returns true if the checker should issue warnings about irrelevant java types.- Returns:
- true if the checker should issue warnings about irrelevant java types
-
checkExceptionParameter
Issue error if the exception parameter is not a supertype of the annotation specified bygetExceptionParameterLowerBoundAnnotations()
, which is top by default.Subclasses may override this method to change the behavior of this check. Subclasses wishing to enforce that exception parameter be annotated with other annotations can just override
getExceptionParameterLowerBoundAnnotations()
.- Parameters:
tree
- a CatchTree to check
-
getExceptionParameterLowerBoundAnnotations
Returns a set of AnnotationMirrors that is a lower bound for exception parameters.This implementation returns top; subclasses can change this behavior.
Note: by default this method is called by
getThrowUpperBoundAnnotations()
, so that this annotation is enforced.- Returns:
- set of annotation mirrors, one per hierarchy, that form a lower bound of annotations that can be written on an exception parameter
-
checkThrownExpression
Checks the type of the thrown expression.By default, this method checks that the thrown expression is a subtype of top.
Issue error if the thrown expression is not a sub type of the annotation given by
getThrowUpperBoundAnnotations()
, the same asgetExceptionParameterLowerBoundAnnotations()
by default.Subclasses may override this method to change the behavior of this check. Subclasses wishing to enforce that the thrown expression be a subtype of a type besides
getExceptionParameterLowerBoundAnnotations()
, should overridegetThrowUpperBoundAnnotations()
.- Parameters:
tree
- a ThrowTree to check
-
getThrowUpperBoundAnnotations
Returns a set of AnnotationMirrors that is a upper bound for thrown exceptions.Note: by default this method is returns by getExceptionParameterLowerBoundAnnotations(), so that this annotation is enforced.
(Default is top)
- Returns:
- set of annotation mirrors, one per hierarchy, that form an upper bound of thrown expressions
-
commonAssignmentCheck
protected boolean commonAssignmentCheck(Tree varTree, ExpressionTree valueExpTree, @CompilerMessageKey String errorKey, Object... extraArgs) Checks the validity of an assignment (or pseudo-assignment) from a value to a variable and emits an error message (through the compiler's messaging interface) if it is not valid.- Parameters:
varTree
- the AST node for the lvalue (usually a variable)valueExpTree
- the AST node for the rvalue (the new value)errorKey
- the error message key to use if the check failsextraArgs
- arguments to the error message key, before "found" and "expected" types- Returns:
- true if the check succeeds, false if an error message was issued
-
commonAssignmentCheck
protected boolean commonAssignmentCheck(AnnotatedTypeMirror varType, ExpressionTree valueExpTree, @CompilerMessageKey String errorKey, Object... extraArgs) Checks the validity of an assignment (or pseudo-assignment) from a value to a variable and emits an error message (through the compiler's messaging interface) if it is not valid.- Parameters:
varType
- the annotated type for the lvalue (usually a variable)valueExpTree
- the AST node for the rvalue (the new value)errorKey
- the error message key to use if the check failsextraArgs
- arguments to the error message key, before "found" and "expected" types- Returns:
- true if the check succeeds, false if an error message was issued
-
commonAssignmentCheck
protected boolean commonAssignmentCheck(AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, Tree valueExpTree, @CompilerMessageKey String errorKey, Object... extraArgs) Checks the validity of an assignment (or pseudo-assignment) from a value to a variable and emits an error message (through the compiler's messaging interface) if it is not valid.- Parameters:
varType
- the annotated type of the variablevalueType
- the annotated type of the valuevalueExpTree
- the location to use when reporting the error messageerrorKey
- the error message key to use if the check failsextraArgs
- arguments to the error message key, before "found" and "expected" types- Returns:
- true if the check succeeds, false if an error message was issued
-
commonAssignmentCheckStartDiagnostic
protected final void commonAssignmentCheckStartDiagnostic(AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, Tree valueExpTree) Prints a diagnostic about enteringcommonAssignmentCheck()
, if the showchecks option was set.- Parameters:
varType
- the annotated type of the variablevalueType
- the annotated type of the valuevalueExpTree
- the location to use when reporting the error message
-
commonAssignmentCheckEndDiagnostic
protected final void commonAssignmentCheckEndDiagnostic(boolean success, @Nullable String extraMessage, AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, Tree valueExpTree) Prints a diagnostic about exitingcommonAssignmentCheck()
, if the showchecks option was set.- Parameters:
success
- whether the check succeeded or failedextraMessage
- information about why the result is what it is; may be nullvarType
- the annotated type of the variablevalueType
- the annotated type of the valuevalueExpTree
- the location to use when reporting the error message
-
commonAssignmentCheckEndDiagnostic
protected final void commonAssignmentCheckEndDiagnostic(String message, AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, Tree valueExpTree) Helper method for printing a diagnostic about exitingcommonAssignmentCheck()
, if the showchecks option was set.Most clients should call
commonAssignmentCheckEndDiagnostic(boolean, String, AnnotatedTypeMirror, AnnotatedTypeMirror, Tree)
. The purpose of this method is to permit customizing the message that is printed.- Parameters:
message
- the result, plus information about why the result is what it isvarType
- the annotated type of the variablevalueType
- the annotated type of the valuevalueExpTree
- the location to use when reporting the error message
-
checkArrayInitialization
protected boolean checkArrayInitialization(AnnotatedTypeMirror type, List<? extends ExpressionTree> initializers) Checks that the array initializers are consistent with the array type.- Parameters:
type
- the array elemen typeinitializers
- the initializers- Returns:
- true if the check succeeds, false if an error message was issued
-
checkTypeArguments
protected void checkTypeArguments(Tree toptree, List<? extends AnnotatedTypeParameterBounds> paramBounds, List<? extends AnnotatedTypeMirror> typeargs, List<? extends Tree> typeargTrees, CharSequence typeOrMethodName, List<?> paramNames) Checks that the annotations on the type arguments supplied to a type or a method invocation are within the bounds of the type variables as declared, and issues the "type.argument" error if they are not.- Parameters:
toptree
- the tree for error reporting, only used for inferred type argumentsparamBounds
- the bounds of the type parameters from a class or method declarationtypeargs
- the type arguments from the type or method invocationtypeargTrees
- the type arguments as trees, used for error reporting
-
skipReceiverSubtypeCheck
protected boolean skipReceiverSubtypeCheck(MethodInvocationTree tree, AnnotatedTypeMirror methodDefinitionReceiver, AnnotatedTypeMirror methodCallReceiver) Indicates whether to skip subtype checks on the receiver when checking method invocability. A visitor may, for example, allow a method to be invoked even if the receivers are siblings in a hierarchy, provided that some other condition (implemented by the visitor) is satisfied.- Parameters:
tree
- the method invocation treemethodDefinitionReceiver
- the ATM of the receiver of the method definitionmethodCallReceiver
- the ATM of the receiver of the method call- Returns:
- whether to skip subtype checks on the receiver
-
checkMethodInvocability
protected void checkMethodInvocability(AnnotatedTypeMirror.AnnotatedExecutableType method, MethodInvocationTree tree) Tests whether the method can be invoked using the receiver of the 'tree' method invocation, and issues a "method.invocation" if the invocation is invalid.This implementation tests whether the receiver in the method invocation is a subtype of the method receiver type. This behavior can be specialized by overriding skipReceiverSubtypeCheck.
- Parameters:
method
- the type of the invoked methodtree
- the method invocation tree
-
reportMethodInvocabilityError
protected void reportMethodInvocabilityError(MethodInvocationTree tree, AnnotatedTypeMirror found, AnnotatedTypeMirror expected) Report a method invocability error. Allows checkers to change how the message is output.- Parameters:
tree
- the AST node at which to report the errorfound
- the actual type of the receiverexpected
- the expected type of the receiver
-
checkConstructorInvocation
protected void checkConstructorInvocation(AnnotatedTypeMirror.AnnotatedDeclaredType invocation, AnnotatedTypeMirror.AnnotatedExecutableType constructor, NewClassTree newClassTree) Check that the (explicit) annotations on a new class tree are comparable to the result type of the constructor. Issue an error if not.Issue a warning if the annotations on the constructor invocation is a subtype of the constructor result type. This is equivalent to down-casting.
-
checkArguments
protected void checkArguments(List<? extends AnnotatedTypeMirror> requiredTypes, List<? extends ExpressionTree> passedArgs, CharSequence executableName, List<?> paramNames) A helper method to check that each passed argument is a subtype of the corresponding required argument. Issues an "argument" error for each passed argument that not a subtype of the required one.Note this method requires the lists to have the same length, as it does not handle cases like var args.
- Parameters:
requiredTypes
- the required types. This may differ from the formal parameter types, because it replaces a varargs parameter by multiple parameters with the vararg's element type.passedArgs
- the expressions passed to the corresponding typesexecutableName
- the name of the method or constructor being calledparamNames
- the names of the callee's formal parameters- See Also:
-
testTypevarContainment
Returns true if both types are type variables and outer contains inner. Outer contains inner implies: inner.upperBound <: outer.upperBound outer.lowerBound <: inner.lowerBound.- Returns:
- true if both types are type variables and outer contains inner
-
createOverrideChecker
protected BaseTypeVisitor<Factory>.OverrideChecker createOverrideChecker(Tree overriderTree, AnnotatedTypeMirror.AnnotatedExecutableType overriderMethodType, AnnotatedTypeMirror overriderType, AnnotatedTypeMirror overriderReturnType, AnnotatedTypeMirror.AnnotatedExecutableType overriddenMethodType, AnnotatedTypeMirror.AnnotatedDeclaredType overriddenType, AnnotatedTypeMirror overriddenReturnType) Create an OverrideChecker.This exists so that subclasses can subclass OverrideChecker and use their subclass instead of using OverrideChecker itself.
- Parameters:
overriderTree
- the AST node of the overriding method or method referenceoverriderMethodType
- the type of the overriding methodoverriderType
- the type enclosing the overrider method, usually an AnnotatedDeclaredType; for Method References may be something elseoverriderReturnType
- the return type of the overriding methodoverriddenMethodType
- the type of the overridden methodoverriddenType
- the declared type enclosing the overridden methodoverriddenReturnType
- the return type of the overridden method- Returns:
- an OverrideChecker
-
checkOverride
protected boolean checkOverride(MethodTree overriderTree, AnnotatedTypeMirror.AnnotatedDeclaredType overriderType, AnnotatedTypeMirror.AnnotatedExecutableType overriddenMethodType, AnnotatedTypeMirror.AnnotatedDeclaredType overriddenType) Type checks that a method may override another method. Uses an OverrideChecker subclass as created bycreateOverrideChecker(com.sun.source.tree.Tree, org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType, org.checkerframework.framework.type.AnnotatedTypeMirror, org.checkerframework.framework.type.AnnotatedTypeMirror, org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType, org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType, org.checkerframework.framework.type.AnnotatedTypeMirror)
. This version of the method uses the annotated type factory to get the annotated type of the overriding method, and does NOT expose that type.- Parameters:
overriderTree
- declaration tree of overriding methodoverriderType
- type of overriding classoverriddenMethodType
- type of overridden methodoverriddenType
- type of overridden class- Returns:
- true if the override is allowed
- See Also:
-
checkOverride
protected boolean checkOverride(MethodTree overriderTree, AnnotatedTypeMirror.AnnotatedExecutableType overriderMethodType, AnnotatedTypeMirror.AnnotatedDeclaredType overriderType, AnnotatedTypeMirror.AnnotatedExecutableType overriddenMethodType, AnnotatedTypeMirror.AnnotatedDeclaredType overriddenType) Type checks that a method may override another method. Uses an OverrideChecker subclass as created bycreateOverrideChecker(com.sun.source.tree.Tree, org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType, org.checkerframework.framework.type.AnnotatedTypeMirror, org.checkerframework.framework.type.AnnotatedTypeMirror, org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType, org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType, org.checkerframework.framework.type.AnnotatedTypeMirror)
. This version of the method exposes the AnnotatedExecutableType of the overriding method. Override this version of the method if you need to access that type.- Parameters:
overriderTree
- declaration tree of overriding methodoverriderMethodType
- type of the overriding methodoverriderType
- type of overriding classoverriddenMethodType
- type of overridden methodoverriddenType
- type of overridden class- Returns:
- true if the override is allowed
- See Also:
-
checkMethodReferenceAsOverride
Check that a method reference is allowed. Uses the OverrideChecker class.- Parameters:
memberReferenceTree
- the tree for the method reference- Returns:
- true if the method reference is allowed
-
enclosingMemberSelect
Call this only when the current path is an identifier.- Returns:
- the enclosing member select, or null if the identifier is not the field in a member selection
-
enclosingStatement
Returns the statement that encloses the given one.- Parameters:
tree
- an AST node that is on the current path- Returns:
- the statement that encloses the given one
-
visitIdentifier
- Specified by:
visitIdentifier
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitIdentifier
in classTreeScanner<Void,
Void>
-
checkAccess
Issues an error if access not allowed, based on an @Unused annotation.- Parameters:
identifierTree
- the identifier being accessed; the method does nothing if it is not a fieldp
- ignored
-
checkAccessAllowed
protected void checkAccessAllowed(Element field, @Nullable AnnotatedTypeMirror receiverType, @FindDistinct ExpressionTree accessTree) Issues an error if access not allowed, based on an @Unused annotation.- Parameters:
field
- the field to be accessed, whose declaration might be annotated by @Unused. It can also be (for example)this
, in which casereceiverType
is null.receiverType
- the type of the expression whose field is accessed; null if the field is staticaccessTree
- the access expression
-
isValidUse
public boolean isValidUse(AnnotatedTypeMirror.AnnotatedDeclaredType declarationType, AnnotatedTypeMirror.AnnotatedDeclaredType useType, Tree tree) Tests that the qualifiers present onuseType
are valid qualifiers, given the qualifiers on the declaration of the type,declarationType
.The check is shallow, as it does not descend into generic or array types (i.e. only performing the validity check on the raw type or outermost array dimension).
validateTypeOf(Tree)
would call this for each type argument or array dimension separately.In most cases,
useType
simply needs to be a subtype ofdeclarationType
. If a type system makes exceptions to this rule, its implementation should override this method.This method is not called if
BaseTypeValidator.shouldCheckTopLevelDeclaredOrPrimitiveType(AnnotatedTypeMirror, Tree)
returns false -- by default, it is not called on the top level for locals and expressions. To enforce a type validity property everywhere, override methods such asBaseTypeValidator.visitDeclared(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType, com.sun.source.tree.Tree)
rather than this method.- Parameters:
declarationType
- the type of the class (TypeElement)useType
- the use of the class (instance type)tree
- the tree where the type is used- Returns:
- true if the useType is a valid use of elemType
-
isValidUse
Tests that the qualifiers present on the primitive type are valid.- Parameters:
type
- the use of the primitive typetree
- the tree where the type is used- Returns:
- true if the type is a valid use of the primitive type
-
isValidUse
Tests that the qualifiers present on the array type are valid. This method will be invoked for each array level independently, i.e. this method only needs to check the top-level qualifiers of an array.- Parameters:
type
- the array type usetree
- the tree where the type is used- Returns:
- true if the type is a valid array type
-
validateTypeOf
Tests whether the tree expressed by the passed type tree is a valid type, and emits an error if that is not the case (e.g. '@Mutable String'). If the tree is a method or constructor, check the return type.- Parameters:
tree
- the AST type supplied by the user- Returns:
- true if the tree is a valid type
-
validateType
Tests whether the type and corresponding type tree is a valid type, and emits an error if that is not the case (e.g. '@Mutable String'). If the tree is a method or constructor, tests the return type.- Parameters:
tree
- the type tree supplied by the usertype
- the type corresponding to tree- Returns:
- true if the type is valid
-
createTypeValidator
-
shouldSkipUses
Tests whether the expression should not be checked because of the tree referring to unannotated classes, as specified in thechecker.skipUses
property.It returns true if exprTree is a method invocation or a field access to a class whose qualified name matches the
checker.skipUses
property.- Parameters:
exprTree
- any expression tree- Returns:
- true if checker should not test exprTree
-
visitCompilationUnit
Override Compilation Unit so we won't visit package names or imports.- Specified by:
visitCompilationUnit
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitCompilationUnit
in classTreeScanner<Void,
Void>
-