I was perusing the OpenJDK 6 code today, and I learnt about a really cool technique they use in reflection (yes, I'm aware that the technique was implemented even in the 1.4 days, but OpenJDK code is easier to obtain, so that's what I'm looking at). Now, assuming that you, the reader, are not an expert in Java's core library implementation, how would you go about implementing reflection? Have a think about it, then read on (if you want to). If you've played with JNI at all, I would presume you'd probably be thinking of that. Just write a native method that simply calls the JNI's builtin reflection functionality (or some shortcut thereto in the JVM). This, indeed, does work, and is one way that OpenJDK 6 does it. It has a much cooler technique, though, called “inflation”. Inflation means that for the first few runs (default 15) of a reflected method/constructor (from now on, any reference to methods applies to constructors too), it does so via JNI; the next time after that, it assembles a class file on the fly, and loads it. At that point, full JITting applies, and further calls to that reflected method has the same performance as directly calling that method.Read the entire post 'In JIT we trust': http://inferretuation.blogspot.com/2008/11/in-jit-we-trust.html
Blogs: JIT, JNI, Reflection and "Inflation"
JNI(Java Native Interface) is Java's way of invoking non-Java code. This post points out to how OpendJDK 6 relies on JNI to support reflection, JIT and a technique called 'inflation'.
- Posted by: Daniel Rubio
- Posted on: November 10 2008 07:21 EST