diff --git a/src/main/java/org/apache/commons/lang3/builder/CompareToBuilder.java b/src/main/java/org/apache/commons/lang3/builder/CompareToBuilder.java index 6acf01a8a88..483aa37cd9c 100644 --- a/src/main/java/org/apache/commons/lang3/builder/CompareToBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/CompareToBuilder.java @@ -19,7 +19,6 @@ import java.lang.reflect.AccessibleObject; import java.lang.reflect.Field; import java.lang.reflect.Modifier; -import java.time.temporal.Temporal; import java.util.Collection; import java.util.Comparator; import java.util.Objects; @@ -116,7 +115,7 @@ private static void reflectionAppend( final boolean useTransients, final String[] excludeFields) { - if ((lhs instanceof CharSequence || lhs instanceof Number || lhs instanceof Temporal) && lhs instanceof Comparable) { + if (Reflection.isJavaInternalClass(lhs) && lhs instanceof Comparable) { builder.append(lhs, rhs); return; } diff --git a/src/main/java/org/apache/commons/lang3/builder/EqualsBuilder.java b/src/main/java/org/apache/commons/lang3/builder/EqualsBuilder.java index 67183a2855e..0fbfd98cb08 100644 --- a/src/main/java/org/apache/commons/lang3/builder/EqualsBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/EqualsBuilder.java @@ -19,7 +19,6 @@ import java.lang.reflect.AccessibleObject; import java.lang.reflect.Field; import java.lang.reflect.Modifier; -import java.time.temporal.Temporal; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; @@ -732,8 +731,7 @@ public EqualsBuilder append(final Object lhs, final Object rhs) { // to be inlined appendArray(lhs, rhs); } else // The simple case, not an array, just test the element - if (testRecursive && !ClassUtils.isPrimitiveOrWrapper(lhsClass) - && !(CharSequence.class.isAssignableFrom(lhsClass) || Number.class.isAssignableFrom(lhsClass) || Temporal.class.isAssignableFrom(lhsClass))) { + if (testRecursive && !ClassUtils.isPrimitiveOrWrapper(lhsClass) && !Reflection.isJavaInternalClass(lhsClass)) { reflectionAppend(lhs, rhs); } else { isEquals = lhs.equals(rhs); @@ -958,8 +956,7 @@ public EqualsBuilder reflectionAppend(final Object lhs, final Object rhs) { } try { - if (testClass.isArray() || Number.class.isAssignableFrom(testClass) - || Temporal.class.isAssignableFrom(testClass) || CharSequence.class.isAssignableFrom(testClass)) { + if (testClass.isArray() || Reflection.isJavaInternalClass(testClass)) { append(lhs, rhs); } else //If either class is being excluded, call normal object equals method on lhsClass. if (bypassReflectionClasses != null diff --git a/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java b/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java index 00d3b1c3144..a214b2ba7af 100644 --- a/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java @@ -20,7 +20,6 @@ import java.lang.reflect.AccessibleObject; import java.lang.reflect.Field; import java.lang.reflect.Modifier; -import java.time.temporal.Temporal; import java.util.Collection; import java.util.Comparator; import java.util.HashSet; @@ -181,7 +180,7 @@ private static void reflectionAppend(final Object object, final Class clazz, if (isRegistered(object)) { return; } - if (object instanceof CharSequence || object instanceof Number || object instanceof Temporal) { + if (Reflection.isJavaInternalClass(object)) { builder.append(object); return; } diff --git a/src/main/java/org/apache/commons/lang3/builder/Reflection.java b/src/main/java/org/apache/commons/lang3/builder/Reflection.java index fe7ade599a5..1d7f140dcb3 100644 --- a/src/main/java/org/apache/commons/lang3/builder/Reflection.java +++ b/src/main/java/org/apache/commons/lang3/builder/Reflection.java @@ -18,6 +18,7 @@ package org.apache.commons.lang3.builder; import java.lang.reflect.Field; +import java.time.temporal.Temporal; import java.util.Objects; /** @@ -41,4 +42,24 @@ static Object getUnchecked(final Field field, final Object obj) { } } + /** + * Check whether the given object is of a type which is an internal java Class, like String, Integer, Boolean, LocalDateTime, etc + * This is often needed as we cannot look into them via reflection since Java9. + * + * @return {@code true} if the given object is a Java internal class + */ + static boolean isJavaInternalClass(Object o) { + return o != null && isJavaInternalClass(o.getClass()); + } + + /** + * Check whether the given class is an internal java Class, like String, Integer, Boolean, LocalDateTime, etc + * This is often needed as we cannot look into them via reflection since Java9. + * + * @return {@code true} if the given class is a Java internal class + */ + static boolean isJavaInternalClass(Class clazz) { + return CharSequence.class.isAssignableFrom(clazz) || Number.class.isAssignableFrom(clazz) || Boolean.class.isAssignableFrom(clazz) || Temporal.class.isAssignableFrom(clazz); + } + }