Java examples for Reflection:Java Bean
backup Java Bean Property
//package com.java2s; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] argv) throws Exception { Object bean = "java2s.com"; System.out.println(backupProp(bean)); }/*from w w w. j a v a 2 s. co m*/ public static Map<String, Object> backupProp(Object bean) { Map<String, Object> result = new HashMap<>(); try { BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo .getPropertyDescriptors(); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { String fieldName = propertyDescriptor.getName(); Method readMethod = propertyDescriptor.getReadMethod(); Object invoke = readMethod.invoke(bean, new Object[] {}); if (!fieldName.equalsIgnoreCase("class")) { result.put(fieldName, invoke); } } } catch (Exception e) { e.printStackTrace(); } return result; } }