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"})); } }
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; } }
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]); } }
原文地址:http://blog.csdn.net/bear_huangzhen/article/details/28314293