Java反射使我们可以很方便的动态获取对象属性和调用对象方法。使用得当,能够使我们的代码很简洁优雅。下面是反射的一些小技巧。
/**
* 测试用例
* @author zhanzhengqiang
*/
public class Test {
public static void main(String[] args) throws ClassNotFoundException,
SecurityException, NoSuchFieldException,
IllegalArgumentException, IllegalAccessException,
NoSuchMethodException, InvocationTargetException{
// 获取Class字节码
Class clazz = Class.forName("reflect.fortest.JavaBean");
// 构造JavaBean
// this.intField = 1;
// this.stringField = "a";
// this.stringProField = "b";
JavaBean javaBean = new JavaBean(1, "pri-a", "pro-b");
javaBean.stringPubField = "pub-c";
// 获取Public属性值
Field stringPubField = clazz.getField("stringPubField");
String stringPubFieldValue = (String) stringPubField.get(javaBean);
System.out.println("stringPubField=" + stringPubFieldValue);
// 获取Protected属性值
Field stringProField = clazz.getDeclaredField("stringProField");
stringProField.setAccessible(true);
String stringProFieldValue = (String) stringProField.get(javaBean);
System.out.println("stringProField=" + stringProFieldValue);
// 获取Private属性值
Field stringPriField = clazz.getDeclaredField("stringField");
stringPriField.setAccessible(true);
String stringPriFieldValue = (String) stringPriField.get(javaBean);
System.out.println("stringField=" + stringPriFieldValue);
Field intPriField = clazz.getDeclaredField("intField");
intPriField.setAccessible(true);
int intPriFieldValue = intPriField.getInt(javaBean);
System.out.println("intField=" + intPriFieldValue);
// 获取静态属性值
Field staticField = clazz.getField("staticField");
String staticFieldValue = (String) staticField.get(null);
System.out.println("staticField=" + staticFieldValue);
// 调用publicMethod()
Method pubMethod1 = clazz.getMethod("publicMethod", null);
Object ret1 = pubMethod1.invoke(javaBean, null);
System.out.println("ret1=" + ret1);
// 调用publicMethod(String)
Method pubMethod2 = clazz.getMethod("publicMethod", new Class[]{String.class});
String ret2 = (String) pubMethod2.invoke(javaBean, new Object[]{"StringParam"});
System.out.println("ret2=" + ret2);
// 调用protectedMethod()
Method protectedMethod = clazz.getDeclaredMethod("protectedMethod", null);
protectedMethod.setAccessible(true);
protectedMethod.invoke(javaBean, null);
// 调用privateMethod()
Method privateMethod = clazz.getDeclaredMethod("privateMethod", null);
privateMethod.setAccessible(true);
privateMethod.invoke(javaBean, null);
// 调用staticMethod()
Method staticMethod = clazz.getDeclaredMethod("staticMethod", null);
staticMethod.setAccessible(true);
staticMethod.invoke(null, null);
}
}
/**
* 测试用JavaBean
* @author zhanzhengqiang
*/
package reflect.fortest;
public class JavaBean {
private int intField;
private String stringField;
protected String stringProField;
public String stringPubField;
public static String staticField = "static";
public JavaBean(int intField, String stringField, String stringProField) {
this.intField = intField;
this.stringField = stringField;
this.stringProField = stringProField;
}
public void publicMethod() {
System.out.println("publicMethod is invoked");
}
public String publicMethod(String param) {
System.out.println("publicMethod(" + param + ") is invoked");
return "retValue";
}
protected void protectedMethod() {
System.out.println("protectedMethod is invoked");
}
private void privateMethod() {
System.out.println("privateMethod is invoked");
}
public static void staticMethod() {
System.out.println("staticMethod is invoked");
}
}