Class BaseTypeVisitor.OverrideChecker
- Direct Known Subclasses:
GuiEffectVisitor.GuiEffectOverrideChecker,KeyForVisitor.KeyForOverrideChecker
- 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.protected final @Nullable Map<TypeVariable, AnnotatedTypeMirror> A mapping from each type variable of the overriding method to the corresponding type variable of the overridden method; null if the two methods have no corresponding type variables, and also null for every method reference. -
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 AnnotatedTypeMirrorExpresses a type from the overriding method's signature in terms of the overridden method's type variables, by replacing each use of a type variable of the overriding method by the corresponding type variable of the overridden method.booleanPerform the check.protected booleanIssue an "override.receiver" error if the receiver override is not valid.protected booleanIssue an error if a type parameter of the overriding method has bounds that do not contain the bounds of the corresponding type parameter of the overridden method.protected booleancheckTypeParameterLowerBound(int index) Issue an error if the lower bound of the overriding method's type parameter at the given index is not a subtype of the lower bound of the overridden method's type parameter.protected booleancheckTypeParameterUpperBound(int index) Issue an error if the upper bound of the overriding method's type parameter at the given index is not a supertype of the upper bound of the overridden method's type parameter.
-
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. -
typeVarMapping
A mapping from each type variable of the overriding method to the corresponding type variable of the overridden method; null if the two methods have no corresponding type variables, and also null for every method reference.Substituting according to this mapping expresses a type from the overriding method's signature in terms of the overridden method's type variables, which is what makes the two signatures comparable. See
adaptToOverridden(org.checkerframework.framework.type.AnnotatedTypeMirror).This mapping is null for a method reference. No mapping is possible: the overrider is the invocation type of the method reference's compile-time declaration, whose type variables, when any remain, do not correspond to the functional interface method's type variables.
-
-
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
-
adaptToOverridden
Expresses a type from the overriding method's signature in terms of the overridden method's type variables, by replacing each use of a type variable of the overriding method by the corresponding type variable of the overridden method.Both methods are instantiated with the same type arguments at every call site, so this substitution loses no information. It permits the ordinary subtyping rules for two uses of the same type variable to be applied to the two methods' signatures.
- Parameters:
type- a type that may contain uses of the overriding method's type variables- Returns:
type, expressed in terms of the overridden method's type variables
-
checkOverride
public boolean checkOverride()Perform the check.- Returns:
- true if the override is allowed
-
checkReceiverOverride
protected boolean checkReceiverOverride()Issue an "override.receiver" error if the receiver override is not valid.- Returns:
- true if the override is legal
-
checkTypeParameterBounds
protected boolean checkTypeParameterBounds()Issue an error if a type parameter of the overriding method has bounds that do not contain the bounds of the corresponding type parameter of the overridden method.Nothing is checked if the overridden method is unchecked code that receives conservative defaults (see
-AuseConservativeDefaultsForUncheckedCode). Its type parameters then have the widest possible bounds, which are conservative for its clients, but which no overriding method could contain.- Returns:
- true if the type parameter bounds are correct
-
checkTypeParameterUpperBound
protected boolean checkTypeParameterUpperBound(int index) Issue an error if the upper bound of the overriding method's type parameter at the given index is not a supertype of the upper bound of the overridden method's type parameter.- Parameters:
index- the index of the type parameter- Returns:
- true if the upper bound is correct
-
checkTypeParameterLowerBound
protected boolean checkTypeParameterLowerBound(int index) Issue an error if the lower bound of the overriding method's type parameter at the given index is not a subtype of the lower bound of the overridden method's type parameter.- Parameters:
index- the index of the type parameter- Returns:
- true if the lower bound is correct
-