This is the mail archive of the
kawa@sourceware.cygnus.com
mailing list for the Kawa project.
Re: Dynamic classes
- To: <chris at bitmead dot com>
- Subject: Re: Dynamic classes
- From: "Daniel Bonniot" <bonniot at cma dot ensmp dot fr>
- Date: Tue, 13 Jun 2000 13:49:18 +0200
- Cc: <kawa at sourceware dot cygnus dot com>, "Chris Bitmead" <chris at bitmead dot com>
- Organization: d.bonniot@mail.dotcom.fr
- References: <3945D1D9.2334A1FC@nimrod.itg.telecom.com.au>
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