Skip to content

Commit

Permalink
LANG-1265 pull the logic into central helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
struberg committed Dec 4, 2024
1 parent 6733551 commit 0a41e5c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/org/apache/commons/lang3/builder/Reflection.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.commons.lang3.builder;

import java.lang.reflect.Field;
import java.time.temporal.Temporal;
import java.util.Objects;

/**
Expand All @@ -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);
}

}

0 comments on commit 0a41e5c

Please sign in to comment.