通过属性名反射获取get和set方法 发表于 2020-06-14 字数统计 583 | 阅读时长 3 https://blog.csdn.net/weixin_39800144/article/details/79012369 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144package test; import java.lang.annotation.Annotation;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.math.BigDecimal; import everyDay.Person; /** * Created by lightClouds917 * Date 2017/12/29 * Description:根据属性名反射获取get和set方法 */public class TestReflect3 { public static void main(String[] args)throws Exception{ //test removeLine System.out.println(removeLine("abg_dwr")); System.out.println(removeLine("ab_wr")); System.out.println(removeLine("abgwr")); System.out.println(removeLine(null)); //test get Person person1 = new Person(); person1.setAge(11); person1.setName("旺旺"); Object ob = getGetMethod(person1, "name"); System.out.println(ob); //test set Person person2 = new Person(); String field2 = "name"; setValue(person2, person2.getClass(), field2, Person.class.getDeclaredField(field2).getType(), "汪汪"); System.out.println(person2); //获取某个属性的类型 System.out.println(Person.class.getDeclaredField("age").getType()); } /** * 根据属性,获取get方法 * @param ob 对象 * @param name 属性名 * @return * @throws Exception */ public static Object getGetMethod(Object ob , String name)throws Exception{ Method[] m = ob.getClass().getMethods(); for(int i = 0;i < m.length;i++){ if(("get"+name).toLowerCase().equals(m[i].getName().toLowerCase())){ return m[i].invoke(ob); } } return null; } /** * 根据属性,拿到set方法,并把值set到对象中 * @param obj 对象 * @param clazz 对象的class * @param fileName 需要设置值得属性 * @param typeClass * @param value */ public static void setValue(Object obj,Class<?> clazz,String filedName,Class<?> typeClass,Object value){ filedName = removeLine(filedName); String methodName = "set" + filedName.substring(0,1).toUpperCase()+filedName.substring(1); try{ Method method = clazz.getDeclaredMethod(methodName, new Class[]{typeClass}); method.invoke(obj, new Object[]{getClassTypeValue(typeClass, value)}); }catch(Exception ex){ ex.printStackTrace(); } } /** * 通过class类型获取获取对应类型的值 * @param typeClass class类型 * @param value 值 * @return Object */ private static Object getClassTypeValue(Class<?> typeClass, Object value){ if(typeClass == int.class || value instanceof Integer){ if(null == value){ return 0; } return value; }else if(typeClass == short.class){ if(null == value){ return 0; } return value; }else if(typeClass == byte.class){ if(null == value){ return 0; } return value; }else if(typeClass == double.class){ if(null == value){ return 0; } return value; }else if(typeClass == long.class){ if(null == value){ return 0; } return value; }else if(typeClass == String.class){ if(null == value){ return ""; } return value; }else if(typeClass == boolean.class){ if(null == value){ return true; } return value; }else if(typeClass == BigDecimal.class){ if(null == value){ return new BigDecimal(0); } return new BigDecimal(value+""); }else { return typeClass.cast(value); } } /** * 处理字符串 如: abc_dex ---> abcDex * @param str * @return */ public static String removeLine(String str){ if(null != str && str.contains("_")){ int i = str.indexOf("_"); char ch = str.charAt(i+1); char newCh = (ch+"").substring(0, 1).toUpperCase().toCharArray()[0]; String newStr = str.replace(str.charAt(i+1), newCh); String newStr2 = newStr.replace("_", ""); return newStr2; } return str; }} 注:转载文章请注明出处,谢谢~