diff --git a/src/main/java/io/ljunggren/reflectionUtils/ReflectionUtils.java b/src/main/java/io/ljunggren/reflectionUtils/ReflectionUtils.java index 96bd556..cef718e 100644 --- a/src/main/java/io/ljunggren/reflectionUtils/ReflectionUtils.java +++ b/src/main/java/io/ljunggren/reflectionUtils/ReflectionUtils.java @@ -89,6 +89,10 @@ return isEqual(clazz, Double.class); } + public static boolean isShort(T t) { + return t instanceof Short; + } + public static boolean isShort(Type type) { return isParameterized(type) ? false : isShort((Class) type); } @@ -97,6 +101,10 @@ return isEqual(clazz, Short.class); } + public static boolean isLong(T t) { + return t instanceof Long; + } + public static boolean isLong(Type type) { return isParameterized(type) ? false : isLong((Class) type); } @@ -105,6 +113,10 @@ return isEqual(clazz, Long.class); } + public static boolean isFloat(T t) { + return t instanceof Float; + } + public static boolean isFloat(Type type) { return isParameterized(type) ? false : isFloat((Class) type); } @@ -113,6 +125,10 @@ return isEqual(clazz, Float.class); } + public static boolean isBigDecimal(T t) { + return t instanceof BigDecimal; + } + public static boolean isBigDecimal(Type type) { return isParameterized(type) ? false : isBigDecimal((Class) type); } @@ -121,6 +137,10 @@ return isEqual(clazz, BigDecimal.class); } + public static boolean isByte(T t) { + return t instanceof Byte; + } + public static boolean isByte(Type type) { return isParameterized(type) ? false : isByte((Class) type); } @@ -129,6 +149,10 @@ return isEqual(clazz, Byte.class); } + public static boolean isBoolean(T t) { + return t instanceof Boolean; + } + public static boolean isBoolean(Type type) { return isParameterized(type) ? false : isBoolean((Class) type); } @@ -149,12 +173,20 @@ return isEqual(clazz, String.class); } + public static boolean isList(T t) { + return t instanceof List; + } + public static boolean isList(Type type) { return isParameterized(type) ? isList(getOwnerType(type)) : isList((Class) type); } public static boolean isList(Class clazz) { - return List.class.isAssignableFrom(clazz); + return clazz == null ? false : List.class.isAssignableFrom(clazz); + } + + public static boolean isMap(T t) { + return t instanceof Map; } public static boolean isMap(Type type) { @@ -162,7 +194,7 @@ } public static boolean isMap(Class clazz) { - return Map.class.isAssignableFrom(clazz); + return clazz == null ? false : Map.class.isAssignableFrom(clazz); } public static boolean isArray(Type type) { diff --git a/src/test/java/io/ljunggren/reflectionUtils/ReflectionUtilsTest.java b/src/test/java/io/ljunggren/reflectionUtils/ReflectionUtilsTest.java index 299d27a..4e293b8 100644 --- a/src/test/java/io/ljunggren/reflectionUtils/ReflectionUtilsTest.java +++ b/src/test/java/io/ljunggren/reflectionUtils/ReflectionUtilsTest.java @@ -8,6 +8,7 @@ import java.lang.reflect.Field; import java.lang.reflect.Type; import java.math.BigDecimal; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; @@ -21,24 +22,28 @@ Number number = 0; int integerPrimitive = 0; Integer integerObject = 0; - Double doubleObject = 0.0; double doublePrimitive = 0.0; - Short shortObject; - Long longObject; - Float floatObject; - BigDecimal bigDecimal; - List stringList; + Double doubleObject = 0.0; + short shortPrimitive = 0; + Short shortObject = 0; + long longPrimitive = 0L; + Long longObject = 0L; + float floatPrimitive = 0f; + Float floatObject = 0f; + BigDecimal bigDecimal = new BigDecimal(0); + byte bytePrimitive = 0; + Byte byteObject = 0; + boolean booleanPrimitive = false; + Boolean booleanObject = false; + String string = "string"; + List stringList = new ArrayList<>(); + Map integerStringMap = new HashMap<>(); + String[] stringArray; Set integerSet; Collection booleanCollection; - Map integerStringMap; HashMap integerStringHashMap; Set intergerSet; Iterable stringIterable; - Byte byteObject; - String string = "string"; - String[] stringArray; - boolean booleanPrimitive; - Boolean booleanObject; @Test public void constructorTest() { @@ -90,6 +95,20 @@ } @Test + public void isParameterizedTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); + Type fieldType = field.getGenericType(); + assertTrue(ReflectionUtils.isParameterized(fieldType)); + } + + @Test + public void isParameterizedFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("booleanPrimitive"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isParameterized(fieldType)); + } + + @Test public void getParameterizedTypesNullTest() { Type[] types = ReflectionUtils.getParameterizedTypes(null); assertEquals(0, types.length); @@ -149,6 +168,13 @@ } @Test + public void isPrimitiveFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isPrimitive(fieldType)); + } + + @Test public void isNumberGenericTest() { assertTrue(ReflectionUtils.isNumber(number)); } @@ -288,6 +314,21 @@ } @Test + public void isShortGenericTest() { + assertTrue(ReflectionUtils.isShort(shortObject)); + } + + @Test + public void isShortGenericPrimitiveTest() { + assertTrue(ReflectionUtils.isShort(shortPrimitive)); + } + + @Test + public void isShortGenericFalseTest() { + assertFalse(ReflectionUtils.isShort(string)); + } + + @Test public void isShortTest() throws NoSuchFieldException, SecurityException { Field field = ReflectionUtilsTest.class.getDeclaredField("shortObject"); Type fieldType = field.getGenericType(); @@ -309,6 +350,26 @@ } @Test + public void isShortNullTest() { + assertFalse(ReflectionUtils.isShort(null)); + } + + @Test + public void isLongGenericTest() { + assertTrue(ReflectionUtils.isLong(longObject)); + } + + @Test + public void isLongGenericPrimitiveTest() { + assertTrue(ReflectionUtils.isLong(longPrimitive)); + } + + @Test + public void isLongGenericFalseTest() { + assertFalse(ReflectionUtils.isLong(string)); + } + + @Test public void isLongTest() throws NoSuchFieldException, SecurityException { Field field = ReflectionUtilsTest.class.getDeclaredField("longObject"); Type fieldType = field.getGenericType(); @@ -330,6 +391,26 @@ } @Test + public void isLongNullTest() { + assertFalse(ReflectionUtils.isLong(null)); + } + + @Test + public void isFloatGenericTest() { + assertTrue(ReflectionUtils.isFloat(floatObject)); + } + + @Test + public void isFloatGenericPrimitiveTest() { + assertTrue(ReflectionUtils.isFloat(floatPrimitive)); + } + + @Test + public void isFloatGenericFalseTest() { + assertFalse(ReflectionUtils.isFloat(string)); + } + + @Test public void isFloatTest() throws NoSuchFieldException, SecurityException { Field field = ReflectionUtilsTest.class.getDeclaredField("floatObject"); Type fieldType = field.getGenericType(); @@ -351,6 +432,21 @@ } @Test + public void isFloatNullTest() { + assertFalse(ReflectionUtils.isFloat(null)); + } + + @Test + public void isBigDecimalGenericTest() { + assertTrue(ReflectionUtils.isBigDecimal(bigDecimal)); + } + + @Test + public void isBigDecimalGenericFalseTest() { + assertFalse(ReflectionUtils.isBigDecimal(string)); + } + + @Test public void isBigDecimalTest() throws NoSuchFieldException, SecurityException { Field field = ReflectionUtilsTest.class.getDeclaredField("bigDecimal"); Type fieldType = field.getGenericType(); @@ -372,24 +468,136 @@ } @Test - public void isPrimitiveFalseTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); - Type fieldType = field.getGenericType(); - assertFalse(ReflectionUtils.isPrimitive(fieldType)); + public void isBigDecimalNullTest() { + assertFalse(ReflectionUtils.isBigDecimal(null)); } @Test - public void isParameterizedTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); - Type fieldType = field.getGenericType(); - assertTrue(ReflectionUtils.isParameterized(fieldType)); + public void isByteGenericTest() { + assertTrue(ReflectionUtils.isByte(byteObject)); } @Test - public void isParameterizedFalseTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("booleanPrimitive"); + public void isByteGenericPrimitiveTest() { + assertTrue(ReflectionUtils.isByte(bytePrimitive)); + } + + @Test + public void isByteGenericFalseTest() { + assertFalse(ReflectionUtils.isByte(string)); + } + + @Test + public void isByteTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("byteObject"); Type fieldType = field.getGenericType(); - assertFalse(ReflectionUtils.isParameterized(fieldType)); + assertTrue(ReflectionUtils.isByte(fieldType)); + } + + @Test + public void isByteFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("string"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isByte(fieldType)); + } + + @Test + public void isByteParameterizedTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isByte(fieldType)); + } + + @Test + public void isByteNullTest() { + assertFalse(ReflectionUtils.isByte(null)); + } + + @Test + public void isBooleanGenericTest() { + assertTrue(ReflectionUtils.isBoolean(booleanObject)); + } + + @Test + public void isBooleanGenericPrimitiveTest() { + assertTrue(ReflectionUtils.isBoolean(booleanPrimitive)); + } + + @Test + public void isBooleanGenericFalseTest() { + assertFalse(ReflectionUtils.isBoolean(string)); + } + + @Test + public void isBooleanTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("booleanObject"); + Type fieldType = field.getGenericType(); + assertTrue(ReflectionUtils.isBoolean(fieldType)); + } + + @Test + public void isBooleanFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("string"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isBoolean(fieldType)); + } + + @Test + public void isBooleanParameterizedTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isBoolean(fieldType)); + } + + @Test + public void isBooleanNullTest() { + assertFalse(ReflectionUtils.isBoolean(null)); + } + + @Test + public void isStringGenericTest() { + assertTrue(ReflectionUtils.isString(string)); + } + + @Test + public void isStringGenericFalseTest() { + assertFalse(ReflectionUtils.isString(integerObject)); + } + + @Test + public void isStringTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("string"); + Type fieldType = field.getGenericType(); + assertTrue(ReflectionUtils.isString(fieldType)); + } + + @Test + public void isStringFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("booleanObject"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isString(fieldType)); + } + + @Test + public void isStringParameterizedTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isString(fieldType)); + } + + @Test + public void isStringNullTest() { + assertFalse(ReflectionUtils.isString(null)); + } + + @Test + public void isListGenericTest() { + assertTrue(ReflectionUtils.isList(stringList)); + } + + @Test + public void isListGenericFalseTest() { + assertFalse(ReflectionUtils.isList(stringArray)); } @Test @@ -414,6 +622,21 @@ } @Test + public void isListNullTest() { + assertFalse(ReflectionUtils.isList(null)); + } + + @Test + public void isMapGenericTest() { + assertTrue(ReflectionUtils.isMap(integerStringMap)); + } + + @Test + public void isMapGenericFalseTest() { + assertFalse(ReflectionUtils.isMap(integerSet)); + } + + @Test public void isMapTest() throws NoSuchFieldException, SecurityException { Field field = ReflectionUtilsTest.class.getDeclaredField("integerStringMap"); Type fieldType = field.getGenericType(); @@ -442,6 +665,11 @@ } @Test + public void isMapNullTest() { + assertFalse(ReflectionUtils.isMap(null)); + } + + @Test public void isArrayTest() throws NoSuchFieldException, SecurityException { Field field = ReflectionUtilsTest.class.getDeclaredField("stringArray"); Type fieldType = field.getGenericType(); @@ -456,84 +684,6 @@ } @Test - public void isByteTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("byteObject"); - Type fieldType = field.getGenericType(); - assertTrue(ReflectionUtils.isByte(fieldType)); - } - - @Test - public void isByteFalseTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("string"); - Type fieldType = field.getGenericType(); - assertFalse(ReflectionUtils.isByte(fieldType)); - } - - @Test - public void isByteParameterizedTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); - Type fieldType = field.getGenericType(); - assertFalse(ReflectionUtils.isByte(fieldType)); - } - - @Test - public void isBooleanTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("booleanObject"); - Type fieldType = field.getGenericType(); - assertTrue(ReflectionUtils.isBoolean(fieldType)); - } - - @Test - public void isBooleanFalseTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("string"); - Type fieldType = field.getGenericType(); - assertFalse(ReflectionUtils.isBoolean(fieldType)); - } - - @Test - public void isBooleanParameterizedTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); - Type fieldType = field.getGenericType(); - assertFalse(ReflectionUtils.isBoolean(fieldType)); - } - - @Test - public void isStringGenericTest() { - assertTrue(ReflectionUtils.isString(string)); - } - - @Test - public void isStringGenericFalseTest() { - assertFalse(ReflectionUtils.isString(integerObject)); - } - - @Test - public void isStringTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("string"); - Type fieldType = field.getGenericType(); - assertTrue(ReflectionUtils.isString(fieldType)); - } - - @Test - public void isStringFalseTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("booleanObject"); - Type fieldType = field.getGenericType(); - assertFalse(ReflectionUtils.isString(fieldType)); - } - - @Test - public void isStringNullTest() { - assertFalse(ReflectionUtils.isString(null)); - } - - @Test - public void isStringParameterizedTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); - Type fieldType = field.getGenericType(); - assertFalse(ReflectionUtils.isString(fieldType)); - } - - @Test public void isSetTest() throws NoSuchFieldException, SecurityException { Field field = ReflectionUtilsTest.class.getDeclaredField("integerSet"); Type fieldType = field.getGenericType();