public class BaseTypeVisitor<Factory extends GenericAnnotatedTypeFactory<?,?,?,?>> extends SourceVisitor<Void,Void>
SourceVisitor
that performs assignment and pseudo-assignment checking, method
invocation checking, and assignability checking.
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:
TypeHierarchy.isSubtype
method. This includes method invocation and method
overriding checks.
isValidUse
method.
Checker.isAssignable
method.
TypeHierarchy.isSubtype(AnnotatedTypeMirror, AnnotatedTypeMirror)
,
AnnotatedTypeFactory
Modifier and Type | Class and Description |
---|---|
class |
BaseTypeVisitor.OverrideChecker
Class to perform method override and method reference checks.
|
Modifier and Type | Field and Description |
---|---|
protected Factory |
atypeFactory
The factory to use for obtaining "parsed" version of annotations.
|
protected BaseTypeChecker |
checker
The
BaseTypeChecker for error reporting. |
protected ContractsUtils |
contractsUtils
An instance of the
ContractsUtils helper class. |
protected SourcePositions |
positions
For obtaining line numbers in -Ashowchecks debugging output.
|
protected AnnotationMirror |
TARGET
The @java.lang.annotation.Target annotation.
|
protected ExecutableElement |
targetValueElement
The
value element/field of the @java.lang.annotation.Target annotation. |
protected TypeValidator |
typeValidator |
protected VisitorState |
visitorState
For storing visitor state.
|
elements, root, trees, treesWithSuppressWarnings, types
Modifier | Constructor and Description |
---|---|
|
BaseTypeVisitor(BaseTypeChecker checker) |
protected |
BaseTypeVisitor(BaseTypeChecker checker,
Factory typeFactory) |
Modifier and Type | Method and Description |
---|---|
protected void |
checkAccess(IdentifierTree node,
Void p) |
protected void |
checkArguments(List<? extends AnnotatedTypeMirror> requiredArgs,
List<? extends ExpressionTree> passedArgs,
Name executableName,
List<?> paramNames)
A helper method to check that each passed argument is a subtype of the corresponding required
argument, and issues "argument.invalid" error for each passed argument that not a subtype of
the required one.
|
protected void |
checkArrayInitialization(AnnotatedTypeMirror type,
List<? extends ExpressionTree> initializers) |
protected void |
checkConditionalPostcondition(MethodTree method,
AnnotationMirror annotation,
AnnotationMirror contractAnnotation,
FlowExpressions.Receiver expression,
boolean result)
Check that the expression's type is annotated with
annotation at every regular exit
that returns result . |
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 is not top.
|
protected boolean |
checkContract(FlowExpressions.Receiver expr,
AnnotationMirror necessaryAnnotation,
AnnotationMirror inferredAnnotation,
CFAbstractStore<?,?> store)
Returns true if and only if
inferredAnnotation is valid for a given expression to
match the necessaryAnnotation . |
protected void |
checkDefaultConstructor(ClassTree node) |
protected void |
checkExceptionParameter(CatchTree node)
Issue error if the exception parameter is not a supertype of the annotation specified by
getExceptionParameterLowerBoundAnnotations() , which is top by default. |
protected void |
checkExtendsImplements(ClassTree classTree)
If "@B class Y extends @A X {}", then enforce that @B must be a subtype of @A.
|
protected void |
checkFieldInvariantDeclarations(ClassTree classTree)
Check that the field invariant declaration annotations meet the following requirements:
classTree has a field invariant, then the field
invariant for classTree 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 node)
Tests whether the method can be invoked using the receiver of the 'node' method invocation,
and issues a "method.invocation.invalid" 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 overridingType,
AnnotatedTypeMirror.AnnotatedExecutableType overridden,
AnnotatedTypeMirror.AnnotatedDeclaredType overriddenType)
Type checks that a method may override another method.
|
protected boolean |
checkOverride(MethodTree overriderTree,
AnnotatedTypeMirror.AnnotatedExecutableType overrider,
AnnotatedTypeMirror.AnnotatedDeclaredType overridingType,
AnnotatedTypeMirror.AnnotatedExecutableType overridden,
AnnotatedTypeMirror.AnnotatedDeclaredType overriddenType)
Type checks that a method may override another method.
|
protected void |
checkPostcondition(MethodTree methodTree,
AnnotationMirror annotation,
AnnotationMirror contractAnnotation,
FlowExpressions.Receiver expression)
Check that the expression's type is annotated with
annotation at the regular exit
store. |
protected void |
checkPreconditions(MethodInvocationTree tree,
Set<Contract.Precondition> preconditions)
Checks that all the given
preconditions hold true immediately prior to the method
invocation or variable access at tree . |
protected void |
checkPurity(MethodTree node)
Check method purity if needed.
|
protected void |
checkQualifierParameter(ClassTree classTree)
Issues an error if
classTree 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 |
checkThrownExpression(ThrowTree node)
Checks the type of the thrown expression.
|
protected void |
checkTypeArguments(Tree toptree,
List<? extends AnnotatedTypeParameterBounds> paramBounds,
List<? extends AnnotatedTypeMirror> typeargs,
List<? extends Tree> typeargTrees,
Name 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.type.incompatible" error if they are not.
|
protected void |
checkTypecastRedundancy(TypeCastTree typeCastTree)
If the lint option "cast:redundant" is set, this methods 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)
A helper method to check that the array type of actual varargs is a subtype of the
corresponding required varargs, and issues "argument.invalid" error if it's not a subtype of
the required one.
|
protected void |
commonAssignmentCheck(AnnotatedTypeMirror varType,
AnnotatedTypeMirror valueType,
Tree valueTree,
@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 void |
commonAssignmentCheck(AnnotatedTypeMirror varType,
ExpressionTree valueExp,
@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 void |
commonAssignmentCheck(Tree varTree,
ExpressionTree valueExp,
@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 void |
commonAssignmentCheckEndDiagnostic(boolean success,
String extraMessage,
AnnotatedTypeMirror varType,
AnnotatedTypeMirror valueType,
Tree valueTree)
Prints a diagnostic about exiting commonAssignmentCheck, if the showchecks option was set.
|
protected void |
commonAssignmentCheckEndDiagnostic(String message,
AnnotatedTypeMirror varType,
AnnotatedTypeMirror valueType,
Tree valueTree)
Prints a diagnostic about exiting commonAssignmentCheck, if the showchecks option was set.
|
protected void |
commonAssignmentCheckStartDiagnostic(AnnotatedTypeMirror varType,
AnnotatedTypeMirror valueType,
Tree valueTree)
Prints a diagnostic about entering commonAssignmentCheck, if the showchecks option was set.
|
protected BaseTypeVisitor.OverrideChecker |
createOverrideChecker(Tree overriderTree,
AnnotatedTypeMirror.AnnotatedExecutableType overrider,
AnnotatedTypeMirror overridingType,
AnnotatedTypeMirror overridingReturnType,
AnnotatedTypeMirror.AnnotatedExecutableType overridden,
AnnotatedTypeMirror.AnnotatedDeclaredType overriddenType,
AnnotatedTypeMirror overriddenReturnType)
Create an OverrideChecker.
|
protected Factory |
createTypeFactory()
Constructs an instance of the appropriate type factory for the implemented type system.
|
protected TypeValidator |
createTypeValidator() |
protected MemberSelectTree |
enclosingMemberSelect()
Call this only when the current path is an identifier.
|
protected Tree |
enclosingStatement(Tree tree)
Returns the statement that encloses the given one.
|
protected Set<? extends AnnotationMirror> |
getExceptionParameterLowerBoundAnnotations()
Returns a set of AnnotationMirrors that is a lower bound for exception parameters.
|
protected Set<? extends AnnotationMirror> |
getThrowUpperBoundAnnotations()
Returns a set of AnnotationMirrors that is a upper bound for thrown exceptions.
|
Factory |
getTypeFactory() |
protected boolean |
isAccessAllowed(Element field,
AnnotatedTypeMirror receiver,
ExpressionTree accessTree)
Returns true if access is allowed, based on an @Unused annotation
|
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 on the useType 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 node,
AnnotatedTypeMirror found,
AnnotatedTypeMirror expected)
Report a method invocability error.
|
protected void |
reportPurityErrors(PurityChecker.PurityResult result,
MethodTree node,
EnumSet<Pure.Kind> expectedKinds)
Reports errors found during purity checking.
|
Void |
scan(@Nullable Tree tree,
Void p) |
void |
setRoot(CompilationUnitTree root)
Set the CompilationUnitTree to be used during any visits.
|
protected boolean |
shouldSkipUses(ExpressionTree exprTree)
Tests whether the expression should not be checked because of the tree referring to
unannotated classes, as specified in the
checker.skipUses property. |
protected boolean |
skipReceiverSubtypeCheck(MethodInvocationTree node,
AnnotatedTypeMirror methodDefinitionReceiver,
AnnotatedTypeMirror methodCallReceiver)
Indicates whether to skip subtype checks on the receiver when checking method invocability.
|
protected boolean |
testTypevarContainment(AnnotatedTypeMirror inner,
AnnotatedTypeMirror outer)
Returns true if both types are type variables and outer contains inner.
|
protected void |
typeCheckVectorCopyIntoArgument(MethodInvocationTree node,
List<? extends AnnotatedTypeMirror> params)
Type checks the method arguments of
Vector.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 |
visitAnnotation(AnnotationTree node,
Void p)
Ensure that the annotation arguments comply to their declarations.
|
Void |
visitArrayAccess(ArrayAccessTree node,
Void p) |
Void |
visitAssignment(AssignmentTree node,
Void p)
Performs two checks: subtyping and assignability checks, using
commonAssignmentCheck(Tree, ExpressionTree, String, Object[]) . |
Void |
visitCatch(CatchTree node,
Void p)
Checks the type of the exception parameter Subclasses should override
checkExceptionParameter(CatchTree node) rather than this method to change the behavior of
this check.
|
Void |
visitClass(ClassTree classTree,
Void p)
Type-check classTree and skips classes specified by the skipDef option.
|
Void |
visitCompilationUnit(CompilationUnitTree node,
Void p)
Override Compilation Unit so we won't visit package names or imports.
|
Void |
visitCompoundAssignment(CompoundAssignmentTree node,
Void p)
Performs assignability check.
|
Void |
visitConditionalExpression(ConditionalExpressionTree node,
Void p)
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.
|
Void |
visitEnhancedForLoop(EnhancedForLoopTree node,
Void p)
Performs a subtype check, to test whether the node expression iterable type is a subtype of
the variable type in the enhanced for loop.
|
Void |
visitIdentifier(IdentifierTree node,
Void p) |
Void |
visitInstanceOf(InstanceOfTree node,
Void p) |
Void |
visitLambdaExpression(LambdaExpressionTree node,
Void p) |
Void |
visitMemberReference(MemberReferenceTree node,
Void p) |
Void |
visitMethod(MethodTree node,
Void p)
Performs pseudo-assignment check: checks that the method obeys override and subtype rules to
all overridden methods.
|
Void |
visitMethodInvocation(MethodInvocationTree node,
Void p)
Performs a method invocation check.
|
Void |
visitNewArray(NewArrayTree node,
Void p) |
Void |
visitNewClass(NewClassTree node,
Void p)
Performs a new class invocation check.
|
Void |
visitReturn(ReturnTree node,
Void p)
Checks that the type of the return expression is a subtype of the enclosing method required
return type.
|
Void |
visitThrow(ThrowTree node,
Void p)
Checks the type of a thrown exception.
|
Void |
visitTypeCast(TypeCastTree node,
Void p) |
Void |
visitTypeParameter(TypeParameterTree node,
Void p) |
Void |
visitUnary(UnaryTree node,
Void p)
Performs assignability check.
|
Void |
visitVariable(VariableTree node,
Void p) |
visit
getCurrentPath, scan
reduce, scan, visitAnnotatedType, visitArrayType, visitAssert, visitBinary, visitBlock, visitBreak, visitCase, visitContinue, visitDoWhileLoop, visitEmptyStatement, visitErroneous, visitExports, visitExpressionStatement, visitForLoop, visitIf, visitImport, visitIntersectionType, visitLabeledStatement, visitLiteral, visitMemberSelect, visitModifiers, visitModule, visitOpens, visitOther, visitPackage, visitParameterizedType, visitParenthesized, visitPrimitiveType, visitProvides, visitRequires, visitSwitch, visitSynchronized, visitTry, visitUnionType, visitUses, visitWhileLoop, visitWildcard
protected final BaseTypeChecker checker
BaseTypeChecker
for error reporting.protected final Factory extends GenericAnnotatedTypeFactory<?,?,?,?> atypeFactory
protected final SourcePositions positions
protected final VisitorState visitorState
protected final ContractsUtils contractsUtils
ContractsUtils
helper class.protected final AnnotationMirror TARGET
protected final ExecutableElement targetValueElement
value
element/field of the @java.lang.annotation.Target annotation.protected final TypeValidator typeValidator
public BaseTypeVisitor(BaseTypeChecker checker)
checker
- the type-checker associated with this visitor (for callbacks to TypeHierarchy.isSubtype(org.checkerframework.framework.type.AnnotatedTypeMirror, org.checkerframework.framework.type.AnnotatedTypeMirror)
)protected BaseTypeVisitor(BaseTypeChecker checker, Factory typeFactory)
checker
- the type-checker associated with this visitortypeFactory
- the type factory, or null. If null, this calls createTypeFactory()
.protected Factory createTypeFactory()
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.
public final Factory getTypeFactory()
public void setRoot(CompilationUnitTree root)
SourceVisitor
com.sun.source.util.TreePathScanner.scan(TreePath, P)
, the CompilationUnitTree of the
TreePath has to be equal to root
.setRoot
in class SourceVisitor<Void,Void>
public Void scan(@Nullable Tree tree, Void p)
scan
in class SourceVisitor<Void,Void>
public final Void visitClass(ClassTree classTree, Void p)
processClassTree(ClassTree)
instead of this method.visitClass
in interface TreeVisitor<Void,Void>
visitClass
in class SourceVisitor<Void,Void>
classTree
- class to checkp
- nullpublic void processClassTree(ClassTree classTree)
visitClass(ClassTree, Void)
.classTree
- class to checkprotected void checkQualifierParameter(ClassTree classTree)
classTree
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.
classTree
- the ClassTree to check for polymorphic fieldsprotected void checkExtendsImplements(ClassTree classTree)
Also validate the types of the extends and implements clauses.
classTree
- class tree to checkprotected void checkFieldInvariantDeclarations(ClassTree classTree)
classTree
has a field invariant, then the field
invariant for classTree
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.
classTree
.
classTree
- class that might have a field invariantprotected void checkDefaultConstructor(ClassTree node)
public Void visitMethod(MethodTree node, Void p)
The override rule specifies that a method, m1, may override a method m2 only if:
visitMethod
in interface TreeVisitor<Void,Void>
visitMethod
in class SourceVisitor<Void,Void>
protected void checkPurity(MethodTree node)
checkOverride(MethodTree, AnnotatedTypeMirror.AnnotatedExecutableType,
AnnotatedTypeMirror.AnnotatedDeclaredType, AnnotatedTypeMirror.AnnotatedExecutableType,
AnnotatedTypeMirror.AnnotatedDeclaredType)
.node
- the method tree to checkprotected void checkConstructorResult(AnnotatedTypeMirror.AnnotatedExecutableType constructorType, ExecutableElement constructorElement)
isValidUse(AnnotatedTypeMirror.AnnotatedDeclaredType,AnnotatedTypeMirror.AnnotatedDeclaredType,Tree)
.constructorType
- AnnotatedExecutableType for the constructorconstructorElement
- element that declares the constructorprotected void reportPurityErrors(PurityChecker.PurityResult result, MethodTree node, EnumSet<Pure.Kind> expectedKinds)
result
- whether the method is deterministic and/or side-effect-freenode
- the methodexpectedKinds
- the expected purity for the methodprotected void checkPostcondition(MethodTree methodTree, AnnotationMirror annotation, AnnotationMirror contractAnnotation, FlowExpressions.Receiver expression)
annotation
at the regular exit
store.methodTree
- declaration of the methodannotation
- expression's type must have this annotationcontractAnnotation
- the user-written postcondition annotation, which mentions expression
. Used only for diagnostic messages.expression
- the expression that the postcondition contractAnnotation
concernsprotected void checkConditionalPostcondition(MethodTree method, AnnotationMirror annotation, AnnotationMirror contractAnnotation, FlowExpressions.Receiver expression, boolean result)
annotation
at every regular exit
that returns result
.method
- tree of method with the postconditionannotation
- expression's type must have this annotationcontractAnnotation
- the user-written postcondition annotation, which mentions expression
. Used only for diagnostic messages.expression
- the expression that the postcondition concernsresult
- result for which the postcondition is validpublic Void visitTypeParameter(TypeParameterTree node, Void p)
visitTypeParameter
in interface TreeVisitor<Void,Void>
visitTypeParameter
in class TreeScanner<Void,Void>
public Void visitVariable(VariableTree node, Void p)
visitVariable
in interface TreeVisitor<Void,Void>
visitVariable
in class SourceVisitor<Void,Void>
public Void visitAssignment(AssignmentTree node, Void p)
commonAssignmentCheck(Tree, ExpressionTree, String, Object[])
.
If the subtype check fails, it issues a "assignment.type.incompatible" error.
visitAssignment
in interface TreeVisitor<Void,Void>
visitAssignment
in class TreeScanner<Void,Void>
public Void visitEnhancedForLoop(EnhancedForLoopTree node, Void p)
If the subtype check fails, it issues a "enhancedfor.type.incompatible" error.
visitEnhancedForLoop
in interface TreeVisitor<Void,Void>
visitEnhancedForLoop
in class TreeScanner<Void,Void>
public Void visitMethodInvocation(MethodInvocationTree node, Void p)
An invocation of a method, m, on the receiver, r is valid only if:
visitMethodInvocation
in interface TreeVisitor<Void,Void>
visitMethodInvocation
in class TreeScanner<Void,Void>
protected void checkThisConstructorCall(MethodInvocationTree thisCall)
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.
thisCall
- the AST node for the constructor callprotected void checkSuperConstructorCall(MethodInvocationTree superCall)
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.
superCall
- the AST node for the super constructor callprotected void checkThisOrSuperConstructorCall(MethodInvocationTree call, @CompilerMessageKey String errorKey)
call
- the AST node for the constructor callerrorKey
- the error message key to use if the check failsprotected void checkVarargs(AnnotatedTypeMirror.AnnotatedExecutableType invokedMethod, Tree tree)
Note it's required that type checking for each element in varargs is executed by the caller before or after calling this method.
invokedMethod
- the method type to be invokedtree
- method or constructor invocation treecheckArguments(java.util.List<? extends org.checkerframework.framework.type.AnnotatedTypeMirror>, java.util.List<? extends com.sun.source.tree.ExpressionTree>, javax.lang.model.element.Name, java.util.List<?>)
protected void checkPreconditions(MethodInvocationTree tree, Set<Contract.Precondition> preconditions)
preconditions
hold true immediately prior to the method
invocation or variable access at tree
.tree
- the Tree immediately prior to which the preconditions must hold truepreconditions
- the preconditions to be checkedprotected boolean checkContract(FlowExpressions.Receiver expr, AnnotationMirror necessaryAnnotation, AnnotationMirror inferredAnnotation, CFAbstractStore<?,?> store)
inferredAnnotation
is valid for a given expression to
match the necessaryAnnotation
.
By default, inferredAnnotation
must be a subtype of necessaryAnnotation
,
but subclasses might override this behavior.
protected void typeCheckVectorCopyIntoArgument(MethodInvocationTree node, List<? extends AnnotatedTypeMirror> params)
Vector.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 type Vector<E>
, the method invocation v.copyInto(arr)
is type-safe iff arr
is an array of type T[]
, where T
is a subtype of E
.
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.
node
- a method invocation of Vector.copyInto()
params
- the types of the parameters of Vectory.copyInto()
public Void visitNewClass(NewClassTree node, Void p)
An invocation of a constructor, c, is valid only if:
visitNewClass
in interface TreeVisitor<Void,Void>
visitNewClass
in class TreeScanner<Void,Void>
public Void visitLambdaExpression(LambdaExpressionTree node, Void p)
visitLambdaExpression
in interface TreeVisitor<Void,Void>
visitLambdaExpression
in class TreeScanner<Void,Void>
public Void visitMemberReference(MemberReferenceTree node, Void p)
visitMemberReference
in interface TreeVisitor<Void,Void>
visitMemberReference
in class TreeScanner<Void,Void>
public Void visitReturn(ReturnTree node, Void p)
visitReturn
in interface TreeVisitor<Void,Void>
visitReturn
in class TreeScanner<Void,Void>
public Void visitAnnotation(AnnotationTree node, Void p)
visitAnnotation
in interface TreeVisitor<Void,Void>
visitAnnotation
in class TreeScanner<Void,Void>
public Void visitConditionalExpression(ConditionalExpressionTree node, Void p)
visitConditionalExpression
in interface TreeVisitor<Void,Void>
visitConditionalExpression
in class TreeScanner<Void,Void>
public Void visitUnary(UnaryTree node, Void p)
visitUnary
in interface TreeVisitor<Void,Void>
visitUnary
in class TreeScanner<Void,Void>
public Void visitCompoundAssignment(CompoundAssignmentTree node, Void p)
visitCompoundAssignment
in interface TreeVisitor<Void,Void>
visitCompoundAssignment
in class TreeScanner<Void,Void>
public Void visitNewArray(NewArrayTree node, Void p)
visitNewArray
in interface TreeVisitor<Void,Void>
visitNewArray
in class TreeScanner<Void,Void>
protected void checkTypecastRedundancy(TypeCastTree typeCastTree)
protected void checkTypecastSafety(TypeCastTree typeCastTree)
typeCastTree
- an explicitly-written typecastprotected boolean isTypeCastSafe(AnnotatedTypeMirror castType, AnnotatedTypeMirror exprType)
Only primary qualifiers are checked unless the command line option "checkCastElementType" is supplied.
castType
- annotated type of the castexprType
- annotated type of the casted expressionpublic Void visitTypeCast(TypeCastTree node, Void p)
visitTypeCast
in interface TreeVisitor<Void,Void>
visitTypeCast
in class TreeScanner<Void,Void>
public Void visitInstanceOf(InstanceOfTree node, Void p)
visitInstanceOf
in interface TreeVisitor<Void,Void>
visitInstanceOf
in class TreeScanner<Void,Void>
public Void visitArrayAccess(ArrayAccessTree node, Void p)
visitArrayAccess
in interface TreeVisitor<Void,Void>
visitArrayAccess
in class TreeScanner<Void,Void>
public Void visitCatch(CatchTree node, Void p)
visitCatch
in interface TreeVisitor<Void,Void>
visitCatch
in class TreeScanner<Void,Void>
public Void visitThrow(ThrowTree node, Void p)
visitThrow
in interface TreeVisitor<Void,Void>
visitThrow
in class TreeScanner<Void,Void>
protected void checkExceptionParameter(CatchTree node)
getExceptionParameterLowerBoundAnnotations()
, 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()
.
node
- CatchTree to checkprotected Set<? extends AnnotationMirror> getExceptionParameterLowerBoundAnnotations()
Note: by default this method is called by getThrowUpperBoundAnnotations()
, so
that this annotation is enforced.
(Default is top)
protected void checkThrownExpression(ThrowTree node)
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 as getExceptionParameterLowerBoundAnnotations()
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 override getThrowUpperBoundAnnotations()
.
node
- ThrowTree to checkprotected Set<? extends AnnotationMirror> getThrowUpperBoundAnnotations()
Note: by default this method is returns by getExceptionParameterLowerBoundAnnotations(), so that this annotation is enforced.
(Default is top)
protected void commonAssignmentCheck(Tree varTree, ExpressionTree valueExp, @CompilerMessageKey String errorKey, Object... extraArgs)
varTree
- the AST node for the lvalue (usually a variable)valueExp
- 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" typesprotected void commonAssignmentCheck(AnnotatedTypeMirror varType, ExpressionTree valueExp, @CompilerMessageKey String errorKey, Object... extraArgs)
varType
- the annotated type for the lvalue (usually a variable)valueExp
- 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" typesprotected void commonAssignmentCheck(AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, Tree valueTree, @CompilerMessageKey String errorKey, Object... extraArgs)
varType
- the annotated type of the variablevalueType
- the annotated type of the valuevalueTree
- 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" typesprotected final void commonAssignmentCheckStartDiagnostic(AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, Tree valueTree)
varType
- the annotated type of the variablevalueType
- the annotated type of the valuevalueTree
- the location to use when reporting the error messageprotected final void commonAssignmentCheckEndDiagnostic(boolean success, String extraMessage, AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, Tree valueTree)
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 valuevalueTree
- the location to use when reporting the error messageprotected final void commonAssignmentCheckEndDiagnostic(String message, AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, Tree valueTree)
Most clients should call commonAssignmentCheckEndDiagnostic(boolean, String,
AnnotatedTypeMirror, AnnotatedTypeMirror, Tree)
. The purpose of this method is to permit
customizing the message that is printed.
message
- the result, plus information about why the result is what it is; may be nullvarType
- the annotated type of the variablevalueType
- the annotated type of the valuevalueTree
- the location to use when reporting the error messageprotected void checkArrayInitialization(AnnotatedTypeMirror type, List<? extends ExpressionTree> initializers)
protected void checkTypeArguments(Tree toptree, List<? extends AnnotatedTypeParameterBounds> paramBounds, List<? extends AnnotatedTypeMirror> typeargs, List<? extends Tree> typeargTrees, Name typeOrMethodName, List<?> paramNames)
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 reportingprotected boolean skipReceiverSubtypeCheck(MethodInvocationTree node, AnnotatedTypeMirror methodDefinitionReceiver, AnnotatedTypeMirror methodCallReceiver)
node
- the method invocation nodemethodDefinitionReceiver
- the ATM of the receiver of the method definitionmethodCallReceiver
- the ATM of the receiver of the method callprotected void checkMethodInvocability(AnnotatedTypeMirror.AnnotatedExecutableType method, MethodInvocationTree node)
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.
method
- the type of the invoked methodnode
- the method invocation nodeprotected void reportMethodInvocabilityError(MethodInvocationTree node, AnnotatedTypeMirror found, AnnotatedTypeMirror expected)
node
- the AST node at which to report the errorfound
- the actual type of the receiverexpected
- the expected type of the receiverprotected void checkConstructorInvocation(AnnotatedTypeMirror.AnnotatedDeclaredType invocation, AnnotatedTypeMirror.AnnotatedExecutableType constructor, NewClassTree newClassTree)
Issue a warning if the annotations on the constructor invocation is a subtype of the constructor result type. This is equivalent to down-casting.
protected void checkArguments(List<? extends AnnotatedTypeMirror> requiredArgs, List<? extends ExpressionTree> passedArgs, Name executableName, List<?> paramNames)
Note this method requires the lists to have the same length, as it does not handle cases like var args.
requiredArgs
- 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 parameterscheckVarargs(AnnotatedTypeMirror.AnnotatedExecutableType, Tree)
protected boolean testTypevarContainment(AnnotatedTypeMirror inner, AnnotatedTypeMirror outer)
protected BaseTypeVisitor.OverrideChecker createOverrideChecker(Tree overriderTree, AnnotatedTypeMirror.AnnotatedExecutableType overrider, AnnotatedTypeMirror overridingType, AnnotatedTypeMirror overridingReturnType, AnnotatedTypeMirror.AnnotatedExecutableType overridden, AnnotatedTypeMirror.AnnotatedDeclaredType overriddenType, AnnotatedTypeMirror overriddenReturnType)
This exists so that subclasses can subclass OverrideChecker and use their subclass instead of using OverrideChecker itself.
protected boolean checkOverride(MethodTree overriderTree, AnnotatedTypeMirror.AnnotatedDeclaredType overridingType, AnnotatedTypeMirror.AnnotatedExecutableType overridden, AnnotatedTypeMirror.AnnotatedDeclaredType overriddenType)
createOverrideChecker(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.overriderTree
- declaration tree of overriding methodoverridingType
- type of overriding classoverridden
- type of overridden methodoverriddenType
- type of overridden classcheckOverride(MethodTree, AnnotatedTypeMirror.AnnotatedExecutableType,
AnnotatedTypeMirror.AnnotatedDeclaredType, AnnotatedTypeMirror.AnnotatedExecutableType,
AnnotatedTypeMirror.AnnotatedDeclaredType)
protected boolean checkOverride(MethodTree overriderTree, AnnotatedTypeMirror.AnnotatedExecutableType overrider, AnnotatedTypeMirror.AnnotatedDeclaredType overridingType, AnnotatedTypeMirror.AnnotatedExecutableType overridden, AnnotatedTypeMirror.AnnotatedDeclaredType overriddenType)
createOverrideChecker(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
AnnotatedExecutableType of the overriding method. Override this version of the method if you
need to access that type.overriderTree
- declaration tree of overriding methodoverrider
- type of the overriding methodoverridingType
- type of overriding classoverridden
- type of overridden methodoverriddenType
- type of overridden classcheckOverride(MethodTree, AnnotatedTypeMirror.AnnotatedDeclaredType,
AnnotatedTypeMirror.AnnotatedExecutableType, AnnotatedTypeMirror.AnnotatedDeclaredType)
protected boolean checkMethodReferenceAsOverride(MemberReferenceTree memberReferenceTree, Void p)
memberReferenceTree
- the tree for the method referenceprotected MemberSelectTree enclosingMemberSelect()
protected Tree enclosingStatement(@FindDistinct Tree tree)
tree
- an AST node that is on the current pathpublic Void visitIdentifier(IdentifierTree node, Void p)
visitIdentifier
in interface TreeVisitor<Void,Void>
visitIdentifier
in class TreeScanner<Void,Void>
protected void checkAccess(IdentifierTree node, Void p)
protected boolean isAccessAllowed(Element field, AnnotatedTypeMirror receiver, @FindDistinct ExpressionTree accessTree)
field
- the field to be accessed, whose declaration might be annotated by @Unusedreceiver
- the expression whose field is accessedaccessTree
- the access expressionpublic boolean isValidUse(AnnotatedTypeMirror.AnnotatedDeclaredType declarationType, AnnotatedTypeMirror.AnnotatedDeclaredType useType, Tree tree)
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 of declarationType
. If
a type system makes exceptions to this rule, its implementation should override this method.
declarationType
- the type of the class (TypeElement)useType
- the use of the class (instance type)tree
- the tree where the type is usedpublic boolean isValidUse(AnnotatedTypeMirror.AnnotatedPrimitiveType type, Tree tree)
type
- the use of the primitive typetree
- the tree where the type is usedpublic boolean isValidUse(AnnotatedTypeMirror.AnnotatedArrayType type, Tree tree)
type
- the array type usetree
- the tree where the type is usedpublic boolean validateTypeOf(Tree tree)
tree
- the AST type supplied by the userprotected boolean validateType(Tree tree, AnnotatedTypeMirror type)
tree
- the type tree supplied by the usertype
- the type corresponding to treeprotected TypeValidator createTypeValidator()
protected final boolean shouldSkipUses(ExpressionTree exprTree)
checker.skipUses
property.
It returns true if exprTree is a method invocation or a field access to a class whose qualified name matches @{link checker.skipUses} expression.
exprTree
- any expression treepublic Void visitCompilationUnit(CompilationUnitTree node, Void p)
visitCompilationUnit
in interface TreeVisitor<Void,Void>
visitCompilationUnit
in class TreeScanner<Void,Void>