Reflection: A New Way to Discover Information about Java classes
By Dan Becker2003-05-24
Using the Reflection Features of Java 1.1: Fields
You can now make a great leap in the amount and quality of information that is available from a Java class. You can query a class's constructors, methods, and fields (another name for object or instance data), using classes in the java.lang.reflect packag package. The classes Constructor, Method, and Field all implement the Member interface, and the methods for enumerating and printing these classes are very similar. The following examples concentrate on the Fields class. A complete example supporting Constructors and Methods is available on the IBM Developer Connection Web site and on the set of CDs.
The next example adds a printFields() method to the printType(...) method. The parameters for this method are a String for pretty printing and a class name that you would like to query.The definition of the printFields() method is shown below.
|
The first step is to query the fields of the class using the getDeclaredFields() method of the Class class. This method returns an array of all the fields of a class. Then a loop goes through each Field in the array and prints its modifiers, type, and name using the getModifiers() , getType() , and getName() methods.
The getType() method returns a class. The name of this class is not printed directly; rather, the class is passed to the printArrayorClassType(...) method. This is an important step because the field may be an array and not merely a primitive or simple type.
If you use getName() to print the name of an array field, you get a garbled name. So it is important to find the type a class represents using the isArray() and getComponentType() methods of the class Class as shown in the following example.
|
Notice that I queried the fields of the class using a getDeclaredFields() method. This returns all the fields declared in a class: public, protected, and private. Typically, programs such as debuggers and heavy-duty developer-class inspectors use this type of method to report on all declared fields. There is also a method called getFields() that returns all public fields of a class, including any field inherited from a class's super type. Typically, applications such as Java beans and run-time object inspectors use this method to report on the public signature of an object. The example program in this article reports on all class information using the former type of reflection.
When you compile and run this program on a class such as java.lang.Integer, you can see five declared fields of the Integer class as well as one declared field of the Number class.
|
Tutorial Pages:
» Introduction
» Outlining the Class Hierarchy Using Java 1.02
» Querying Interfaces Using Java 1.02
» An Improved Java 1.02 Reflection Program
» Using the Reflection Features of Java 1.1: Modifiers
» Using the Reflection Features of Java 1.1: Fields
» Security Issues
» Conclusion
First published by IBM DeveloperWorks
