Class InitializationVisitor
- All Implemented Interfaces:
TreeVisitor<Void,
Void>
NullnessChecker
.-
Nested Class Summary
Nested classes/interfaces inherited from class org.checkerframework.common.basetype.BaseTypeVisitor
BaseTypeVisitor.FoundRequired, BaseTypeVisitor.OverrideChecker
-
Field Summary
Modifier and TypeFieldDescriptionprotected final boolean
The value of the assumeInitialized option.protected Tree
The assignment/variable/method invocation tree currently being checked.protected final List
<VariableTree> List of fields in the current compilation unit that have been initialized.Fields inherited from class org.checkerframework.common.basetype.BaseTypeVisitor
assumePureGetters, atypeFactory, checker, DETERMINISTIC, ignoreTargetLocations, IMPURE, methodTree, positions, PURE, qualAllowedLocations, 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
ConstructorDescriptionInitializationVisitor
(BaseTypeChecker checker) Create an InitializationVisitor. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
checkConstructorInvocation
(AnnotatedTypeMirror.AnnotatedDeclaredType dt, AnnotatedTypeMirror.AnnotatedExecutableType constructor, NewClassTree src) Check that the (explicit) annotations on a new class tree are comparable to the result type of the constructor.protected void
checkConstructorResult
(AnnotatedTypeMirror.AnnotatedExecutableType constructorType, ExecutableElement constructorElement) Issue a warning if the result type of the constructor declaration is not top.protected void
Issue error if the exception parameter is not a supertype of the annotation specified byBaseTypeVisitor.getExceptionParameterLowerBoundAnnotations()
, which is top by default.protected void
checkFieldsInitialized
(Tree tree, boolean staticFields, InitializationStore initExitStore, List<? extends AnnotationMirror> receiverAnnotations) Checks that all fields (all static fields ifstaticFields
is true) are initialized at the end of a given constructor or static class initializer.protected void
checkThisOrSuperConstructorCall
(MethodInvocationTree superCall, @CompilerMessageKey String errorKey) Checks that the following rule is satisfied: The type on a constructor declaration must be a supertype of the return type of "this()" or "super()" invocation within that constructor.protected boolean
commonAssignmentCheck
(Tree varTree, ExpressionTree valueExpTree, @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 InitializationAnnotatedTypeFactory
Constructs an instance of the appropriate type factory for the implemented type system.void
processClassTree
(ClassTree tree) Type-check classTree.void
setRoot
(CompilationUnitTree root) Set the CompilationUnitTree to be used during any visits.void
Entry point for a type processor: the TreePath leaf is a top-level type tree within root.visitAssignment
(AssignmentTree node, Void p) Performs two checks: subtyping and assignability checks, usingBaseTypeVisitor.commonAssignmentCheck(Tree, ExpressionTree, String, Object[])
.visitMethod
(MethodTree tree, Void p) Checks that the method obeys override and subtype rules to all overridden methods.Performs a method invocation check.visitVariable
(VariableTree tree, Void p) Methods inherited from class org.checkerframework.common.basetype.BaseTypeVisitor
checkAccess, checkAccessAllowed, checkArguments, checkArrayInitialization, checkConditionalPostcondition, checkContract, checkDefaultConstructor, checkEnclosingExpr, checkExplicitAnnotationsOnIntersectionBounds, checkExtendsAndImplements, checkExtendsOrImplements, checkFieldInvariantDeclarations, checkForPolymorphicQualifiers, checkForPolymorphicQualifiers, checkMethodInvocability, checkMethodReferenceAsOverride, checkOverride, checkOverride, checkPostcondition, checkPreconditions, checkPurity, checkQualifierParameter, checkSuperConstructorCall, checkThisConstructorCall, checkThrownExpression, checkTypeArguments, checkTypecastRedundancy, checkTypecastSafety, checkVarargs, commonAssignmentCheck, commonAssignmentCheck, commonAssignmentCheckEndDiagnostic, commonAssignmentCheckEndDiagnostic, commonAssignmentCheckStartDiagnostic, contractExpressionAndType, createOverrideChecker, createQualAllowedLocations, createTypeFactoryPublic, createTypeValidator, enclosingMemberSelect, enclosingStatement, getExceptionParameterLowerBoundAnnotations, getThrowUpperBoundAnnotations, getTypeFactory, isTypeCastSafe, isValidUse, isValidUse, isValidUse, reportCommonAssignmentError, reportMethodInvocabilityError, reportPurityErrors, scan, shouldSkipUses, shouldWarnAboutIrrelevantJavaTypes, skipReceiverSubtypeCheck, testAnnotationInsertion, testJointJavacJavaParserVisitor, testTypevarContainment, typeCheckVectorCopyIntoArgument, validateTargetLocation, validateType, validateTypeOf, validateVariablesTargetLocation, visitAnnotatedType, visitAnnotatedType, visitAnnotation, visitCatch, visitClass, visitCompilationUnit, visitCompoundAssignment, visitConditionalExpression, visitEnhancedForLoop, visitIdentifier, visitInstanceOf, visitLambdaExpression, visitMemberReference, visitNewArray, visitNewClass, visitReturn, visitSwitchExpression17, visitThrow, visitTypeCast, visitTypeParameter, visitUnary, warnAboutIrrelevantJavaTypes, warnRedundantAnnotations
Methods inherited from class com.sun.source.util.TreePathScanner
getCurrentPath, scan
Methods inherited from class com.sun.source.util.TreeScanner
reduce, scan, visitAnyPattern, visitArrayAccess, visitArrayType, visitAssert, visitBinary, visitBindingPattern, visitBlock, visitBreak, visitCase, visitConstantCaseLabel, visitContinue, visitDeconstructionPattern, visitDefaultCaseLabel, visitDoWhileLoop, visitEmptyStatement, visitErroneous, visitExports, visitExpressionStatement, visitForLoop, visitIf, visitImport, visitIntersectionType, visitLabeledStatement, visitLiteral, visitMemberSelect, visitModifiers, visitModule, visitOpens, visitOther, visitPackage, visitParameterizedType, visitParenthesized, visitPatternCaseLabel, visitPrimitiveType, visitProvides, visitRequires, visitStringTemplate, visitSwitch, visitSwitchExpression, visitSynchronized, visitTry, visitUnionType, visitUses, visitWhileLoop, visitWildcard, visitYield
-
Field Details
-
initializedFields
List of fields in the current compilation unit that have been initialized. -
assumeInitialized
protected final boolean assumeInitializedThe value of the assumeInitialized option. -
commonAssignmentTree
The assignment/variable/method invocation tree currently being checked.In the case that the right-hand side is an object, this is used by
BaseTypeVisitor.reportCommonAssignmentError(AnnotatedTypeMirror, AnnotatedTypeMirror, Tree, String, Object...)
to get the correct store value for the right-hand side's fields and check whether they are initialized according to the target checker.
-
-
Constructor Details
-
InitializationVisitor
Create an InitializationVisitor.- Parameters:
checker
- the initialization checker
-
-
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<InitializationAnnotatedTypeFactory>
- Returns:
- the appropriate type factory
-
visit
Description copied from class:SourceVisitor
Entry point for a type processor: the TreePath leaf is a top-level type tree within root.- Overrides:
visit
in classSourceVisitor<Void,
Void>
-
setRoot
Description copied from class:SourceVisitor
Set the CompilationUnitTree to be used during any visits. For any later calls ofcom.sun.source.util.TreePathScanner.scan(TreePath, P)
, the CompilationUnitTree of the TreePath has to be equal toroot
.- Overrides:
setRoot
in classBaseTypeVisitor<InitializationAnnotatedTypeFactory>
-
checkConstructorInvocation
protected void checkConstructorInvocation(AnnotatedTypeMirror.AnnotatedDeclaredType dt, AnnotatedTypeMirror.AnnotatedExecutableType constructor, NewClassTree src) Description copied from class:BaseTypeVisitor
Check that the (explicit) annotations on a new class tree are comparable to the result type of the constructor. Issue an error if not.Issue a warning if the annotations on the constructor invocation is a subtype of the constructor result type. This is equivalent to down-casting.
For type checking of the enclosing expression of inner type instantiations, see
BaseTypeVisitor.checkEnclosingExpr(NewClassTree, AnnotatedExecutableType)
- Overrides:
checkConstructorInvocation
in classBaseTypeVisitor<InitializationAnnotatedTypeFactory>
- Parameters:
dt
- the AnnotatedDeclaredType of the constructor invocationconstructor
- the AnnotatedExecutableType of the constructor declarationsrc
- the NewClassTree
-
checkConstructorResult
protected void checkConstructorResult(AnnotatedTypeMirror.AnnotatedExecutableType constructorType, ExecutableElement constructorElement) Description copied from class:BaseTypeVisitor
Issue a warning if the result type of the constructor declaration is not top. If it is a supertype of the class, then a type.invalid.conflicting.annos error will also be issued byBaseTypeVisitor.isValidUse(AnnotatedTypeMirror.AnnotatedDeclaredType,AnnotatedTypeMirror.AnnotatedDeclaredType,Tree)
.- Overrides:
checkConstructorResult
in classBaseTypeVisitor<InitializationAnnotatedTypeFactory>
- Parameters:
constructorType
- the AnnotatedExecutableType for the constructorconstructorElement
- the element that declares the constructor
-
checkThisOrSuperConstructorCall
protected void checkThisOrSuperConstructorCall(MethodInvocationTree superCall, @CompilerMessageKey String errorKey) Description copied from class:BaseTypeVisitor
Checks that the following rule is satisfied: The type on a constructor declaration must be a supertype of the return type of "this()" or "super()" invocation within that constructor.- Overrides:
checkThisOrSuperConstructorCall
in classBaseTypeVisitor<InitializationAnnotatedTypeFactory>
- Parameters:
superCall
- the AST node for the constructor callerrorKey
- the error message key to use if the check fails
-
commonAssignmentCheck
protected boolean commonAssignmentCheck(Tree varTree, ExpressionTree valueExpTree, @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<InitializationAnnotatedTypeFactory>
- Parameters:
varTree
- the AST node for the lvalue (usually a variable)valueExpTree
- 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
-
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<InitializationAnnotatedTypeFactory>
- Parameters:
node
- a CatchTree to check
-
processClassTree
Description copied from class:BaseTypeVisitor
Type-check classTree. Subclasses should override this method instead ofBaseTypeVisitor.visitClass(ClassTree, Void)
.- Overrides:
processClassTree
in classBaseTypeVisitor<InitializationAnnotatedTypeFactory>
- Parameters:
tree
- class to check
-
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 interfaceTreeVisitor<Void,
Void> - Overrides:
visitMethod
in classBaseTypeVisitor<InitializationAnnotatedTypeFactory>
-
visitVariable
- Specified by:
visitVariable
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitVariable
in classBaseTypeVisitor<InitializationAnnotatedTypeFactory>
-
visitAssignment
Description copied from class:BaseTypeVisitor
Performs two checks: subtyping and assignability checks, usingBaseTypeVisitor.commonAssignmentCheck(Tree, ExpressionTree, String, Object[])
.If the subtype check fails, it issues an "assignment.type.incompatible" error.
- Specified by:
visitAssignment
in interfaceTreeVisitor<Void,
Void> - Overrides:
visitAssignment
in classBaseTypeVisitor<InitializationAnnotatedTypeFactory>
-
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<InitializationAnnotatedTypeFactory>
-
checkFieldsInitialized
protected void checkFieldsInitialized(Tree tree, boolean staticFields, InitializationStore initExitStore, List<? extends AnnotationMirror> receiverAnnotations) Checks that all fields (all static fields ifstaticFields
is true) are initialized at the end of a given constructor or static class initializer.- Parameters:
tree
- aClassTree
ifstaticFields
is true; aMethodTree
for a constructor ifstaticFields
is false. This is where errors are reported, if they are not reported at the fields themselvesstaticFields
- whether to check static fields or instance fieldsinitExitStore
- the initialization exit store for the constructor or static initializerreceiverAnnotations
- the annotations on the receiver
-