![]() The Java Reflection API grew out of the needs of the JavaBeans user interface component API. Alternatively, there needs to be an interface for tools that manipulate user components without having to know what the components are, or, more importantly, without access to the components' source code. On the one hand, the component objects are linked together to form a user interface as part of some application. User interface components are an ideal design point for an introspection system because they have two very different consumers. The "killer app," or the issue that caused the Java Reflection API to be created, was the development of an object component model for Java. The challenge with the existing Java 1.0 architecture is that there are problems that could be solved by a more dynamic introspection environment - such as loadable UI components, loadable device drivers in a Java-based OS, and dynamically configurable editing environments. The motivation for a more dynamic solution By convention, that method is named main and takes a single argument - an array of String objects. In that example, the virtual machine has to invoke some static method in the loaded class. In addition to the applet example, introspection helps to solve a problem I mentioned last month: figuring out how to start execution in a class that the command-line version of the Java virtual machine has just loaded. 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. What this means for the applet solution is that the solution only works when you are loading applets. In the case of the Applet interfaces, the hosting environment has to implement AppletContext. 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 applets, a well-specified interface exists in the form of a common superclass. 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. However, the problem of invoking a particular class is solved using the Java class .Ĭommon superclasses, like Applet, and Java interfaces, like AppletContext, address the problem of anonymous classes by creating a previously agreed upon contract. These Java classes are anonymous because the run time does not know ahead of time the necessary information to invoke each individual class. Java applets are Java classes that are loaded by a running Java virtual machine in the context of a Web browser and invoked. 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. 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. Supporting anonymous classes is hard to explain and even harder to design for in a program. ![]() 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. What has not been very dynamic historically is the ability to manipulate "anonymous" classes. Classes are loaded dynamically, binding is done dynamically, and object instances are created dynamically on the fly when they are needed. 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. ![]() 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. That example is a form of static introspection. Further, I showed that with the addition of a class loader, those classes could be loaded into the running environment and executed. 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |