标签:通过 read field object str system ack parse tar
1、内省
1 /** 2 * 测试内省(可以得到从父类继承的get、set方法) 3 * 4 * @author feigu 5 * 6 */ 7 public class TestIntrospector { 8 public static void main(String[] args) { 9 try { 10 BeanInfo info = Introspector.getBeanInfo(Person.class); 11 // 得到属性描述符 12 PropertyDescriptor[] pds = info.getPropertyDescriptors(); 13 for (PropertyDescriptor pd : pds) { 14 System.out.println(pd.getName()); 15 System.out.println("-------------"); 16 // 得到get方法 17 System.out.println(pd.getReadMethod()); 18 System.out.println("_______________"); 19 // 得到set方法 20 System.out.println(pd.getWriteMethod()); 21 } 22 } catch (IntrospectionException e) { 23 // TODO Auto-generated catch block 24 e.printStackTrace(); 25 } 26 } 27 }
2、反射案例(通过反射实现对象属性的复制)
/** * 通过反射实现对象属性的复制 * @author feigu * */ public class PersonDemo { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub Person p1 = new Person("xiaoming", 12); //Person p2 = new Person(); Dog dog = new Dog(); // propCopy(p1, p2); //promCopy(p1, p2); promCopy(p1, dog); System.out.println(dog); } // 通过字段进行属性复制 public static void propCopy(Person a, Person b) throws Exception { Field[] clazz = a.getClass().getDeclaredFields(); for (Field f : clazz) { // String f=field.getName(); f.setAccessible(true); Object ob = f.get(a); f.set(b, ob); // System.out.println(f); } } // 通过方法进行属性复制 public static void promCopy(Person a, Dog b) throws Exception { Method[] methods_a = a.getClass().getDeclaredMethods(); Class clazz= b.getClass(); for (Method mm_a : methods_a) { // 方法名 String m_a = mm_a.getName(); // c参数类型 Class[] parameterTypes = mm_a.getParameterTypes(); // 返回值类型 Class returnType = mm_a.getReturnType(); if (m_a.startsWith("get") && (parameterTypes == null || parameterTypes.length == 0) && returnType != void.class) { //得到对应参数类型和返回值的b对象方法 String m_b= m_a.replace("get", "set"); try { mm_a.setAccessible(true); Method mm_b = clazz.getDeclaredMethod(m_b, returnType); Object obj = mm_a.invoke(a); mm_b.setAccessible(true); mm_b.invoke(b, obj); } catch (Exception e) { continue; } } } } }
3、属性测试
1 public class TestReflectApp { 2 3 public static void main(String[] args) throws Exception { 4 // 5 InputStream is = new FileInputStream("D:\\eclipse-workspace-bigdata4\\Java25\\src\\objects.properties"); 6 // byte[] bytes = new byte[is.available()]; 7 // is.read(bytes); 8 // //is.close(); 9 //System.out.println(new String(bytes)); 10 Properties prop = new Properties(); 11 prop.load(is); 12 13 // 14 String objClass = prop.getProperty("object.class"); 15 Class clazz = Class.forName(objClass); 16 Object obj = clazz.newInstance(); 17 18 //name 19 String propName = prop.getProperty("object.prop1.name"); 20 String propValue = prop.getProperty("object.prop1.value"); 21 Field f = clazz.getDeclaredField(propName); 22 if(f.getType() == String.class){ 23 f.setAccessible(true); 24 f.set(obj, propValue); 25 } 26 27 28 //name 29 propName = prop.getProperty("object.prop2.name"); 30 propValue = prop.getProperty("object.prop2.value"); 31 f = clazz.getDeclaredField(propName); 32 if(f.getType() == int.class){ 33 f.setAccessible(true); 34 Integer i = Integer.parseInt(propValue) ; 35 f.set(obj, i); 36 } 37 38 }
标签:通过 read field object str system ack parse tar
原文地址:http://www.cnblogs.com/yihaifutai/p/6784612.html