Created a class dynamically but could not use it to create typed
objects
I know I am pushing things a bit but I need to do things this way
I need to create some classes (which will be mostly used as DTO and
DAO) and I used ASM
The thing is that then I cannot do such things as:(this is pseudo
code I have tried many different things to no avail):
Class K = _.getKlass();
K0 Obj = (K0)K.newInstance
ArrayList<K0> ALK0 = new ArrayList<K0>();
ALK0.add(Obj);
Even though I know he name of the class is that I created saved and
loaded in the same context using the URLClassLoader is "K0"
I know the created classes themselves are fine because the JVM (javap
and java) did not complain at all and I actually used the classes by
starting other JVM and reaching out for them
Again I need to use the created classes to create and used typed
objects right within the same context that they were created, without
having to restart java
How could you achieve such a thing, either in a plain way or hacking
my way through?
In case this can not be done (well, this may be why some frameworks
such as JAXB use precompilation of type classes), please let me know
what am I missing here
Thanks
lbrtchx
Here is a short code sample of what I am trying to achieve:
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
import java.util.*;
import java.io.*;
import java.net.*;
import java.security.*;
import org.objectweb.asm.*;
import static org.objectweb.asm.Opcodes.*;
// __
class ASMKW02 extends ClassLoader{
private File FlDir;
private String aKName;
private String[][] aFldDefs;
// __ more sanity checks
ASMKW02(File FlDir, String aKName, String[][] aFldDefs){
if(!FlDir.exists()){ FlDir.mkdirs(); }
this.FlDir = FlDir;
this.aKName = aKName;
this.aFldDefs = aFldDefs;
}
// __
public Class getK(){
Class K = null;
// __
ClassWriter KW = new ClassWriter(ClassWriter.COMPUTE_MAXS);
// __ class def.
KW.visit(V1_6, ACC_PUBLIC, aKName, null, "java/lang/Object", null);
// __ default constructor
MethodVisitor DfltCtor = KW.visitMethod(ACC_PUBLIC, "<init>", "()V",
null, null);
if(DfltCtor != null) {
DfltCtor.visitCode();
DfltCtor.visitVarInsn(ALOAD, 0);
DfltCtor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object",
"<init>", "()V");
DfltCtor.visitInsn(RETURN);
DfltCtor.visitMaxs(1, 1);
DfltCtor.visitEnd();
}
// __ K Fields
FieldVisitor FV;
for(int i = 0; (i < aFldDefs.length); ++i){
FV = KW.visitField(ACC_PUBLIC, aFldDefs[i][0], aFldDefs[i][1],
null, null);
if(FV != null){ FV.visitEnd(); }
else{} // . . .
}
// __
KW.visitEnd();
// __
byte[] bKAr = KW.toByteArray();
// __ Checking if class has been already loaded by JVM . . .
File Fl = new File(FlDir, aKName + ".class");
try{
ClassLoader KLCtxt =
Thread.currentThread().getContextClassLoader();
URL U = (Fl.toURI()).toURL();
URLClassLoader UKL = new URLClassLoader(new URL[]{U}, KLCtxt);
try{ K = Class.forName(aKName, true, UKL); }
catch(ClassNotFoundException KNFX)
{ KNFX.printStackTrace(System.err); }
if(K == null){
// __
K = defineClass(aKName, bKAr, 0, bKAr.length);
if(K != null){
System.out.println("// __ Class K: |" + K + "|");
try{
FileOutputStream FOS = new FileOutputStream(Fl);
FOS.write(bKAr);
FOS.close();
}catch(FileNotFoundException FNFX)
{ FNFX.printStackTrace(System.err); }
catch(IOException IOX){ IOX.printStackTrace(System.err); }
}
}
}catch(MalformedURLException MFX)
{ MFX.printStackTrace(System.err); }
// __
return(K);
}
}
// __
public class ASMKW02Test{
public static void main(String[] aArgs){
// __
String aKLoadDir = "/media/sda2/prjx/java/Ks";
File FlDir = new File(aKLoadDir);
// __ K Name
String aKName = "KNm00";
// __ K Fields
String[][] aFldDefs = new String[][]{
{"bByte", "B"}, // byte
{"cChar", "C"}, // char
{"dDouble", "D"}, // double
{"fFloat", "F"}, // float
{"iInt", "I"}, // int
{"lLong", "J"}, // long
{"sShort", "S"}, // short
{"Is", "Z"}, // boolean
{"aNm", "Ljava/lang/String;"},
{"ALS", "Ljava/util/ArrayList<String>;"},
{"HMSI", "Ljava/util/Hasmap<String, Integer>;"}
};
// __
ASMKW02 ASMK = new ASMKW02(FlDir, aKName, aFldDefs);
Class K = ASMK.getK();
System.out.println("// __ Class K: |" + K + "|");
try{
K.newInstance();
}catch(InstantiationException InstX)
{ InstX.printStackTrace(System.err); }
catch(IllegalAccessException IlglAxX)
{ IlglAxX.printStackTrace(System.err); }
}
}
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~