1. 程式人生 > >利用反射機制編寫校驗參數(對象及屬性)為空的情況

利用反射機制編寫校驗參數(對象及屬性)為空的情況

check 參數 lec reflect ram declare 異常 ase urn

  2 
  3 import java.lang.reflect.Field;
  4 import java.lang.reflect.InvocationTargetException;
  5 import java.lang.reflect.Method;
  6 import java.util.ArrayList;
  7 import java.util.Arrays;
  8 import java.util.List;
  9 
 10 public class MyCheck {
 11     public static void main(String[] args) throws
IllegalAccessException, 12 IllegalArgumentException, InvocationTargetException { 13 MyCheck check = new MyCheck(); 14 Student student = new Student(); 15 student.setName("zzzz"); 16 student.setAddress("sss"); 17 student.setIdno(""); 18 // student.setPhone("sss");
19 check.test1(student); 20 21 } 22 23 public void test(Student student) throws IllegalAccessException, 24 IllegalArgumentException, InvocationTargetException { 25 CheckParamUtil<Student> checkParamUtil = new CheckParamUtil<Student>(); 26 checkParamUtil.checkParam(student);
27 System.out.println("student:" + student); 28 } 29 30 public void test1(Student student) throws IllegalAccessException, 31 IllegalArgumentException, InvocationTargetException { 32 CheckParamUtil<Student> checkParamUtil = new CheckParamUtil<Student>(); 33 checkParamUtil.checkParam(student, "name", "address", "idno"); 34 System.out.println("student:" + student); 35 } 36 37 } 38 39 class Student { 40 private String name; 41 private int age; 42 private String address; 43 private String idno; 44 private String phone; 45 46 public Student() { 47 } 48 49 public Student(String name, int age, String address, String idno, 50 String phone) { 51 super(); 52 this.name = name; 53 this.age = age; 54 this.address = address; 55 this.idno = idno; 56 this.phone = phone; 57 } 58 59 public String getName() { 60 return name; 61 } 62 63 public void setName(String name) { 64 this.name = name; 65 } 66 67 public int getAge() { 68 return age; 69 } 70 71 public void setAge(int age) { 72 this.age = age; 73 } 74 75 public String getAddress() { 76 return address; 77 } 78 79 public void setAddress(String address) { 80 this.address = address; 81 } 82 83 public String getIdno() { 84 return idno; 85 } 86 87 public void setIdno(String idno) { 88 this.idno = idno; 89 } 90 91 public String getPhone() { 92 return phone; 93 } 94 95 public void setPhone(String phone) { 96 this.phone = phone; 97 } 98 99 @Override 100 public String toString() { 101 return "Student [name=" + name + ", age=" + age + ", address=" 102 + address + ", idno=" + idno + ", phone=" + phone + "]"; 103 } 104 105 public void a() { 106 System.out.println("haha"); 107 } 108 } 109 110 class CheckParamUtil<T> { 111 /** 112 * 113 * @Title:checkParam 114 * @Description:(該方法用來校驗對象及其屬性是否為空) 115 * @param t 116 * @param args 117 * @author 118 * @throws InvocationTargetException 119 * @throws IllegalArgumentException 120 * @throws IllegalAccessException 121 * @修改時間:2017年6月9日 下午2:18:54 122 * @修改內容:創建 123 */ 124 public void checkParam(T t, String... args) throws IllegalAccessException, 125 IllegalArgumentException, InvocationTargetException { 126 // 如果傳入的對象為空,則直接跑出異常 127 if (t == null) { 128 throw new IllegalArgumentException("This object cann‘t be empty!"); 129 } 130 Class<? extends Object> clazz = t.getClass(); 131 // 定義屬性列表 132 List<String> argsList = new ArrayList<String>(); 133 // 如果傳入的屬性名不為空,則將傳入的屬性名放入屬性列表 134 if (args != null && args.length > 0) { 135 argsList = Arrays.asList(args); 136 } else {// 如果傳入的屬性名為空,則將所有屬性名放入屬性列表 137 Field[] fields = clazz.getDeclaredFields(); 138 for (Field field : fields) { 139 argsList.add(field.getName()); 140 } 141 } 142 // 獲取該類自定義的方法數組 143 Method[] methods = clazz.getDeclaredMethods(); 144 for (Method method : methods) { 145 // 方法名 146 String methodName = method.getName(); 147 // 獲取方法對應的屬性名 148 String fieldName = ""; 149 if (methodName.length() >= 4) { 150 fieldName = methodName.substring(3, 4).toLowerCase() 151 + methodName.substring(4); 152 // 如果方法是“get方法”,並且屬性列表中包含該方法對應的屬性名 153 if (methodName.startsWith("get") 154 && argsList.contains(fieldName)) { 155 // 如果為null,拋出異常 156 if (method.invoke(t) == null) { 157 throw new IllegalArgumentException(fieldName 158 + " cann‘t be null!"); 159 } 160 // 如果該方法返回類型為String,返回結果為空字符串,拋出異常。 161 Class<?> returnType = method.getReturnType(); 162 String returnTypeName = returnType.getSimpleName(); 163 if (returnTypeName.equals("String") 164 && "".equals(method.invoke(t))) { 165 throw new IllegalArgumentException(fieldName 166 + " cann‘t be empty String!"); 167 } 168 } 169 } 170 171 } 172 173 } 174 }

利用反射機制編寫校驗參數(對象及屬性)為空的情況