Class OptionalVisitor

  • All Implemented Interfaces:
    com.sun.source.tree.TreeVisitor<java.lang.Void,​java.lang.Void>

    public class OptionalVisitor
    extends BaseTypeVisitor<BaseAnnotatedTypeFactory>
    The OptionalVisitor enforces the Optional Checker rules. These rules are described in the Checker Framework Manual.
    See the Checker Framework Manual:
    Optional Checker
    • Constructor Detail

      • OptionalVisitor

        public OptionalVisitor​(BaseTypeChecker checker)
        Create an OptionalVisitor.
    • Method Detail

      • visitConditionalExpression

        public java.lang.Void visitConditionalExpression​(com.sun.source.tree.ConditionalExpressionTree tree,
                                                         java.lang.Void p)
        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 interface com.sun.source.tree.TreeVisitor<java.lang.Void,​java.lang.Void>
        Overrides:
        visitConditionalExpression in class BaseTypeVisitor<BaseAnnotatedTypeFactory>
      • handleTernaryIsPresentGet

        public void handleTernaryIsPresentGet​(com.sun.source.tree.ConditionalExpressionTree tree)
        Part of rule #3.

        Pattern match for: VAR.isPresent() ? VAR.get().METHOD() : VALUE

        Prefer: VAR.map(METHOD).orElse(VALUE);

        Parameters:
        tree - a conditional expression that can perhaps be simplified
      • visitIf

        public java.lang.Void visitIf​(com.sun.source.tree.IfTree tree,
                                      java.lang.Void p)
        Specified by:
        visitIf in interface com.sun.source.tree.TreeVisitor<java.lang.Void,​java.lang.Void>
        Overrides:
        visitIf in class com.sun.source.util.TreeScanner<java.lang.Void,​java.lang.Void>
      • handleConditionalStatementIsPresentGet

        public void handleConditionalStatementIsPresentGet​(com.sun.source.tree.IfTree tree)
        Part of rule #3.

        Pattern match for: if (VAR.isPresent()) { METHOD(VAR.get()); }

        Prefer: VAR.ifPresent(METHOD);

        Parameters:
        tree - an if statement that can perhaps be simplified
      • visitMethodInvocation

        public java.lang.Void visitMethodInvocation​(com.sun.source.tree.MethodInvocationTree tree,
                                                    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<BaseAnnotatedTypeFactory>
      • handleCreationElimination

        public void handleCreationElimination​(com.sun.source.tree.MethodInvocationTree tree)
        Rule #4.

        Pattern match for: CREATION().ELIMINATION()

        Prefer: VAR.ifPresent(METHOD);

        Parameters:
        tree - a method invocation that can perhaps be simplified
      • visitVariable

        public java.lang.Void visitVariable​(com.sun.source.tree.VariableTree tree,
                                            java.lang.Void p)
        Rule #6 (partial).

        Don't use Optional in fields and method parameters.

        Specified by:
        visitVariable in interface com.sun.source.tree.TreeVisitor<java.lang.Void,​java.lang.Void>
        Overrides:
        visitVariable in class BaseTypeVisitor<BaseAnnotatedTypeFactory>
      • skipBlocks

        public static com.sun.source.tree.StatementTree skipBlocks​(com.sun.source.tree.StatementTree tree)
        If the given tree is a block tree with a single element, return the enclosed non-block statement. Otherwise, return the same tree.
        Parameters:
        tree - a statement tree
        Returns:
        the single enclosed statement, if it exists; otherwise, the same tree