diff --git a/src/main/java/io/ljunggren/reflectionUtils/ReflectionUtils.java b/src/main/java/io/ljunggren/reflectionUtils/ReflectionUtils.java index cef718e..b10194c 100644 --- a/src/main/java/io/ljunggren/reflectionUtils/ReflectionUtils.java +++ b/src/main/java/io/ljunggren/reflectionUtils/ReflectionUtils.java @@ -197,12 +197,20 @@ return clazz == null ? false : Map.class.isAssignableFrom(clazz); } + public static boolean isArray(T t) { + return isArray(t.getClass()); + } + public static boolean isArray(Type type) { return isParameterized(type) ? false : isArray((Class) type); } public static boolean isArray(Class clazz) { - return clazz.isArray(); + return clazz == null ? false : clazz.isArray(); + } + + public static boolean isSet(T t) { + return t instanceof Set; } public static boolean isSet(Type type) { @@ -210,7 +218,11 @@ } public static boolean isSet(Class clazz) { - return Set.class.isAssignableFrom(clazz); + return clazz == null ? false : Set.class.isAssignableFrom(clazz); + } + + public static boolean isCollection(T t) { + return t instanceof Collection; } public static boolean isCollection(Type type) { @@ -218,7 +230,11 @@ } public static boolean isCollection(Class clazz) { - return Collection.class.isAssignableFrom(clazz); + return clazz == null ? false : Collection.class.isAssignableFrom(clazz); + } + + public static boolean isIterable(T t) { + return t instanceof Iterable; } public static boolean isIterable(Type type) { @@ -226,7 +242,11 @@ } public static boolean isIterable(Class clazz) { - return Iterable.class.isAssignableFrom(clazz); + return clazz == null ? false : Iterable.class.isAssignableFrom(clazz); + } + + public static boolean hasNoArgsContrustor(Type type) { + return hasNoArgsContrustor((Class) type); } public static boolean hasNoArgsContrustor(Class clazz) { @@ -239,9 +259,6 @@ } private static boolean isEqual(Class class1, Class class2) { - if (class1 == null && class2 == null) { - return true; - } if (class1 == null || class2 == null) { return false; } diff --git a/src/test/java/io/ljunggren/reflectionUtils/ReflectionUtilsTest.java b/src/test/java/io/ljunggren/reflectionUtils/ReflectionUtilsTest.java index 4e293b8..e6b709b 100644 --- a/src/test/java/io/ljunggren/reflectionUtils/ReflectionUtilsTest.java +++ b/src/test/java/io/ljunggren/reflectionUtils/ReflectionUtilsTest.java @@ -11,6 +11,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -38,12 +39,12 @@ String string = "string"; List stringList = new ArrayList<>(); Map integerStringMap = new HashMap<>(); - String[] stringArray; - Set integerSet; + String[] stringArray = new String[] {}; + Set integerSet = new HashSet<>(); Collection booleanCollection; HashMap integerStringHashMap; Set intergerSet; - Iterable stringIterable; + Iterable stringIterable = new ArrayList<>(); @Test public void constructorTest() { @@ -109,6 +110,11 @@ } @Test + public void isParameterizedNullTest() { + assertFalse(ReflectionUtils.isParameterized(null)); + } + + @Test public void getParameterizedTypesNullTest() { Type[] types = ReflectionUtils.getParameterizedTypes(null); assertEquals(0, types.length); @@ -658,7 +664,7 @@ } @Test - public void isHshMapFalseTest() throws NoSuchFieldException, SecurityException { + public void isHashMapFalseTest() throws NoSuchFieldException, SecurityException { Field field = ReflectionUtilsTest.class.getDeclaredField("string"); Type fieldType = field.getGenericType(); assertFalse(ReflectionUtils.isMap(fieldType)); @@ -670,6 +676,16 @@ } @Test + public void isArrayGenericTest() { + assertTrue(ReflectionUtils.isArray(stringArray)); + } + + @Test + public void isArrayGenericFalseTest() { + assertFalse(ReflectionUtils.isArray(stringList)); + } + + @Test public void isArrayTest() throws NoSuchFieldException, SecurityException { Field field = ReflectionUtilsTest.class.getDeclaredField("stringArray"); Type fieldType = field.getGenericType(); @@ -684,6 +700,21 @@ } @Test + public void isArrayNullTest() { + assertFalse(ReflectionUtils.isArray(null)); + } + + @Test + public void isSetGenericTest() { + assertTrue(ReflectionUtils.isSet(integerSet)); + } + + @Test + public void isSetGenericFalseTest() { + assertFalse(ReflectionUtils.isSet(stringList)); + } + + @Test public void isSetTest() throws NoSuchFieldException, SecurityException { Field field = ReflectionUtilsTest.class.getDeclaredField("integerSet"); Type fieldType = field.getGenericType(); @@ -705,6 +736,21 @@ } @Test + public void isSetNullTest() { + assertFalse(ReflectionUtils.isSet(null)); + } + + @Test + public void isCollectionGenericTest() { + assertTrue(ReflectionUtils.isCollection(stringList)); + } + + @Test + public void isCollectionGenericFalseTest() { + assertFalse(ReflectionUtils.isCollection(string)); + } + + @Test public void isCollectionTest() throws NoSuchFieldException, SecurityException { Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); Type fieldType = field.getGenericType(); @@ -719,6 +765,21 @@ } @Test + public void isCollectionNullTest() { + assertFalse(ReflectionUtils.isCollection(null)); + } + + @Test + public void isIterableGenericTest() { + assertTrue(ReflectionUtils.isIterable(stringIterable)); + } + + @Test + public void isIterableGenericFalseTest() { + assertFalse(ReflectionUtils.isIterable(string)); + } + + @Test public void isIterableTest() throws NoSuchFieldException, SecurityException { Field field = ReflectionUtilsTest.class.getDeclaredField("stringIterable"); Type fieldType = field.getGenericType(); @@ -733,10 +794,20 @@ } @Test + public void isIterableNullTest() { + assertFalse(ReflectionUtils.isIterable(null)); + } + + @Test + public void hasNoArgsConstructorTest() throws NoSuchFieldException, SecurityException { + assertTrue(ReflectionUtils.hasNoArgsContrustor(ReflectionUtilsTest.class)); + } + + @Test public void hasNoArgsConstructorFalseTest() throws NoSuchFieldException, SecurityException { Field field = ReflectionUtilsTest.class.getDeclaredField("string"); Type fieldType = field.getGenericType(); - assertFalse(ReflectionUtils.hasNoArgsContrustor((Class) fieldType)); + assertFalse(ReflectionUtils.hasNoArgsContrustor(fieldType)); } @Test