ReflectionUtils { @Nonnull public static Map<String, Object> readFieldsAsMap(Object object) { Map<String, Object> fieldsAsMap = Maps.newLinkedHashMap(); Class<?> type = object.getClass(); Field[] fields = type.getDeclaredFields(); for (Field field : fields) { if (Modifier.isStatic(field.getModifiers())) { continue; } field.setAccessible(true); try { String fieldName = field.getName(); Object fieldValue = field.get(object); if (fieldValue != null) { Class<?> fieldValueType = fieldValue.getClass(); if (ClassUtils.isPrimitiveOrWrapper(fieldValueType)) { } else if (fieldValueType.isArray()) { fieldValue = toList(fieldValue); } else if ("java.lang".equals(fieldValueType.getPackage().getName())) { } else { fieldValue = readFieldsAsMap(fieldValue); } } fieldsAsMap.put(fieldName, fieldValue); } catch (IllegalAccessException e) { e.printStackTrace(); } } return fieldsAsMap; }  @Nonnull static String getCallerClassName(); @Nonnull static Class<?> getCallerClass(); static Package getCallerPackage(); static void assertArrayIndex(Object array, int index); static void assertArrayType(Object array); static void assertFieldMatchType(Object object, String fieldName, Class<?> expectedType); @Nonnull static List<T> toList(Object array); @Nonnull static Map<String, Object> readFieldsAsMap(Object object); static final String SUN_REFLECT_REFLECTION_CLASS_NAME; }
@Test public void testReadFieldsAsMap() { Map<String, Object> map = ReflectionUtils.readFieldsAsMap(new String("abc")); echo(map); map = ReflectionUtils.readFieldsAsMap(Arrays.asList(1, 2, 3, 4)); echo(map); Map<String, String> value = Maps.newHashMapWithExpectedSize(3); value.put("a", "a"); value.put("b", "b"); value.put("c", "c"); map = ReflectionUtils.readFieldsAsMap(value); echo(map); map = ReflectionUtils.readFieldsAsMap(String.class); echo(map); map = ReflectionUtils.readFieldsAsMap(Class.class); echo(map); }