原始类型是没有通用信息的类型.以下是如何打败方法的类型安全性:
Favorites favorites = new Favorites();
favorites.putFavorite((Class)Integer.class, "foo"); // no compile error
而这不会编译:
favorites.putFavorite(Integer.class, "foo"); // compile error
因为参数的类型是Class(而不是Class< T>),所以不能确定通用方法参数T并且为该调用关闭类型推断.就好像调用代码的代码是pre-generics,java向后兼容(通过忽略泛型).
这是你如何防范这个问题:
public void putFavorite(Class type, T instance) {
if (type == null)
throw new NullPointerException("Type is null");
if (!type.isInstance(instance)) // add check for instance type
throw new IllegalArgumentException("Class/instance mismatch");
favorites.put(type, instance);
}
或者更残酷(因为你不能提供有关错误信息的信息),只需尝试演员:
public void putFavorite(Class type, T instance) {
if (type == null)
throw new NullPointerException("Type is null");
favorites.put(type, type.cast(instance)); // will throw ClassCastException
}
但是,当恶意代码试图造成损害时,这只会在运行时发现问题,但它仍然比在其他客户端尝试使用狡猾的实例时在使用时提起问题更好.