Skip to content

Commit e29a500

Browse files
committed
Replace cascading if-else with a map lookup
Remove unused org.apache.commons.beanutils2.MethodUtils.getPrimitiveWrapper(Class) in favor of org.apache.commons.lang3.ClassUtils.primitiveToWrapper(Class)
1 parent 98ed36c commit e29a500

File tree

2 files changed

+4
-37
lines changed

2 files changed

+4
-37
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove unused org.apache.commons.beanutils2.MethodUtils.invokeStaticMethod(Class, String, Object[], Class[]) in favor of Apache Commons Lang's org.apache.commons.lang3.reflect.MethodUtils.</action>
5050
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove unused org.apache.commons.beanutils2.MethodUtils.toNonPrimitiveClass(Class) in favor of org.apache.commons.lang3.ClassUtils.primitiveToWrapper(Class).</action>
5151
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove unused org.apache.commons.beanutils2.MethodUtils.getPrimitiveType(Class) in favor of org.apache.commons.lang3.ClassUtils.wrapperToPrimitive(Class).</action>
52+
<action dev="ggregory" type="remove" due-to="Gary Gregory">Remove unused org.apache.commons.beanutils2.MethodUtils.getPrimitiveWrapper(Class) in favor of org.apache.commons.lang3.ClassUtils.primitiveToWrapper(Class).</action>
5253
</release>
5354
<release version="2.0.0-M2" date="2025-05-25" description="This is a major release and requires Java 8.">
5455
<!-- FIX -->

src/main/java/org/apache/commons/beanutils2/MethodUtils.java

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Objects;
2929
import java.util.WeakHashMap;
3030

31+
import org.apache.commons.lang3.ClassUtils;
3132
import org.apache.commons.logging.Log;
3233
import org.apache.commons.logging.LogFactory;
3334

@@ -493,7 +494,7 @@ private static float getObjectTransformationCost(Class<?> srcClass, final Class<
493494
float cost = 0.0f;
494495
while (srcClass != null && !destClass.equals(srcClass)) {
495496
if (destClass.isPrimitive()) {
496-
final Class<?> destClassWrapperClazz = getPrimitiveWrapper(destClass);
497+
final Class<?> destClassWrapperClazz = ClassUtils.wrapperToPrimitive(destClass);
497498
if (destClassWrapperClazz != null && destClassWrapperClazz.equals(srcClass)) {
498499
cost += 0.25f;
499500
break;
@@ -521,41 +522,6 @@ private static float getObjectTransformationCost(Class<?> srcClass, final Class<
521522
return cost;
522523
}
523524

524-
/**
525-
* Gets the wrapper object class for the given primitive type class. For example, passing {@code boolean.class</code> returns <code>Boolean.class}
526-
*
527-
* @param primitiveType the primitive type class for which a match is to be found
528-
* @return the wrapper type associated with the given primitive or null if no match is found
529-
*/
530-
public static Class<?> getPrimitiveWrapper(final Class<?> primitiveType) {
531-
// does anyone know a better strategy than comparing names?
532-
if (boolean.class.equals(primitiveType)) {
533-
return Boolean.class;
534-
}
535-
if (float.class.equals(primitiveType)) {
536-
return Float.class;
537-
}
538-
if (long.class.equals(primitiveType)) {
539-
return Long.class;
540-
}
541-
if (int.class.equals(primitiveType)) {
542-
return Integer.class;
543-
}
544-
if (short.class.equals(primitiveType)) {
545-
return Short.class;
546-
}
547-
if (byte.class.equals(primitiveType)) {
548-
return Byte.class;
549-
}
550-
if (double.class.equals(primitiveType)) {
551-
return Double.class;
552-
}
553-
if (char.class.equals(primitiveType)) {
554-
return Character.class;
555-
}
556-
return null;
557-
}
558-
559525
/**
560526
* Returns the sum of the object transformation cost for each class in the source argument list.
561527
*
@@ -749,7 +715,7 @@ public static boolean isAssignmentCompatible(final Class<?> parameterType, final
749715
if (parameterType.isPrimitive()) {
750716
// this method does *not* do widening - you must specify exactly
751717
// is this the right behavior?
752-
final Class<?> parameterWrapperClazz = getPrimitiveWrapper(parameterType);
718+
final Class<?> parameterWrapperClazz = ClassUtils.wrapperToPrimitive(parameterType);
753719
if (parameterWrapperClazz != null) {
754720
return parameterWrapperClazz.equals(parameterization);
755721
}

0 commit comments

Comments
 (0)