background preloader

Byte Code Manipulation

Facebook Twitter

Javassist. Commons BCEL™ - Byte Code Engineering Library (BCEL) Abstract Extensions and improvements of the programming language Java and its related execution environment (Java Virtual Machine, JVM) are the subject of a large number of research projects and proposals.

Commons BCEL™ - Byte Code Engineering Library (BCEL)

There are projects, for instance, to add parameterized types to Java, to implement Aspect-Oriented Programming, to perform sophisticated static analysis, and to improve the run-time performance. Since Java classes are compiled into portable binary class files (called byte code), it is the most convenient and platform-independent way to implement these improvements not by writing a new compiler or changing the JVM, but by transforming the byte code.

These transformations can either be performed after compile-time, or at load-time. Many programmers are doing this by implementing their own specialized byte code manipulation tools, which are, however, restricted in the range of their re-usability. 1 Introduction 2 The Java Virtual Machine Figure 1: Compilation and execution of Java classes. Byteman 2.0.0: Bytecode Manipulation, Testing, Fault Injection, Logging. JBoss has released Byteman 2.0.0, an open source Java bytecode manipulation tool licensed under GNU LGPL 2.1.

Byteman 2.0.0: Bytecode Manipulation, Testing, Fault Injection, Logging

Byteman is a Java agent which helps testing, tracing, and monitoring code. It allows developers to change the operation of Java applications, either as it is loaded or during runtime. It works without the need to rewrite or recompile the application, and can even modify Java Platform classes like String, Thread, etc. Here are what's new with Byteman 2.0.0. File and Line Debug Information. Byteman uses a simple Event Condition Action (ECA) scripting language to specify where, when and how the target Java code should be transformed. . # script.btm RULE trace main entry CLASS App METHOD main AT ENTRY IF true DO traceln("entering main") ENDRULE Here we have a script that tells Byteman to print "entering main" at the start of the App.main() method.

-javaagent:<BYTEMAN-DIR>\lib\byteman.jar=script:script.btm Note you can also add Byteman on already running application using bminstall.sh. ASM - Home Page. Java bytecode: The information about bytecode, as well as the bytecode presented here, is based on the Java 2 SDK Standard Edition v1.2.1 javac compiler.

Java bytecode:

The bytecode generated by other compilers may vary slightly. Why understand bytecode? Bytecode is the intermediate representation of Java programs just as assembler is the intermediate representation of C or C++ programs. The most knowlegable C and C++ programmers know the assembler instruction set of the processor for which they are compiling. This knowledge is crucial when debugging and doing performance and memory usage tuning. An often overlooked aspect of Java is the bytecode that is generated by the javac compiler. The bytecode is your program. Back to top Generating bytecode javac Employee.java javap -c Employee > Employee.bc This class is very simple. You might notice on closer inspection of the bytecode that certain opcodes are prefixed with an `a' or an `i'.

Note: Individual codes are generally referred to as opcode.