Annotation Type CompatibleWith


@Documented
@Retention(CLASS)
@Target(PARAMETER)
public @interface CompatibleWith
Declares that a parameter to a method must be "compatible with" one of the type parameters in the method's enclosing class, or on the method itself. "Compatible with" means that there can exist a "reference casting conversion" from one type to the other (JLS 5.5.1).

For example, Collection.contains(java.lang.Object) would be annotated as follows:


 interface Collection<E> {
   boolean contains(@CompatibleWith("E") Object o);
 }
 

To indicate that invocations of Collection.contains(java.lang.Object) must be passed an argument whose type is compatible with the generic type argument of the Collection instance:


 Collection<String> stringCollection = ...;
 boolean shouldBeFalse = stringCollection.contains(42); // BUG! int isn't compatible with String
 

Note: currently, this annotation can't be used if the method overrides another method that has @CompatibleWith already present.

  • Required Element Summary

    Required Elements
    Modifier and Type Required Element Description
    String value  
  • Element Details