I need to get a List of generic lazy loaders that will instantiate instances of their inferred type
Sorry for the cryptic title, but this is difficult to explain. The general rule is that I need a lazy loader that will give me N instances of a bound wildcard type. I'm calling the lazy loader a storage unit.
import java.util.ArrayList;
import java.util.List;
public class StorageUnit<T extends MyInterface> implements Storable<T> {
private int count;
public StorageUnit(int count) {
this.count = count;
}
private List<T> storables = new ArrayList<T>();
public List<T> getInstances(Class<T> c) {
try {
if (storables.size() == 0) {
for (int i = 0; i < count; i++) {
storables.add(c.newInstance());
}
} else {
return storables;
}
}catch (IllegalAccessException illegalAccessException) {
illegalAccessException.printStackTrace();
} catch (InstantiationException instantiationException) {
instantiationException.printStackTrace();
}
return storables;
}
}
Elsewhere in my application I have another class that has a reference to several of these storage units. I need to get instances of the storage unit type, and then I will do something with that type.
import java.util.ArrayList;
import java.util.List;
public class MyStorageUnitContainer {
private List<StorageUnit<? extends MyInterface>> storageUnits;
public MyStorageUnitContainer(List<StorageUnit<? extends MyInterface>> storageUnits) {
this.storageUnits = storageUnits;
}
public List<StorageUnit<? extends MyInterface>> getInstances() {
List<StorageUnit<? extends MyInterface>> instances = new ArrayList<StorageUnit<? extends MyInterface>>();
for (StorageUnit<? extends MyInterface> storageUnit : storageUnits) {
storageUnit.getInstances(/* I can't get the bound wildcard... */);
// Now loop through those instances and do something with them...
}
return instances;
}
}
That code sucks, so the best analogy I can think of is an actual storage unit container. That storage unit container has several individual storage units (think boxes). Each one of those boxes contains items of a certain type (think baseball cards). I know that a box contains 100 baseball cards开发者_如何学运维, but until I open the box I don't know anything about the details of each baseball card. I'm basically trying to treat each box as a lazy loader. Opening the box loads N implementations if they don't exist already.
Paulo is correct, and (also as per Paulo's answer), I often just pass a class object into a constructor to get around this problem. It allows the getInstances()
method to appear as you would like it - ie without parameters. Internally, the instance keeps a reference to the generic class so it can call newInstance()
on it.
This code illustrates this using your example. I have tested this an it executes OK.
import java.util.ArrayList;
import java.util.List;
public class Sandbox
{
static interface MyInterface
{
}
static interface Storable<T>
{
List<T> getInstances();
};
static abstract class MyStorableImpl implements MyInterface
{
@Override
public String toString()
{
return "I'm a " + getClass() + " with hashcode " + hashCode();
}
}
static class MyStorable1 extends MyStorableImpl
{
}
static class MyStorable2 extends MyStorableImpl
{
}
static class StorageUnit<T extends MyInterface> implements Storable<T>
{
private final int count;
private final Class<T> clazz;
public StorageUnit(Class<T> clazz, int count)
{
this.count = count;
this.clazz = clazz;
}
private List<T> storables = new ArrayList<T>();
public List<T> getInstances()
{
try
{
if (storables.size() == 0)
{
for (int i = 0; i < count; i++)
{
storables.add(clazz.newInstance());
}
}
else
{
return storables;
}
}
catch (IllegalAccessException illegalAccessException)
{
illegalAccessException.printStackTrace();
}
catch (InstantiationException instantiationException)
{
instantiationException.printStackTrace();
}
return storables;
}
}
static class MyStorageUnitContainer
{
private List<StorageUnit<? extends MyInterface>> storageUnits;
public MyStorageUnitContainer(List<StorageUnit<? extends MyInterface>> storageUnits)
{
this.storageUnits = storageUnits;
}
public List<MyInterface> getAllInstances()
{
List<MyInterface> instances = new ArrayList<MyInterface>();
for (StorageUnit<? extends MyInterface> storageUnit : storageUnits)
{
List<? extends MyInterface> list = storageUnit.getInstances();
instances.addAll(list);
}
return instances;
}
}
public static void main(String[] args)
{
StorageUnit<? extends MyInterface> box1 = new StorageUnit<MyStorable1>(MyStorable1.class, 2);
StorageUnit<? extends MyInterface> box2 = new StorageUnit<MyStorable2>(MyStorable2.class, 3);
List<StorageUnit<? extends MyInterface>> boxes = new ArrayList<Sandbox.StorageUnit<? extends MyInterface>>();
boxes.add(box1);
boxes.add(box2);
MyStorageUnitContainer container = new MyStorageUnitContainer(boxes);
List<MyInterface> allInstances = container.getAllInstances();
for (MyInterface myInterface : allInstances)
{
System.out.println(myInterface.toString());
}
}
}
With Java Generics you can't get from a type variable (or even less from a wildcard) to an actual class object.
The reason is the way generics are implemented: by type erasure. This means that actually on run-time your generic types are not there anymore, they are erased to raw types. Only the compiler checks that you are using the right types at the right place.
In your case, the StorageUnit<T>
objects do not contain any information about the T used here, if you don't give them a class object of the right type. They also all have the same class object.
So, the best bet here would be to give the StorageUnit objects a class object of their parameter class in the constructor, then the getInstances()
method would not need to take it. Of course, this only shifts the problem on having to have a class object to another location, but somewhere it is necessary.
精彩评论