2013. szeptember 23., hétfő

JavaOne Monday: Bytecode Instrumentation for Beginners

This one was an introductory presentation to bytecode instrumentation in Java today, which is widely used by different code analysis (e.g. FindBugs) and AOP tools (e.g. AspectJ) and dynamic languages (Nashorn for JavaScript, JRuby). The session was a nice summary about instrumentation and the options you can choose from if you need to instrument classes yourself.

Instrumentation was introduced in JDK 1.5. You can start the JVM specifying a Java agent, or you can attach agents later. An agent's agentmain and/or premain methods will be called by the JVM, and this is where it can register a ClassFileTransformer to modify classes at runtime. (You can find a nice tutorial about instrumentation here.)

The hard way do instrument a class is to implement your own ClassFileTransformer and work with byte arrays representing class files directly, e.g. using a DataInputStream. Of course this is not recommended if you don't want to go into details of the class file structure, but as mentioned during the presentation, this way you can learn a lot about it. ;)

Easier ways:

Update: Here is the link for the presentation's site.

Nincsenek megjegyzések:

Megjegyzés küldése