I have the following class hierarchy:
public abstract class MyParentObject {}
public abstract class MyObject<T> extends MyParentObject {}
public abstract class MyParentContainer<T extends MyParentObject> {
private Class<T> valueType;
protected MyParentContainer(Class<T> valueType) {
this.valueType = valueType;
}
}
public abstract class MyObjectContainer<T extends MyObject<?>> extends MyParentContainer<T> {
protected MyObjectContainer(Class<T> valueType) {
super(valueType);
}
}
This works fine so far. There are several subclasses of MyObject
and several container classes having some of those subclasses as valueType
, e.g.
public class IntObject extends MyObject<Integer> {}
public class IntContainer extends MyObjectContainer<IntObject> {
public IntContainer() {
super(IntObject.class);
}
}
Now I need to implement a container though that works with a mixed set of subclasses of MyObject
.
public class AllObjectContainer extends MyObjectContainer<MyObject<?>> {
public AllObjectContainer() {
super(MyObject.class); // compile error here
}
}
This does not compile, because "The constructor MyObjectContainer<MyObject>(Class) is undefined". Casting `MyObject.class` to `Class>` is not possible.
To solve this, I could use raw types:
- Change
public class AllObjectContainer extends MyObjectContainer<MyObject<?>>
topublic class AllObjectContainer extends MyObjectContainer<MyObject>
This has consequences though because of type erasure, so I do not like this solution.
Alternatively I could enable casting by
- Change
protected MyObjectContainer(Class<T> valueType) {
toprotected MyObjectContainer(Class<? extends T> valueType) {
(add? extends
) - Change
super(MyObject.class)
tosuper((Class<? extends MyObject<?>>) MyObject.class)
This method works, but the cast is marked as unchecked and changing the MyMiddleContainer
-Constructor has potential side effects on other classes that extend it.
Are there any better solutions?
Aucun commentaire:
Enregistrer un commentaire