Class BaseTypeVisitor.OverrideChecker

java.lang.Object
org.checkerframework.common.basetype.BaseTypeVisitor.OverrideChecker
Direct Known Subclasses:
GuiEffectVisitor.GuiEffectOverrideChecker
Enclosing class:
BaseTypeVisitor<Factory extends GenericAnnotatedTypeFactory<?,?,?,?>>

public class BaseTypeVisitor.OverrideChecker extends Object
Class to perform method override and method reference checks.

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 Details

  • 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 reference
      overrider - the type of the overriding method
      overriderType - the type enclosing the overrider method, usually an AnnotatedDeclaredType; for Method References may be something else
      overriderReturnType - the return type of the overriding method
      overridden - the type of the overridden method
      overriddenType - the declared type enclosing the overridden method
      overriddenReturnType - 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 if overriderParam is an acceptable override of overriddenParam (after capturedOverriddenParam, the capture-converted form of overriddenParam, has been computed).

      The default implementation requires contravariance: capturedOverriddenParam must be a subtype of overriderParam, 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 -- overriddenParam must be contained by overriderParam. 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 to testTypevarContainment(overriderParam, overriddenParam) for the same reason the default implementation does in the forward direction.

      Parameters:
      capturedOverriddenParam - the capture-converted overridden parameter type
      overriddenParam - the (uncaptured) overridden parameter type, used by the type-variable containment fallback
      overriderParam - 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 if overriderTypeVar's declared bound is an acceptable override of overriddenTypeVar's declared bound.

      "Declared bound" means both AnnotatedTypeMirror.AnnotatedTypeVariable.getUpperBound() and AnnotatedTypeMirror.AnnotatedTypeVariable.getLowerBound(): ordinary Java syntax only lets a type parameter declaration write an upper bound (the extends clause), 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 contain overriddenTypeVar's: overriddenTypeVar's upper bound must be a subtype of overriderTypeVar's (per BaseTypeVisitor.typeHierarchy), and overriderTypeVar's lower bound must be a subtype of overriddenTypeVar'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 method
      overriderTypeVar - 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 if actualReturnType is an acceptable override of requiredReturnType.

      The default implementation requires covariance: actualReturnType must be a subtype of requiredReturnType, 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 -- actualReturnType must contain requiredReturnType: the overriding occurrence's bound range must contain the overridden one's, the same direction isTypeParameterBoundOverrideValid(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 method
      actualReturnType - the return type of the overriding method
      Returns:
      true if the override is compatible for the return type