dinamically creating a class (then an object)
you simply create an object if you know the class, K, by going
ClassType IObj = (ClassType)K.newInstance();
Then you can do what ever you want with the created object, for
example introspecting the fields:
public String getObjFieldsInfo(ClassType IObj){
StringBuffer aB = new StringBuffer();
Field[] Flds[] = (IObj.getClass()).getDeclaredFields();
for (int i = 0; i < Flds.length; i++) {
System.out.println("// __ Flds[" + i + "].getName(): |" +
Flds[i].getName() + "|");
System.out.println("// __ Flds[" + i + "].getType(): |" +
Flds[i].getType() + "|");
System.out.println("// __ Modifier.toString(Flds[" + i +
"].getModifiers()): |" + Modifier.toString(Flds[i].getModifiers()));
System.out.println("~");
}
Now, say you know you will need a certain class with some primitive
and some other types, say, an int, a long, a String and a
java.awt.Rectangle; how could you:
1) actually created such a class programmatically (other than going
the monkey way and creating it by adding lines of text, saving the
file as ".java" and trying to compile it)
2) loading it with the same classloader that is running the actual
code you are using
so that you can then create objects as I outlined above
THanks
lbrtchx