Class InitializationVisitor
- java.lang.Object
-
- com.sun.source.util.TreeScanner<R,P>
-
- com.sun.source.util.TreePathScanner<R,P>
-
- org.checkerframework.framework.source.SourceVisitor<java.lang.Void,java.lang.Void>
-
- org.checkerframework.common.basetype.BaseTypeVisitor<InitializationAnnotatedTypeFactory>
-
- org.checkerframework.checker.initialization.InitializationVisitor
-
- All Implemented Interfaces:
com.sun.source.tree.TreeVisitor<java.lang.Void,java.lang.Void>
public class InitializationVisitor extends BaseTypeVisitor<InitializationAnnotatedTypeFactory>
The visitor for the freedom-before-commitment type-system. The freedom-before-commitment type-system and this class are abstract and need to be combined with another type-system whose safe initialization should be tracked. For an example, see theNullnessChecker
.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.checkerframework.common.basetype.BaseTypeVisitor
BaseTypeVisitor.FoundRequired, BaseTypeVisitor.OverrideChecker
-
-
Field Summary
Fields Modifier and Type Field Description protected com.sun.source.tree.Tree
commonAssignmentTree
The assignment/variable/method invocation tree currently being checked.protected java.util.List<com.sun.source.tree.VariableTree>
initializedFields
List of fields in the current compilation unit that have been initialized.-
Fields inherited from class org.checkerframework.common.basetype.BaseTypeVisitor
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
Constructors Constructor Description InitializationVisitor(BaseTypeChecker checker)
Create an InitializationVisitor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
checkConstructorInvocation(AnnotatedTypeMirror.AnnotatedDeclaredType dt, AnnotatedTypeMirror.AnnotatedExecutableType constructor, com.sun.source.tree.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, javax.lang.model.element.ExecutableElement constructorElement)
Issue a warning if the result type of the constructor declaration is not top.protected 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
checkFieldsInitialized(com.sun.source.tree.Tree tree, boolean staticFields, InitializationStore store, java.util.List<? extends javax.lang.model.element.AnnotationMirror> receiverAnnotations)
Checks that all fields (all static fields ifstaticFields
is true) are initialized in the given store.protected void
checkThisOrSuperConstructorCall(com.sun.source.tree.MethodInvocationTree superCall, @CompilerMessageKey java.lang.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(com.sun.source.tree.Tree varTree, com.sun.source.tree.ExpressionTree valueExp, @CompilerMessageKey java.lang.String errorKey, java.lang.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
createTypeFactory()
Constructs an instance of the appropriate type factory for the implemented type system.protected void
filterInitializedFields(com.sun.source.tree.Tree tree, java.util.List<com.sun.source.tree.VariableTree> uninitializedFields, boolean storeBefore)
Use the target checker to remove fields that are initialized or do not need to be initialized in the store before or aftertree
fromuninitializedFields
.void
processClassTree(com.sun.source.tree.ClassTree tree)
Type-check classTree.protected void
reportCommonAssignmentError(AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, com.sun.source.tree.Tree valueTree, @CompilerMessageKey java.lang.String errorKey, java.lang.Object... extraArgs)
Report a common assignment error.protected void
reportMethodInvocabilityError(com.sun.source.tree.MethodInvocationTree node, AnnotatedTypeMirror found, AnnotatedTypeMirror expected)
Report a method invocability error.void
setRoot(com.sun.source.tree.CompilationUnitTree root)
Set the CompilationUnitTree to be used during any visits.void
visit(com.sun.source.util.TreePath path)
Entry point for a type processor: the TreePath leaf is a top-level type tree within root.java.lang.Void
visitAssignment(com.sun.source.tree.AssignmentTree node, java.lang.Void p)
Performs two checks: subtyping and assignability checks, usingBaseTypeVisitor.commonAssignmentCheck(Tree, ExpressionTree, String, Object[])
.java.lang.Void
visitMethod(com.sun.source.tree.MethodTree tree, java.lang.Void p)
Checks that the method obeys override and subtype rules to all overridden methods.java.lang.Void
visitMethodInvocation(com.sun.source.tree.MethodInvocationTree node, java.lang.Void p)
Performs a method invocation check.java.lang.Void
visitVariable(com.sun.source.tree.VariableTree tree, java.lang.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, createOverrideChecker, createQualAllowedLocations, createTypeFactoryPublic, createTypeValidator, enclosingMemberSelect, enclosingStatement, getExceptionParameterLowerBoundAnnotations, getThrowUpperBoundAnnotations, getTypeFactory, isTypeCastSafe, isValidUse, isValidUse, isValidUse, 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.TreeScanner
reduce, scan, visitArrayAccess, visitArrayType, visitAssert, visitBinary, visitBlock, visitBreak, visitCase, visitContinue, visitDoWhileLoop, visitEmptyStatement, visitErroneous, visitExports, visitExpressionStatement, visitForLoop, visitIf, visitImport, visitIntersectionType, visitLabeledStatement, visitLiteral, visitMemberSelect, visitModifiers, visitModule, visitOpens, visitOther, visitPackage, visitParameterizedType, visitParenthesized, visitPrimitiveType, visitProvides, visitRequires, visitSwitch, visitSynchronized, visitTry, visitUnionType, visitUses, visitWhileLoop, visitWildcard
-
-
-
-
Field Detail
-
initializedFields
protected final java.util.List<com.sun.source.tree.VariableTree> initializedFields
List of fields in the current compilation unit that have been initialized.
-
commonAssignmentTree
protected com.sun.source.tree.Tree 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
reportCommonAssignmentError(AnnotatedTypeMirror, AnnotatedTypeMirror, Tree, String, Object...)
to get the correct store value for the right-hand side's fields and checker whether they are initialized according to the target checker.
-
-
Constructor Detail
-
InitializationVisitor
public InitializationVisitor(BaseTypeChecker checker)
Create an InitializationVisitor.- Parameters:
checker
- the initialization checker
-
-
Method Detail
-
createTypeFactory
protected InitializationAnnotatedTypeFactory 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
public void visit(com.sun.source.util.TreePath path)
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<java.lang.Void,java.lang.Void>
-
setRoot
public void setRoot(com.sun.source.tree.CompilationUnitTree root)
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, com.sun.source.tree.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, javax.lang.model.element.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(com.sun.source.tree.MethodInvocationTree superCall, @CompilerMessageKey java.lang.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(com.sun.source.tree.Tree varTree, com.sun.source.tree.ExpressionTree valueExp, @CompilerMessageKey java.lang.String errorKey, java.lang.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)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
-
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<InitializationAnnotatedTypeFactory>
- Parameters:
node
- a CatchTree to check
-
processClassTree
public void processClassTree(com.sun.source.tree.ClassTree tree)
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
public java.lang.Void visitMethod(com.sun.source.tree.MethodTree tree, java.lang.Void p)
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<java.lang.Void,java.lang.Void>
- Overrides:
visitMethod
in classBaseTypeVisitor<InitializationAnnotatedTypeFactory>
-
visitVariable
public java.lang.Void visitVariable(com.sun.source.tree.VariableTree tree, java.lang.Void p)
- Specified by:
visitVariable
in interfacecom.sun.source.tree.TreeVisitor<java.lang.Void,java.lang.Void>
- Overrides:
visitVariable
in classBaseTypeVisitor<InitializationAnnotatedTypeFactory>
-
visitAssignment
public java.lang.Void visitAssignment(com.sun.source.tree.AssignmentTree node, java.lang.Void p)
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 interfacecom.sun.source.tree.TreeVisitor<java.lang.Void,java.lang.Void>
- Overrides:
visitAssignment
in classBaseTypeVisitor<InitializationAnnotatedTypeFactory>
-
visitMethodInvocation
public java.lang.Void visitMethodInvocation(com.sun.source.tree.MethodInvocationTree node, java.lang.Void p)
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<java.lang.Void,java.lang.Void>
- Overrides:
visitMethodInvocation
in classBaseTypeVisitor<InitializationAnnotatedTypeFactory>
-
reportCommonAssignmentError
protected void reportCommonAssignmentError(AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, com.sun.source.tree.Tree valueTree, @CompilerMessageKey java.lang.String errorKey, java.lang.Object... extraArgs)
Description copied from class:BaseTypeVisitor
Report a common assignment error. Allows checkers to change how the message is output.- Overrides:
reportCommonAssignmentError
in classBaseTypeVisitor<InitializationAnnotatedTypeFactory>
- 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
-
reportMethodInvocabilityError
protected void reportMethodInvocabilityError(com.sun.source.tree.MethodInvocationTree node, AnnotatedTypeMirror found, AnnotatedTypeMirror expected)
Description copied from class:BaseTypeVisitor
Report a method invocability error. Allows checkers to change how the message is output.- Overrides:
reportMethodInvocabilityError
in classBaseTypeVisitor<InitializationAnnotatedTypeFactory>
- Parameters:
node
- the AST node at which to report the errorfound
- the actual type of the receiverexpected
- the expected type of the receiver
-
checkFieldsInitialized
protected void checkFieldsInitialized(com.sun.source.tree.Tree tree, boolean staticFields, InitializationStore store, java.util.List<? extends javax.lang.model.element.AnnotationMirror> receiverAnnotations)
Checks that all fields (all static fields ifstaticFields
is true) are initialized in the given store.- 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 fieldsstore
- the storereceiverAnnotations
- the annotations on the receiver
-
filterInitializedFields
protected void filterInitializedFields(com.sun.source.tree.Tree tree, java.util.List<com.sun.source.tree.VariableTree> uninitializedFields, boolean storeBefore)
Use the target checker to remove fields that are initialized or do not need to be initialized in the store before or aftertree
fromuninitializedFields
.A field is initialized if it has a non-top qualifier in the given store that does not have the meta-annotation
HoldsForDefaultValue
in the given store. A field does not need to be initialized if its declared type eitherHoldsForDefaultValue
or is the top qualifier.- Parameters:
tree
- the tree at whose location to check for initializationuninitializedFields
- the possibly uninitialized fields to checkstoreBefore
- whether to check for initialization in the store before (or after)tree
-
-