java反射类内容获取

 1  private void DtoReflect(Object obj, MqDto mqDto) throws Exception {
 2   Map map = getMap(mqDto);
 3   if(obj==null)
 4     return;
 5   //获取Class对象
 6   Class clazz =obj.getClass();
 7   //获取Calss中的所有方法
 8   Method[] methods = clazz.getDeclaredMethods();
 9      //遍历Class中的方法
10    for(int j=0;j<methods.length;j++)
11    {
12     //获取方法对象
13     Method method=methods[j];
14     //获取方法名字
15                 String methodName=method.getName();
16                 //方法名进行小写
17                 String methodNameLower=methodName.toLowerCase();
18                 //寻找xml给过来的字段名找在Class中的方法
19     if (map.containsKey(methodNameLower)) {
20      //数据set到Class的set方法中
21      Method finishTask =clazz.getMethod(methodName,new Class[]{String.class});
22      finishTask.invoke(obj, new Object[]{MqUtil.getFieldValue(mqDto.getBody(),(String)map.get(methodNameLower))});
23     }
24    
25    }
26   
27  }
原文地址:https://www.cnblogs.com/xiayahui/p/5683984.html