how to create a dynamic class
843811May 18 2003 — edited May 19 2003Hiya,
I want to create classes dynamically. I know that this can be done by extending ClassLoader and defineClass method. My problem is i dont know what goes in byte[] coz my program needs to get the class name, attributes and methods from the user.
For example something like visual modeler that loads up the UI than lets the user to create the classes, attributes and methods.
The generic class called gentity is as follows:
public class Gentity {
private String gname;
public Gentity(String mgname) {
gname=mgname;
}
public void setGname(String mgname)
{
gname=mgname;
}
public String getGname()
{return(gname);
}
}
This class will then be used in my main class which is as follows:
public class GentityClassLoader extends ClassLoader
{
Gentity customer=new Gentity("HSBC");
public static void main(String argv[])
{
byte b[]=new byte[1];
GentityClassLoader obj = new GentityClassLoader();
try{
// this is my problem here, i dont know what goes in bytes
Class cls = obj.defineClass(obj.customer.getGname(),b,0,b.length);
Class x=obj.loadClass(obj.obj.getGname());
obj.resolveClass(x);
System.out.println(x.toString()+"asdf");
}catch(Exception e)
{
System.out.println("Error");
}
}
}
Cheers,
Deepak