diff --git a/src/main/java/com/ljunggren/reflectionUtils/ReflectionUtils.java b/src/main/java/com/ljunggren/reflectionUtils/ReflectionUtils.java index 3c386a1..e76dfc9 100644 --- a/src/main/java/com/ljunggren/reflectionUtils/ReflectionUtils.java +++ b/src/main/java/com/ljunggren/reflectionUtils/ReflectionUtils.java @@ -86,6 +86,28 @@ return Double.class.getName().equals(clazz.getName()); } + public static boolean isShort(Type type) { + if (isParameterized(type)) { + return false; + } + return isShort((Class) type); + } + + public static boolean isShort(Class clazz) { + return Short.class.getName().equals(clazz.getName()); + } + + public static boolean isLong(Type type) { + if (isParameterized(type)) { + return false; + } + return isLong((Class) type); + } + + public static boolean isLong(Class clazz) { + return Long.class.getName().equals(clazz.getName()); + } + public static boolean isFloat(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 ad23a2a..8ea1f24 100644 --- a/src/test/java/com/ljunggren/reflectionUtils/ReflectionUtilsTest.java +++ b/src/test/java/com/ljunggren/reflectionUtils/ReflectionUtilsTest.java @@ -19,8 +19,12 @@ public class ReflectionUtilsTest { Number number; - Double objectDouble; - double primitiveDouble; + int integerPrimitive; + Integer integerObject; + Double doubleObject; + double doublePrimitive; + Short shortObject; + Long longObject; Float floatObject; List stringList; Set integerSet; @@ -28,12 +32,10 @@ Map integerStringMap; HashMap integerStringHashMap; String[] stringArray; - boolean primitiveBoolean; - Boolean objectBoolean; + boolean booleanPrimitive; + Boolean booleanObject; String string; Set intergerSet; - int primitiveInteger; - Integer objectInteger; Iterable stringIterable; BigDecimal bigDecimal; @@ -140,7 +142,7 @@ @Test public void isPrimitiveTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("primitiveBoolean"); + Field field = ReflectionUtilsTest.class.getDeclaredField("booleanPrimitive"); Type fieldType = field.getGenericType(); assertTrue(ReflectionUtils.isPrimitive(fieldType)); } @@ -154,14 +156,14 @@ @Test public void isNumberIntegerTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("objectInteger"); + Field field = ReflectionUtilsTest.class.getDeclaredField("integerObject"); Type fieldType = field.getGenericType(); assertTrue(ReflectionUtils.isNumber(fieldType)); } @Test public void isNumberPrimitiveTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("primitiveInteger"); + Field field = ReflectionUtilsTest.class.getDeclaredField("integerPrimitive"); Type fieldType = field.getGenericType(); assertFalse(ReflectionUtils.isNumber(fieldType)); } @@ -175,14 +177,14 @@ @Test public void isIntegerPrimitiveTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("primitiveInteger"); + Field field = ReflectionUtilsTest.class.getDeclaredField("integerPrimitive"); Type fieldType = field.getGenericType(); assertFalse(ReflectionUtils.isInteger(fieldType)); } @Test public void isIntegerObjectTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("objectInteger"); + Field field = ReflectionUtilsTest.class.getDeclaredField("integerObject"); Type fieldType = field.getGenericType(); assertTrue(ReflectionUtils.isInteger(fieldType)); } @@ -203,14 +205,14 @@ @Test public void isDoublePrimitiveTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("primitiveDouble"); + Field field = ReflectionUtilsTest.class.getDeclaredField("doublePrimitive"); Type fieldType = field.getGenericType(); assertFalse(ReflectionUtils.isDouble(fieldType)); } @Test public void isDoubleObjectTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("objectDouble"); + Field field = ReflectionUtilsTest.class.getDeclaredField("doubleObject"); Type fieldType = field.getGenericType(); assertTrue(ReflectionUtils.isDouble(fieldType)); } @@ -230,6 +232,48 @@ } @Test + public void isShortTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("shortObject"); + Type fieldType = field.getGenericType(); + assertTrue(ReflectionUtils.isShort(fieldType)); + } + + @Test + public void isShortFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("floatObject"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isShort(fieldType)); + } + + @Test + public void isShortParameterizedTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isShort(fieldType)); + } + + @Test + public void isLongTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("longObject"); + Type fieldType = field.getGenericType(); + assertTrue(ReflectionUtils.isLong(fieldType)); + } + + @Test + public void isLongFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("floatObject"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isLong(fieldType)); + } + + @Test + public void isLongParameterizedTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isLong(fieldType)); + } + + @Test public void isFloatTest() throws NoSuchFieldException, SecurityException { Field field = ReflectionUtilsTest.class.getDeclaredField("floatObject"); Type fieldType = field.getGenericType(); @@ -287,7 +331,7 @@ @Test public void isParameterizedFalseTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("primitiveBoolean"); + Field field = ReflectionUtilsTest.class.getDeclaredField("booleanPrimitive"); Type fieldType = field.getGenericType(); assertFalse(ReflectionUtils.isParameterized(fieldType)); } @@ -364,7 +408,7 @@ @Test public void isStringFalseTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("objectBoolean"); + Field field = ReflectionUtilsTest.class.getDeclaredField("booleanObject"); Type fieldType = field.getGenericType(); assertFalse(ReflectionUtils.isString(fieldType)); }