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, initializedFieldsFields inherited from class org.checkerframework.common.basetype.BaseTypeVisitor
atypeFactory, checker, DETERMINISTIC, methodTree, positions, PURE, SIDE_EFFECT_FREE, TARGET, targetValueElement, typeValidator, unusedWhenElementFields inherited from class org.checkerframework.framework.source.SourceVisitor
elements, root, trees, treesWithSuppressWarnings, types -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcheckExceptionParameter(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 voidcheckForRedundantTests(com.sun.source.tree.BinaryTree node) Reports an error if a comparison of a @NonNull expression with the null literal is performed.protected voidcheckMethodInvocability(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 voidcheckThrownExpression(com.sun.source.tree.ThrowTree node) Case 4: Check for thrown exception nullness.protected voidcommonAssignmentCheck(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 voidcommonAssignmentCheck(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 voidcommonAssignmentCheck(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 TypeValidatorbooleanisValidUse(AnnotatedTypeMirror.AnnotatedPrimitiveType type, com.sun.source.tree.Tree tree) Tests that the qualifiers present on the primitive type are valid.voidprocessClassTree(com.sun.source.tree.ClassTree classTree) Type-check classTree.voidvisitAnnotatedType(@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.iteratorcall.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, visitVariableMethods 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, warnAboutIrrelevantJavaTypesMethods inherited from class org.checkerframework.framework.source.SourceVisitor
visitMethods inherited from class com.sun.source.util.TreePathScanner
getCurrentPath, scanMethods 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:BaseTypeVisitorConstructs 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:
createTypeFactoryin classBaseTypeVisitor<NullnessAnnotatedTypeFactory>- Returns:
- the appropriate type factory
-
isValidUse
public boolean isValidUse(AnnotatedTypeMirror.AnnotatedPrimitiveType type, com.sun.source.tree.Tree tree) Description copied from class:BaseTypeVisitorTests that the qualifiers present on the primitive type are valid.- Overrides:
isValidUsein 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:BaseTypeVisitorChecks 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:
commonAssignmentCheckin 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:BaseTypeVisitorChecks 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:
commonAssignmentCheckin 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:BaseTypeVisitorChecks 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:
commonAssignmentCheckin 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.iteratorcall.- Specified by:
visitEnhancedForLoopin interfacecom.sun.source.tree.TreeVisitor<Void,Void> - Overrides:
visitEnhancedForLoopin classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-
visitArrayAccess
Case 3: Check for array dereferencing. -
visitNewArray
- Specified by:
visitNewArrayin interfacecom.sun.source.tree.TreeVisitor<Void,Void> - Overrides:
visitNewArrayin classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-
checkThrownExpression
protected void checkThrownExpression(com.sun.source.tree.ThrowTree node) Case 4: Check for thrown exception nullness.- Overrides:
checkThrownExpressionin classBaseTypeVisitor<NullnessAnnotatedTypeFactory>- Parameters:
node- ThrowTree to check
-
visitSynchronized
Case 5: Check for synchronizing locks. -
visitAssert
-
visitIf
-
visitInstanceOf
- Specified by:
visitInstanceOfin interfacecom.sun.source.tree.TreeVisitor<Void,Void> - Overrides:
visitInstanceOfin 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:
visitUnaryin interfacecom.sun.source.tree.TreeVisitor<Void,Void> - Overrides:
visitUnaryin classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-
visitCompoundAssignment
Case 7: unboxing case: primitive operation.- Specified by:
visitCompoundAssignmentin interfacecom.sun.source.tree.TreeVisitor<Void,Void> - Overrides:
visitCompoundAssignmentin classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-
visitTypeCast
Case 7: unboxing case: casting to a primitive.- Specified by:
visitTypeCastin interfacecom.sun.source.tree.TreeVisitor<Void,Void> - Overrides:
visitTypeCastin classInitializationVisitor<NullnessAnnotatedTypeFactory,NullnessValue, NullnessStore>
-
visitMethod
Description copied from class:BaseTypeVisitorChecks 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:
visitMethodin interfacecom.sun.source.tree.TreeVisitor<Void,Void> - Overrides:
visitMethodin classInitializationVisitor<NullnessAnnotatedTypeFactory,NullnessValue, NullnessStore>
-
visitMethodInvocation
Description copied from class:BaseTypeVisitorPerforms 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:
visitMethodInvocationin interfacecom.sun.source.tree.TreeVisitor<Void,Void> - Overrides:
visitMethodInvocationin classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-
processClassTree
public void processClassTree(com.sun.source.tree.ClassTree classTree) Description copied from class:BaseTypeVisitorType-check classTree. Subclasses should override this method instead ofBaseTypeVisitor.visitClass(ClassTree, Void).- Overrides:
processClassTreein 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:BaseTypeVisitorTests 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:
checkMethodInvocabilityin classBaseTypeVisitor<NullnessAnnotatedTypeFactory>- Parameters:
method- the type of the invoked methodnode- the method invocation node
-
visitSwitch
-
visitForLoop
-
visitNewClass
Description copied from class:BaseTypeVisitorPerforms 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:
visitNewClassin interfacecom.sun.source.tree.TreeVisitor<Void,Void> - Overrides:
visitNewClassin classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-
visitWhileLoop
-
visitDoWhileLoop
-
visitConditionalExpression
Description copied from class:BaseTypeVisitorIf 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:
visitConditionalExpressionin interfacecom.sun.source.tree.TreeVisitor<Void,Void> - Overrides:
visitConditionalExpressionin classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-
checkExceptionParameter
protected void checkExceptionParameter(com.sun.source.tree.CatchTree node) Description copied from class:BaseTypeVisitorIssue 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:
checkExceptionParameterin classBaseTypeVisitor<NullnessAnnotatedTypeFactory>- Parameters:
node- CatchTree to check
-
visitAnnotation
Description copied from class:BaseTypeVisitorEnsure that the annotation arguments comply to their declarations. This needs some special casing, as annotation arguments form special trees.- Specified by:
visitAnnotationin interfacecom.sun.source.tree.TreeVisitor<Void,Void> - Overrides:
visitAnnotationin 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:BaseTypeVisitorChecks 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 invisitVariableandvisitMethodis 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:
visitAnnotatedTypein 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:
createTypeValidatorin classBaseTypeVisitor<NullnessAnnotatedTypeFactory>
-