Class ModifiabilityBaseVisitor
- All Implemented Interfaces:
TreeVisitor<Void,Void>
- Direct Known Subclasses:
IteratorVisitor
This class contains logic shared across all the sub-checkers:
- Suppressing the "constructor result must be TOP" check, since collection constructors may
legitimately produce
@Modifiable. - Suppressing the rule that relates a constructor's result to that of the
this()orsuper()call within it, since a class may declare a different modifiability than its superclass does. - Requiring all the constructors of a class to declare the same result qualifier, and
requiring the body of each method that requires the capability -- either on its own
receiver parameter or from a method that it overrides -- to agree with that qualifier about
whether the method throws
UnsupportedOperationException. - Requiring a concrete class that claims the capability not to inherit, without overriding, a
method whose implementation always throws
UnsupportedOperationException. - Requiring an override to preserve a positive receiver capability of the method it overrides.
-
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
assumePureGetters, atypeFactory, checker, DETERMINISTIC, IMPURE, methodTree, positions, PURE, qualHierarchy, showchecks, SIDE_EFFECT_FREE, slowTypecheckingSeconds, slowTypecheckingTree, TARGET, targetValueElement, typeHierarchy, typeValidator, unusedWhenElement, warnRedundantAnnotationsFields inherited from class org.checkerframework.framework.source.SourceVisitor
elements, root, trees, treesWithSuppressWarnings, types -
Constructor Summary
ConstructorsConstructorDescriptionModifiabilityBaseVisitor(BaseTypeChecker checker) Create a ModifiabilityBaseVisitor. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcheckConstructorResult(AnnotatedTypeMirror.AnnotatedExecutableType constructorType, ExecutableElement constructorElement) Issue a warning if the result type of the constructor declaration is not top.protected booleancheckOverride(MethodTree overriderTree, AnnotatedTypeMirror.AnnotatedExecutableType overriderMethodType, AnnotatedTypeMirror.AnnotatedDeclaredType overriderType, AnnotatedTypeMirror.AnnotatedExecutableType overriddenMethodType, AnnotatedTypeMirror.AnnotatedDeclaredType overriddenType) Checks the normal override rules, then requires overrides to preserve any positive modifiability receiver capability from the overridden method.protected voidcheckThisOrSuperConstructorCall(MethodInvocationTree call, @CompilerMessageKey String errorKey) Suppresses the framework's rule that a constructor's result type must be a supertype of the result of thethis()orsuper()call within it.voidprocessClassTree(ClassTree tree) Type-check classTree.voidprocessMethodTree(String className, MethodTree tree) Type-check methodTree.protected booleanReturns true if overrides should preserve positive receiver capabilities from overridden methods.Methods inherited from class org.checkerframework.common.basetype.BaseTypeVisitor
checkAccess, checkAccessAllowed, checkArguments, checkArrayInitialization, checkConditionalPostcondition, checkConstructorInvocation, checkContract, checkDefaultConstructor, checkExceptionParameter, checkExplicitAnnotationsOnIntersectionBounds, checkExtendsAndImplements, checkExtendsOrImplements, checkFieldInvariantDeclarations, checkLambdaPurity, checkLambdaSideEffectsOnly, checkMethodInvocability, checkMethodReferenceAsOverride, checkOverride, checkPostcondition, checkPreconditions, checkPurityAnnotations, checkQualifierParameter, checkSideEffectsOnlyAnnotation, checkSideEffectsOnlyExpressions, checkSuperConstructorCall, checkThisConstructorCall, checkThrownExpression, checkTypeArguments, checkTypecastRedundancy, checkTypecastSafety, checkVarargs, commonAssignmentCheck, commonAssignmentCheck, commonAssignmentCheck, commonAssignmentCheckEndDiagnostic, commonAssignmentCheckEndDiagnostic, commonAssignmentCheckStartDiagnostic, contractExpressionAndType, createOverrideChecker, createTypeFactory, createTypeFactoryPublic, createTypeValidator, enclosingMemberSelect, enclosingStatement, getExceptionParameterLowerBoundAnnotations, getThrowUpperBoundAnnotations, getTypeFactory, isTypeCastSafe, isValidUse, isValidUse, isValidUse, reportMethodInvocabilityError, reportPurityErrors, scan, setRoot, shouldPerformContractInference, shouldSkipUses, shouldWarnAboutIrrelevantJavaTypes, sideEffectsOnlyParseError, skipReceiverSubtypeCheck, testAnnotationInsertion, testJointJavacJavaParserVisitor, testTypevarContainment, typeCheckVectorCopyIntoArgument, validateType, validateTypeOf, visitAnnotatedType, visitAnnotatedType, visitAnnotation, visitAssignment, visitCatch, visitClass, visitCompilationUnit, visitCompoundAssignment, visitConditionalExpression, visitEnhancedForLoop, visitIdentifier, visitInstanceOf, visitLambdaExpression, visitMemberReference, visitMethod, visitMethodInvocation, visitNewArray, visitNewClass, visitReturn, visitSwitchExpression, visitThrow, visitTypeCast, visitTypeParameter, visitUnary, visitVariable, warnAboutIrrelevantJavaTypes, warnInvalidPolymorphicQualifier, warnInvalidPolymorphicQualifier, warnRedundantAnnotationsMethods inherited from class org.checkerframework.framework.source.SourceVisitor
storeSuppressWarningsAnno, visitMethods inherited from class com.sun.source.util.TreePathScanner
getCurrentPath, scanMethods inherited from class com.sun.source.util.TreeScanner
reduce, scan, visitAnyPattern, visitArrayAccess, visitArrayType, visitAssert, visitBinary, visitBindingPattern, visitBlock, visitBreak, visitCase, visitConstantCaseLabel, visitContinue, visitDeconstructionPattern, visitDefaultCaseLabel, visitDoWhileLoop, visitEmptyStatement, visitErroneous, visitExports, visitExpressionStatement, visitForLoop, visitIf, visitImport, visitIntersectionType, visitLabeledStatement, visitLiteral, visitMemberSelect, visitModifiers, visitModule, visitOpens, visitOther, visitPackage, visitParameterizedType, visitParenthesized, visitPatternCaseLabel, visitPrimitiveType, visitProvides, visitRequires, visitStringTemplate, visitSwitch, visitSynchronized, visitTry, visitUnionType, visitUses, visitWhileLoop, visitWildcard, visitYield
-
Constructor Details
-
ModifiabilityBaseVisitor
Create a ModifiabilityBaseVisitor.- Parameters:
checker- the checker that uses this visitor
-
-
Method Details
-
checkThisOrSuperConstructorCall
protected void checkThisOrSuperConstructorCall(MethodInvocationTree call, @CompilerMessageKey String errorKey) Suppresses the framework's rule that a constructor's result type must be a supertype of the result of thethis()orsuper()call within it.A collection class may legitimately declare a different modifiability than its superclass does; for example, a class whose constructors are
@Ungrowablemay extendAbstractList, whose constructor is@Growable, and override every grow method to throwUnsupportedOperationException. The framework's rule would reject every such class, including at the implicitsuper()call of a constructor that has no explicit one.What makes the suppression less unsafe is that the declared modifiability of a class is checked against its method bodies; see
processClassMembers(com.sun.source.tree.ClassTree). That check is not complete, so the suppression does permit some unsound code: it does nothing unless some constructor of the class declares a qualifier in this hierarchy.- Overrides:
checkThisOrSuperConstructorCallin classBaseTypeVisitor<ModifiabilityBaseAnnotatedTypeFactory>- Parameters:
call- the AST node for the constructor callerrorKey- the error message key to use if the check fails
-
processClassTree
Description copied from class:BaseTypeVisitorType-check classTree. Subclasses should override this method instead ofBaseTypeVisitor.visitClass(ClassTree, Void).- Overrides:
processClassTreein classBaseTypeVisitor<ModifiabilityBaseAnnotatedTypeFactory>- Parameters:
tree- class to check
-
processMethodTree
Description copied from class:BaseTypeVisitorType-check methodTree. Subclasses should override this method instead ofBaseTypeVisitor.visitMethod(MethodTree, Void).- Overrides:
processMethodTreein classBaseTypeVisitor<ModifiabilityBaseAnnotatedTypeFactory>- Parameters:
className- the class that contains the method, for diagnostics onlytree- the method to type-check
-
checkOverride
protected boolean checkOverride(MethodTree overriderTree, AnnotatedTypeMirror.AnnotatedExecutableType overriderMethodType, AnnotatedTypeMirror.AnnotatedDeclaredType overriderType, AnnotatedTypeMirror.AnnotatedExecutableType overriddenMethodType, AnnotatedTypeMirror.AnnotatedDeclaredType overriddenType) Checks the normal override rules, then requires overrides to preserve any positive modifiability receiver capability from the overridden method.For example, if the overridden method requires a
@Growablereceiver, then the overriding method must also require a@Growablereceiver.The framework's ordinary receiver override rule allows an overriding method to relax receiver preconditions. For modifiability operations, that would allow a subtype method to drop a required
@Growable,@Shrinkable, or@Replaceablereceiver capability.For example:
abstract class Super { abstract void add(@Growable Super this, String s); } class Sub extends Super { @Override void add(@MaybeGrowable Sub this, String s) { throw new UnsupportedOperationException(); } }Without requiring the override to preserve the
@Growablereceiver,Sub.add()would be permitted even though the body always throwsUnsupportedOperationException.- Overrides:
checkOverridein classBaseTypeVisitor<ModifiabilityBaseAnnotatedTypeFactory>- Parameters:
overriderTree- declaration tree of overriding methodoverriderMethodType- type of the overriding methodoverriderType- type of overriding classoverriddenMethodType- type of overridden methodoverriddenType- type of overridden class- Returns:
- true if the override is allowed
- See Also:
-
shouldCheckReceiverOverrideCapabilityPreservation
protected boolean shouldCheckReceiverOverrideCapabilityPreservation()Returns true if overrides should preserve positive receiver capabilities from overridden methods.- Returns:
- true if overrides should preserve positive receiver capabilities
-
checkConstructorResult
protected void checkConstructorResult(AnnotatedTypeMirror.AnnotatedExecutableType constructorType, ExecutableElement constructorElement) Description copied from class:BaseTypeVisitorIssue a warning if the result type of the constructor declaration is not top. If it is a supertype of the class, then a conflicting.annos error will also be issued byBaseTypeVisitor.isValidUse(AnnotatedTypeMirror.AnnotatedDeclaredType,AnnotatedTypeMirror.AnnotatedDeclaredType,Tree).- Overrides:
checkConstructorResultin classBaseTypeVisitor<ModifiabilityBaseAnnotatedTypeFactory>- Parameters:
constructorType- the AnnotatedExecutableType for the constructorconstructorElement- the element that declares the constructor
-