public class NullnessVisitor extends InitializationVisitor<NullnessAnnotatedTypeFactory,NullnessValue,NullnessStore>
BaseTypeVisitor.OverrideChecker
annoFormatter, initializedFields
atypeFactory, checker, DETERMINISTIC, inferPurity, methodTree, positions, PURE, SIDE_EFFECT_FREE, TARGET, targetValueElement, typeValidator, unusedWhenElement
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" 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.AnnotatedPrimitiveType type,
Tree tree)
Tests that the qualifiers present on the primitive type are valid.
|
void |
processClassTree(ClassTree classTree)
Type-check classTree.
|
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 |
visitMethod(MethodTree node,
Void p)
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 |
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, setRoot, visitVariable
checkAccess, checkAccessAllowed, checkArguments, checkArrayInitialization, checkConditionalPostcondition, checkDefaultConstructor, checkExplicitAnnotationsOnIntersectionBounds, checkExtendsImplements, checkFieldInvariantDeclarations, checkForPolymorphicQualifiers, checkForPolymorphicQualifiers, 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, isValidUse, reportMethodInvocabilityError, reportPurityErrors, scan, shouldSkipUses, skipReceiverSubtypeCheck, testAnnotationInsertion, testJointJavacJavaParserVisitor, 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.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" types@FormatMethod protected 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 TreeScanner<Void,Void>
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 visitMethod(MethodTree node, Void p)
BaseTypeVisitor
The override rule specifies that a method, m1, may override a method m2 only if:
visitMethod
in interface TreeVisitor<Void,Void>
visitMethod
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>
public void processClassTree(ClassTree classTree)
BaseTypeVisitor
BaseTypeVisitor.visitClass(ClassTree, Void)
.processClassTree
in class InitializationVisitor<NullnessAnnotatedTypeFactory,NullnessValue,NullnessStore>
classTree
- class to checkprotected 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 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 toprotected TypeValidator createTypeValidator()
createTypeValidator
in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>