Previous | Table of Contents | Next |
Reflection in Java 1.1 refers to the ability of Java classes to reflect upon themselves, or to look inside themselves. The java.lang.Class class has been greatly enhanced in Java 1.1. It now includes methods that return the fields, methods, and constructors defined by a class. These items are returned as objects of type Field, Method, and Constructor, respectively. These new classes are part of the new java.lang.reflect package, and they each provide methods to obtain complete information about the field, method, or constructor they represent. For example, the Method object has methods to query the name, the parameter types, and the return type of the method it represents.
Besides allowing a program to inspect the members of a class, the java.lang.reflect package also allows a program to manipulate these fields and methods. The Field class defines methods that get and set the value of the represented field for any given object of the appropriate type. Similarly, the Method object defines an invoke() method that allows the represented method to be invoked, and the Constructor class defines a newInstance() method that creates a new object and invokes the represented constructor on it. java.lang.reflect also defines an Array class. It does not represent a specific array, but defines static methods that read and write array elements and dynamically create new arrays.
With the addition of reflection, the Class class has been expanded to represent not just Java classes, but any Java type, including primitive types and array types. There is a special Class object that represents each of the eight Java primitive types, and another special Class object that represents the void type. These special Class objects are available as constants in the wrapper objects for the primitive types. Integer.TYPE is a Class object that represents the int type, for example, and Void.TYPE is a Class object that represents the void type.
Finally, new Java language syntax makes it easier to obtain a Class object that represents a Java class. If you follow the name of a class, interface, or other type with .class, Java evaluates that expression and returns the corresponding Class object. So, for example, the following two expressions are equivalent:
String.class Class.forName(java.lang.String)
Note that this syntax also works with primitive type names: You can write short.class, for example, which returns the same value as Short.TYPE.
JavaBeans is a software component model for Java that has generated quite a lot of interest from many quarters. The JavaBeans API specification defines beans as follows: A Java bean is a reusable software component that can be manipulated visually in a builder tool. The java.beans package defines classes and interfaces designed to work with beans at three distinct levels, described below.
Much of the JavaBeans API is intended for use only by those few people who are writing interface builder tools that manipulate beans. The main thing that a builder tool needs to be able to do with beans is introspect on themi.e., to determine what properties are exposed by a bean, what methods it exports, and what events it can generate. This is information that a builder tool must be able to display to the programmer who is using the tool. The JavaBeans API defines a set of naming conventions for the methods that a bean defines. If a bean follows these conventions, a builder tool can use the new Reflection API to determine what properties, methods, and events the bean supports. The Introspector class uses reflection to obtain information about a bean and presents it to the builder tool in the form of a BeanInfo object, which itself contains various FeatureDescriptor objects describing the properties, methods, and events of the bean.
At the second level, the JavaBeans API contains classes and interfaces intended for use by programmers who are creating beans for others to use. One of the surprising features of the JavaBeans API is that there is no Bean class that all beans must extend. A bean can be of any class; however, as weve seen, beans should follow certain naming conventions. The java.beans classes that a bean creator uses are generally auxiliary classes, used not by the bean, but by the builder tool that manipulates the bean. These auxiliary classes are shipped with a bean, and provide additional information or methods that a builder tool may use with the bean. These classes are not included in finished software built with the bean.
For example, one of the auxiliary classes a bean may define is a custom BeanInfo class to provide information to the builder tool that is not available through the Reflection API. This information might include a human-readable description of the beans properties, methods, and events, for example. Or, if a bean does not follow the standard naming conventions, this custom BeanInfo class must also provide more basic information about the beans properties, methods, and events.
Besides a BeanInfo class, complex beans may also provide a Customizer class and one or more PropertyEditor classes. A Customizer class is a kind of configuration tool or wizard for a bean. It is instantiated by the builder tool in order to guide the user through bean customization. A PropertyEditor class is used to allow the user to edit the value of bean properties of a particular class. Builder tools have built-in property editors for common types such as strings, colors, and fonts, but a bean that has properties of some unusual or custom type may want to provide a PropertyEditor subclass to allow the user to easily specify values for those properties.
Previous | Table of Contents | Next |