1 import java.beans.IntrospectionException; 2 import java.beans.PropertyDescriptor; 3 import java.lang.reflect.InvocationTargetException; 4 import java.lang.reflect.Method; 5 import java.util.ArrayList; 6 import java.util.List; 7 8 import org.junit.Test; 9 10 public class Main { 11 /** 12 * @param args 13 * @throws Exception 14 */ 15 public static void main(String[] args) throws Exception { 16 Main main = new Main(); 17 Hello hello = new Hello(); 18 List<String> list = new ArrayList<String>(); 19 list.add("phone"); 20 list.add("Android"); 21 list.add("WIFI"); 22 list.add("isGood"); 23 /* 24 * 出現錯誤: 25 * Exception in thread "main" java.beans.IntrospectionException: Method not found: isASdf 26 * at java.beans.PropertyDescriptor.<init>(Unknown Source) 27 * at java.beans.PropertyDescriptor.<init>(Unknown Source) 28 * at hello.Main.getMethod(Main.java:34) 29 * at hello.Main.main(Main.java:28) 30 */ 31 list.add("aSdf"); 32 list.add("Nokia"); 33 list.add("canCrackWalnuts"); 34 list.add("isUsable"); 35 for(String str : list) { 36 main.getMethod(str, hello); 37 } 38 } 39 40 41 public void getMethod(String name, Object obj) throws Exception { 42 String field = name; 43 PropertyDescriptor desc = new PropertyDescriptor(field, obj.getClass()); 44 Method method = desc.getReadMethod(); 45 System.out.println(method.invoke(obj)); 46 Method setMethod = desc.getWriteMethod(); 47 System.out.printf("%s\r\n", method); 48 System.out.printf("%s\r\n", method.invoke(obj)); 49 System.out.printf("%s\r\n", setMethod); 50 } 51 52 53 54 } 55 56 class Hello { 57 String phone; 58 String Android; 59 String WIFI; 60 String isGood; 61 String Nokia; 62 String aSdf; 63 boolean isUsable; 64 boolean canCrackWalnuts; 65 public String getPhone() { 66 return phone; 67 } 68 public void setPhone(String phone) { 69 this.phone = phone; 70 } 71 public String getAndroid() { 72 return Android; 73 } 74 public void setAndroid(String android) { 75 Android = android; 76 } 77 public String getWIFI() { 78 return WIFI; 79 } 80 public void setWIFI(String wIFI) { 81 WIFI = wIFI; 82 } 83 public String getIsGood() { 84 return isGood; 85 } 86 public void setIsGood(String isGood) { 87 this.isGood = isGood; 88 } 89 public String getNokia() { 90 return Nokia; 91 } 92 public void setNokia(String nokia) { 93 Nokia = nokia; 94 } 95 /** 96 * 並不是getASdf 97 * @return 98 */ 99 public String getaSdf() { 100 return aSdf; 101 } 102 public void setaSdf(String aSdf) { 103 this.aSdf = aSdf; 104 } 105 106 /** 107 * 並不是isIsUsable 108 * @return 109 */ 110 public boolean isUsable() { 111 return isUsable; 112 } 113 public void setUsable(boolean isUsable) { 114 this.isUsable = isUsable; 115 } 116 public boolean isCanCrackWalnuts() { 117 return canCrackWalnuts; 118 } 119 public void setCanCrackWalnuts(boolean canCrackWalnuts) { 120 this.canCrackWalnuts = canCrackWalnuts; 121 } 122 @Override 123 public String toString() { 124 return "Hello [phone=" + phone + ", Android=" + Android + ", WIFI=" + WIFI + ", isGood=" + isGood + ", Nokia=" 125 + Nokia + ", aSdf=" + aSdf + ", isUsable=" + isUsable + ", canCrackWalnuts=" + canCrackWalnuts + "]"; 126 } 127 128 }