Skip to content

Commit

Permalink
Added objectToMap
Browse files Browse the repository at this point in the history
  • Loading branch information
ymenager committed Jan 21, 2019
1 parent 42b2122 commit 9ecb8c3
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
57 changes: 57 additions & 0 deletions core/src/main/java/com/kloudtek/util/ReflectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,72 @@
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

/**
* Various reflection related utility functions
*/
public class ReflectionUtils {
private static final HashSet<Class<?>> OBJ2MAP_PASSTHROUGH = new HashSet<>(Arrays.asList(String.class,
Number.class, Boolean.class, Date.class, Collection.class));
private static final HashSet<String> OBJ2MAP_METHODBLACKLIST = new HashSet<>(Arrays.asList("getClass"));


public static String toString(Method method) {
return "Method " + method.getDeclaringClass().getName() + "#" + method.getName();
}

public static Map<String, Object> objectToMap(Object object) throws InvocationTargetException, IllegalAccessException {
if (object == null) {
return null;
}
Map<String, Object> map = new HashMap<String, Object>();
for (Method method : object.getClass().getMethods()) {
final String methodName = method.getName();
String key = null;
if (! OBJ2MAP_METHODBLACKLIST.contains(methodName) && method.getParameters().length == 0 && !method.getReturnType().getName().equals("void")) {
if (methodName.startsWith("get") && methodName.length() > 3) {
key = getJBFieldName(methodName, 3);
} else if (methodName.startsWith("is") && methodName.length() > 3) {
key = getJBFieldName(methodName, 2);
}
if (key != null) {
Object value = method.invoke(object);
map.put(key, objectToMapConvertObject(value));
}
}
}
return map;
}

private static Object objectToMapConvertObject(Object val) throws InvocationTargetException, IllegalAccessException {
if( val == null ) {
return null;
}
Class<?> cl = val.getClass();
for (Class<?> pt : OBJ2MAP_PASSTHROUGH) {
if( pt.isAssignableFrom(cl)) {
return val;
}
}
if (cl.isPrimitive() || cl.isEnum() ) {
return val;
} else if (val instanceof Optional) {
return ((Optional) val).isPresent() ? objectToMapConvertObject(((Optional) val).get()) : null;
} else {
return objectToMap(val);
}
}

private static String getJBFieldName(String methodName, int index) {
char c = Character.toLowerCase(methodName.charAt(index));
if (methodName.length() > (index + 1)) {
return c + methodName.substring(index + 1);
} else {
return new String(new char[]{c});
}
}

public static String toString(Field field) {
return field.getDeclaringClass().getName() + "#" + field.getName();
}
Expand Down
30 changes: 30 additions & 0 deletions core/src/test/java/com/kloudtek/util/ReflectionUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.kloudtek.util;

import org.junit.Assert;
import org.junit.Test;
import org.omg.CORBA.OBJ_ADAPTER;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.*;

public class ReflectionUtilsTest {
@Test
public void testConvertObjectToMap() throws InvocationTargetException, IllegalAccessException {
Map<String, Object> map = ReflectionUtils.objectToMap(new TestObj1());
HashMap<String, Object> expected = new HashMap<>();
expected.put("text","text");
expected.put("nb", 0);
expected.put("varNull",null);
expected.put("optVar","foo");
expected.put("optVarNull",null);
expected.put("obj2null",null);
HashMap<String, Object> expectedSubMap = new HashMap<>();
expectedSubMap.put("var1",false);
expectedSubMap.put("var2","ccc");
expected.put("obj2NonNull",expectedSubMap);
Assert.assertEquals(expected,map);
}
}
41 changes: 41 additions & 0 deletions core/src/test/java/com/kloudtek/util/TestObj1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.kloudtek.util;

import java.util.Optional;

public class TestObj1 {
private String text = "text";
private String varNull;
private int nb;
private TestObj2 obj2NonNull = new TestObj2();
private TestObj2 obj2null;
private Optional<String> optVar = Optional.of("foo");
private Optional<String> optVarNull = Optional.ofNullable(null);

public String getText() {
return text;
}

public int getNb() {
return nb;
}

public TestObj2 getObj2NonNull() {
return obj2NonNull;
}

public TestObj2 getObj2null() {
return obj2null;
}

public Optional<String> getOptVar() {
return optVar;
}

public Optional<String> getOptVarNull() {
return optVarNull;
}

public String getVarNull() {
return varNull;
}
}
16 changes: 16 additions & 0 deletions core/src/test/java/com/kloudtek/util/TestObj2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.kloudtek.util;

import java.util.Optional;

public class TestObj2 {
private boolean var1 = false;
private String var2 = "ccc";

public boolean isVar1() {
return var1;
}

public String getVar2() {
return var2;
}
}
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
Expand Down

0 comments on commit 9ecb8c3

Please sign in to comment.