13

I'm looking for a well-maintained Java bytecode manipulation library with an intuitive API. There seem to be quite a lot of them out there. Any suggestions on which ones to try?

Daniel
  • 10,115
  • 3
  • 44
  • 62

3 Answers3

7

The best answer to your question will be governed by your specific needs and objectives; if you can expand upon what you're trying to accomplish, I can perhaps offer a more tailored response.

In lieu of that, however, in my experience, ASM offers probably the best combination of maturity, flexibility, and ease-of-use:

  • It's under relatively active development: even though the latest release is from June 2009, developers are regularly making commits to their VCS.
  • It's already in wide use in a number of prominent Java products, such as AspectJ, Groovy, Cobertura, and many others, which promises a wide user-base and thus a wide community for support.
  • It's fairly thoroughly documented, and there are a number of tutorials, user guides, and reference documents available both from the OW2 Consortium and the community at large.

N.B. The comment @Yuri left below describes a situation I personally haven't yet encountered but that may potentially pose a significant obstacle. Keep his observation/experience in mind while evaluating the various bytecode manipulation libraries.

RTBarnard
  • 4,374
  • 27
  • 27
  • Sorry about the lack of specifics in the question. I'd prefer not to give out the specifics of what I'm doing, but your answer was quite helpful. Thanks. I think I'll end up using ASM. – Daniel Mar 28 '10 at 08:16
  • 1
    Looking at this answer I tried ASM and it (4.0) has either a bug or really weird behaviour. I'm using Tree API and iterating over method's instructions InsnList. Now, first iteration gives one result and another identical iteration following the first one gives another result - one instruction gets duplicated and put on top of the list. No modification code at all, just two iterations one by one. Switching to Javassist. – Yuri Geinish Oct 04 '12 at 12:42
  • @YuriUshakov: That's not a situation I've personally experienced. Thanks for the heads-up! – RTBarnard Oct 18 '12 at 04:56
4

That actually depends on how you define intuitive ;-) I started using ASM two weeks ago for a certain task on my current project and it worked like a charm. It only took me a couple hours to understand it and be able to use it, but I wouldn't exactly call the API intuitive. But if you know a little bit about Java bytecode and are familiar with the visitor pattern, the learning curve is not very high, IMO.

Another advantage of ASM is that it is apparently bundled with the standard API at least in the Sun JDK, although in a different package (com.sun.xml.internal.ws.org.objectweb.asm and subpackages).

Robert Petermeier
  • 4,122
  • 4
  • 29
  • 37
  • You're right: judging by some of the weird APIs out there, people's definitions of intuitive seem to differ widely ;) . I've just spent a little time looking at ASM, and I'm far less intimidated than I expected to be! I think I'll stick with it. Thanks for your answer. – Daniel Mar 28 '10 at 08:14
  • 1
    Yeah, right, don't use implementation packages. And don't manipulate bytecode either, unless you really have to ;-) – Robert Petermeier Mar 29 '10 at 05:57
3

Take a look at this article : http://www.pabrantes.net/blog/space/start/2008-03-24/1

A little old but still relevant.

ASM And Javassist are the most used ones now. Bcel is dying.

Roman
  • 7,933
  • 17
  • 56
  • 72