Enter your Email Address:

Delivered by FeedBurner

To get posts delivered to your Email

19 March 2011

Interesting Tools on Bytecode Manipulation

The last week, i got a chance to experiment on couple of bytecode manipulation APIs/tools and thought i can share my learning here.

Javassist is one of bytecode manipulation APIs that helps you to manipulate bytecode and it is simpler to use. I wrote a simple method profiler that takes class files as input and instruments the bytecodes in each method. Added, method start time, complete time and measured the time spent in the method. One good thing about Javassist is that you can directly add code written in Java that compiles on the fly while instrumentation. Seems like bytecode instrumentation is bit easier in Javassist. With Javassist, you can also do on the fly instrumentation during classloading phase.

Retroweaver is another bytecode manipulation tool that back ports the code compiled by Java 5 to Java 1.4. If you have written classes (or a third party classes/libraries) in Java 5 and if your production system still runs with Java 1.4/1.3/1.2, you may want to check out this tool. It is really cool to use latest APIs/libraries in old JVM. Retrotranslator is a similar tool.

Bytecode engineering seems to be very interesting.

5 comments:

Sandy said...

Excellent Lakshmi.. Kudos !! Always sharing something new :-) Way back in 2004 when I was new to IT I was wondering if it was possible to convert .exe back to .c. Well it wasn't possible but now with java, it's almost possible :-)

Lakshmi Narayanan N said...

@Sandy

Yes, it is very much possible to recover some of the code (but not fully though). Hope you heard/learned abt Reverse Engineering. it is exciting area. As a side topic, neuro scientists are reverse engineering brain :-)

Shankar Ramakrishnan said...

.Net was having this feature right from day one (Reflection). was wonderinf if Java had this one.
Thanks Lakshmi for posting this.

Bala Vijay said...

Last week I attended a session on "Bytecode and its details". I attended simply to know how class file is generated and wat is in it. But my manager suddenly came and raised a question "What is the use of knowing about bytecode unless we are going to design a compiler; already many tools are available for reverse engineering". I am still thinking what can be done by knowing the bytecode structure details. Just thought of sharing that incident with you guru...

Lakshmi Narayanan N said...

@Bala Vijay

Your manager is absolutely right. A developer doesn't need to know all these stuffs. But an engineer has to.

In general with knowledge you cannot be absolutely sure about how are you going to apply. But having the knowledge helps us to appreciate how a language is designed and how its features can be exploited it.

For example, knowing bytecode help us to write tools such as profiler. Also, you can write a new language (compiler) and generate bytecode that is run in JVM.