Java Reference
In-Depth Information
Instantiate
CLEmitter
In order to create a class, one must create an instance of
CLEmitter
as the first step in
the process. All subsequent steps involve sending an appropriate message to that instance.
Each instance corresponds to a single class.
To instantiate a
CLEmitter
, one simply invokes its constructor:
CLEmitteroutput=newCLEmitter(true);
Change the argument to the constructor to false if only an in-memory representation of
the class is needed, in which case the class will not be written to the file system.
One then goes about adding classes, method, fields, and instructions. There are methods
for adding all of these.
Set Destination Directory for the Class
The destination directory for the class file can be set by sending to the
CLEmitter
instance
the following message:
publicvoiddestinationDir(StringdestDir)
where
destDir
is the directory where the class file will be written. If the class that is being
created specifies a package, then the class file will be written to the directory obtained by
appending the package name to the destination directory. If a destination directory is not
set explicitly, the default is \.", the current directory.
Add Class
A class can be added by sending to the
CLEmitter
instance the following message:
publicvoidaddClass(ArrayList<String>accessFlags,
StringthisClass,
StringsuperClass,
ArrayList<String>superInterfaces,
booleanisSynthetic)
where
accessFlags
7
is a list of class access and property flags,
thisClass
is the name of
the class in internal form,
superClass
is the name of the super class in internal form,
superInterfaces
is a list of direct super interfaces of the class in internal form, and
isSynthetic
specifies whether the class is created by the compiler.
If the class being added is an interface,
accessFlags
must contain appropriate (\interac-
tive" and \abstract") modiers,
superInterfaces
must contain the names of the interface's
super interfaces (if any) in internal form, and
superClass
must always be \java/lang/Ob-
ject".
Add Inner Classes
While an inner class C is just another class and can be created using the
CLEmitter
interface,
the parent class P that contains the class C must be informed about C, which can be done
by sending the
CLEmitter
instance for P the following message:
publicvoidaddInnerClass(ArrayList<String>accessFlags,
StringinnerClass,
StringouterClass,
StringinnerName)
7
Note that the
CLEmitter
expects the access and property ags as
String
s and internally translates
them to a mask of ags. For example, \public" is translated to
ACC_PUBLIC(0x0001)
.
Search WWH ::
Custom Search