diff --git a/src/main/java/io/ljunggren/reflectionUtils/ReflectionUtils.java b/src/main/java/io/ljunggren/reflectionUtils/ReflectionUtils.java index 45affc2..c48662b 100644 --- a/src/main/java/io/ljunggren/reflectionUtils/ReflectionUtils.java +++ b/src/main/java/io/ljunggren/reflectionUtils/ReflectionUtils.java @@ -1,194 +1,265 @@ -package io.ljunggren.reflectionUtils; - -import java.lang.reflect.Field; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; -import java.math.BigDecimal; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.stream.Stream; - -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 boolean isParameterized(Type type) { - return type instanceof ParameterizedType; - } - - 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) { - return isParameterized(type) ? false : isPrimitive((Class) type); - } - - public static boolean isPrimitive(Class clazz) { - return clazz.isPrimitive(); - } - - public static boolean isNumber(Type type) { - return isParameterized(type) ? false : isNumber((Class) type); - } - - public static boolean isNumber(Class clazz) { - return Number.class.isAssignableFrom(clazz); - } - - public static boolean isInteger(Type type) { - return isParameterized(type) ? false : isInteger((Class) type); - } - - public static boolean isInteger(Class clazz) { - return isEqual(clazz, Integer.class); - } - - public static boolean isDouble(Type type) { - return isParameterized(type) ? false : isDouble((Class) type); - } - - public static boolean isDouble(Class clazz) { - return isEqual(clazz, Double.class); - } - - public static boolean isShort(Type type) { - return isParameterized(type) ? false : isShort((Class) type); - } - - public static boolean isShort(Class clazz) { - return isEqual(clazz, Short.class); - } - - public static boolean isLong(Type type) { - return isParameterized(type) ? false : isLong((Class) type); - } - - public static boolean isLong(Class clazz) { - return isEqual(clazz, Long.class); - } - - public static boolean isFloat(Type type) { - return isParameterized(type) ? false : isFloat((Class) type); - } - - public static boolean isFloat(Class clazz) { - return isEqual(clazz, Float.class); - } - - public static boolean isBigDecimal(Type type) { - return isParameterized(type) ? false : isBigDecimal((Class) type); - } - - public static boolean isBigDecimal(Class clazz) { - return isEqual(clazz, BigDecimal.class); - } - - public static boolean isByte(Type type) { - return isParameterized(type) ? false : isByte((Class) type); - } - - public static boolean isByte(Class clazz) { - return isEqual(clazz, Byte.class); - } - - public static boolean isBoolean(Type type) { - return isParameterized(type) ? false : isBoolean((Class) type); - } - - public static boolean isBoolean(Class clazz) { - return isEqual(clazz, Boolean.class); - } - - public static boolean isString(Type type) { - return isParameterized(type) ? false : isString((Class) type); - } - - public static boolean isString(Class clazz) { - return isEqual(clazz, String.class); - } - - 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); - } - - public static boolean isMap(Type type) { - return isParameterized(type) ? isMap(getOwnerType(type)) : isMap((Class) type); - } - - public static boolean isMap(Class clazz) { - return Map.class.isAssignableFrom(clazz); - } - - public static boolean isArray(Type type) { - return isParameterized(type) ? false : isArray((Class) type); - } - - public static boolean isArray(Class clazz) { - return clazz.isArray(); - } - - public static boolean isSet(Type type) { - return isParameterized(type) ? isSet(getOwnerType(type)) : isSet((Class) type); - } - - public static boolean isSet(Class clazz) { - return Set.class.isAssignableFrom(clazz); - } - - public static boolean isCollection(Type type) { - return isParameterized(type) ? isCollection(getOwnerType(type)) : isCollection((Class) type); - } - - public static boolean isCollection(Class clazz) { - return Collection.class.isAssignableFrom(clazz); - } - - public static boolean isIterable(Type type) { - return isParameterized(type) ? isIterable(getOwnerType(type)) : 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); - } - - private static boolean isEqual(Class class1, Class class2) { - return class1.getName().equals(class2.getName()); - } - -} +package io.ljunggren.reflectionUtils; + +import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.math.BigDecimal; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Stream; + +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 boolean isParameterized(Type type) { + return type == null ? false : type instanceof ParameterizedType; + } + + 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) { + return isParameterized(type) ? false : isPrimitive((Class) type); + } + + public static boolean isPrimitive(Class clazz) { + return clazz.isPrimitive(); + } + + public static boolean isNumber(T t) { + return t instanceof Number; + } + + public static boolean isNumber(Type type) { + return isParameterized(type) ? false : isNumber((Class) type); + } + + public static boolean isNumber(Class clazz) { + return clazz == null ? false : Number.class.isAssignableFrom(clazz); + } + + public static boolean isInteger(T t) { + return t instanceof Integer; + } + + public static boolean isInteger(Type type) { + return isParameterized(type) ? false : isInteger((Class) type); + } + + public static boolean isInteger(Class clazz) { + return isEqual(clazz, Integer.class); + } + + public static boolean isDouble(T t) { + return t instanceof Double; + } + + public static boolean isDouble(Type type) { + return isParameterized(type) ? false : isDouble((Class) type); + } + + public static boolean isDouble(Class clazz) { + 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); + } + + public static boolean isShort(Class clazz) { + 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); + } + + public static boolean isLong(Class clazz) { + 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); + } + + public static boolean isFloat(Class clazz) { + 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); + } + + public static boolean isBigDecimal(Class clazz) { + 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); + } + + public static boolean isByte(Class clazz) { + 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); + } + + public static boolean isBoolean(Class clazz) { + return isEqual(clazz, Boolean.class); + } + + public static boolean isString(T t) { + return t instanceof String; + } + + public static boolean isString(Type type) { + return isParameterized(type) ? false : isString((Class) type); + } + + public static boolean isString(Class clazz) { + 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 clazz == null ? false : List.class.isAssignableFrom(clazz); + } + + public static boolean isMap(T t) { + return t instanceof Map; + } + + public static boolean isMap(Type type) { + return isParameterized(type) ? isMap(getOwnerType(type)) : isMap((Class) type); + } + + public static boolean isMap(Class clazz) { + 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 == null ? false : clazz.isArray(); + } + + public static boolean isSet(T t) { + return t instanceof Set; + } + + public static boolean isSet(Type type) { + return isParameterized(type) ? isSet(getOwnerType(type)) : isSet((Class) type); + } + + public static boolean isSet(Class 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) { + return isParameterized(type) ? isCollection(getOwnerType(type)) : isCollection((Class) type); + } + + public static boolean isCollection(Class 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) { + return isParameterized(type) ? isIterable(getOwnerType(type)) : isIterable((Class) type); + } + + public static boolean isIterable(Class 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) { + 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); + } + + private static boolean isEqual(Class class1, Class class2) { + if (class1 == null || class2 == null) { + return false; + } + return class1.getName().equals(class2.getName()); + } + +} diff --git a/src/test/java/io/ljunggren/reflectionUtils/ReflectionUtilsTest.java b/src/test/java/io/ljunggren/reflectionUtils/ReflectionUtilsTest.java index 15163d5..5cb9dc8 100644 --- a/src/test/java/io/ljunggren/reflectionUtils/ReflectionUtilsTest.java +++ b/src/test/java/io/ljunggren/reflectionUtils/ReflectionUtilsTest.java @@ -1,530 +1,826 @@ -package io.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.Type; -import java.math.BigDecimal; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import org.junit.Test; - -public class ReflectionUtilsTest { - - Number number; - int integerPrimitive; - Integer integerObject; - Double doubleObject; - double doublePrimitive; - Short shortObject; - Long longObject; - Float floatObject; - BigDecimal bigDecimal; - List stringList; - Set integerSet; - Collection booleanCollection; - Map integerStringMap; - HashMap integerStringHashMap; - Set intergerSet; - Iterable stringIterable; - Byte byteObject; - String string; - String[] stringArray; - boolean booleanPrimitive; - Boolean booleanObject; - - @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("booleanPrimitive"); - Type fieldType = field.getGenericType(); - assertTrue(ReflectionUtils.isPrimitive(fieldType)); - } - - @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("integerObject"); - Type fieldType = field.getGenericType(); - assertTrue(ReflectionUtils.isNumber(fieldType)); - } - - @Test - public void isNumberPrimitiveTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("integerPrimitive"); - 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("integerPrimitive"); - Type fieldType = field.getGenericType(); - assertFalse(ReflectionUtils.isInteger(fieldType)); - } - - @Test - public void isIntegerObjectTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("integerObject"); - Type fieldType = field.getGenericType(); - assertTrue(ReflectionUtils.isInteger(fieldType)); - } - - @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(); - assertFalse(ReflectionUtils.isInteger(fieldType)); - } - - @Test - public void isDoublePrimitiveTest() throws NoSuchFieldException, SecurityException { - 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("doubleObject"); - 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 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(); - assertTrue(ReflectionUtils.isFloat(fieldType)); - } - - @Test - public void isFloatFalseTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("string"); - Type fieldType = field.getGenericType(); - assertFalse(ReflectionUtils.isFloat(fieldType)); - } - - @Test - public void isFloatParameterizedTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); - Type fieldType = field.getGenericType(); - assertFalse(ReflectionUtils.isFloat(fieldType)); - } - - @Test - public void isBigDecimalTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("bigDecimal"); - Type fieldType = field.getGenericType(); - assertTrue(ReflectionUtils.isBigDecimal(fieldType)); - } - - @Test - public void isBigDecimalFalseTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("string"); - Type fieldType = field.getGenericType(); - assertFalse(ReflectionUtils.isBigDecimal(fieldType)); - } - - @Test - public void isBigDecimalParameterizedTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); - Type fieldType = field.getGenericType(); - assertFalse(ReflectionUtils.isBigDecimal(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 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 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 isMapTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("integerStringMap"); - Type fieldType = field.getGenericType(); - assertTrue(ReflectionUtils.isMap(fieldType)); - } - - @Test - public void isMapFalseTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("string"); - Type fieldType = field.getGenericType(); - assertFalse(ReflectionUtils.isMap(fieldType)); - } - - @Test - public void isHashMapTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("integerStringHashMap"); - Type fieldType = field.getGenericType(); - assertTrue(ReflectionUtils.isMap(fieldType)); - } - - @Test - public void isHshMapFalseTest() throws NoSuchFieldException, SecurityException { - Field field = ReflectionUtilsTest.class.getDeclaredField("string"); - Type fieldType = field.getGenericType(); - assertFalse(ReflectionUtils.isMap(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 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 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 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 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 { - Field field = ReflectionUtilsTest.class.getDeclaredField("string"); - Type fieldType = field.getGenericType(); - assertFalse(ReflectionUtils.hasNoArgsContrustor((Class) fieldType)); - } - - @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); - } - -} +package io.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.Type; +import java.math.BigDecimal; +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; + +import org.junit.Test; + +public class ReflectionUtilsTest { + + Number number = 0; + int integerPrimitive = 0; + Integer integerObject = 0; + double doublePrimitive = 0.0; + 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 = new String[] {}; + Set integerSet = new HashSet<>(); + Collection booleanCollection; + HashMap integerStringHashMap; + Set intergerSet; + Iterable stringIterable = new ArrayList<>(); + + @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 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 isParameterizedNullTest() { + assertFalse(ReflectionUtils.isParameterized(null)); + } + + @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 getArrayComponentTypeNullTest() { + assertNull(ReflectionUtils.getArrayComponentType(null)); + } + + @Test + public void isPrimitiveTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("booleanPrimitive"); + Type fieldType = field.getGenericType(); + assertTrue(ReflectionUtils.isPrimitive(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 isNumberGenericTest() { + assertTrue(ReflectionUtils.isNumber(number)); + } + + @Test + public void isNumberGenericFalseTest() { + assertFalse(ReflectionUtils.isNumber(string)); + } + + @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("integerObject"); + Type fieldType = field.getGenericType(); + assertTrue(ReflectionUtils.isNumber(fieldType)); + } + + @Test + public void isNumberPrimitiveTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("integerPrimitive"); + 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 isNumberNullTest() { + assertFalse(ReflectionUtils.isNumber(null)); + } + + @Test + public void isIntegerGenericTest() { + assertTrue(ReflectionUtils.isInteger(integerObject)); + } + + @Test + public void isIntegerGenericPrimitiveTest() { + assertTrue(ReflectionUtils.isInteger(integerPrimitive)); + } + + @Test + public void isIntegerGenericFalseTest() { + assertFalse(ReflectionUtils.isInteger(string)); + } + + @Test + public void isIntegerPrimitiveTest() throws NoSuchFieldException, SecurityException { + 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("integerObject"); + Type fieldType = field.getGenericType(); + assertTrue(ReflectionUtils.isInteger(fieldType)); + } + + @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(); + assertFalse(ReflectionUtils.isInteger(fieldType)); + } + + @Test + public void isIntegerNullTest() { + assertFalse(ReflectionUtils.isInteger(null)); + } + + @Test + public void isDoubleGenericTest() { + assertTrue(ReflectionUtils.isDouble(doubleObject)); + } + + @Test + public void isDoubleGenericPrimitiveTest() { + assertTrue(ReflectionUtils.isDouble(doublePrimitive)); + } + + @Test + public void isDoubleGenericFalseTest() { + assertFalse(ReflectionUtils.isDouble(string)); + } + + @Test + public void isDoublePrimitiveTest() throws NoSuchFieldException, SecurityException { + 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("doubleObject"); + 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 isDoubleNullTest() { + assertFalse(ReflectionUtils.isDouble(null)); + } + + @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(); + 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 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(); + 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 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(); + assertTrue(ReflectionUtils.isFloat(fieldType)); + } + + @Test + public void isFloatFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("string"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isFloat(fieldType)); + } + + @Test + public void isFloatParameterizedTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isFloat(fieldType)); + } + + @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(); + assertTrue(ReflectionUtils.isBigDecimal(fieldType)); + } + + @Test + public void isBigDecimalFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("string"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isBigDecimal(fieldType)); + } + + @Test + public void isBigDecimalParameterizedTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isBigDecimal(fieldType)); + } + + @Test + public void isBigDecimalNullTest() { + assertFalse(ReflectionUtils.isBigDecimal(null)); + } + + @Test + public void isByteGenericTest() { + assertTrue(ReflectionUtils.isByte(byteObject)); + } + + @Test + 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(); + 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 + 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 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(); + assertTrue(ReflectionUtils.isMap(fieldType)); + } + + @Test + public void isMapFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("string"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isMap(fieldType)); + } + + @Test + public void isHashMapTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("integerStringHashMap"); + Type fieldType = field.getGenericType(); + assertTrue(ReflectionUtils.isMap(fieldType)); + } + + @Test + public void isHashMapFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("string"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isMap(fieldType)); + } + + @Test + public void isMapNullTest() { + assertFalse(ReflectionUtils.isMap(null)); + } + + @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(); + 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 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(); + 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 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(); + 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 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(); + 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 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(fieldType)); + } + + @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); + } + +}