Class OptionalVisitor
- 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<BaseAnnotatedTypeFactory>
 - 
- org.checkerframework.checker.optional.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
 
 
- 
- 
Nested Class Summary
- 
Nested classes/interfaces inherited from class org.checkerframework.common.basetype.BaseTypeVisitor
BaseTypeVisitor.FoundRequired, BaseTypeVisitor.OverrideChecker 
 - 
 
- 
Field Summary
- 
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 OptionalVisitor(BaseTypeChecker checker)Create an OptionalVisitor. 
- 
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected BaseTypeValidatorcreateTypeValidator()voidhandleConditionalStatementIsPresentGet(com.sun.source.tree.IfTree tree)Part of rule #3.voidhandleCreationElimination(com.sun.source.tree.MethodInvocationTree tree)Rule #4.voidhandleTernaryIsPresentGet(com.sun.source.tree.ConditionalExpressionTree tree)Part of rule #3.static com.sun.source.tree.StatementTreeskipBlocks(com.sun.source.tree.StatementTree tree)If the given tree is a block tree with a single element, return the enclosed non-block statement.java.lang.VoidvisitConditionalExpression(com.sun.source.tree.ConditionalExpressionTree tree, java.lang.Void p)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.java.lang.VoidvisitIf(com.sun.source.tree.IfTree tree, java.lang.Void p)java.lang.VoidvisitMethodInvocation(com.sun.source.tree.MethodInvocationTree tree, java.lang.Void p)Performs a method invocation check.java.lang.VoidvisitVariable(com.sun.source.tree.VariableTree tree, java.lang.Void p)Rule #6 (partial).- 
Methods inherited from class org.checkerframework.common.basetype.BaseTypeVisitor
checkAccess, checkAccessAllowed, checkArguments, checkArrayInitialization, checkConditionalPostcondition, checkConstructorInvocation, checkConstructorResult, checkContract, checkDefaultConstructor, checkEnclosingExpr, checkExceptionParameter, checkExplicitAnnotationsOnIntersectionBounds, checkExtendsAndImplements, checkExtendsOrImplements, checkFieldInvariantDeclarations, checkForPolymorphicQualifiers, checkForPolymorphicQualifiers, checkMethodInvocability, checkMethodReferenceAsOverride, checkOverride, checkOverride, checkPostcondition, checkPreconditions, checkPurity, checkQualifierParameter, checkSuperConstructorCall, checkThisConstructorCall, checkThisOrSuperConstructorCall, checkThrownExpression, checkTypeArguments, checkTypecastRedundancy, checkTypecastSafety, checkVarargs, commonAssignmentCheck, commonAssignmentCheck, commonAssignmentCheck, commonAssignmentCheckEndDiagnostic, commonAssignmentCheckEndDiagnostic, commonAssignmentCheckStartDiagnostic, createOverrideChecker, createQualAllowedLocations, createTypeFactory, createTypeFactoryPublic, enclosingMemberSelect, enclosingStatement, getExceptionParameterLowerBoundAnnotations, getThrowUpperBoundAnnotations, getTypeFactory, isTypeCastSafe, isValidUse, isValidUse, isValidUse, processClassTree, reportCommonAssignmentError, reportMethodInvocabilityError, reportPurityErrors, scan, setRoot, shouldSkipUses, shouldWarnAboutIrrelevantJavaTypes, skipReceiverSubtypeCheck, testAnnotationInsertion, testJointJavacJavaParserVisitor, testTypevarContainment, typeCheckVectorCopyIntoArgument, validateTargetLocation, validateType, validateTypeOf, validateVariablesTargetLocation, visitAnnotatedType, visitAnnotatedType, visitAnnotation, visitAssignment, visitCatch, visitClass, visitCompilationUnit, visitCompoundAssignment, visitEnhancedForLoop, visitIdentifier, visitInstanceOf, visitLambdaExpression, visitMemberReference, visitMethod, visitNewArray, visitNewClass, visitReturn, visitSwitchExpression17, visitThrow, visitTypeCast, visitTypeParameter, visitUnary, warnAboutIrrelevantJavaTypes, warnRedundantAnnotations 
- 
Methods inherited from class org.checkerframework.framework.source.SourceVisitor
visit 
- 
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, visitImport, visitIntersectionType, visitLabeledStatement, visitLiteral, visitMemberSelect, visitModifiers, visitModule, visitOpens, visitOther, visitPackage, visitParameterizedType, visitParenthesized, visitPrimitiveType, visitProvides, visitRequires, visitSwitch, visitSynchronized, visitTry, visitUnionType, visitUses, visitWhileLoop, visitWildcard 
 - 
 
 - 
 
- 
- 
Constructor Detail
- 
OptionalVisitor
public OptionalVisitor(BaseTypeChecker checker)
Create an OptionalVisitor. 
 - 
 
- 
Method Detail
- 
createTypeValidator
protected BaseTypeValidator createTypeValidator()
- Overrides:
 createTypeValidatorin classBaseTypeVisitor<BaseAnnotatedTypeFactory>
 
- 
visitConditionalExpression
public java.lang.Void visitConditionalExpression(com.sun.source.tree.ConditionalExpressionTree tree, java.lang.Void p)Description copied from class:BaseTypeVisitorIf 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:
 visitConditionalExpressionin interfacecom.sun.source.tree.TreeVisitor<java.lang.Void,java.lang.Void>- Overrides:
 visitConditionalExpressionin classBaseTypeVisitor<BaseAnnotatedTypeFactory>
 
- 
handleTernaryIsPresentGet
public void handleTernaryIsPresentGet(com.sun.source.tree.ConditionalExpressionTree tree)
Part of rule #3.Pattern match for:
VAR.isPresent() ? VAR.get().METHOD() : VALUEPrefer:
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:
 visitIfin interfacecom.sun.source.tree.TreeVisitor<java.lang.Void,java.lang.Void>- Overrides:
 visitIfin classcom.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:BaseTypeVisitorPerforms 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:
 visitMethodInvocationin interfacecom.sun.source.tree.TreeVisitor<java.lang.Void,java.lang.Void>- Overrides:
 visitMethodInvocationin classBaseTypeVisitor<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:
 visitVariablein interfacecom.sun.source.tree.TreeVisitor<java.lang.Void,java.lang.Void>- Overrides:
 visitVariablein classBaseTypeVisitor<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
 
 
 - 
 
 -