Class OptionalVisitor

java.lang.Object
com.sun.source.util.TreeScanner<R,P>
com.sun.source.util.TreePathScanner<R,P>
All Implemented Interfaces:
com.sun.source.tree.TreeVisitor<Void,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 Details

    • OptionalVisitor

      public OptionalVisitor(BaseTypeChecker checker)
      Create an OptionalVisitor.
  • Method Details

    • createTypeValidator

      protected BaseTypeValidator createTypeValidator()
      Overrides:
      createTypeValidator in class BaseTypeVisitor<BaseAnnotatedTypeFactory>
    • visitConditionalExpression

      public Void visitConditionalExpression(com.sun.source.tree.ConditionalExpressionTree node, 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<Void,Void>
      Overrides:
      visitConditionalExpression in class BaseTypeVisitor<BaseAnnotatedTypeFactory>
    • handleTernaryIsPresentGet

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

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

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

    • visitIf

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

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

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

      Prefer: VAR.ifPresent(METHOD);

    • visitMethodInvocation

      public Void visitMethodInvocation(com.sun.source.tree.MethodInvocationTree node, 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<Void,Void>
      Overrides:
      visitMethodInvocation in class BaseTypeVisitor<BaseAnnotatedTypeFactory>
    • handleCreationElimination

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

      Pattern match for: CREATION().ELIMINATION()

      Prefer: VAR.ifPresent(METHOD);

    • visitVariable

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

      Don't use Optional in fields and method parameters.

      Specified by:
      visitVariable in interface com.sun.source.tree.TreeVisitor<Void,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