轉載於:http://blog.csdn.net/bear_huangzhen/article/details/28314293
Java中的反射機制:
- import java.lang.reflect.Method;
- public class DumpMethods {
- public static void main(String[] args) throws Exception{
- //Class對象描述了某個特定的類對象
- Class<?> classType = Class.forName("java.lang.String");
- //通過反射獲取到所有的方法
- Method[] methods = classType.getDeclaredMethods();
- //打印所有的方法
- for(Method m : methods){
- System.out.println(m);
- }
- }
- }
- import java.lang.reflect.Method;
- public class InvokeTester {
- public int add(int parm1, int parm2) {
- return parm1 + parm2;
- }
- public String echo(String message) {
- return "hello:" + message;
- }
- public static void main(String[] args) throws Exception {
- // InvokeTester test = new InvokeTester();
- // System.out.println(test.add(1, 2));
- // System.out.println(test.echo("Java"));
- Class<?> classType = InvokeTester.class;
- //通過反射生成一個InvokeTester類型的對象
- Object o = classType.newInstance();
- //通過反射獲取到add方法
- Method addMethod = classType.getMethod("add", new Class[] { int.class,
- int.class });
- //調用對象的add方法並把參數傳進去
- System.out.println(addMethod.invoke(o,new Object[]{1,2}));
- //通過反射獲取到echo方法
- Method echoMethod = classType.getMethod("echo", new Class[]{String.class});
- //調用對象的echo方法並把參數傳進去
- System.out.println(echoMethod.invoke(o, new Object[]{"Java"}));
- }
- }
要想使用反射,首先需要獲得待處理類或對象所對應的Class對象。
Object o2 = constructor.newInstance(new Object[]{"張三",20});
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
- public class ReflectTest {
- // 該方法實現對Customer的拷貝
- public static Object copy(Object object) throws Exception {
- Class<?> classType = object.getClass();
- Object objectCopy = classType.getConstructor(new Class[] {})
- .newInstance(new Object[] {});
- Field[] fields = classType.getDeclaredFields();
- for (Field field : fields) {
- String name = field.getName();
- String firstLetter = name.substring(0, 1).toUpperCase();// 將屬性的首字母轉換爲大寫
- String getMethodName = "get" + firstLetter + name.substring(1);
- String setMethodName = "set" + firstLetter + name.substring(1);
- Method getMethod = classType.getMethod(getMethodName,
- new Class[] {});
- Method setMethod = classType.getMethod(setMethodName,
- new Class[] { field.getType() });
- Object value = getMethod.invoke(object, new Object[] {});
- setMethod.invoke(objectCopy, new Object[] { value });
- }
- return objectCopy;
- }
- public static void main(String[] args) throws Exception {
- Customer c = new Customer("Tom", 20);
- c.setId(1L);
- Object o = copy(c);
- Customer customerCopy = (Customer) o;
- System.out.println(customerCopy.getId() + "," + customerCopy.getName()
- + "," + customerCopy.getAge());
- }
- }
- class Customer {
- private long id;
- private String name;
- private int age;
- public Customer() {
- }
- public Customer(String name, int age) {
- this.name = name;
- this.age = age;
- }
- public long getId() {
- return id;
- }
- public void setId(long id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- }
Java.lang.Array類提供了動態創建和訪問數組元素的各種靜態方法。
- import java.lang.reflect.Array;
- public class ArrayTester {
- public static void main(String[] args) {
- Class<?> classType = String.class;
- Object array = Array.newInstance(classType, 10);
- Array.set(array, 5, "hello");
- String s = (String) Array.get(array, 5);
- System.out.println(s);
- }
- }
第二個示例:
- import java.lang.reflect.Array;
- public class ArrayTester2 {
- public static void main(String[] args) {
- int[] dims = new int[]{5,10,15};
- Object array = Array.newInstance(Integer.TYPE, dims);
- Object arrayObj = Array.get(array, 3);
- // Class<?> classType = arrayObj.getClass().getComponentType();
- //
- // System.out.println(classType);
- arrayObj = Array.get(arrayObj, 5);
- Array.set(arrayObj, 10, 37);
- int[][][] arrayCast = (int[][][]) array;
- System.out.println(arrayCast[3][5][10]);
- }
- }