Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making simple performance modifications to an already compiled jar?

Like many log4j users, we often have debug level logging that is expensive to evaluate. So we guard those cases with code like:

if( _logger.isDebugEnabled )
  _logger.debug("Interesting, my foojes are goofed up: " + getFullDetails())

However, that is uglier than a plain _logger.debug call, and sometimes the programmer doesn't realize the evaluation could be expensive.

It seems like it should be fairly simple to write a program that takes a compiled jar and guards all the _logger.debug calls with the isDebugEnabled check. We would likely be willing to accept the extra overhead of checking isDebugEnabled in all cases.

Has anyone tried this approach, or done similar post-processing of a jar?

like image 646
Ted Graham Avatar asked Oct 22 '09 13:10

Ted Graham


People also ask

Can a JAR file be decompiled?

Unless you explicitly packaged your source code in the JAR file, there's no way to get back the original source. You could try using a decompiler (like this) but that will hardly give you nice, readable code. The best solution would be to stop using your computer right now.


2 Answers

Rather than looking at modifying the jar, I'd search for a solution using Bytecode Instrumentation. The problem will be to identify those parts of the code you want to wrap inside a .isDebugEnabled() - you will have to identify objects that are only used for log4j invocations.

like image 96
sfussenegger Avatar answered Sep 24 '22 17:09

sfussenegger


Have you looked at AspectJ ? This supports aspects using bytecode weaving, and can interceptions into a previously compiled .jar file.

like image 40
Brian Agnew Avatar answered Sep 25 '22 17:09

Brian Agnew