Package gnu.bytecode

Contains classes to generate, read, write, and print Java bytecode (.class) files.

See:
          Description

Interface Summary
AttrContainer An interface for objects that (may) contain Attribute objects.
Filter  
 

Class Summary
Access Access flags.
ArrayClassLoader Load classes from a set of byte arrays.
ArrayType  
Attribute Represents an Attribute of an AttrContainer.
ClassFileInput Class to read a ClassType from a DataInputStream (.class file).
ClassType  
ClassTypeWriter This class prints out in contents of a ClassType in human-readable form.
CodeAttr Represents the contents of a standard "Code" attribute.
ConstantPool Manages a pool of constants, as used in .class files and Java interpreters.
ConstantValueAttr  
CpoolClass A CONSTANT_Class entry in the constant pool.
CpoolEntry An entry in the constant pool for a ClassType.
CpoolFloat A CONSTANT_Float entry in the constant pool.
CpoolNameAndType A CONSTANT_NameAndType entry in the constant pool.
CpoolRef A CONSTANT_{Field,Method,InterfaceMethod}Ref entry in the constant pool.
CpoolString A CONSTANT_String entry in the constant pool.
CpoolUtf8  
CpoolValue1 A CONSTANT_Integer or CONSTANT_Float entry in the constant pool.
CpoolValue2 A CONSTANT_Long or CONSTANT_Double entry in the constant pool.
dump Class to read a ClassType from a DataInputStream (.class file).
ExceptionsAttr Represents the contents of a standard "Exceptions" attribute.
Field  
IfState The state of a conditional expression or statement.
InnerClassesAttr  
Label A Label represents a location in a Code attribute.
LineNumbersAttr Represents the contents of a standard "LineNumberTable" attribute.
LocalVarsAttr  
Location An abstracted "variable", inherited by Field and Variable.
Method Represents a method in a ClassType.
MiscAttr  
ObjectType Semi-abstract class object reference types.
PrimType  
Scope  
SourceDebugExtAttr Represents the contents of a JSR-45 "SourceDebugExtension" attribute.
SourceFileAttr  
SwitchState Maintains the state for generating a switch statement.
TryState The state of a try statement.
Type  
VarEnumerator Use this Enuemration class to iterate over the Variables in a Scope.
Variable  
ZipArchive A class to manipulate a .zip archive.
ZipLoader Load classes from a Zip archive.
 

Package gnu.bytecode Description

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.