The Holy Java

Building the right thing, building it right, fast

Posts Tagged ‘Javassist’

Hacking A Maven Dependency with Javassist to Fix It

Posted by Jakub Holý on October 19, 2011

Have you ever wondered what to do when needing “just a small change” to a third-part library your project depended on? This post describes how to use Maven and Javassist to take a dependency of your project, instrument it to modify its behavior, re-pack it, and release it as an artifact with a different name (so that you me depend on my-customized-lib instead of on lib).

The process is as follows:

  1. Phase process-sources - maven-dependency-plugin unpacks the dependency to classes/
  2. Phase compile (implicit) – compile the bytecode manipulation code
  3. Phase process-classes - exec-maven-plugin executes the compiled Javassist instrumenter to modify the unpacked classes
  4. Phase test – run tests on the instrumented code
  5. Phase package – let maven-jar re-package the instrumented classes, excluding the instrumenter itself

Read the rest of this entry »

Posted in Languages, Tools | Tagged: , , , | 2 Comments »

Practical Introduction into Code Injection with AspectJ, Javassist, and Java Proxy

Posted by Jakub Holý on September 7, 2011

The ability to inject pieces of code into compiled classes and methods, either statically or at runtime, may be of immense help. This applies especially to troubleshooting problems in third-party libraries without source codes or in an environment where it isn’t possible to use a debugger or a profiler. Code injection is also useful for dealing with concerns that cut across the whole application, such as performance monitoring. Using code injection in this way became popular under the name Aspect-Oriented Programming (AOP). Code injection isn’t something used only rarely as you might think, quite the contrary; every programmer will come into a situation where this ability could prevent a lot of pain and frustration.

This post is aimed at giving you the knowledge that you may (or I should rather say “will”) need and at persuading you that learning basics of code injection is really worth the little of your time that it takes. I’ll present three different real-world cases where code injection came to my rescue, solving each one with a different tool, fitting best the constraints at hand.

Read the rest of this entry »

Posted in Languages, Tools | Tagged: , , , , | 9 Comments »

Implementing build-time bytecode instrumentation with Javassist

Posted by Jakub Holý on June 25, 2010

If you need to modify the code in class files at the (post-)build time without adding any third-party dependencies, for example to inject cross-cutting concerns such as logging, and you don’t wan’t to deal with the low-level byte code details, Javassist is the right tool for you. I’ve already blogged about “Injecting better logging into a binary .class using Javassist” and today I shall elaborate on the instrumentation capabilities of Javassist and its integration into the build process using a custom Ant task.

Read the rest of this entry »

Posted in Languages | Tagged: , , , | 3 Comments »