- The Byte Code Engineering Library is intended to give users a convenient possibility to analyze, create, and manipulate (binary) Java class files (those ending with .class). Classes are represented by objects which contain all the symbolic information of the given class: methods, fields and byte code instructions, in particular.
Such objects can be read from an existing file, be transformed by a program (e.g. a class loader at run-time) and dumped to a file again. An even more interesting application is the creation of classes from scratch at run-time. The Byte Code Engineering Library (BCEL) may be also useful if you want to learn about the Java Virtual Machine (JVM) and the format of Java .class files.
BCEL is already being used successfully in several projects such as compilers, optimizers, obsfuscators, code generators and analysis tools
- cglib is a powerful, high performance and quality Code Generation Library, It is used to extend JAVA classes and implements interfaces at runtime. See samples and API documentation to learn more about features
Classfile Reader & Writer
- This package makes it easy to read and write java classfiles. It does not, however, provide any help with displaying the contents of a classfile to the user (unless you count debug output), or disassembling the bytecodes.
This code snippet will read in a classfile and write it back out to a different file.
InputStream is = new FileInputStream(Foo.class);
OutputStream os = new FileOutputStream(FooCopy.class);
ClassInfo classInfo = new ClassInfo();
new ClassFileReader().read(is, classInfo);
classInfo.setName(FooCopy); // Java requires the class name to match the file name
new ClassFileWriter().write(classInfo, os);
Here is the API documentation.
Here is the source (56K).
Here is the VM Spec that I used.
The package can read obfuscated classfiles, like those generated by Crema, but it can not write them. Obfuscated classfiles have invalid data in them and the only reason they work is because most VMs ignore the data that is invalid (attributes like SourceFile, LineNumberTable, and LocalVariableTable). If a ClassFileReader encounters invalid data, it just ignores it.
- Cojen primary goal is making raw Java classfile generation easy, without hiding any of the advanced features. Basic knowledge of how Java classfiles are structured is still required, however. The Virtual Machine Specification is an excellent reference.
Method bytecode is constructed via a builder, in which virtual machine opcodes are represented as slightly higher level instructions. It takes care of selecting optimal opcodes, managing branch labels, computing operand stack depth, choosing local variable registers, and reducing register usage by liveness analysis. In addition, it supports long branches, pushing large string constants onto the stack, boxing and unboxing conversions, and inlining of Java code.
- The Java Binary Enhancement Tool (JBET) is a general Java program analysis and manipulation tool. Existing class files can be disassembled, reassembled, or edited programmatically through the JBET API. JBET can also be used to create new Java class files from scratch. JBET uses a convenient internal representation of all the contents of Java binary (.class) files, allowing the user to edit the classes easily, in a structured manner.
JBET was developed as part of the DARPA Self-Protecting Mobile Agents project under the OASIS and Active Networks programs (contract number N66001-00-C-8602) in order to study automated software obfuscation. The Java language was chosen for this project because of the (relative) ease of constructing binary editing tools provided by the large amount of type information present in the class files. Our two reports, the Obfuscation Techniques Evaluation Report, and the Obfuscation Report, are available from the download area. The obfuscation tool developed is not part of this release.
- jclasslib bytecode viewer is a tool that visualizes all aspects of compiled Java class files and the contained bytecode. In addition, it contains a library that enables developers to read, modify and write Java class files and bytecode.
- When a Java virtual machine encounters a request for a class which has not been loaded to its memory, it consults a class loader to locate and load the class. Typically, the classes are loaded from a filesystem or URL as a bytecode array and then converted into an instance of class java.lang.Class within the virtual machine. Jiapi is a tool which can be used to alter this normal class loading behavior. Instead of letting the class to be loaded as it is, Jiapi is used to manipulate classes bytecode. The manipulated class is then passed to a class loader which loads it into a Java virtual machine. In addition to just described just-in-time bytecode weaving, Jiapi can be used to instrument a class ahead-of-time. In this mode compiled classes are preprocessed by Jiapi instrumentors and modified classes are then serialized on a filesystem to be executed later.
- ASM is a Java bytecode manipulation framework. It can be used to dynamically generate stub classes or other proxy classes, directly in binary form, or to dynamically modify classes at load time, i.e., just before they are loaded into the Java Virtual Machine.
ASM offers similar functionalities as BCEL or SERP, but is much smaller (33KB instead of 350KB for BCEL and 150KB for SERP) and faster than these tools (the overhead of a load time class transformation is of the order of 60% with ASM, 700% or more with BCEL, and 1100% or more with SERP). Indeed ASM was designed to be used in a dynamic way* and was therefore designed and implemented to be as small and as fast as possible.
- 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.
- Serp is an open source framework for manipulating Java bytecode. If you have a suggestion for functionality that you would like to see incorporated into serp, please feel free to send it to me. Bug reports are also greatly appreciated. And if you choose to incorporate serp into any of your projects, I would love to hear about it!
Trove Class File API
- Trove is a library of useful classes that is used by Tea and the TeaServlet. Trove includes the Class File API, the Logging API and a set of utility classes.
The Class File API simplifies creation of Java class files. Instead of having to write byte-code, the Class File API allows you to call high-level methods which generate the byte-code for you.