/ blog / java / dependency-killer.html

Root Beer Logo Root Beer

Chris Nokleberg's Fizzy Weblog

August 2004
Su M Tu W Th F Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 26 27 28
29 30
Previous  |  Next  |  More...
#  Dependency killer

I lurk on a bunch of mailing lists via Gmane and recently came across this message in which Henri Yandell asks if it would be possible to write a tool using bytecode manipulation which strips out any dependency on commons-logging.

After getting in touch with Henri he suggested making it a generic tool that could strip out any dependency. That's pretty much right up my alley, and since I've had a few logging headaches of my own I figured it was worth a try. So I've checked in some experimental code to the Jar Jar Links project, which has similar dependency-reducing goals.

Here is an examples of stripping commons-logging out of commons-beanutils:

java -cp jarjar.jar com.tonicsystems.jarjar.DepKill \
  commons-beanutils.jar org.apache.commons.logging > \

First argument is the jar to process, remaining args are package names to kill.

The transformed jar is sent to stdout. The actual transformations performed are pretty simple. If J is the code in the jar that is being transformed, and K is a "killed package":

  • Field declarations and method arguments/return values in J have all types from K "erased" to java.lang.Object.
  • All methods declared in J have Exception classes in K removed from their throws declaration.
  • Methods invocations from J on a class in K are replaced with the appropriate number of "pop"s to remove the arguments off the stack. Depending on the method signature, a fake return value of either null or zero is pushed.
  • Catch blocks in J matching an exception class from K are skipped entirely.

I haven't yet tried using this to strip out anything other than commons-logging, so feedback is welcome.

[Powered By FreeMarker]  [Valid Atom 1.0]  [Weblog Commenting by]