Java反射的用处:
- 在运行时分析类
- 在运行时查看对象
1、Class对象
Class类时描述类的属性的模板类,每个类都有自己的Class对象,获取类的Class对象的方式有多种:
// 1、借助类对象用getClass()方法获取
Employee e;
Class c1 = e.getClass();
//2、借助类名Class.forName()方式获取
String className = "java.util.Random"
Class c1 = Class.forName(className);
//3、借助类类型class属性获取
Class c1 = Random.class;
//获取Class对象的类名
String className = c1.getName() ;
//返回Class对象的一个实例
Object obj = c1.newInstance();
2、利用反射分析类
java.lang.reflect包中有三个类Field、Method、Constructor分别用于描述类的域、方法和构造器。
这三个类都有getName方法返回项目名称;都有getModifiers方法返回一个整型数值,用不同的位开关描述public、static这样的修饰符使用状况,也可以使用Modifier类的静态方法isPublic, isPrivate, isStatic来判断方法或构造器的修饰属性;Field类有getType方法返回描述域类型的Class对象;Method和Constructor类有返回报告参数类型的方法。Class类中的getFields、getMethods、getConstructors将分别返回类提供的public域、方法和构造器数组,其中包括超类的公有成员;Class类的getDeclaredFields、getDeclaredMethods、getDeclaredConstructors方法将分别返回类中声明的全部域、方法和构造器,其中包括私有和受保护成员,但不包括超类成员。
package javaTest;
import java.util.*;
import java.lang.reflect.*;
/**
* This program uses reflection to print all features of a class
* @author suisui
*/
public class ReflectionTest {
public static void main(String[] args) throws ClassNotFoundException {
String className = "java.util.Random";
Class c1 = Class.forName(className);
printConstructors(c1);
printMethods(c1);
printFields(c1);
}
/**
* Prints all constructors of a class
*/
public static void printConstructors(Class c1) {
Constructor[] constructors = c1.getDeclaredConstructors();
for(Constructor c : constructors) {
String name = c.getName();
String modifiers = Modifier.toString(c.getModifiers());
if(modifiers.length()>0) System.out.print(modifiers+" ");
System.out.print(name+"(");
Class[] paramTypes = c.getExceptionTypes();
for(int j=0; j<paramTypes.length; j++) {
if(j>0) System.out.print(", ");
System.out.print(paramTypes[j].getName());
}
System.out.println(");");
}
}
/**
* print all methods of a class
*/
public static void printMethods(Class c1) {
Method[] methods = c1.getDeclaredMethods();
for(Method m : methods) {
String modifiers = Modifier.toString(m.getModifiers());
if(modifiers.length()>0) System.out.print(modifiers+" ");
Class retType = m.getReturnType();
System.out.print(retType.getName()+" ");
System.out.print(m.getName()+"(");
Class[] paramTypes = m.getParameterTypes();
for(int j=0; j<paramTypes.length; j++) {
if(j>0) System.out.print(", ");
System.out.print(paramTypes[j].getName());
}
System.out.println(");");
}
}
/**
* print all fields of a class
*/
public static void printFields(Class c1) {
Field[] fields = c1.getDeclaredFields();
for(Field f : fields) {
String modifiers = Modifier.toString(f.getModifiers());
if(modifiers.length()>0) System.out.print(modifiers+" ");
System.out.println(f.getType().getName()+ " " + f.getName()+";");
}
}
}
2、运行时用反射分析对象。
运行时,用Field.setAccessible方法更改对象域的修饰属性,Field.get和Field.set方法更改对象域的值。
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
/**
* This object uses reflection to spy on objects
* @author Arya.ATR
*
*/
public class ObjectAnalyzer {
private ArrayList<Object> visited = new ArrayList();
public static void main(String[] args) {
int[] squares = new int[5];
for(int i=1; i<=5; i++) {
squares[i-1] = i*i;
}
System.out.println(new ObjectAnalyzer().toString(squares));
//output : int[]{1,4,9,16,25}
}
/**
* Converts an object to a string representation that lists all fields
* @param obj a Object
* @return a string with the object's class name and all field names and values
*/
public String toString(Object obj) {
if(obj==null) return "null";
//if(visited.contains(obj)) return "...";
Class c1 = obj.getClass();
if(c1 == String.class) return (String)obj;
String res;
if(c1.isArray()) {
res = c1.getComponentType()+"[]{";
for(int i=0; i<Array.getLength(obj); i++) {
if(i>0) res+=",";
if(c1.getComponentType().isPrimitive()) res += Array.get(obj, i);
else res += toString(Array.get(obj, i));
}
return res+"}";
}
res = c1.getName();
do {
res += "[";
Field[] fields = c1.getDeclaredFields();
AccessibleObject.setAccessible(fields, true);
for(Field f : fields) {
if(!Modifier.isStatic(f.getModifiers())) {
if(!res.endsWith("[")) res += ",";
res += f.getName() + " = ";
try {
Class t = f.getType();
Object val = f.get(obj);
if(t.isPrimitive()) res += val;
else res += toString(val);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
res += "]";
c1 = c1.getSuperclass();
}while(c1 != null);
return res;
}
}