Class NullnessVisitor
- All Implemented Interfaces:
TreeVisitor<Void,
Void>
-
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
assumePureGetters, atypeFactory, checker, DETERMINISTIC, IMPURE, methodTree, positions, PURE, qualHierarchy, showchecks, SIDE_EFFECT_FREE, TARGET, targetValueElement, typeHierarchy, typeValidator, unusedWhenElement
Fields inherited from class org.checkerframework.framework.source.SourceVisitor
elements, root, trees, treesWithSuppressWarnings, types
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
Issue error if the exception parameter is not a supertype of the annotation specified byBaseTypeVisitor.getExceptionParameterLowerBoundAnnotations()
, which is top by default.protected void
Reports an error if a comparison of a @NonNull expression with the null literal is performed.protected void
checkMethodInvocability
(AnnotatedTypeMirror.AnnotatedExecutableType method, MethodInvocationTree tree) Tests whether the method can be invoked using the receiver of the 'tree' method invocation, and issues a "method.invocation" if the invocation is invalid.protected void
Case 4: Check for thrown exception nullness.protected boolean
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 boolean
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 boolean
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.Constructs an instance of the appropriate type factory for the implemented type system.protected TypeValidator
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.visitAnnotation
(AnnotationTree tree, Void p) Ensure that the annotation arguments comply to their declarations.visitArrayAccess
(ArrayAccessTree tree, Void p) Case 3: Check for array dereferencing.visitAssert
(AssertTree tree, Void p) visitBinary
(BinaryTree tree, Void p) Case 6: Check for redundant nullness tests Case 7: unboxing case: primitive operations.Case 7: unboxing case: primitive operation.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
(DoWhileLoopTree tree, Void p) visitEnhancedForLoop
(EnhancedForLoopTree tree, Void p) Case 2: Check for implicit.iterator
call.visitForLoop
(ForLoopTree tree, Void p) visitInstanceOf
(InstanceOfTree tree, Void p) visitMemberSelect
(MemberSelectTree tree, Void p) Case 1: Check for null dereferencing.visitMethod
(MethodTree tree, Void p) Checks that the method or constructor obeys override and subtype rules to all overridden methods.Performs a method invocation check.visitNewArray
(NewArrayTree tree, Void p) visitNewClass
(NewClassTree tree, Void p) Performs a new class invocation check.visitSwitch
(SwitchTree tree, Void p) void
visitSwitchExpression17
(Tree switchExprTree) This method validates the type of the switch expression.visitSynchronized
(SynchronizedTree tree, Void p) Case 5: Check for synchronizing locks.visitTypeCast
(TypeCastTree tree, Void p) Case 7: unboxing case: casting to a primitive.visitUnary
(UnaryTree tree, Void p) Case 7: unboxing case: primitive operation.visitWhileLoop
(WhileLoopTree tree, 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, checkExtendsAndImplements, checkExtendsOrImplements, checkFieldInvariantDeclarations, checkMethodReferenceAsOverride, checkOverride, checkOverride, checkPostcondition, checkPreconditions, checkPurityAnnotations, checkQualifierParameter, checkSuperConstructorCall, checkThisConstructorCall, checkTypeArguments, checkTypecastRedundancy, checkTypecastSafety, checkVarargs, commonAssignmentCheckEndDiagnostic, commonAssignmentCheckEndDiagnostic, commonAssignmentCheckStartDiagnostic, contractExpressionAndType, createOverrideChecker, createTypeFactoryPublic, enclosingMemberSelect, enclosingStatement, getExceptionParameterLowerBoundAnnotations, getThrowUpperBoundAnnotations, getTypeFactory, isTypeCastSafe, isValidUse, isValidUse, reportMethodInvocabilityError, reportPurityErrors, scan, shouldPerformContractInference, shouldSkipUses, shouldWarnAboutIrrelevantJavaTypes, skipReceiverSubtypeCheck, testAnnotationInsertion, testJointJavacJavaParserVisitor, testTypevarContainment, typeCheckVectorCopyIntoArgument, validateType, validateTypeOf, visitAnnotatedType, visitAssignment, visitCatch, visitClass, visitCompilationUnit, visitIdentifier, visitLambdaExpression, visitMemberReference, visitReturn, visitThrow, visitTypeParameter, warnAboutIrrelevantJavaTypes, warnInvalidPolymorphicQualifier, warnInvalidPolymorphicQualifier, warnRedundantAnnotations
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
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 boolean commonAssignmentCheck(Tree varTree, 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- Returns:
- true if the check succeeds, false if an error message was issued
-
commonAssignmentCheck
protected boolean commonAssignmentCheck(AnnotatedTypeMirror varType, 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- Returns:
- true if the check succeeds, false if an error message was issued
-
commonAssignmentCheck
@FormatMethod protected boolean commonAssignmentCheck(AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, 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- Returns:
- true if the check succeeds, false if an error message was issued
-
visitMemberSelect
Case 1: Check for null dereferencing.- Specified by:
visitMemberSelect
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitMemberSelect
in classTreeScanner<Void,
Void>
-
visitEnhancedForLoop
Case 2: Check for implicit.iterator
call.- Specified by:
visitEnhancedForLoop
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitEnhancedForLoop
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-
visitArrayAccess
Case 3: Check for array dereferencing.- Specified by:
visitArrayAccess
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitArrayAccess
in classTreeScanner<Void,
Void>
-
visitNewArray
- Specified by:
visitNewArray
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitNewArray
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-
checkThrownExpression
Case 4: Check for thrown exception nullness.- Overrides:
checkThrownExpression
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
- Parameters:
tree
- a ThrowTree to check
-
visitSynchronized
Case 5: Check for synchronizing locks.- Specified by:
visitSynchronized
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitSynchronized
in classTreeScanner<Void,
Void>
-
visitAssert
- Specified by:
visitAssert
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitAssert
in classTreeScanner<Void,
Void>
-
visitIf
- Specified by:
visitIf
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitIf
in classTreeScanner<Void,
Void>
-
visitInstanceOf
- Specified by:
visitInstanceOf
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitInstanceOf
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-
checkForRedundantTests
Reports an error if a comparison of a @NonNull expression with the null literal is performed. Does nothing unless-Alint=redundantNullComparison
is passed on the command line.- Parameters:
tree
- a tree that might be a comparison of a @NonNull expression with the null literal
-
visitBinary
Case 6: Check for redundant nullness tests Case 7: unboxing case: primitive operations.- Specified by:
visitBinary
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitBinary
in classTreeScanner<Void,
Void>
-
visitUnary
Case 7: unboxing case: primitive operation.- Specified by:
visitUnary
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitUnary
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-
visitCompoundAssignment
Case 7: unboxing case: primitive operation.- Specified by:
visitCompoundAssignment
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitCompoundAssignment
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-
visitTypeCast
Case 7: unboxing case: casting to a primitive.- Specified by:
visitTypeCast
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitTypeCast
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-
visitMethod
Description copied from class:BaseTypeVisitor
Checks that the method or constructor 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 interfaceTreeVisitor<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 interfaceTreeVisitor<Void,
Void> - Overrides:
visitMethodInvocation
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-
processClassTree
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, MethodInvocationTree tree) Description copied from class:BaseTypeVisitor
Tests whether the method can be invoked using the receiver of the 'tree' 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 methodtree
- the method invocation tree
-
visitSwitch
- Specified by:
visitSwitch
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitSwitch
in classTreeScanner<Void,
Void>
-
visitSwitchExpression17
Description copied from class:BaseTypeVisitor
This method validates the type of the switch expression. It issues an error if the type of a value that the switch expression can result is not a subtype of the switch type.If a subclass overrides this method, it must call
super.scan(switchExpressionTree, null)
so that the blocks and statements in the cases are checked.- Overrides:
visitSwitchExpression17
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
- Parameters:
switchExprTree
- aSwitchExpressionTree
-
visitForLoop
- Specified by:
visitForLoop
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitForLoop
in classTreeScanner<Void,
Void>
-
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 interfaceTreeVisitor<Void,
Void> - Overrides:
visitNewClass
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-
visitWhileLoop
- Specified by:
visitWhileLoop
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitWhileLoop
in classTreeScanner<Void,
Void>
-
visitDoWhileLoop
- Specified by:
visitDoWhileLoop
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitDoWhileLoop
in classTreeScanner<Void,
Void>
-
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 interfaceTreeVisitor<Void,
Void> - Overrides:
visitConditionalExpression
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-
checkExceptionParameter
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:
tree
- a 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 interfaceTreeVisitor<Void,
Void> - Overrides:
visitAnnotation
in classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-
visitAnnotatedType
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>
-