Class NullnessVisitor
-
Nested Class Summary
Nested classes/interfaces inherited from class org.checkerframework.common.basetype.BaseTypeVisitor
BaseTypeVisitor.OverrideChecker
-
Field Summary
Fields inherited from class org.checkerframework.checker.initialization.InitializationVisitor
annoFormatter, initializedFields
Fields inherited from class org.checkerframework.common.basetype.BaseTypeVisitor
atypeFactory, checker, DETERMINISTIC, methodTree, positions, PURE, SIDE_EFFECT_FREE, TARGET, targetValueElement, typeValidator, unusedWhenElement
Fields inherited from class org.checkerframework.framework.source.SourceVisitor
elements, root, trees, treesWithSuppressWarnings, types
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
checkExceptionParameter
(com.sun.source.tree.CatchTree node) Issue error if the exception parameter is not a supertype of the annotation specified byBaseTypeVisitor.getExceptionParameterLowerBoundAnnotations()
, which is top by default.protected void
checkForRedundantTests
(com.sun.source.tree.BinaryTree node) Reports an error if a comparison of a @NonNull expression with the null literal is performed.protected void
checkMethodInvocability
(AnnotatedTypeMirror.AnnotatedExecutableType method, com.sun.source.tree.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
(com.sun.source.tree.ThrowTree node) Case 4: Check for thrown exception nullness.protected void
commonAssignmentCheck
(com.sun.source.tree.Tree varTree, com.sun.source.tree.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
(AnnotatedTypeMirror varType, com.sun.source.tree.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
(AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, com.sun.source.tree.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.Constructs an instance of the appropriate type factory for the implemented type system.protected TypeValidator
boolean
isValidUse
(AnnotatedTypeMirror.AnnotatedPrimitiveType type, com.sun.source.tree.Tree tree) Tests that the qualifiers present on the primitive type are valid.void
processClassTree
(com.sun.source.tree.ClassTree classTree) Type-check classTree.void
visitAnnotatedType
(@Nullable List<? extends com.sun.source.tree.AnnotationTree> annoTrees, com.sun.source.tree.Tree typeTree) Checks an annotated type.visitAnnotation
(com.sun.source.tree.AnnotationTree node, Void p) Ensure that the annotation arguments comply to their declarations.visitArrayAccess
(com.sun.source.tree.ArrayAccessTree node, Void p) Case 3: Check for array dereferencing.visitAssert
(com.sun.source.tree.AssertTree node, Void p) visitBinary
(com.sun.source.tree.BinaryTree node, Void p) Case 6: Check for redundant nullness tests Case 7: unboxing case: primitive operations.visitCompoundAssignment
(com.sun.source.tree.CompoundAssignmentTree node, Void p) Case 7: unboxing case: primitive operation.visitConditionalExpression
(com.sun.source.tree.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.visitDoWhileLoop
(com.sun.source.tree.DoWhileLoopTree node, Void p) visitEnhancedForLoop
(com.sun.source.tree.EnhancedForLoopTree node, Void p) Case 2: Check for implicit.iterator
call.visitForLoop
(com.sun.source.tree.ForLoopTree node, Void p) visitInstanceOf
(com.sun.source.tree.InstanceOfTree tree, Void p) visitMemberSelect
(com.sun.source.tree.MemberSelectTree node, Void p) Case 1: Check for null dereferencing.visitMethod
(com.sun.source.tree.MethodTree node, Void p) Checks that the method obeys override and subtype rules to all overridden methods.visitMethodInvocation
(com.sun.source.tree.MethodInvocationTree node, Void p) Performs a method invocation check.visitNewArray
(com.sun.source.tree.NewArrayTree node, Void p) visitNewClass
(com.sun.source.tree.NewClassTree node, Void p) Performs a new class invocation check.visitSwitch
(com.sun.source.tree.SwitchTree node, Void p) visitSynchronized
(com.sun.source.tree.SynchronizedTree node, Void p) Case 5: Check for synchronizing locks.visitTypeCast
(com.sun.source.tree.TypeCastTree node, Void p) Case 7: unboxing case: casting to a primitive.visitUnary
(com.sun.source.tree.UnaryTree node, Void p) Case 7: unboxing case: primitive operation.visitWhileLoop
(com.sun.source.tree.WhileLoopTree node, Void p) Methods inherited from class org.checkerframework.checker.initialization.InitializationVisitor
checkConstructorInvocation, checkConstructorResult, checkContract, checkFieldsInitialized, checkThisOrSuperConstructorCall, setRoot, visitVariable
Methods inherited from class org.checkerframework.common.basetype.BaseTypeVisitor
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, shouldWarnAboutIrrelevantJavaTypes, skipReceiverSubtypeCheck, testAnnotationInsertion, testJointJavacJavaParserVisitor, testTypevarContainment, typeCheckVectorCopyIntoArgument, validateType, validateTypeOf, visitAnnotatedType, visitAssignment, visitCatch, visitClass, visitCompilationUnit, visitIdentifier, visitLambdaExpression, visitMemberReference, visitReturn, visitSwitchExpression17, visitThrow, visitTypeParameter, warnAboutIrrelevantJavaTypes
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, visitArrayType, visitBindingPattern, visitBlock, visitBreak, visitCase, visitContinue, visitDefaultCaseLabel, visitEmptyStatement, visitErroneous, visitExports, visitExpressionStatement, visitGuardedPattern, visitImport, visitIntersectionType, visitLabeledStatement, visitLiteral, visitModifiers, visitModule, visitOpens, visitOther, visitPackage, visitParameterizedType, visitParenthesized, visitParenthesizedPattern, visitPrimitiveType, visitProvides, visitRequires, visitSwitchExpression, visitTry, visitUnionType, visitUses, visitWildcard, visitYield
-
Constructor Details
-
NullnessVisitor
Create a new NullnessVisitor.- Parameters:
checker
- the checker to which this visitor belongs
-
-
Method Details
-
createTypeFactory
Description copied from class:BaseTypeVisitor
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.
- Overrides:
createTypeFactory
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
- Returns:
- the appropriate type factory
-
isValidUse
public boolean isValidUse(AnnotatedTypeMirror.AnnotatedPrimitiveType type, com.sun.source.tree.Tree tree) Description copied from class:BaseTypeVisitor
Tests that the qualifiers present on the primitive type are valid.- Overrides:
isValidUse
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
- 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
-
commonAssignmentCheck
protected void commonAssignmentCheck(com.sun.source.tree.Tree varTree, com.sun.source.tree.ExpressionTree valueExp, @CompilerMessageKey String errorKey, Object... extraArgs) Description copied from class:BaseTypeVisitor
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.- Overrides:
commonAssignmentCheck
in classInitializationVisitor<NullnessAnnotatedTypeFactory,
NullnessValue, NullnessStore> - Parameters:
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" types
-
commonAssignmentCheck
protected void commonAssignmentCheck(AnnotatedTypeMirror varType, com.sun.source.tree.ExpressionTree valueExp, @CompilerMessageKey String errorKey, Object... extraArgs) Description copied from class:BaseTypeVisitor
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.- Overrides:
commonAssignmentCheck
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
- Parameters:
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
-
commonAssignmentCheck
@FormatMethod protected void commonAssignmentCheck(AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, com.sun.source.tree.Tree valueTree, @CompilerMessageKey String errorKey, Object... extraArgs) Description copied from class:BaseTypeVisitor
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.- Overrides:
commonAssignmentCheck
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
- Parameters:
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" types
-
visitMemberSelect
Case 1: Check for null dereferencing. -
visitEnhancedForLoop
Case 2: Check for implicit.iterator
call.- Specified by:
visitEnhancedForLoop
in interfacecom.sun.source.tree.TreeVisitor<Void,
Void> - Overrides:
visitEnhancedForLoop
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-
visitArrayAccess
Case 3: Check for array dereferencing. -
visitNewArray
- Specified by:
visitNewArray
in interfacecom.sun.source.tree.TreeVisitor<Void,
Void> - Overrides:
visitNewArray
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-
checkThrownExpression
protected void checkThrownExpression(com.sun.source.tree.ThrowTree node) Case 4: Check for thrown exception nullness.- Overrides:
checkThrownExpression
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
- Parameters:
node
- ThrowTree to check
-
visitSynchronized
Case 5: Check for synchronizing locks. -
visitAssert
-
visitIf
-
visitInstanceOf
- Specified by:
visitInstanceOf
in interfacecom.sun.source.tree.TreeVisitor<Void,
Void> - Overrides:
visitInstanceOf
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-
checkForRedundantTests
protected void checkForRedundantTests(com.sun.source.tree.BinaryTree node) Reports an error if a comparison of a @NonNull expression with the null literal is performed. -
visitBinary
Case 6: Check for redundant nullness tests Case 7: unboxing case: primitive operations. -
visitUnary
Case 7: unboxing case: primitive operation.- Specified by:
visitUnary
in interfacecom.sun.source.tree.TreeVisitor<Void,
Void> - Overrides:
visitUnary
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-
visitCompoundAssignment
Case 7: unboxing case: primitive operation.- Specified by:
visitCompoundAssignment
in interfacecom.sun.source.tree.TreeVisitor<Void,
Void> - Overrides:
visitCompoundAssignment
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-
visitTypeCast
Case 7: unboxing case: casting to a primitive.- Specified by:
visitTypeCast
in interfacecom.sun.source.tree.TreeVisitor<Void,
Void> - Overrides:
visitTypeCast
in classInitializationVisitor<NullnessAnnotatedTypeFactory,
NullnessValue, NullnessStore>
-
visitMethod
Description copied from class:BaseTypeVisitor
Checks that the method 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 interfacecom.sun.source.tree.TreeVisitor<Void,
Void> - Overrides:
visitMethod
in classInitializationVisitor<NullnessAnnotatedTypeFactory,
NullnessValue, NullnessStore>
-
visitMethodInvocation
Description copied from class:BaseTypeVisitor
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 interfacecom.sun.source.tree.TreeVisitor<Void,
Void> - Overrides:
visitMethodInvocation
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-
processClassTree
public void processClassTree(com.sun.source.tree.ClassTree classTree) Description copied from class:BaseTypeVisitor
Type-check classTree. Subclasses should override this method instead ofBaseTypeVisitor.visitClass(ClassTree, Void)
.- Overrides:
processClassTree
in classInitializationVisitor<NullnessAnnotatedTypeFactory,
NullnessValue, NullnessStore> - Parameters:
classTree
- class to check
-
checkMethodInvocability
protected void checkMethodInvocability(AnnotatedTypeMirror.AnnotatedExecutableType method, com.sun.source.tree.MethodInvocationTree node) Description copied from class:BaseTypeVisitor
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.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.
- Overrides:
checkMethodInvocability
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
- Parameters:
method
- the type of the invoked methodnode
- the method invocation node
-
visitSwitch
-
visitForLoop
-
visitNewClass
Description copied from class:BaseTypeVisitor
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 interfacecom.sun.source.tree.TreeVisitor<Void,
Void> - Overrides:
visitNewClass
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-
visitWhileLoop
-
visitDoWhileLoop
-
visitConditionalExpression
Description copied from class:BaseTypeVisitor
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 interfacecom.sun.source.tree.TreeVisitor<Void,
Void> - Overrides:
visitConditionalExpression
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-
checkExceptionParameter
protected void checkExceptionParameter(com.sun.source.tree.CatchTree node) Description copied from class:BaseTypeVisitor
Issue error if the exception parameter is not a supertype of the annotation specified byBaseTypeVisitor.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()
.- Overrides:
checkExceptionParameter
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
- Parameters:
node
- CatchTree to check
-
visitAnnotation
Description copied from class:BaseTypeVisitor
Ensure that the annotation arguments comply to their declarations. This needs some special casing, as annotation arguments form special trees.- Specified by:
visitAnnotation
in interfacecom.sun.source.tree.TreeVisitor<Void,
Void> - Overrides:
visitAnnotation
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-
visitAnnotatedType
public void visitAnnotatedType(@Nullable List<? extends com.sun.source.tree.AnnotationTree> annoTrees, com.sun.source.tree.Tree typeTree) Description copied from class:BaseTypeVisitor
Checks an annotated type. Invoked byBaseTypeVisitor.visitAnnotatedType(AnnotatedTypeTree, Void)
,BaseTypeVisitor.visitVariable(com.sun.source.tree.VariableTree, java.lang.Void)
, andBaseTypeVisitor.visitMethod(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.- Overrides:
visitAnnotatedType
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
- 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
-
createTypeValidator
- Overrides:
createTypeValidator
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-