News: A Working Developer's Guide to Java Bytecode

  1. In the last year or two, interest in "alternative" languages on top of the Java Virtual Machine has grown exponentially, to the point where three languages were blessed with official acceptance in the Java Community Process: JRuby (a port of the Ruby programming language), Groovy (a Java-like language with more dynamic features), and Bean-Shell (a more scripting-oriented shell- like language). In addition, the Java6 (code-named "Mustang") release includes an API layer designed to encapsulate the details of various scripting engines away in the same manner JDBC does for database access. Plus, we have all the changes that took place in the Java5 version of the language itself. In short, the Java platform programming language landscape has changed pretty significantly. One thing that hasn't changed, however, is the basic fact that all of these languages, despite all their interesting features or capabilities, eventually end up in the lingua franca of the Java Virtual Machine, the JVM bytecode set. In The Working Developer's Guide to Java Bytecode, we're going to examine the JVM bytecode set, disassemble some code to see how it works, and play with some tools that allow us to manipulate bytecode directly.

    Threaded Messages (6)

  2. Some comments[ Go to top ]

    It seems like this article describing lots of unnecessary details, especially those, around constant pool references. Too bad, Ted haven't stayed for our ASM talk ath the JavaOne. :-) Also, as far as I know, BCEL is only used by XSLTC compiler in Sun JDK, and proxies are built by sun.misc.ProxyGenerator which implements some limited subset of the class generation.
  3. Re: Some comments[ Go to top ]

    Yeah, ASM is a great library. I use it in Retrotranslator to backport bytecode from Java 5 to Java 1.4. ASM gives me full control over each bytecode instruction and at the same time I don't have to think about things like a constant pool. The feature I'd like ASM to have is ability to match NEW instructions with constructor calls :)
  4. Re: Some comments[ Go to top ]

    Taras, there is no prepackaged utility to do such matching, but you can use Analyzer to calculate what instructions produced values for every variable or stack slot: class MethodNodeAnalyzer extends MethodNode { public void visitEnd() { Analyzer a = new Analyzer(new SourceInterpreter()); a.analyze(className, this); Frame[] frames = a.getFrames(); ... SourceValue source = (SourceValue) frames[insn].getStack(slot); Set sources = source.insns; ... if(mv!=null) accept(mv); }In above example, "frames" array will contain frames for each instruction in the method code and "sources" set will give you instructions that produced value of the stack "slot" for instruction "insn". So, you can use this information to change method instructions and then pass updated code to the next visitor.
  5. Re: Some comments[ Go to top ]

    Thank you for the example, Eugene. It's been very helpful.
  6. No offense intended, Eugene :-)[ Go to top ]

    It seems like this article describing lots of unnecessary details, especially those, around constant pool references. Too bad, Ted haven't stayed for our ASM talk ath the JavaOne. :-)
    I would have loved to see that talk, Eugene, and my use of any particular library shouldn't necessarily be taken to be an endorsement of any. :-) Truthfully, though, while the details of constant pool entries may seem "unnecessary" at first, it's not too long before people start to run into those same details when trying to read or pick apart bytecode. It's generally good to know those details are there, and that your favorite library or tool (ASM, perhaps?) is handling them for you, than to not know them at all.
    Or at least, that's my opinion, based on Joel Spolsky's "Law of Leaky Abstractions". And, unfortunately, the details of Java's constant pool format do leak. :-/
  7. Hello, I am trying to figure out how dup instructions work I went to sun and look at jvm spec but I need some examples of how to use dup opcode. is there a url for this? Thanks