I'd like extend the class, get a class literal, use it as a type parameter, or cast to it.
I already have a workaround, but I'm still curious.
Currently I can define a class at runtime with ASM, create an instance with Object type and invoke its methods with reflection, but I don't know how to use it as a class type.
Granted, the compiler doesn't know about classes defined at runtime, but in this case I know about it, and I think that I see it hidden in the ASMified .java file (example below). How can convince the compiler that it actually does know the class type at compile time?
Thanks for taking a look,
-Julian
Example of an ASMified class B: bytecode-generating code that spoofs class B{String hello = "Hello B!"}:
import java.util.*;
import org.objectweb.asm.*;
//import org.objectweb.asm.attrs.*;
public class BDump implements Opcodes {
public static byte[] dump () throws Exception {
ClassWriter cw = new ClassWriter(0);
FieldVisitor fv;
MethodVisitor mv;
AnnotationVisitor av0;
cw.visit(V1_6, ACC_SUPER, "B", null, "java/lang/Object", null);
{
fv = cw.visitField(0, "hello", "Ljava/lang/String;", null, null);
fv.visitEnd();
}
{
mv = cw.visitMethod(0, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
mv.visitVarInsn(ALOAD, 0);
mv.visitLdcInsn("Hello B!");
mv.visitFieldInsn(PUTFIELD, "B", "hello", "Ljava/lang/String;");
mv.visitInsn(RETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
}
cw.visitEnd();
return cw.toByteArray();
}
}