public class NullnessVisitor extends InitializationVisitor<NullnessAnnotatedTypeFactory,NullnessValue,NullnessStore>
BaseTypeVisitor.OverrideChecker
annoFormatter, initializedFields
atypeFactory, checker, positions, TARGET, targetValueElement, typeValidator, visitorState
elements, root, trees, treesWithSuppressWarnings, types
Constructor and Description |
---|
NullnessVisitor(BaseTypeChecker checker)
Create a new NullnessVisitor.
|
Modifier and Type | Method and Description |
---|---|
protected void |
checkExceptionParameter(CatchTree node)
Issue error if the exception parameter is not a supertype of the annotation specified by
BaseTypeVisitor.getExceptionParameterLowerBoundAnnotations() , which is top by default. |
protected void |
checkForRedundantTests(BinaryTree node)
Reports an error if a comparison of a @NonNull expression with the null literal is performed.
|
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 void |
checkThrownExpression(ThrowTree node)
Case 4: Check for thrown exception nullness.
|
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.
|
NullnessAnnotatedTypeFactory |
createTypeFactory()
Constructs an instance of the appropriate type factory for the implemented type system.
|
protected TypeValidator |
createTypeValidator() |
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 |
visitAnnotatedType(@Nullable List<? extends AnnotationTree> annoTrees,
Tree typeTree)
Checks an annotated type.
|
Void |
visitAnnotation(AnnotationTree node,
Void p)
Ensure that the annotation arguments comply to their declarations.
|
Void |
visitArrayAccess(ArrayAccessTree node,
Void p)
Case 3: Check for array dereferencing.
|
Void |
visitAssert(AssertTree node,
Void p) |
Void |
visitBinary(BinaryTree node,
Void p)
Case 6: Check for redundant nullness tests Case 7: unboxing case: primitive operations.
|
Void |
visitCompoundAssignment(CompoundAssignmentTree node,
Void p)
Case 7: unboxing case: primitive operation.
|
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 |
visitDoWhileLoop(DoWhileLoopTree node,
Void p) |
Void |
visitEnhancedForLoop(EnhancedForLoopTree node,
Void p)
Case 2: Check for implicit
.iterator call. |
Void |
visitForLoop(ForLoopTree node,
Void p) |
Void |
visitIf(IfTree node,
Void p) |
Void |
visitInstanceOf(InstanceOfTree node,
Void p) |
Void |
visitMemberSelect(MemberSelectTree node,
Void p)
Case 1: Check for null dereferencing.
|
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 |
visitSwitch(SwitchTree node,
Void p) |
Void |
visitSynchronized(SynchronizedTree node,
Void p)
Case 5: Check for synchronizing locks.
|
Void |
visitTypeCast(TypeCastTree node,
Void p)
Case 7: unboxing case: casting to a primitive.
|
Void |
visitUnary(UnaryTree node,
Void p)
Case 7: unboxing case: primitive operation.
|
Void |
visitWhileLoop(WhileLoopTree node,
Void p) |
checkConstructorInvocation, checkConstructorResult, checkContract, checkFieldsInitialized, checkThisOrSuperConstructorCall, processClassTree, setRoot, visitMethod, visitVariable
checkAccess, checkAccessAllowed, checkArguments, checkArrayInitialization, checkConditionalPostcondition, checkDefaultConstructor, checkExplicitAnnotationsOnIntersectionBounds, checkExtendsImplements, checkFieldInvariantDeclarations, checkMethodReferenceAsOverride, checkOverride, checkOverride, checkPostcondition, checkPreconditions, checkPurity, checkQualifierParameter, checkSuperConstructorCall, checkThisConstructorCall, checkTypeArguments, checkTypecastRedundancy, checkTypecastSafety, checkVarargs, commonAssignmentCheckEndDiagnostic, commonAssignmentCheckEndDiagnostic, commonAssignmentCheckStartDiagnostic, createOverrideChecker, createTypeFactoryPublic, enclosingMemberSelect, enclosingStatement, getExceptionParameterLowerBoundAnnotations, getThrowUpperBoundAnnotations, getTypeFactory, isTypeCastSafe, isValidUse, reportMethodInvocabilityError, reportPurityErrors, scan, shouldSkipUses, skipReceiverSubtypeCheck, testTypevarContainment, typeCheckVectorCopyIntoArgument, validateType, validateTypeOf, visitAnnotatedType, visitAssignment, visitCatch, visitClass, visitCompilationUnit, visitIdentifier, visitLambdaExpression, visitMemberReference, visitReturn, visitThrow, visitTypeParameter, warnAboutIrrelevantJavaTypes
visit
getCurrentPath, scan
reduce, scan, visitArrayType, visitBlock, visitBreak, visitCase, visitContinue, visitEmptyStatement, visitErroneous, visitExports, visitExpressionStatement, visitImport, visitIntersectionType, visitLabeledStatement, visitLiteral, visitModifiers, visitModule, visitOpens, visitOther, visitPackage, visitParameterizedType, visitParenthesized, visitPrimitiveType, visitProvides, visitRequires, visitTry, visitUnionType, visitUses, visitWildcard
public NullnessVisitor(BaseTypeChecker checker)
checker
- the checker to which this visitor belongspublic NullnessAnnotatedTypeFactory createTypeFactory()
BaseTypeVisitor
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.
createTypeFactory
in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
public boolean isValidUse(AnnotatedTypeMirror.AnnotatedDeclaredType declarationType, AnnotatedTypeMirror.AnnotatedDeclaredType useType, Tree tree)
BaseTypeVisitor
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). BaseTypeVisitor.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.
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 everwhere, override methods such as BaseTypeValidator.visitDeclared(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType, com.sun.source.tree.Tree)
rather than this method.
isValidUse
in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
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)
BaseTypeVisitor
isValidUse
in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
type
- the use of the primitive typetree
- the tree where the type is usedprotected void commonAssignmentCheck(Tree varTree, ExpressionTree valueExp, @CompilerMessageKey String errorKey, Object... extraArgs)
BaseTypeVisitor
commonAssignmentCheck
in class InitializationVisitor<NullnessAnnotatedTypeFactory,NullnessValue,NullnessStore>
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)
BaseTypeVisitor
commonAssignmentCheck
in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
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)
BaseTypeVisitor
commonAssignmentCheck
in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
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" typespublic Void visitMemberSelect(MemberSelectTree node, Void p)
visitMemberSelect
in interface TreeVisitor<Void,Void>
visitMemberSelect
in class TreeScanner<Void,Void>
public Void visitEnhancedForLoop(EnhancedForLoopTree node, Void p)
.iterator
call.visitEnhancedForLoop
in interface TreeVisitor<Void,Void>
visitEnhancedForLoop
in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
public Void visitArrayAccess(ArrayAccessTree node, Void p)
visitArrayAccess
in interface TreeVisitor<Void,Void>
visitArrayAccess
in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
public Void visitNewArray(NewArrayTree node, Void p)
visitNewArray
in interface TreeVisitor<Void,Void>
visitNewArray
in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
protected void checkThrownExpression(ThrowTree node)
checkThrownExpression
in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
node
- ThrowTree to checkpublic Void visitSynchronized(SynchronizedTree node, Void p)
visitSynchronized
in interface TreeVisitor<Void,Void>
visitSynchronized
in class TreeScanner<Void,Void>
public Void visitAssert(AssertTree node, Void p)
visitAssert
in interface TreeVisitor<Void,Void>
visitAssert
in class TreeScanner<Void,Void>
public Void visitIf(IfTree node, Void p)
visitIf
in interface TreeVisitor<Void,Void>
visitIf
in class TreeScanner<Void,Void>
public Void visitInstanceOf(InstanceOfTree node, Void p)
visitInstanceOf
in interface TreeVisitor<Void,Void>
visitInstanceOf
in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
protected void checkForRedundantTests(BinaryTree node)
public Void visitBinary(BinaryTree node, Void p)
visitBinary
in interface TreeVisitor<Void,Void>
visitBinary
in class TreeScanner<Void,Void>
public Void visitUnary(UnaryTree node, Void p)
visitUnary
in interface TreeVisitor<Void,Void>
visitUnary
in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
public Void visitCompoundAssignment(CompoundAssignmentTree node, Void p)
visitCompoundAssignment
in interface TreeVisitor<Void,Void>
visitCompoundAssignment
in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
public Void visitTypeCast(TypeCastTree node, Void p)
visitTypeCast
in interface TreeVisitor<Void,Void>
visitTypeCast
in class InitializationVisitor<NullnessAnnotatedTypeFactory,NullnessValue,NullnessStore>
public Void visitMethodInvocation(MethodInvocationTree node, Void p)
BaseTypeVisitor
An invocation of a method, m, on the receiver, r is valid only if:
visitMethodInvocation
in interface TreeVisitor<Void,Void>
visitMethodInvocation
in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
protected void checkMethodInvocability(AnnotatedTypeMirror.AnnotatedExecutableType method, MethodInvocationTree node)
BaseTypeVisitor
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.
checkMethodInvocability
in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
method
- the type of the invoked methodnode
- the method invocation nodepublic Void visitSwitch(SwitchTree node, Void p)
visitSwitch
in interface TreeVisitor<Void,Void>
visitSwitch
in class TreeScanner<Void,Void>
public Void visitForLoop(ForLoopTree node, Void p)
visitForLoop
in interface TreeVisitor<Void,Void>
visitForLoop
in class TreeScanner<Void,Void>
public Void visitNewClass(NewClassTree node, Void p)
BaseTypeVisitor
An invocation of a constructor, c, is valid only if:
visitNewClass
in interface TreeVisitor<Void,Void>
visitNewClass
in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
public Void visitWhileLoop(WhileLoopTree node, Void p)
visitWhileLoop
in interface TreeVisitor<Void,Void>
visitWhileLoop
in class TreeScanner<Void,Void>
public Void visitDoWhileLoop(DoWhileLoopTree node, Void p)
visitDoWhileLoop
in interface TreeVisitor<Void,Void>
visitDoWhileLoop
in class TreeScanner<Void,Void>
public Void visitConditionalExpression(ConditionalExpressionTree node, Void p)
BaseTypeVisitor
visitConditionalExpression
in interface TreeVisitor<Void,Void>
visitConditionalExpression
in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
protected void checkExceptionParameter(CatchTree node)
BaseTypeVisitor
BaseTypeVisitor.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 BaseTypeVisitor.getExceptionParameterLowerBoundAnnotations()
.
checkExceptionParameter
in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
node
- CatchTree to checkpublic Void visitAnnotation(AnnotationTree node, Void p)
BaseTypeVisitor
visitAnnotation
in interface TreeVisitor<Void,Void>
visitAnnotation
in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
public void visitAnnotatedType(@Nullable List<? extends AnnotationTree> annoTrees, Tree typeTree)
BaseTypeVisitor
BaseTypeVisitor.visitAnnotatedType(AnnotatedTypeTree, Void)
,
BaseTypeVisitor.visitVariable(com.sun.source.tree.VariableTree, java.lang.Void)
, and BaseTypeVisitor.visitMethod(com.sun.source.tree.MethodTree, java.lang.Void)
. Exists to prevent code duplication among
the three. Checking in visitVariable
and visitMethod
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.visitAnnotatedType
in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
annoTrees
- annotations written before a variable/method declaration, if this type is
from one; null otherwisetypeTree
- the type that any type annotations in annoTrees apply toprotected TypeValidator createTypeValidator()
createTypeValidator
in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>