Invokedynamic Bytecode Question from Cleveland JUG
By Roger Brinkley on Apr 27, 2012
On April 11 I visited the Cleveland JUG doing a presentation on JavaFX. At the end of the presentation I opened the floor to an questions and got one I didn't expect.
"Where can I find documentation on the new invokedynamic byte code?"
Well that was certainly out of the blue. It not a question that you get everyday. I'd say 99.9% of the Java developers will never deal directly with the Java bytecodes and 99.9995% will never deal directly with the invokedynamic byte code. The invokeddynamic byte code is part of JSR 292: Supporting Dynamically Typed Languages on the JavaTM Platform and was introduced in JDK 7. As the JSR name implies it's intended for languages like JRuby, Scala, and Nashorn (the new Java Scripting language currently underway in OpenJDK) to leverage the JVM. And while invokedynamic has made JRuby run significantly faster, the usage is going to to be restricted to a very limited set of developers. Intrigued, I asked why they would want to know?
Turns out the developer use to work for a company that provided an obfuscation tool and since JDK 7 added a new bytecode they thought they needed to be able to accommodate it. Having not worked in a commercial environment for most of my professional career, I don't have much experience with obfuscation tools. Oh sure I've done some decompiling in my time but mostly out of curiosity than anything else.
Obfuscators are designed to make the class files more difficult to understand if decompiled. There are a number of different obfuscation techniques like name obfuscation, string encryption, code and data flow obfuscation, and of course the very useful class file optimization (which is extremely useful for mobile deployments). Back in the early days of Java someone figured out they could decompile source code which then swung the pendulum to obfuscating class files to protect a developer's code. I'm guessing, but I think the pendulum has swung back and we're somewhere back to the middle but I'm sure there are still some developers obfuscating code.
So back to the question at hand, which was where is the documentation of invokedyamic bytecode? The answer is to look at Java SE 7 edition of The Java™ Virtual Machine Specification which Alex Buckley recently reorganized and is now available on OTN. Specifically, turn to Chapter 6 and slide down section 6.5 Instructions and find invokedynamic.
From an obfuscation point of view it appears to be very similar to invokeinterface (scroll down a smidge) except that both the third and fourth operand bytes are zero and the operand stack is little different.
But all of this is rather a moot point since none of the Java compilers can generate the invokedynamic byte codes. That's something for the scripting languages like JRuby and Nashorn that will leverage the JVM. And off the top of my head I can't think of a reason, other than completeness, as to why an obsfucator would want to implement support. To which I say...
Eschew Obfuscation at least as far as invokedynamic is concerned.