利用反射机制编写校验参数(对象及属性)为空的情况
Posted doubledumbao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用反射机制编写校验参数(对象及属性)为空的情况相关的知识,希望对你有一定的参考价值。
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 }
以上是关于利用反射机制编写校验参数(对象及属性)为空的情况的主要内容,如果未能解决你的问题,请参考以下文章