根据com.google.errorprone.annotations.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:
这里是 com.google.common.cache.Cache 的使用方法:
public interface Cache<K, V> {
V getIfPresent(@CompatibleWith("K") Object key);
V get(K key, Callable<? extends V> loader) throws ExecutionException;
...
拥有
@CompatibleWith("E") Object
作为参数类型相比于E
有什么好处?为什么在缓存的getIfPresent
方法中使用了@CompatibleWith
注解,但在get
方法中没有使用?请注意保留HTML标签。