This section provides a tutorial example on how to reflect a class as Class object and introspect its members: fields, methods, etc.
java.lang.Class also offers methods to support class reflection and introspection:
String getName() - Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String.
Constructor<?>[] getDeclaredConstructors() - Returns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object.
Field[] getDeclaredFields() - Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object.
Method[] getDeclaredMethods() - Returns an array of Method objects reflecting all the methods declared by the class or interface represented by this Class object.
Class<?>[] getDeclaredClasses() - Returns an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object.
Class<? super T> getSuperclass() - Returns the Class representing the superclass of the entity (class, interface, primitive type or void) represented by this Class.
Here is tutorial example program showing you how to introspect a Java class:
/**
* ClassReflection.java
* Copyright (c) 2010 by Dr. Herong Yang, herongyang.com
*/
import java.lang.reflect.*;
class ClassReflection {
public static void main(String[] a) {
java.io.PrintStream out = System.out;
Class cls = java.io.FilterOutputStream.class;
out.println("Class name:");
out.println(" "+cls.getName());
Class p = cls.getSuperclass();
out.println("Parent class:");
out.println(" "+p.getName());
Constructor[] c = cls.getDeclaredConstructors();
out.println("Constructors:");
for (int i = 0; i<c.length; i++)
out.println(" "+c[i].toString());
Field[] f = cls.getDeclaredFields();
out.println("Fields:");
for (int i = 0; i<f.length; i++)
out.println(" "+f[i].toString());
Method[] m = cls.getDeclaredMethods();
out.println("Methods:");
for (int i = 0; i<m.length; i++)
out.println(" "+m[i].toString());
Class[] s = cls.getDeclaredClasses();
out.println("Inner classes:");
for (int i = 0; i<s.length; i++)
out.println(" "+s[i].toString());
}
}
When executed on my Windows XP system with JDK 1.6.0,
I got this result:
C:\herong\jvm>java ClassReflection
Class name:
java.io.FilterOutputStream
Parent class:
java.io.OutputStream
Constructors:
public java.io.FilterOutputStream(java.io.OutputStream)
Fields:
protected java.io.OutputStream java.io.FilterOutputStream.out
Methods:
public void java.io.FilterOutputStream.write(int) throws java.i...
public void java.io.FilterOutputStream.write(byte[]) throws jav...
public void java.io.FilterOutputStream.write(byte[],int,int) th...
public void java.io.FilterOutputStream.close() throws java.io.I...
public void java.io.FilterOutputStream.flush() throws java.io.I...
Inner classes:
The class reflection feature in Java can be useful to introspect any class by
retrieving fields, methods and other members out of the class as reflection objects.
If you want to, you can actually invoke a class method as a reflection object using
the Method.invoke() method.