diff --git a/src/main/java/com/ljunggren/reflectionUtils/ReflectionUtils.java b/src/main/java/com/ljunggren/reflectionUtils/ReflectionUtils.java index 57cc356..f86d60c 100644 --- a/src/main/java/com/ljunggren/reflectionUtils/ReflectionUtils.java +++ b/src/main/java/com/ljunggren/reflectionUtils/ReflectionUtils.java @@ -9,7 +9,6 @@ import java.util.Set; import java.util.stream.Stream; -import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.reflect.FieldUtils; public class ReflectionUtils { @@ -18,6 +17,10 @@ return type instanceof ParameterizedType ? ((ParameterizedType) type).getRawType() : null; } + public static boolean isParameterized(Type type) { + return type instanceof ParameterizedType; + } + public static Type[] getParameterizedTypes(Type type) { if (type instanceof ParameterizedType) { return ((ParameterizedType) type).getActualTypeArguments(); @@ -42,38 +45,71 @@ if (isParameterized(type)) { return false; } - return ClassUtils.isPrimitiveOrWrapper((Class) type); + return isPrimitive((Class) type); + } + + public static boolean isPrimitive(Class clazz) { + return clazz.isPrimitive(); + } + + public static boolean isNumber(Type type) { + if (isParameterized(type)) { + return false; + } + return isNumber((Class) type); + } + + public static boolean isNumber(Class clazz) { + return Number.class.isAssignableFrom(clazz); } public static boolean isInteger(Type type) { - if (isPrimitive(type)) { - return type.getTypeName().equals(Integer.class.getName()) || - type.getTypeName().equals(int.class.getName()); + if (isParameterized(type)) { + return false; } - return false; + return isInteger((Class) type); } - public static boolean isParameterized(Type type) { - return type instanceof ParameterizedType; + public static boolean isInteger(Class clazz) { + return Integer.class.getName().equals(clazz.getName()); + } + + public static boolean isDouble(Type type) { + if (isParameterized(type)) { + return false; + } + return isDouble((Class) type); + } + + public static boolean isDouble(Class clazz) { + return Double.class.getName().equals(clazz.getName()); } public static boolean isString(Type type) { if (isParameterized(type)) { return false; } - return type.getTypeName().equals(String.class.getName()); + return isString((Class) type); + } + + public static boolean isString(Class clazz) { + return String.class.getName().equals(clazz.getName()); } public static boolean isList(Type type) { if (isParameterized(type)) { - return getOwnerType(type).getTypeName().equals(List.class.getName()); + return isList(getOwnerType(type)); } - return false; + return isList((Class) type); + } + + public static boolean isList(Class clazz) { + return List.class.isAssignableFrom(clazz); } public static boolean isMap(Type type) { if (isParameterized(type)) { - return isMap((Class) getOwnerType(type)); + return isMap(getOwnerType(type)); } return isMap((Class) type); } @@ -86,25 +122,51 @@ if (isParameterized(type)) { return false; } - return ((Class) type).isArray(); + return isArray((Class) type); + } + + public static boolean isArray(Class clazz) { + return clazz.isArray(); } public static boolean isSet(Type type) { if (isParameterized(type)) { - return getOwnerType(type).getTypeName().equals(Set.class.getName()); + return isSet(getOwnerType(type)); } - return false; + return isSet((Class) type); } - public static boolean hasNoArgsContrustor(Class clazz) { - return Stream.of(clazz.getConstructors()) - .allMatch(constructor -> constructor.getParameterCount() == 0); + public static boolean isSet(Class clazz) { + return Set.class.isAssignableFrom(clazz); + } + + public static boolean isCollection(Type type) { + if (isParameterized(type)) { + return isCollection(getOwnerType(type)); + } + return isCollection((Class) type); } public static boolean isCollection(Class clazz) { return Collection.class.isAssignableFrom(clazz); } + + public static boolean isIterable(Type type) { + if (isParameterized(type)) { + return isIterable(getOwnerType(type)); + } + return isIterable((Class) type); + } + + public static boolean isIterable(Class clazz) { + return Iterable.class.isAssignableFrom(clazz); + } + public static boolean hasNoArgsContrustor(Class clazz) { + return Stream.of(clazz.getConstructors()) + .allMatch(constructor -> constructor.getParameterCount() == 0); + } + public static Object getFieldValue(Field field, Object target) throws IllegalAccessException { return FieldUtils.readField(field, target, true); } diff --git a/src/test/java/com/ljunggren/reflectionUtils/ReflectionUtilsTest.java b/src/test/java/com/ljunggren/reflectionUtils/ReflectionUtilsTest.java index 8cb89c3..9c566b2 100644 --- a/src/test/java/com/ljunggren/reflectionUtils/ReflectionUtilsTest.java +++ b/src/test/java/com/ljunggren/reflectionUtils/ReflectionUtilsTest.java @@ -6,7 +6,6 @@ import static org.junit.Assert.assertTrue; import java.lang.reflect.Field; -import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.Collection; import java.util.HashMap; @@ -18,6 +17,9 @@ public class ReflectionUtilsTest { + Number number; + Double objectDouble; + double primitiveDouble; List stringList; Set integerSet; Collection booleanCollection; @@ -30,6 +32,7 @@ Set intergerSet; int primitiveInteger; Integer objectInteger; + Iterable stringIterable; @Test public void constructorTest() { @@ -140,10 +143,38 @@ } @Test + public void isNumberTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("number"); + Type fieldType = field.getGenericType(); + assertTrue(ReflectionUtils.isNumber(fieldType)); + } + + @Test + public void isNumberIntegerTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("objectInteger"); + Type fieldType = field.getGenericType(); + assertTrue(ReflectionUtils.isNumber(fieldType)); + } + + @Test + public void isNumberPrimitiveTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("primitiveInteger"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isNumber(fieldType)); + } + + @Test + public void isNumberParameterizedTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("integerSet"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isNumber(fieldType)); + } + + @Test public void isIntegerPrimitiveTest() throws NoSuchFieldException, SecurityException { Field field = ReflectionUtilsTest.class.getDeclaredField("primitiveInteger"); Type fieldType = field.getGenericType(); - assertTrue(ReflectionUtils.isInteger(fieldType)); + assertFalse(ReflectionUtils.isInteger(fieldType)); } @Test @@ -154,6 +185,13 @@ } @Test + public void isIntegerParameterizedTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isInteger(fieldType)); + } + + @Test public void isIntegerFalseTest() throws NoSuchFieldException, SecurityException { Field field = ReflectionUtilsTest.class.getDeclaredField("string"); Type fieldType = field.getGenericType(); @@ -161,6 +199,34 @@ } @Test + public void isDoublePrimitiveTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("primitiveDouble"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isDouble(fieldType)); + } + + @Test + public void isDoubleObjectTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("objectDouble"); + Type fieldType = field.getGenericType(); + assertTrue(ReflectionUtils.isDouble(fieldType)); + } + + @Test + public void isDoubleParameterizedTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isDouble(fieldType)); + } + + @Test + public void isDoubleFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("string"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isDouble(fieldType)); + } + + @Test public void isPrimitiveFalseTest() throws NoSuchFieldException, SecurityException { Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); Type fieldType = field.getGenericType(); @@ -168,13 +234,6 @@ } @Test - public void isPrimitiveWrappedTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("objectBoolean"); - Type fieldType = field.getGenericType(); - assertTrue(ReflectionUtils.isPrimitive(fieldType)); - } - - @Test public void isParameterizedTest() throws NoSuchFieldException, SecurityException { Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); Type fieldType = field.getGenericType(); @@ -293,12 +352,33 @@ assertFalse(ReflectionUtils.isSet(fieldType)); } -// @Test -// public void hasNoArgsConstructorTest() throws NoSuchFieldException, SecurityException { -// Field field = ReflectionUtilsTest.class.getDeclaredField("testUser"); -// Type fieldType = field.getGenericType(); -// assertTrue(ReflectionUtils.hasNoArgsContrustor((Class) fieldType)); -// } + @Test + public void isCollectionTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); + Type fieldType = field.getGenericType(); + assertTrue(ReflectionUtils.isCollection(fieldType)); + } + + @Test + public void isCollectionFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("string"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isCollection(fieldType)); + } + + @Test + public void isIterableTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("stringIterable"); + Type fieldType = field.getGenericType(); + assertTrue(ReflectionUtils.isIterable(fieldType)); + } + + @Test + public void isIterableFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("string"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isIterable(fieldType)); + } @Test public void hasNoArgsConstructorFalseTest() throws NoSuchFieldException, SecurityException { @@ -308,14 +388,6 @@ } @Test - public void isCollectionTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); - Type fieldType = ((ParameterizedType) field.getGenericType()).getRawType(); - Class clazz = ((Class) fieldType); - assertTrue(ReflectionUtils.isCollection(clazz)); - } - - @Test public void getFieldValueTest() throws NoSuchFieldException, SecurityException, IllegalAccessException { string = "test"; Field field = ReflectionUtilsTest.class.getDeclaredField("string");