这个问题类似于Factory和generics,可能有相同的答案,但是有所不同。我有一个通用基类,该基类将由完全独立的JAR中的类扩展。所说的JAR应该能够被插入并替换而无需更改任何其他代码(代码在命令上执行)。
基类:
public abstract class ThingBase<A> {
public abstract A doThing();
public abstract Thing getThingType();
// This might be the wrong way to do this, and I elaborate below.
public final SortedMap<Long, A> getCollectionContainer() {
return new TreeMap<Long, A>();
}
}
我有一个枚举,我将为每个条目创建不同的事物扩展。
public enum Thing { T1, T2, T3 }
工厂创建此类对象:
public class ThingFactory {
private static final Map<Thing, Class<? extends ThingInterface<?>>> thingMap;
static{//populate map by finding all extensions of base class}
public static ThingBase<?> getThing(Thing thing) {
try {
Class<? extends ThingBase<?>> thingImpl = thingMap.get(thing);
if (thingImpl != null) {
return thingImpl.newInstance();
} else {
return null;
}
} catch (InstantiationException e) {
return null;
} catch (IllegalAccessException e) {
return null;
}
}
}
我的问题是下面的代码中未检查的和原始类型的警告(注意不在上面,上面的代码编译就很好)。在以下用法中,代码成功执行,但是出现未经检查的类型警告。
public class ThingAccumulator {
// Raw Type
private final ThingBase myThing;
// Raw Type
private SortedMap myMap;
public ThingAccumulator(Thing thing) {
myThing = ThingFactory.getThing(thing);
myMap = myThing.getCollectionData();
//Type safety: The method put(Object, Object) belongs to the raw type Map. References to generic type Map<K,V> should be parameterized
myMap.put(System.currentTimeMillis(), myThing.doThing());
}
}
放<?ThingBase上的>和SortedMap字段上的<Long,?>都抱怨?s不同:
Map类型的put(Long,capture#17-of?)方法不适用于参数(long,capture#18-of?)
现在这里的主要问题似乎是编译器无法假定?和?是相同的,这很公平,但是到目前为止我唯一想出的解决方案是将类型放入保持类中,这不是一个选择,因为只有实现才知道数据类型:/
是否有人对如何消除这些警告有任何想法?有没有更好的方法可以做到这一点?
谢谢,
SDY
您可以在上添加一个通用类型参数,从而ThingAccumulator
使用它来移动内容,以便Java抱怨的唯一代码是一条可以证明确实是类型安全的简单行,然后抑制该警告,因为您知道这不是问题。
赋予类型参数以ThingAccumulator
使其一起使用时可以对其字段进行类型检查。您可以提出“但类型是什么?”的问题。通过将ThingBase<A>
实例作为参数而不是Thing
枚举值作为参数在构造函数之外。然后,静态工厂方法可以处理对象构造:
public class ThingAccumulator<A> {
private final ThingBase<A> myThing;
private SortedMap<Long, A> myMap;
public static ThingAccumulator<?> create(Thing thing) {
return new ThingAccumulator(ThingFactory.getThing(thing));
}
protected ThingAccumulator(ThingBase<A> thing) {
myThing = thing;
myMap = myThing.getCollectionData();
myMap.put(System.currentTimeMillis(), myThing.doThing());
}
}
我希望Java会喜欢这个create()
功能。逻辑可能是这样的:
ThingFactory.getThing()
返回ThingBase<?>
。这就像是ThingBase<X>
一些未知的东西X
。仅仅因为我们不知道它并不意味着它不存在。new ThingAccumulator()
可以ThingBase<X>
任意取一个,X
并返回一个ThingAccumulator<X>
。ThingAccumulator<?>
带有未知类型参数的实例。类型安全性尚未破坏。但是,Java不喜欢它(至少在我的Java 1.7.0版本中)。但是,如果我们喜欢它,我们可以取消警告:
@SuppressWarnings("unchecked")
public static ThingAccumulator<?> create(Thing thing) {
return new ThingAccumulator(ThingFactory.getThing(thing));
}
而且由于所有其余代码(到目前为止……)都是类型安全的,因此不再发出警告。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句