diff --git a/README.md b/README.md index bb0d82e..2c0d9be 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Get Parameterized Type ```java -Type parameterizedType = ReflectionUtils.getParameterizedTypes(type); +Type parameterizedType = ReflectionUtils.getParameterizedType(type); ``` Get Parameterized Types @@ -125,6 +125,21 @@ boolean isString = ReflectionUtils.isString(clazz); ``` +Is Enum + +```java +boolean isEnum = ReflectionUtils.isEnum(t); +boolean isEnum = ReflectionUtils.isEnum(type); +boolean isEnum = ReflectionUtils.isEnum(clazz); +``` + +Is Boxed + +```java +boolean isBoxed = ReflectionUtils.isBoxed(type); +boolean isBoxed = ReflectionUtils.isBoxed(clazz); +``` + Is List ```java @@ -173,11 +188,11 @@ boolean isIterable = ReflectionUtils.isIterable(clazz); ``` -Has no Arguments Constructor +Has No Args Constructor ```java -boolean hasNoArgsContrustor = ReflectionUtils.hasNoArgsContrustor(type); -boolean hasNoArgsContrustor = ReflectionUtils.hasNoArgsContrustor(clazz); +boolean hasNoArgsConstructor = ReflectionUtils.hasNoArgsConstructor(type); +boolean hasNoArgsConstructor = ReflectionUtils.hasNoArgsConstructor(clazz); ``` Get Field Value @@ -185,9 +200,3 @@ ```java Object value = ReflectionUtils.getFieldValue(field, object); ``` - -Classes isEqual - -```java -boolean isEqual = ReflectionUtils.isEqual(clazz1, clazz2); -``` diff --git a/src/main/java/io/ljunggren/reflectionUtils/ReflectionUtils.java b/src/main/java/io/ljunggren/reflectionUtils/ReflectionUtils.java index b04bd1c..efc8f75 100644 --- a/src/main/java/io/ljunggren/reflectionUtils/ReflectionUtils.java +++ b/src/main/java/io/ljunggren/reflectionUtils/ReflectionUtils.java @@ -47,7 +47,7 @@ } public static boolean isPrimitive(Class clazz) { - return clazz.isPrimitive(); + return clazz != null && clazz.isPrimitive(); } public static boolean isNumber(T t) { @@ -175,11 +175,11 @@ } public static boolean isEnum(Type type) { - return isEnum((Class) type); + return isParameterized(type) ? false : isEnum((Class) type); } - + public static boolean isEnum(Class clazz) { - return clazz.isEnum(); + return clazz != null && clazz.isEnum(); } public static boolean isBoxed(Type type) { @@ -187,10 +187,10 @@ } public static boolean isBoxed(Class clazz) { - if (isPrimitive(clazz)) { + if (clazz == null || isPrimitive(clazz)) { return false; } - if (!"java.lang".equals(clazz.getPackage().getName())) { + if (clazz.getPackage() == null || !"java.lang".equals(clazz.getPackage().getName())) { return false; } try { @@ -273,11 +273,17 @@ return clazz == null ? false : Iterable.class.isAssignableFrom(clazz); } - public static boolean hasNoArgsContrustor(Type type) { - return hasNoArgsContrustor((Class) type); + public static boolean hasNoArgsConstructor(Type type) { + if (type == null || isParameterized(type)) { + return false; + } + return hasNoArgsConstructor((Class) type); } - public static boolean hasNoArgsContrustor(Class clazz) { + public static boolean hasNoArgsConstructor(Class clazz) { + if (clazz == null) { + return false; + } return Stream.of(clazz.getConstructors()) .allMatch(constructor -> constructor.getParameterCount() == 0); } diff --git a/src/test/java/io/ljunggren/reflectionUtils/ReflectionUtilsTest.java b/src/test/java/io/ljunggren/reflectionUtils/ReflectionUtilsTest.java index 2a4d63a..16f86ec 100644 --- a/src/test/java/io/ljunggren/reflectionUtils/ReflectionUtilsTest.java +++ b/src/test/java/io/ljunggren/reflectionUtils/ReflectionUtilsTest.java @@ -19,7 +19,9 @@ import org.junit.Test; public class ReflectionUtilsTest { - + + enum TestEnum { A, B, C } + Number number = 0; int integerPrimitive = 0; Integer integerObject = 0; @@ -45,6 +47,7 @@ HashMap integerStringHashMap; Set intergerSet; Iterable stringIterable = new ArrayList<>(); + TestEnum testEnum = TestEnum.A; @Test public void constructorTest() { @@ -184,7 +187,12 @@ Type fieldType = field.getGenericType(); assertFalse(ReflectionUtils.isPrimitive(fieldType)); } - + + @Test + public void isPrimitiveNullTest() { + assertFalse(ReflectionUtils.isPrimitive((Class) null)); + } + @Test public void isNumberGenericTest() { assertTrue(ReflectionUtils.isNumber(number)); @@ -600,7 +608,53 @@ public void isStringNullTest() { assertFalse(ReflectionUtils.isString(null)); } - + + @Test + public void isEnumGenericTest() { + assertTrue(ReflectionUtils.isEnum(TestEnum.A)); + } + + @Test + public void isEnumGenericFalseTest() { + assertFalse(ReflectionUtils.isEnum(string)); + } + + @Test + public void isEnumTypeTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("testEnum"); + Type fieldType = field.getGenericType(); + assertTrue(ReflectionUtils.isEnum(fieldType)); + } + + @Test + public void isEnumTypeFalseTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("string"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isEnum(fieldType)); + } + + @Test + public void isEnumTypeParameterizedTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.isEnum(fieldType)); + } + + @Test + public void isEnumClassTest() { + assertTrue(ReflectionUtils.isEnum(TestEnum.class)); + } + + @Test + public void isEnumClassFalseTest() { + assertFalse(ReflectionUtils.isEnum(String.class)); + } + + @Test + public void isEnumNullTest() { + assertFalse(ReflectionUtils.isEnum((Class) null)); + } + @Test public void isBoxedTest() { assertTrue(ReflectionUtils.isBoxed(Boolean.class)); @@ -619,7 +673,18 @@ assertFalse(ReflectionUtils.isBoxed(List.class)); assertFalse(ReflectionUtils.isBoxed(String.class)); } - + + @Test + public void isBoxedPrimitiveTest() { + assertFalse(ReflectionUtils.isBoxed(int.class)); + assertFalse(ReflectionUtils.isBoxed(boolean.class)); + } + + @Test + public void isBoxedNullTest() { + assertFalse(ReflectionUtils.isBoxed((Class) null)); + } + @Test public void isListGenericTest() { assertTrue(ReflectionUtils.isList(stringList)); @@ -824,16 +889,33 @@ @Test public void hasNoArgsConstructorTest() throws NoSuchFieldException, SecurityException { - assertTrue(ReflectionUtils.hasNoArgsContrustor(ReflectionUtilsTest.class)); + assertTrue(ReflectionUtils.hasNoArgsConstructor(ReflectionUtilsTest.class)); } @Test public void hasNoArgsConstructorFalseTest() throws NoSuchFieldException, SecurityException { Field field = ReflectionUtilsTest.class.getDeclaredField("string"); Type fieldType = field.getGenericType(); - assertFalse(ReflectionUtils.hasNoArgsContrustor(fieldType)); + assertFalse(ReflectionUtils.hasNoArgsConstructor(fieldType)); } - + + @Test + public void hasNoArgsConstructorParameterizedTest() throws NoSuchFieldException, SecurityException { + Field field = ReflectionUtilsTest.class.getDeclaredField("stringList"); + Type fieldType = field.getGenericType(); + assertFalse(ReflectionUtils.hasNoArgsConstructor(fieldType)); + } + + @Test + public void hasNoArgsConstructorNullTypeTest() { + assertFalse(ReflectionUtils.hasNoArgsConstructor((Type) null)); + } + + @Test + public void hasNoArgsConstructorNullClassTest() { + assertFalse(ReflectionUtils.hasNoArgsConstructor((Class) null)); + } + @Test public void getFieldValueTest() throws NoSuchFieldException, SecurityException, IllegalAccessException { string = "test";