BeanUtils { public static void copyPropertiesToMap(Object bean, String prefix, Map<String, Object> map) { Class clazz = bean.getClass(); Method[] methods = bean.getClass().getMethods(); for (Method method : methods) { Class returnc = method.getReturnType(); if (ReflectUtils.isBeanPropertyReadMethod(method)) { String propertyName = ReflectUtils.getPropertyNameFromBeanReadMethod(method); try { if (ReflectUtils.getPropertySetterMethod(clazz, propertyName, returnc) == null) { continue; } } catch (Exception e) { continue; } Object val; try { val = method.invoke(bean); } catch (InvocationTargetException e) { throw new SofaRpcRuntimeException("Can't access copy " + propertyName, e.getCause()); } catch (IllegalAccessException e) { throw new SofaRpcRuntimeException("Can't access copy " + propertyName, e); } if (val != null) { map.put(prefix + propertyName, val); } } } Field[] fields = bean.getClass().getFields(); for (Field field : fields) { String fieldName = field.getName(); if (map.containsKey(prefix + fieldName)) { continue; } int m = field.getModifiers(); if (!Modifier.isStatic(m) && !Modifier.isTransient(m)) { Object val = null; try { if (field.isAccessible()) { val = field.get(bean); } else { try { field.setAccessible(true); val = field.get(bean); } finally { field.setAccessible(false); } } } catch (IllegalAccessException e) { } if (val != null) { map.put(prefix + fieldName, val); } } } }  static void setProperty(Object bean, String name, Class<T> clazz, T value); static T getProperty(Object bean, String name, Class<T> clazz); static void copyPropertiesToMap(Object bean, String prefix, Map<String, Object> map); static void copyProperties(Object src, Object dst, String... ignoreFields); static List<String> getModifiedFields(T src, T dst, String... ignoreFields);  }
@Test public void testCopyProptertiesToMap() throws Exception { TestBean config = new TestBean(); config.setAlias("1111aaaa"); Map<String, Object> map = new HashMap<String, Object>(); BeanUtils.copyPropertiesToMap(config, "", map); Assert.assertTrue(map.size() > 0); Assert.assertEquals(map.get("alias"), "1111aaaa"); }