This is the mail archive of the kawa@sourceware.cygnus.com mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Dynamic classes



You can generate new classes using gnu.bytecode.*

For instance, your example could be done with:


import gnu.bytecode.*;

ClassType c = new ClassType("my.package.ClassName");
c.addField("attr1", Type.getType("java.lang.Integer"));
c.addField("attr2", Type.string_type);
c.addField("attr3", Type.boolean_type);

c.writeToFile();

This will generate a my/package/ClassName.class file, that is what javac
would produce for

> public class {
>    public Integer attr1;
>    public String attr2;
>    public Boolean attr3;
> }
>

There are more possibilities, like setting the fields attributes (public,
...). Have a look at the javadoc for gnu.bytecode.ClassType

It might be possible to load the class without writing to a file first.
Maybe using a custom class loader.

Daniel



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]