Please forgive me if you already know most of the answer: it's hard to make an assumption on your level.
The reason of the problem is type erasure, as you know already. In order to get rid of type erasure, Guice uses a trick with concrete ancestors, as shown below:
class Trick<T> {
T t;
}
public class GenericTest {
public static void main(String[] args) {
Trick<Set<String>> trick = new Trick<Set<String>>() {
};
// Prints "class org.acm.afilippov.GenericTest$1"
System.out.println(trick.getClass());
// Prints "org.acm.afilippov.Trick<java.util.Set<java.lang.String>>"
System.out.println(trick.getClass().getGenericSuperclass());
}
}
Point is, when you create a class which extends a generic superclass and explicitly specifies the type parameter, you will usually need to write metods which accept that very specific type, and these methods' signatures cannot be erased. In this case we have no problem discussed in FAQ, but compiler saves the type information, anyway: the users of your class will need to know the exact types in order to use the methods.
Now your version does not have a concrete class inherited from TypeLiteral<Set<YourSpecificType>>
, it only has TypeLiteral<Set<T>>
—and that's where it all fails.
Changing my little example, that would be:
public class GenericTest {
public static void main(String[] args) {
tryMe(String.class);
}
private static <T> void tryMe(Class<T> clazz) {
Trick<Set<T>> trick = new Trick<Set<T>>() {
};
// Prints "class org.acm.afilippov.GenericTest$1"
System.out.println(trick.getClass());
// Prints "org.acm.afilippov.Trick<java.util.Set<T>>"
System.out.println(trick.getClass().getGenericSuperclass());
}
}
As you can see, our GenericTest$1
is not concrete any more: it still have a type parameter, and its concrete value, here String
, is lost during the compilation.
You can of course avoid that, but in order to do so, you need to create a class with a specific type parameter used for inheritance—so that Guice would be able to work out the details. Wait a bit, I'll try to come up with an example.
Update: it turned out to be a VERY long bit. So here's an updated version for you:
public class GenericTest {
public static void main(String[] args) throws Exception {
tryMe(String.class);
}
private static <T> void tryMe(Class<T> clazz) throws IllegalAccessException, InstantiationException {
Class c = loadClass("org.acm.afilippov.ASMTrick", generateClass(clazz));
Trick<Set<T>> trick = (Trick<Set<T>>) c.newInstance();
// Prints "class org.acm.afilippov.ASMTrick"
System.out.println(trick.getClass());
// Prints "org.acm.afilippov.Trick<java.util.Set<java.lang.String>>"
System.out.println(trick.getClass().getGenericSuperclass());
}
private static byte[] generateClass(Class<?> element) {
ClassWriter cw = new ClassWriter(0);
MethodVisitor mv;
cw.visit(V1_6, ACC_FINAL + ACC_SUPER, "org/acm/afilippov/ASMTrick",
"Lorg/acm/afilippov/Trick<Ljava/util/Set<L" + element.getName().replaceAll("\\.", "/") + ";>;>;",
"org/acm/afilippov/Trick", null);
{
mv = cw.visitMethod(0, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "org/acm/afilippov/Trick", "<init>", "()V");
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
cw.visitEnd();
return cw.toByteArray();
}
private static Class loadClass(String className, byte[] b) {
//override classDefine (as it is protected) and define the class.
Class clazz = null;
try {
ClassLoader loader = ClassLoader.getSystemClassLoader();
Class cls = Class.forName("java.lang.ClassLoader");
java.lang.reflect.Method method =
cls.getDeclaredMethod("defineClass", new Class[]{String.class, byte[].class, int.class, int.class});
// protected method invocaton
method.setAccessible(true);
try {
Object[] args = new Object[]{className, b, new Integer(0), new Integer(b.length)};
clazz = (Class) method.invoke(loader, args);
} finally {
method.setAccessible(false);
}
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
return clazz;
}
}
As you can see, the type information is now preserved. I believe this approach is not used because it's way too painful even for this draft.