Class BaseTypeVisitor.OverrideChecker
- Direct Known Subclasses:
GuiEffectVisitor.GuiEffectOverrideChecker
- Enclosing class:
BaseTypeVisitor<Factory extends GenericAnnotatedTypeFactory<?,?, ?, ?>>
Method references are checked similarly to method overrides, with the method reference viewed as overriding the functional interface's method.
Checks that an overriding method's return type, parameter types, and receiver type are correct with respect to the annotations on the overridden method's return type, parameter types, and receiver type.
Furthermore, any contracts on the method must satisfy behavioral subtyping, that is, postconditions must be at least as strong as the postcondition on the superclass, and preconditions must be at most as strong as the condition on the superclass.
This method returns the result of the check, but also emits error messages as a side effect.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final booleanTrue ifoverriderTreeis a MEMBER_REFERENCE.protected final AnnotatedTypeMirror.AnnotatedExecutableTypeThe type of the overridden method.protected final AnnotatedTypeMirrorThe teturn type of the overridden method.protected final AnnotatedTypeMirror.AnnotatedDeclaredTypeThe supertype that declares the overridden method.protected final AnnotatedTypeMirror.AnnotatedExecutableTypeThe type of the overriding method.protected final AnnotatedTypeMirrorThe return type of the overriding method.protected final TreeThe declaration of an overriding method.protected final AnnotatedTypeMirrorThe subtype that declares the overriding method. -
Constructor Summary
ConstructorsConstructorDescriptionOverrideChecker(Tree overriderTree, AnnotatedTypeMirror.AnnotatedExecutableType overrider, AnnotatedTypeMirror overriderType, AnnotatedTypeMirror overriderReturnType, AnnotatedTypeMirror.AnnotatedExecutableType overridden, AnnotatedTypeMirror.AnnotatedDeclaredType overriddenType, AnnotatedTypeMirror overriddenReturnType) Create an OverrideChecker. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanIssue a "methodref.receiver.invalid" or "methodref.receiver.bound.invalid" error if the receiver for the method reference does not satify overriding rules.booleanPerform the check.protected booleanIssue an "override.receiver.invalid" error if the receiver override is not valid.protected booleanisParameterOverrideValid(AnnotatedTypeMirror capturedOverriddenParam, AnnotatedTypeMirror overriddenParam, AnnotatedTypeMirror overriderParam) Returns true ifoverriderParamis an acceptable override ofoverriddenParam(aftercapturedOverriddenParam, the capture-converted form ofoverriddenParam, has been computed).protected booleanisReturnOverrideValid(AnnotatedTypeMirror requiredReturnType, AnnotatedTypeMirror actualReturnType) Returns true ifactualReturnTypeis an acceptable override ofrequiredReturnType.protected booleanisTypeParameterBoundOverrideValid(AnnotatedTypeMirror.AnnotatedTypeVariable overriddenTypeVar, AnnotatedTypeMirror.AnnotatedTypeVariable overriderTypeVar) Returns true ifoverriderTypeVar's declared bound is an acceptable override ofoverriddenTypeVar's declared bound.
-
Field Details
-
overriderTree
The declaration of an overriding method. Or, it could be a method reference that is being passed to a method. -
isMethodReference
protected final boolean isMethodReferenceTrue ifoverriderTreeis a MEMBER_REFERENCE. -
overrider
The type of the overriding method. -
overriderType
The subtype that declares the overriding method. -
overridden
The type of the overridden method. -
overriddenType
The supertype that declares the overridden method. -
overriddenReturnType
The teturn type of the overridden method. -
overriderReturnType
The return type of the overriding method.
-
-
Constructor Details
-
OverrideChecker
public OverrideChecker(Tree overriderTree, AnnotatedTypeMirror.AnnotatedExecutableType overrider, AnnotatedTypeMirror overriderType, AnnotatedTypeMirror overriderReturnType, AnnotatedTypeMirror.AnnotatedExecutableType overridden, AnnotatedTypeMirror.AnnotatedDeclaredType overriddenType, AnnotatedTypeMirror overriddenReturnType) Create an OverrideChecker.Notice that the return types are passed in separately. This is to support some types of method references where the overrider's return type is not the appropriate type to check.
- Parameters:
overriderTree- the AST node of the overriding method or method referenceoverrider- the type of the overriding methodoverriderType- the type enclosing the overrider method, usually an AnnotatedDeclaredType; for Method References may be something elseoverriderReturnType- the return type of the overriding methodoverridden- the type of the overridden methodoverriddenType- the declared type enclosing the overridden methodoverriddenReturnType- the return type of the overridden method
-
-
Method Details
-
checkOverride
public boolean checkOverride()Perform the check.- Returns:
- true if the override is allowed
-
checkMemberReferenceReceivers
protected boolean checkMemberReferenceReceivers()Issue a "methodref.receiver.invalid" or "methodref.receiver.bound.invalid" error if the receiver for the method reference does not satify overriding rules.- Returns:
- true if the override is legal
-
checkReceiverOverride
protected boolean checkReceiverOverride()Issue an "override.receiver.invalid" error if the receiver override is not valid.- Returns:
- true if the override is legal
-
isParameterOverrideValid
protected boolean isParameterOverrideValid(AnnotatedTypeMirror capturedOverriddenParam, AnnotatedTypeMirror overriddenParam, AnnotatedTypeMirror overriderParam) Returns true ifoverriderParamis an acceptable override ofoverriddenParam(aftercapturedOverriddenParam, the capture-converted form ofoverriddenParam, has been computed).The default implementation requires contravariance:
capturedOverriddenParammust be a subtype ofoverriderParam, or -- as a fallback for corresponding type variables declared by the overriding and overridden methods themselves, where a direct subtype check can fail even though the override is valid --overriddenParammust be contained byoverriderParam. A checker that requires parameter invariance for overrides (both directions must be subtypes) should override this method to also require the reverse:typeHierarchy.isSubtype(overriderParam, capturedOverriddenParam), falling back totestTypevarContainment(overriderParam, overriddenParam)for the same reason the default implementation does in the forward direction.- Parameters:
capturedOverriddenParam- the capture-converted overridden parameter typeoverriddenParam- the (uncaptured) overridden parameter type, used by the type-variable containment fallbackoverriderParam- the overriding parameter type- Returns:
- true if the override is compatible for this parameter
-
isTypeParameterBoundOverrideValid
protected boolean isTypeParameterBoundOverrideValid(AnnotatedTypeMirror.AnnotatedTypeVariable overriddenTypeVar, AnnotatedTypeMirror.AnnotatedTypeVariable overriderTypeVar) Returns true ifoverriderTypeVar's declared bound is an acceptable override ofoverriddenTypeVar's declared bound."Declared bound" means both
AnnotatedTypeMirror.AnnotatedTypeVariable.getUpperBound()andAnnotatedTypeMirror.AnnotatedTypeVariable.getLowerBound(): ordinary Java syntax only lets a type parameter declaration write an upper bound (theextendsclause), but the Checker Framework additionally lets a checker's type system give meaning to the annotation written directly on the type variable itself (as in<@A T extends @B Object>), which applies to the lower bound -- see the manual section "Syntax for upper and lower bounds". An override of this method should compare whichever of the two bounds its type system attaches enforceable meaning to.The overriding method's body is type-checked once, generically, against its own declared bounds: it may consume a value of the type parameter as its upper bound, and produce one from its lower bound. Both must remain valid for every instantiation the overridden declaration permits, so the default implementation requires
overriderTypeVar's bound range to containoverriddenTypeVar's:overriddenTypeVar's upper bound must be a subtype ofoverriderTypeVar's (perBaseTypeVisitor.typeHierarchy), andoverriderTypeVar's lower bound must be a subtype ofoverriddenTypeVar's. This holds regardless of whether, or where, the type parameter is used in the method's parameter or return types.Ordinary Java places no constraint here beyond erasure compatibility, which javac itself already enforces (in particular, javac requires two overriding declarations' bounds to be identical up to erasure); this default only rejects an override when a checker's type system attaches enforceable meaning to a qualifier that differs between the two bounds.
- Parameters:
overriddenTypeVar- the corresponding type parameter of the overridden methodoverriderTypeVar- the type parameter of the overriding method- Returns:
- true if the override is compatible for this type parameter's bound
-
isReturnOverrideValid
protected boolean isReturnOverrideValid(AnnotatedTypeMirror requiredReturnType, AnnotatedTypeMirror actualReturnType) Returns true ifactualReturnTypeis an acceptable override ofrequiredReturnType.The default implementation requires covariance:
actualReturnTypemust be a subtype ofrequiredReturnType, or -- as a fallback for corresponding type variables declared by the overriding and overridden methods themselves, where a direct subtype check can fail even though the override is valid --actualReturnTypemust containrequiredReturnType: the overriding occurrence's bound range must contain the overridden one's, the same directionisTypeParameterBoundOverrideValid(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable, org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)requires for the type parameter's own declared bound, and for the same reason -- an override may widen a return occurrence's upper bound, because what a body can manufacture as a fresh value of the type variable is governed by the occurrence's lower bound, which containment does not let the override widen. Widening the upper bound only makes the body more conservative about values it was handed.- Parameters:
requiredReturnType- the return type of the overridden methodactualReturnType- the return type of the overriding method- Returns:
- true if the override is compatible for the return type
-