Class 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 the NullnessChecker.
    • 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.
    • 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 class BaseTypeVisitor<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 class SourceVisitor<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 of com.sun.source.util.TreePathScanner.scan(TreePath, P), the CompilationUnitTree of the TreePath has to be equal to root.
        Overrides:
        setRoot in class BaseTypeVisitor<InitializationAnnotatedTypeFactory>
      • 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 class BaseTypeVisitor<InitializationAnnotatedTypeFactory>
        Parameters:
        superCall - the AST node for the constructor call
        errorKey - 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 class BaseTypeVisitor<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 fails
        extraArgs - arguments to the error message key, before "found" and "expected" types
        Returns:
        true if the check succeeds, false if an error message was issued
      • 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
        Also, it issues a "missing.this" error for static method annotated receivers.
        Specified by:
        visitMethod in interface com.sun.source.tree.TreeVisitor<java.lang.Void,​java.lang.Void>
        Overrides:
        visitMethod in class BaseTypeVisitor<InitializationAnnotatedTypeFactory>
      • visitVariable

        public java.lang.Void visitVariable​(com.sun.source.tree.VariableTree tree,
                                            java.lang.Void p)
        Specified by:
        visitVariable in interface com.sun.source.tree.TreeVisitor<java.lang.Void,​java.lang.Void>
        Overrides:
        visitVariable in class BaseTypeVisitor<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 interface com.sun.source.tree.TreeVisitor<java.lang.Void,​java.lang.Void>
        Overrides:
        visitMethodInvocation in class BaseTypeVisitor<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 class BaseTypeVisitor<InitializationAnnotatedTypeFactory>
        Parameters:
        varType - the annotated type of the variable
        valueType - the annotated type of the value
        valueTree - the location to use when reporting the error message
        errorKey - the error message key to use if the check fails
        extraArgs - arguments to the error message key, before "found" and "expected" types
      • 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 if staticFields is true) are initialized in the given store.
        Parameters:
        tree - a ClassTree if staticFields is true; a MethodTree for a constructor if staticFields is false. This is where errors are reported, if they are not reported at the fields themselves
        staticFields - whether to check static fields or instance fields
        store - the store
        receiverAnnotations - 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 after tree from uninitializedFields.

        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 either HoldsForDefaultValue or is the top qualifier.

        Parameters:
        tree - the tree at whose location to check for initialization
        uninitializedFields - the possibly uninitialized fields to check
        storeBefore - whether to check for initialization in the store before (or after) tree