1 public class Demo02 { 2 @SuppressWarnings("all") 3 public static void main(String[] args) throws Exception { 4 // 另外一個com.sg.myReflection.bean包下的User類 5 String path = "com.sg.myReflection.bean.User"; 6 try { 7 Class clazz = Class.forName(path); 8 9 // 獲取類名 10 String strName01 = clazz.getName();// 獲取完整類名com.sg.myReflection.bean.User 11 String strName02 = clazz.getSimpleName();// 直接獲取類名 User 12 13 // 獲取屬性 14 Field[] field01 = clazz.getFields(); // 返回屬性爲public的字段 15 Field[] field02 = clazz.getDeclaredFields(); // 返回全部的屬性 16 Field field03 = clazz.getDeclaredField("id"); // 獲取屬性爲id的字段 17 18 // 獲取普通方法 19 Method[] Method01 = clazz.getDeclaredMethods(); // 返回public方法 20 Method method = clazz.getDeclaredMethod("getId", null); // 返回getId這個方法,若是沒有參數,就默認爲null 21 22 23 // 獲取構造方法 24 User u1 = (User) clazz.newInstance(); // 獲取無參的構造函數這裏的前提的保證類中應該有無參的構造函數 25 // 獲取參數爲(int,String,int)的構造函數 26 Constructor c2 = clazz.getDeclaredConstructor(int.class, String.class, int.class); 27 // 經過有參構造函數建立對象 28 User u2 = (User) c2.newInstance(1001, "小小", 18); 29 30 31 // 經過反射調用普通方法 32 User u3 = (User) clazz.newInstance(); 33 Method method03 = clazz.getDeclaredMethod("setId", int.class); 34 method.invoke(u3, 1002); // 把對象u3的id設置爲1002 35 36 37 38 // 經過反射操做普通的屬性 39 User u4 = (User) clazz.newInstance(); 40 Field f = clazz.getDeclaredField("name"); 41 f.setAccessible(true); // 設置屬性能夠直接的進行訪問 42 f.set(u4, "石頭"); 43 44 } catch (ClassNotFoundException e) { 45 e.printStackTrace(); 46 } 47 } 48 }
1 public static void getObjectValue(Object object) throws Exception { 2 //咱們項目的全部實體類都繼承BaseDomain (全部實體基類:該類只是串行化一下) 3 //不須要的本身去掉便可 4 if (object != null && object instanceof BaseDomain) {//if (object!=null ) ----begin 5 // 拿到該類 6 Class<?> clz = object.getClass(); 7 // 獲取實體類的全部屬性,返回Field數組 8 Field[] fields = clz.getDeclaredFields(); 9 10 for (Field field : fields) {// --for() begin 11 System.out.println(field.getGenericType());//打印該類的全部屬性類型 12 13 // 若是類型是String 14 if (field.getGenericType().toString().equals( 15 "class java.lang.String")) { // 若是type是類類型,則前面包含"class ",後面跟類名 16 // 拿到該屬性的gettet方法 17 /** 18 * 這裏須要說明一下:他是根據拼湊的字符來找你寫的getter方法的 19 * 在Boolean值的時候是isXXX(默認使用ide生成getter的都是isXXX) 20 * 若是出現NoSuchMethod異常 就說明它找不到那個gettet方法 須要作個規範 21 */ 22 Method m = (Method) object.getClass().getMethod( 23 "get" + getMethodName(field.getName())); 24 25 String val = (String) m.invoke(object);// 調用getter方法獲取屬性值 26 if (val != null) { 27 System.out.println("String type:" + val); 28 } 29 30 } 31 32 // 若是類型是Integer 33 if (field.getGenericType().toString().equals( 34 "class java.lang.Integer")) { 35 Method m = (Method) object.getClass().getMethod( 36 "get" + getMethodName(field.getName())); 37 Integer val = (Integer) m.invoke(object); 38 if (val != null) { 39 System.out.println("Integer type:" + val); 40 } 41 42 } 43 44 // 若是類型是Double 45 if (field.getGenericType().toString().equals( 46 "class java.lang.Double")) { 47 Method m = (Method) object.getClass().getMethod( 48 "get" + getMethodName(field.getName())); 49 Double val = (Double) m.invoke(object); 50 if (val != null) { 51 System.out.println("Double type:" + val); 52 } 53 54 } 55 56 // 若是類型是Boolean 是封裝類 57 if (field.getGenericType().toString().equals( 58 "class java.lang.Boolean")) { 59 Method m = (Method) object.getClass().getMethod( 60 field.getName()); 61 Boolean val = (Boolean) m.invoke(object); 62 if (val != null) { 63 System.out.println("Boolean type:" + val); 64 } 65 66 } 67 68 // 若是類型是boolean 基本數據類型不同 這裏有點說名若是定義名是 isXXX的 那就全都是isXXX的 69 // 反射找不到getter的具體名 70 if (field.getGenericType().toString().equals("boolean")) { 71 Method m = (Method) object.getClass().getMethod( 72 field.getName()); 73 Boolean val = (Boolean) m.invoke(object); 74 if (val != null) { 75 System.out.println("boolean type:" + val); 76 } 77 78 } 79 // 若是類型是Date 80 if (field.getGenericType().toString().equals( 81 "class java.util.Date")) { 82 Method m = (Method) object.getClass().getMethod( 83 "get" + getMethodName(field.getName())); 84 Date val = (Date) m.invoke(object); 85 if (val != null) { 86 System.out.println("Date type:" + val); 87 } 88 89 } 90 // 若是類型是Short 91 if (field.getGenericType().toString().equals( 92 "class java.lang.Short")) { 93 Method m = (Method) object.getClass().getMethod( 94 "get" + getMethodName(field.getName())); 95 Short val = (Short) m.invoke(object); 96 if (val != null) { 97 System.out.println("Short type:" + val); 98 } 99 100 } 101 // 若是還須要其餘的類型請本身作擴展 102 103 }//for() --end 104 105 }//if (object!=null ) ----end 106 } 107 108 // 把一個字符串的第一個字母大寫、效率是最高的、 109 private static String getMethodName(String fildeName) throws Exception{ 110 byte[] items = fildeName.getBytes(); 111 items[0] = (byte) ((char) items[0] - 'a' + 'A'); 112 return new String(items); 113 }