Using Java Reflection (一)

2014-11-24 08:24:26 · 作者: · 浏览: 0

Reflection is a feature in the Java programming language. It allows an executing Java program to examine or "introspect" upon itself, and manipulate internal properties of the program. For example, it's possible for a Java class to obtain the names of all its members and display them.

The ability to examine and manipulate a Java class from within itself may not sound like very much, but in other programming languages this feature simply doesn't exist. For example, there is no way in a Pascal, C, or C++ program to obtain information about the functions defined within that program.

One tangible use of reflection is in JavaBeans, where software components can be manipulated visually via a builder tool. The tool uses reflection to obtain the properties of Java components (classes) as they are dynamically loaded.

A Simple Example

To see how reflection works, consider this simple example:


package org.duke.java.reflect;

import java.lang.reflect.Method;

public class DumpMethods {
public static void main(String[] args) {
try {
args = new String[] { "java.util.Stack" };
Class c = Class.forName(args[0]);
// Returns an array of Method objects reflecting all the methods
// declared by the class or interface represented by this Class
// object.
Method m[] = c.getDeclaredMethods();

for (int i = 0; i < m.length; i++)
System.out.println(m[i].toString());

System.out.println("--------------------");
// Returns an array containing Method objects reflecting all the
// public member methods of the class or interface represented by
// this Class object, including those declared by the class or
// interface and those inherited from superclasses and
// superinterfaces.
Method[] methods = c.getMethods();
for (Method method : methods) {
System.out.println(method.toString());
}
} catch (Throwable e) {
System.err.println(e);
}

}
}

the output is:

public synchronized boolean java.util.Stack.empty()
public synchronized java.lang.Object java.util.Stack.peek()
public synchronized java.lang.Object java.util.Stack.pop()
public java.lang.Object java.util.Stack.push(java.lang.Object)
public synchronized int java.util.Stack.search(java.lang.Object)
--------------------
public synchronized java.lang.Object java.util.Vector.clone()
public synchronized boolean java.util.Vector.equals(java.lang.Object)
public synchronized int java.util.Vector.hashCode()
public synchronized java.lang.String java.util.Vector.toString()
public synchronized boolean java.util.Vector.add(java.lang.Object)
public synchronized boolean java.util.Vector.addAll(java.util.Collection)
public void java.util.Vector.clear()
public boolean java.util.Vector.contains(java.lang.Object)
public synchronized boolean java.util.Vector.containsAll(java.util.Collection)
public synchronized boolean java.util.Vector.isEmpty()
public java.util.Iterator java.util.AbstractList.iterator()
public boolean java.util.Vector.remove(java.lang.Object)
public synchronized boolean java.util.Vector.removeAll(java.util.Collection)
public synchronized boolean java.util.Vector.retainAll(java.util.Collection)
public synchronized int java.util.Vector.size()
pub