Current implementations use native code to implement much of the functionality of the Reflection API. The Sun JDK and Kaffe VM provide full source of their implementation. GNU has a Classpath project which is attempting to produce a free version of the standard Java class libraries but they seem to be stuck with reflection (probably because it is so implementation-dependent). The Kaffe source code is somewhat easier to follow than Sun but they are both basically doing the same thing. I give a general outline below of how they are handling reflection.
Leaving reflection aside for a moment, the JVM implementation has
several structures used to represent classes and their fields and
methods, when they are loaded from a .class file. A simplified
version of the internal data structures would look something like:
struct Hjava_lang_Class { ... name; srcFileName; accessFlags;
struct method_[] methods;
struct field_[] fields ... }
struct method_ { ... name; signature; stacksz; localsz; code; exceptions; ... }
struct field_ { ... name; Hjava_lang_Class type; accessFlags; size; address; ... }
Given these structures, the JVM implementation will have a set of
procedures for reading in and parsing .class files into these
structures and for creating (allocating/initializing) new instances of
classes given a Class structure.
In order to support reflection, (not suprisingly) many of these data
structures are re-used. Class.forName() will go through the same
process of finding and loading a class file as when the JVM
dynamically loads class files. Creating new objects of type Class,
Field, Method, etc. for the reflection API simply requires allocating
and creating new Java objects of those classes and filling them with
the information that is already stored in the internal structures
above. Field::get()/set() can re-use the code used by the JVM
to perform getField/setField operations. Similarly for
Method::invoke().
The native code supporting reflection also carries out the various access checks to make sure that member access privileges are not violated.
Most of the native implementation for Sun Linux version (Blackdown)can be found in the
src/share/javavm/runtime/jvm.c file. Kaffe's native stuff is
in the libraries/clib/native directory.
MORE DETAILS NEEDED ? ...