diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..81a66a0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +/target/ +/.settings/ +/.classpath +/.project +/build/ +/.idea/ +*.iml + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..4a3fe15 --- /dev/null +++ b/pom.xml @@ -0,0 +1,33 @@ + + 4.0.0 + + com.ljunggren + reflection-utils + 1.0.0-SNAPSHOT + jar + + reflection-utils + http://maven.apache.org + + + UTF-8 + 1.8 + 1.8 + + + + + org.apache.commons + commons-lang3 + 3.10 + + + junit + junit + 4.13 + test + + + diff --git a/src/main/java/com/ljunggren/reflectionUtils/ReflectionUtils.java b/src/main/java/com/ljunggren/reflectionUtils/ReflectionUtils.java new file mode 100644 index 0000000..9dfd9b7 --- /dev/null +++ b/src/main/java/com/ljunggren/reflectionUtils/ReflectionUtils.java @@ -0,0 +1,100 @@ +package com.ljunggren.reflectionUtils; + +import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.Collection; +import java.util.List; +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 { + + public static Type getOwnerType(Type type) { + return type instanceof ParameterizedType ? ((ParameterizedType) type).getRawType() : null; + } + + public static Type[] getParameterizedTypes(Type type) { + if (type instanceof ParameterizedType) { + return ((ParameterizedType) type).getActualTypeArguments(); + } + return new Type[] {}; + } + + public static Type getParameterizedType(Type type) { + Type[] types = getParameterizedTypes(type); + return types.length == 1 ? types[0] : null; + } + + public static Type getArrayComponentType(Type type) { + return isArray(type) ? ((Class) type).getComponentType() : null; + } + + public static List getObjectFields(Class clazz) { + return FieldUtils.getAllFieldsList(clazz); + } + + public static boolean isPrimitive(Type type) { + if (isParameterized(type)) { + return false; + } + return ClassUtils.isPrimitiveOrWrapper((Class) type); + } + + public static boolean isInteger(Type type) { + if (isPrimitive(type)) { + return type.getTypeName().equals(Integer.class.getName()) || + type.getTypeName().equals(int.class.getName()); + } + return false; + } + + public static boolean isParameterized(Type type) { + return type instanceof ParameterizedType; + } + + public static boolean isString(Type type) { + if (isParameterized(type)) { + return false; + } + return type.getTypeName().equals(String.class.getName()); + } + + public static boolean isList(Type type) { + if (isParameterized(type)) { + return getOwnerType(type).getTypeName().equals(List.class.getName()); + } + return false; + } + + public static boolean isArray(Type type) { + if (isParameterized(type)) { + return false; + } + return ((Class) type).isArray(); + } + + public static boolean isSet(Type type) { + if (isParameterized(type)) { + return getOwnerType(type).getTypeName().equals(Set.class.getName()); + } + return false; + } + + public static boolean hasNoArgsContrustor(Class clazz) { + return Stream.of(clazz.getConstructors()) + .allMatch(constructor -> constructor.getParameterCount() == 0); + } + + public static boolean isCollection(Class clazz) { + return Collection.class.isAssignableFrom(clazz); + } + + 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 new file mode 100644 index 0000000..bec900a --- /dev/null +++ b/src/test/java/com/ljunggren/reflectionUtils/ReflectionUtilsTest.java @@ -0,0 +1,296 @@ +package com.ljunggren.reflectionUtils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +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.List; +import java.util.Map; +import java.util.Set; + +import org.junit.Test; + +public class ReflectionUtilsTest { + + List stringList; + Set integerSet; + Collection booleanCollection; + Map integerStringMap; + String[] stringArray; + boolean primitiveBoolean; + Boolean objectBoolean; + String string; + Set intergerSet; + int primitiveInteger; + Integer objectInteger; + + @Test + public void constructorTest() { + ReflectionUtils reflectionUtils = new ReflectionUtils(); + assertTrue(reflectionUtils instanceof ReflectionUtils); + } + + @Test + public void getObjectFieldsTest() { + List fields = ReflectionUtils.getObjectFields(this.getClass()); + boolean hasStringList = fields.stream().anyMatch(field -> field.getName().equals("stringList")); + assertTrue(hasStringList); + } + + @Test + public void getOwnerTypeNullTest() { + Type type = ReflectionUtils.getOwnerType(null); + assertEquals(null, type); + } + + @Test + public void getOwnerTypeStringTest() { + Type type = ReflectionUtils.getOwnerType(String.class); + assertEquals(null, type); + } + + @Test + public void getOwnerTypeListTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); + Type fieldType = field.getGenericType(); + Type type = ReflectionUtils.getOwnerType(fieldType); + assertEquals(List.class, type); + } + + @Test + public void getOwnerTypeSetTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("integerSet"); + Type fieldType = field.getGenericType(); + Type type = ReflectionUtils.getOwnerType(fieldType); + assertEquals(Set.class, type); + } + + @Test + public void getOwnerTypeBooleanTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("booleanCollection"); + Type fieldType = field.getGenericType(); + Type type = ReflectionUtils.getOwnerType(fieldType); + assertEquals(Collection.class, type); + } + + @Test + public void getParameterizedTypesNullTest() { + Type[] types = ReflectionUtils.getParameterizedTypes(null); + assertEquals(0, types.length); + } + + @Test + public void getParameterizedTypesMapTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("integerStringMap"); + Type fieldType = field.getGenericType(); + Type[] types = ReflectionUtils.getParameterizedTypes(fieldType); + assertEquals(2, types.length); + assertEquals(Integer.class, types[0]); + assertEquals(String.class, types[1]); + } + + @Test + public void getParameterizedTypeNullTest() { + Type type = ReflectionUtils.getParameterizedType(null); + assertNull(type); + } + + @Test + public void getParameterizedTypeNonParameterizedTest() { + Type type = ReflectionUtils.getOwnerType(String.class); + assertEquals(null, type); + } + + @Test + public void getParameterizedTypeStringTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); + Type fieldType = field.getGenericType(); + Type type = ReflectionUtils.getParameterizedType(fieldType); + assertEquals(String.class, type); + } + + @Test + public void getArrayComponentTypeTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("stringArray"); + Type fieldType = field.getGenericType(); + Type type = ReflectionUtils.getArrayComponentType(fieldType); + assertEquals(String.class, type); + } + + @Test + public void getArrayComponentTypeFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("string"); + Type fieldType = field.getGenericType(); + Type type = ReflectionUtils.getArrayComponentType(fieldType); + assertEquals(null, type); + } + + @Test + public void isPrimitiveTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("primitiveBoolean"); + Type fieldType = field.getGenericType(); + assertTrue(ReflectionUtils.isPrimitive(fieldType)); + } + + @Test + public void isIntegerPrimitiveTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("primitiveInteger"); + Type fieldType = field.getGenericType(); + assertTrue(ReflectionUtils.isInteger(fieldType)); + } + + @Test + public void isIntegerObjectTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("objectInteger"); + Type fieldType = field.getGenericType(); + assertTrue(ReflectionUtils.isInteger(fieldType)); + } + + @Test + public void isIntegerFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("string"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isInteger(fieldType)); + } + + @Test + public void isPrimitiveFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isPrimitive(fieldType)); + } + + @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(); + assertTrue(ReflectionUtils.isParameterized(fieldType)); + } + + @Test + public void isParameterizedFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("primitiveBoolean"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isParameterized(fieldType)); + } + + @Test + public void isListTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); + Type fieldType = field.getGenericType(); + assertTrue(ReflectionUtils.isList(fieldType)); + } + + @Test + public void isListFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("string"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isList(fieldType)); + } + + @Test + public void isListParamaterizedFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("integerSet"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isList(fieldType)); + } + + @Test + public void isArrayTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("stringArray"); + Type fieldType = field.getGenericType(); + assertTrue(ReflectionUtils.isArray(fieldType)); + } + + @Test + public void isArrayFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isArray(fieldType)); + } + + @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("objectBoolean"); + 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 isSetTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("integerSet"); + Type fieldType = field.getGenericType(); + assertTrue(ReflectionUtils.isSet(fieldType)); + } + + @Test + public void isSetParameterizedFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isSet(fieldType)); + } + + @Test + public void isSetFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("string"); + Type fieldType = field.getGenericType(); + 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 hasNoArgsConstructorFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("string"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.hasNoArgsContrustor((Class) fieldType)); + } + + @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"); + String value = (String) ReflectionUtils.getFieldValue(field, this); + assertEquals("test", value); + } + +}