【java】java反射机制,动态获取对象的属性和对应的参数值,并属性按照字典序排序,Field.setAccessible()方法的说明【可用于微信支付 签名生成】
时间:2017-09-05 13:26:05
收藏:0
阅读:2647
方法1:通过get()方法获取属性值
package com.sxd.test.controller; public class FirstCa{ private Integer num; private String name; private Boolean flag; public Integer getNum() { return num; } public void setNum(Integer num) { this.num = num; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Boolean getFlag() { return flag; } public void setFlag(Boolean flag) { this.flag = flag; } }
package com.sxd.test.controller; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; public class Test { @org.junit.Test public void testName() throws Exception { FirstCa ca = new FirstCa(); ca.setFlag(true); ca.setNum(30); Map<String,String> map = getSortMap(ca); map.forEach((k,v)->{ System.out.println(k+">>>"+v); }); } /** * 使用java反射机制,动态获取对象的属性和参数值,排除值为null的情况,并按字典序排序 * @param object * @return * @throws Exception */ private Map<String, String> getSortMap(Object object) throws Exception{ Field[] fields = object.getClass().getDeclaredFields(); Map<String, String> map = new HashMap<String, String>(); //迭代属性 for(Field field : fields){ String name = field.getName(); String methodName = "get" + name.replaceFirst(name.substring(0, 1), name.substring(0, 1) .toUpperCase()); // 调用getter方法获取属性值 // Method getter = object.getClass().getMethod(methodName); // String value = getter.invoke(object)+""; //通过get方法直接获取属性值 field.setAccessible(true); Object value = field.get(object); if (value != null){ map.put(name, value.toString()); } System.out.println("字段名:"+name); System.out.println("字段值:"+field.get(object)); System.out.println("字段java语言修饰符:"+field.getModifiers()); System.out.println("字段类型:"+field.getType()); System.out.println(""); } Map<String, String> sortMap = new TreeMap<String, String>( new Comparator<String>() { @Override public int compare(String arg0, String arg1) { return arg0.compareTo(arg1); } }); sortMap.putAll(map); return sortMap; } }
Field.setAccessible(true);得作用就是让我们在用反射时访问私有变量
运行结果:
方法2: 通过属性自己的getter方法获取属性值
package com.sxd.test.controller; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; public class Test { @org.junit.Test public void testName() throws Exception { FirstCa ca = new FirstCa(); ca.setFlag(true); ca.setNum(30); Map<String,String> map = getSortMap(ca); map.forEach((k,v)->{ System.out.println(k+">>>"+v); }); } /** * 使用java反射机制,动态获取对象的属性和参数值,排除值为null的情况,并按字典序排序 * @param object * @return * @throws Exception */ private Map<String, String> getSortMap(Object object) throws Exception{ Field[] fields = object.getClass().getDeclaredFields(); Map<String, String> map = new HashMap<String, String>(); //迭代属性 for(Field field : fields){ String name = field.getName(); String methodName = "get" + name.replaceFirst(name.substring(0, 1), name.substring(0, 1) .toUpperCase()); // 调用getter方法获取属性值 Method getter = object.getClass().getMethod(methodName); String value = getter.invoke(object)+""; //通过get方法直接获取属性值 // field.setAccessible(true); // Object value = field.get(object); if (value != null && !"null".equals(value)){ map.put(name, value); } System.out.println("字段名:"+name); System.out.println("字段值:"+value); System.out.println("字段java语言修饰符:"+field.getModifiers()); System.out.println("字段类型:"+field.getType()); System.out.println(""); } Map<String, String> sortMap = new TreeMap<String, String>( new Comparator<String>() { @Override public int compare(String arg0, String arg1) { return arg0.compareTo(arg1); } }); sortMap.putAll(map); return sortMap; } }
运行结果:
评论(0)