Summary
When a field, method return type, or parameter is declared with @ReceiverDependentQual (or another receiver-dependent / polymorphic qualifier) in a context where the enclosing class or receiver is invariant (e.g., the class declaration is annotated with a fixed concrete qualifier like @A class C or @A final class C), the receiver-dependent qualifier will always adapt to that fixed qualifier.
Allowing @ReceiverDependentQual in such contexts can be misleading to developers, as it suggests the member varies dynamically with the receiver when it is effectively constant.
Example
@A class InvariantContainer {
// Misleading: field always viewpoint-adapts to @A Object for instances of InvariantContainer
@ReceiverDependentQual Object field;
// Misleading: method return type and parameter always adapt to @A
@ReceiverDependentQual Object get() { return field; }
void set(@ReceiverDependentQual Object o) { this.field = o; }
}
Proposed Enhancement
Consider issuing a warning (e.g., under -AwarnRedundantAnnotations or a viewpoint-specific lint):
- Static members: When
@ReceiverDependentQual is placed on static fields or static methods (where there is no receiver to adapt against).
- Fixed final classes: When
@ReceiverDependentQual is used in a final class whose qualifier is fixed at class declaration time (e.g. @A final class C).
- Monomorphic/Invariant class hierarchies: When a class fixes its qualifier and cannot be subclassed with different qualifiers.
Summary
When a field, method return type, or parameter is declared with
@ReceiverDependentQual(or another receiver-dependent / polymorphic qualifier) in a context where the enclosing class or receiver is invariant (e.g., the class declaration is annotated with a fixed concrete qualifier like@A class Cor@A final class C), the receiver-dependent qualifier will always adapt to that fixed qualifier.Allowing
@ReceiverDependentQualin such contexts can be misleading to developers, as it suggests the member varies dynamically with the receiver when it is effectively constant.Example
Proposed Enhancement
Consider issuing a warning (e.g., under
-AwarnRedundantAnnotationsor a viewpoint-specific lint):@ReceiverDependentQualis placed onstaticfields orstaticmethods (where there is no receiver to adapt against).@ReceiverDependentQualis used in afinalclass whose qualifier is fixed at class declaration time (e.g.@A final class C).