JAVA中map中键的类型_java – Map的参数化类型键

原始类型是没有通用信息的类型.以下是如何打败方法的类型安全性:

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

}

但是,当恶意代码试图造成损害时,这只会在运行时发现问题,但它仍然比在其他客户端尝试使用狡猾的实例时在使用时提起问题更好.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值