Take an in-depth look at the Java Reflection API
Learn about the new Java 1.1 tools for finding out information about classes
By Chuck Mcmanis, JavaWorld.com, 09/01/97
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
In
last month's "Java In-Depth," I talked about introspection and ways in which a Java class with access to raw class data could look "inside" a class and
figure out how the class was constructed. Further, I showed that with the addition of a class loader, those classes could
be loaded into the running environment and executed. That example is a form of
static introspection. This month I'll take a look at the Java Reflection API, which gives Java classes the ability to perform
dynamic introspection: the ability to look inside classes that are already loaded.
The utility of introspection
One of Java's strengths is that it was designed with the assumption that the environment in which it was running would be
changing dynamically. Classes are loaded dynamically, binding is done dynamically, and object instances are created dynamically
on the fly when they are needed. What has not been very dynamic historically is the ability to manipulate "anonymous" classes.
In this context, an anonymous class is one that is loaded or presented to a Java class at run time and whose type was previously
unknown to the Java program.
Anonymous classes
Supporting anonymous classes is hard to explain and even harder to design for in a program. The challenge of supporting an
anonymous class can be stated like this: "Write a program that, when given a Java object, can incorporate that object into
its continuing operation." The general solution is rather difficult, but by constraining the problem, some specialized solutions
can be created. There are two examples of specialized solutions to this class of problem in the 1.0 version of Java: Java
applets and the command-line version of the Java interpreter.
Java applets are Java classes that are loaded by a running Java virtual machine in the context of a Web browser and invoked.
These Java classes are anonymous because the run time does not know ahead of time the necessary information to invoke each
individual class. However, the problem of invoking a particular class is solved using the Java class java.applet.Applet.
Common superclasses, like Applet, and Java interfaces, like AppletContext, address the problem of anonymous classes by creating a previously agreed upon contract. Specifically, a runtime environment
supplier advertises that she can use any object that conforms to a specified interface, and the runtime environment consumer
uses that specified interface in any object he intends to supply to the run time. In the case of applets, a well-specified
interface exists in the form of a common superclass.
The downside of a common superclass solution, especially in the absence of multiple inheritance, is that the objects built
to run in the environment cannot also be used in some other system unless that system implements the entire contract. In the
case of the Applet interfaces, the hosting environment has to implement AppletContext. What this means for the applet solution is that the solution only works when you are loading applets. If you put an instance
of a Hashtable object on your Web page and point your browser to it, it would fail to load because the applet system cannot operate outside
its limited range.
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- "Take a look inside Java classes"
Learn to deduce properties of a Java class from inside a Java program. http://www.javaworld.com/javaworld/jw-08-1997/jw-08-indepth.html
- "Build an interpreter in Java -- Implement the execution engine"
Here's how to take the interpreter classes and run with them. http://www.javaworld.com/javaworld/jw-07-1997/jw-07-indepth.html
- "How to build an interpreter in Java, Part 2The structure"
The trick to assembling the foundation classes for a simple interpreter. http://www.javaworld.com/javaworld/jw-06-1997/jw-06-indepth.html
- "How to build an interpreter in Java, Part 1The BASICs"
For complex applications requiring a scripting language, Java can be used to implement the interpreter, adding scripting
abilities to any Java app. http://www.javaworld.com/javaworld/jw-05-1997/jw-05-indepth.html
- "Lexical analysis, Part 2Build an application"
How to use the StreamTokenizer object to implement an interactive calculator. http://www.javaworld.com/javaworld/jw-02-1997/jw-02-indepth.html
- "Lexical analysis and JavaPart 1"
Learn how to convert human-readable text into machine-readable data using the StringTokenizer and StreamTokenizer classes.
http://www.javaworld.com/javaworld/jw-01-1997/jw-01-indepth.html
- "Code reuse and object-oriented systems"
Use a helper class to enforce dynamic behavior. http://www.javaworld.com/javaworld/jw-12-1996/jw-12-indepth.html
- "Container support for objects in Java 1.0.2"
Organizing objects is easy when you put them into containers. This article walks you through the design and implementation
of a container. http://www.javaworld.com/javaworld/jw-11-1996/jw-11-indepth.html
- "The basics of Java class loaders"
The fundamentals of this key component of the Java architecture. http://www.javaworld.com/javaworld/jw-10-1996/jw-10-indepth.html
- "Not using garbage collection"
Minimize heap thrashing in your Java programs. http://www.javaworld.com/javaworld/jw-09-1996/jw-09-indepth.html
- "Threads and applets and visual controls"
This final part of the series explores reading multiple data channels. http://www.javaworld.com/javaworld/jw-07-1996/jw-07-mcmanis.html
- "Using communication channels in applets, Part 3"
Develop Visual Basic-style techniques to applet design -- and convert temperatures in the process. http://www.javaworld.com/javaworld/jw-06-1996/jw-06-mcmanis.html
- "Synchronizing threads in Java, Part II"
Learn how to write a data channel class, and then create a simple example application that illustrates a real-world implementation
of the class. http://www.javaworld.com/javaworld/jw-05-1996/jw-05-mcmanis.html
- "Synchronizing threads in Java"
Former Java team developer Chuck McManis walks you through a simple example illustrating how to synchronize threads to assure
reliable and predictable applet behavior. http://www.javaworld.com/javaworld/jw-04-1996/jw-04-synch.html