Java Tutorial/Trail: The Reflection API

The Reflection API
The Reflection API allows to analyze the structure of the classes composing the running program from inside the program and to access the data contained in those classes. The API consists of the following classes:

JRMLWriter
The class JRMLWriter is an example of a custom class that uses reflection in order to write an XML dump of a hierarchy of objects into a buffer, a stream or to a network socket. In case of a socket this allows to implement a protocol much like RMI but using XML-formatted data. JRML is used as an abbreviation for "Java Remote Method Invocation Language" in reference to RMI's Java Remote Method Protocol.

The method, for instance, iterates over a class and its super classes, retrieving the declared fields of the classes, building a flat array of all fields, changing their accessibility to "true" on the way, because private, protected and package access fields may by default be inaccessible even to reflection.

(source code from vsrs.svn.sourceforge.net)

SwingFactory
For everybody who wondered why Swing has "Pluggable Look & Feel" packages that do a lot of amazing (well at least a lot of) stuff to modify the design and behavior of the unmodified Swing classes instead of using interfaces &mdash; here is SwingFactory. My personal theory is that the Swing authors made a little joke by putting polymorphism "on the wrong side" of Swing. SwingFactory is an unfinished sketch to address the issue; the exercise is to generate widget interfaces using the Reflection API (they merely contain all the public methods in their counterparts as abstract interface methods) and to write an to redirect the calls to an actual Swing implementation, possibly residing in a different virtual machine. The exercise requires some more work to address the issue of non-widgets required to configure the components properly in order to be actually useful. A look at com.sun.j3d.exp.swing.JCanvas3D may also be interesting.