diff --git a/src/main/java/com/ljunggren/reflectionUtils/ReflectionUtils.java b/src/main/java/com/ljunggren/reflectionUtils/ReflectionUtils.java index 366b247..57cc356 100644 --- a/src/main/java/com/ljunggren/reflectionUtils/ReflectionUtils.java +++ b/src/main/java/com/ljunggren/reflectionUtils/ReflectionUtils.java @@ -73,9 +73,13 @@ public static boolean isMap(Type type) { if (isParameterized(type)) { - return getOwnerType(type).getTypeName().equals(Map.class.getName()); + return isMap((Class) getOwnerType(type)); } - return false; + return isMap((Class) type); + } + + public static boolean isMap(Class clazz) { + return Map.class.isAssignableFrom(clazz); } public static boolean isArray(Type type) { diff --git a/src/test/java/com/ljunggren/reflectionUtils/ReflectionUtilsTest.java b/src/test/java/com/ljunggren/reflectionUtils/ReflectionUtilsTest.java index 70062f7..8cb89c3 100644 --- a/src/test/java/com/ljunggren/reflectionUtils/ReflectionUtilsTest.java +++ b/src/test/java/com/ljunggren/reflectionUtils/ReflectionUtilsTest.java @@ -9,6 +9,7 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.Collection; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -21,6 +22,7 @@ Set integerSet; Collection booleanCollection; Map integerStringMap; + HashMap integerStringHashMap; String[] stringArray; boolean primitiveBoolean; Boolean objectBoolean; @@ -28,7 +30,6 @@ Set intergerSet; int primitiveInteger; Integer objectInteger; - Map map; @Test public void constructorTest() { @@ -210,7 +211,7 @@ @Test public void isMapTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("map"); + Field field = ReflectionUtilsTest.class.getDeclaredField("integerStringMap"); Type fieldType = field.getGenericType(); assertTrue(ReflectionUtils.isMap(fieldType)); } @@ -223,6 +224,20 @@ } @Test + public void isHashMapTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("integerStringHashMap"); + Type fieldType = field.getGenericType(); + assertTrue(ReflectionUtils.isMap(fieldType)); + } + + @Test + public void isHshMapFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("string"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isMap(fieldType)); + } + + @Test public void isArrayTest() throws NoSuchFieldException, SecurityException { Field field = ReflectionUtilsTest.class.getDeclaredField("stringArray"); Type fieldType = field.getGenericType();