Contains classes to generate, read,
write, and print Java bytecode (
.class) files.
It is used by
Kawa
to compile Scheme into bytecodes; it should be useful
for other languages that need to be compiled into Java bytecodes.
(An interesting exercise would be an interactive Java expression evaluator.)
The classes here are relatively low-level. If you want to use them
to generate bytecode from a high-level language, it is better to
use the
gnu.expr package, which works
at the expression level, and handles all the code-generation for you.
Javadoc generated
documentation
of the class is available online.
The most important class is
ClassType.
This contains information
about a single class. Note that the difference between
ClassType
and
java.lang.Class is that the latter only represents existing
classes that have been loaded into the Java VM; in contrast,
ClassType can be used to build new classes
incrementally and on the fly.
A
ClassType has a list of
Field objects;
new ones can be added using
the various
addField methods. A
ClassType
manages a
ConstantPool.
A
ClassType also has a list of
Method objects;
new ones can be created by the various
addMethod objects.
A
Method contains many methods you can use to generate bytecode.
See Kawa for examples.
Once you have finished generating a
ClassType, you
can write it to a
.class file with
the
writeToFile method. You can also make a
byte array suitable for
ClassLoader.defineClass using the
writeToArray
method. This is used by Kawa to compile and immediately load a class.
You can also print out the contains of a
ClassType in
human-readable
form using the class
ClassTypeWriter. This prints a fair bit of
information of the generated class, including
dis-assembling the code of the methods.
You can also build a
ClassType by reading it from an
existing
.class
file by using a
ClassFileInput class. This reads the constant
pool, the fields, methods, superclass, and interfaces.
The
gnu.bytecode.dump class has a
main method
that prints out the information in a named class file.
Zip archive manager
To build, extract, list, or print a zip file, you can
use
ZipArchive as an application:
java gnu.bytecode.ZipArchive [txpq] archive [file ...]
See the comments for
ZipArchive.main.
ZipArchive does not do compression or uncompressions, and
it is reported that that some programs do not like the archives it creates.
It is probably best suited for listing and extracting from classes.zip-like
archives. (This class has been partially re-written to use java.util.zip.
It may get dropped in the future.)
Class file dumper
To print out the contents of a class file foo.class, you can use
the class
dump as an application:
java gnu.bytecode.dump foo.class
This will print out the constant pool, fields, methods, superclass,
and implemented interfaces of class
foo.
It is useful for printing out detailed information about the constant
pool, which
javap does not do.
License
See the file
COPYING.
Author
Per Bothner
<per@bothner.com>
How to get it
The
gnu.bytecode package is currently distributed as part of
Kawa, though it can be used independent
of the rest of Kawa.
Bugs and patches
Send them to
per@bothner.com,
or to the
Kawa mailing list.