public class NullnessVisitor extends InitializationVisitor<NullnessAnnotatedTypeFactory,NullnessValue,NullnessStore>
BaseTypeVisitor.OverrideChecker
annoFormatter, initializedFields
atypeFactory, checker, contractsUtils, 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)
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)
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)
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.
|
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.
|
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 |
visitMemberSelect(MemberSelectTree node,
Void p)
Case 1: Check for null dereferencing.
|
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, checkArguments, checkArrayInitialization, checkConditionalPostcondition, checkDefaultConstructor, checkExtendsImplements, checkFieldInvariantDeclarations, checkForAnnotatedJdk, checkMethodReferenceAsOverride, checkOverride, checkOverride, checkPostcondition, checkPreconditions, checkPurity, checkSuperConstructorCall, checkThisConstructorCall, checkTypeArguments, checkTypecastRedundancy, checkTypecastSafety, checkVarargs, commonAssignmentCheckEndDiagnostic, commonAssignmentCheckEndDiagnostic, commonAssignmentCheckStartDiagnostic, createOverrideChecker, createTypeValidator, enclosingMemberSelect, enclosingStatement, getExceptionParameterLowerBoundAnnotations, getThrowUpperBoundAnnotations, getTypeFactory, isAccessAllowed, isTypeCastSafe, isValidUse, isValidUse, reportMethodInvocabilityError, reportPurityErrors, scan, shouldSkipUses, skipReceiverSubtypeCheck, testTypevarContainment, typeCheckVectorCopyIntoArgument, validateType, validateTypeOf, visitAssignment, visitCatch, visitClass, visitCompilationUnit, visitIdentifier, visitInstanceOf, visitLambdaExpression, visitMemberReference, visitMethodInvocation, visitReturn, visitThrow, visitTypeParameter
visit
getCurrentPath, scan
reduce, scan, visitAnnotatedType, 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.
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 usedprotected void commonAssignmentCheck(Tree varTree, ExpressionTree valueExp, @CompilerMessageKey String errorKey)
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 to use if the check fails (must be a compiler message key,
see CompilerMessageKey
)protected void commonAssignmentCheck(AnnotatedTypeMirror varType, ExpressionTree valueExp, @CompilerMessageKey String errorKey)
BaseTypeVisitor
commonAssignmentCheck
in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
varType
- the annotated type of the lvalue (usually a variable)valueExp
- the AST node for the rvalue (the new value)errorKey
- the error message to use if the check fails (must be a compiler message key,
see CompilerMessageKey
)protected void commonAssignmentCheck(AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, Tree valueTree, @CompilerMessageKey String errorKey)
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 to use if the check fails (must be a compiler message key,
see CompilerMessageKey
)public 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>
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>
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>