我有一些通用代码,我无法弄清楚如何合法地防止收到来自的警告;我目前正在使用@suppresswarnings(“unchecked”),因为在没有警告的情况下,强制转换通用类型似乎无法完成。
我怎样才能去掉注释?
我拥有的是:
public MyObject(SharedContext ctx) {
super(ctx); // set protected field 'context'
...
context.set("Input Fields" ,Collections.synchronizedMap(new TreeMap>(String.CASE_INSENSITIVE_ORDER)));
context.set("Output Fields" ,Collections.synchronizedMap(new TreeMap (String.CASE_INSENSITIVE_ORDER)));
context.set("Event Registry",new EventRegistry(log) );
}
@SuppressWarnings("unchecked")
protected void startup() {
inputFields =(Map>)context.get("Input Fields" ,null);
outputFields =(Map )context.get("Output Fields" ,null);
eventRegistry =(EventRegistry )context.get("Event Registry",null);
...
}
受保护的变量上下文是类型
SharedContext
.
如果没有注释,编译器将发出警告:
...\MyClass.java:94: warning: [unchecked] unchecked cast
found : java.lang.Object
required: java.util.Map>
inputFields =(Map>)context.get("Input Fields" ,null);
^
...\MyClass.java:95: warning: [unchecked] unchecked cast
found : java.lang.Object
required: java.util.Map
outputFields =(Map )context.get("Output Fields" ,null);