diff --git a/src/main/java/com/ljunggren/reflectionUtils/ReflectionUtils.java b/src/main/java/com/ljunggren/reflectionUtils/ReflectionUtils.java index 9dfd9b7..366b247 100644 --- a/src/main/java/com/ljunggren/reflectionUtils/ReflectionUtils.java +++ b/src/main/java/com/ljunggren/reflectionUtils/ReflectionUtils.java @@ -5,6 +5,7 @@ import java.lang.reflect.Type; import java.util.Collection; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.stream.Stream; @@ -70,6 +71,13 @@ return false; } + public static boolean isMap(Type type) { + if (isParameterized(type)) { + return getOwnerType(type).getTypeName().equals(Map.class.getName()); + } + return false; + } + public static boolean isArray(Type type) { if (isParameterized(type)) { return false; diff --git a/src/test/java/com/ljunggren/reflectionUtils/ReflectionUtilsTest.java b/src/test/java/com/ljunggren/reflectionUtils/ReflectionUtilsTest.java index bec900a..70062f7 100644 --- a/src/test/java/com/ljunggren/reflectionUtils/ReflectionUtilsTest.java +++ b/src/test/java/com/ljunggren/reflectionUtils/ReflectionUtilsTest.java @@ -28,6 +28,7 @@ Set intergerSet; int primitiveInteger; Integer objectInteger; + Map map; @Test public void constructorTest() { @@ -208,6 +209,20 @@ } @Test + public void isMapTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("map"); + Type fieldType = field.getGenericType(); + assertTrue(ReflectionUtils.isMap(fieldType)); + } + + @Test + public void isMapFalseTest() 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();