|
Helping ordinary people create extraordinary websites! |
Reflection: A New Way to Discover Information about Java classesBy Dan Becker2003-05-24
Introduction Most people think of reflection as the return of an image in a mirror or as serious thought. Developers using the Java programming language can include another meaning: a way to discover information about Java classes. Editor's note: The Reflection API has been enhanced since the publication of this article; readers can consult the JDK documentation for java.lang.Class and java.lang.reflect.* for more information. Java Version 1.1 or later allows a Java program to discover information about the fields, methods, and constructors of loaded classes. Also, a Java program can instantiate a class and operate on the underlying objects, within security restrictions. The Reflection Application Programming Interface (API) accommodates applications that work solely on public members of an object (based on its run-time class) or all the members declared by a given class. Although some of this capability is allowed in Java 1.02, the important methods, such as finding constructors or number and type of parameters, are available only in Java 1.1 or later. In this article, I explore the uses of reflection and develop an example program that has many uses. The program can debug other programs, act as a developer's quick reference tool, or explore the class hierarchy of an object-oriented design. 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 |
|