Browse Source

Removed Commons Lang references and refined Java 5+ implementation details

pull/465/head
Juergen Hoeller 12 years ago
parent
commit
6deb597cdf
  1. 14
      spring-core/src/main/java/org/springframework/util/ClassUtils.java
  2. 68
      spring-core/src/main/java/org/springframework/util/ObjectUtils.java
  3. 20
      spring-core/src/test/java/org/springframework/util/ObjectUtilsTests.java

14
spring-core/src/main/java/org/springframework/util/ClassUtils.java

@ -34,10 +34,8 @@ import java.util.Map; @@ -34,10 +34,8 @@ import java.util.Map;
import java.util.Set;
/**
* Miscellaneous class utility methods. Mainly for internal use within the
* framework; consider
* <a href="http://commons.apache.org/lang/" target="_blank">Apache Commons Lang</a>
* for a more comprehensive suite of class utilities.
* Miscellaneous class utility methods.
* Mainly for internal use within the framework.
*
* @author Juergen Hoeller
* @author Keith Donald
@ -803,7 +801,7 @@ public abstract class ClassUtils { @@ -803,7 +801,7 @@ public abstract class ClassUtils {
* @param method the method to check
* @param targetClass the target class to check against
*/
private static boolean isOverridable(Method method, Class targetClass) {
private static boolean isOverridable(Method method, Class<?> targetClass) {
if (Modifier.isPrivate(method.getModifiers())) {
return false;
}
@ -816,7 +814,7 @@ public abstract class ClassUtils { @@ -816,7 +814,7 @@ public abstract class ClassUtils {
/**
* Return a public static method of a class.
* @param methodName the static method name
* @param clazz the class which defines the method
* @param clazz the class which defines the method
* @param args the parameter types to the method
* @return the static method, or {@code null} if no static method was found
* @throws IllegalArgumentException if the method name is blank or the clazz is null
@ -906,13 +904,13 @@ public abstract class ClassUtils { @@ -906,13 +904,13 @@ public abstract class ClassUtils {
return true;
}
if (lhsType.isPrimitive()) {
Class resolvedPrimitive = primitiveWrapperTypeMap.get(rhsType);
Class<?> resolvedPrimitive = primitiveWrapperTypeMap.get(rhsType);
if (resolvedPrimitive != null && lhsType.equals(resolvedPrimitive)) {
return true;
}
}
else {
Class resolvedWrapper = primitiveTypeToWrapperMap.get(rhsType);
Class<?> resolvedWrapper = primitiveTypeToWrapperMap.get(rhsType);
if (resolvedWrapper != null && lhsType.isAssignableFrom(resolvedWrapper)) {
return true;
}

68
spring-core/src/main/java/org/springframework/util/ObjectUtils.java

@ -21,10 +21,7 @@ import java.util.Arrays; @@ -21,10 +21,7 @@ import java.util.Arrays;
/**
* Miscellaneous object utility methods.
*
* <p>Mainly for internal use within the framework; consider
* <a href="http://jakarta.apache.org/commons/lang/">Jakarta's Commons Lang</a>
* for a more comprehensive suite of object utilities.
* Mainly for internal use within the framework.
*
* <p>Thanks to Alex Ruiz for contributing several enhancements to this class!
*
@ -34,7 +31,6 @@ import java.util.Arrays; @@ -34,7 +31,6 @@ import java.util.Arrays;
* @author Rob Harrop
* @author Chris Beams
* @since 19.03.2004
* @see org.apache.commons.lang.ObjectUtils
*/
public abstract class ObjectUtils {
@ -69,17 +65,15 @@ public abstract class ObjectUtils { @@ -69,17 +65,15 @@ public abstract class ObjectUtils {
* @param declaredExceptions the exceptions declared in the throws clause
* @return whether the given exception is compatible
*/
public static boolean isCompatibleWithThrowsClause(Throwable ex, Class[] declaredExceptions) {
public static boolean isCompatibleWithThrowsClause(Throwable ex, Class<?>... declaredExceptions) {
if (!isCheckedException(ex)) {
return true;
}
if (declaredExceptions != null) {
int i = 0;
while (i < declaredExceptions.length) {
if (declaredExceptions[i].isAssignableFrom(ex.getClass())) {
for (Class<?> declaredException : declaredExceptions) {
if (declaredException.isAssignableFrom(ex.getClass())) {
return true;
}
i++;
}
}
return false;
@ -218,7 +212,7 @@ public abstract class ObjectUtils { @@ -218,7 +212,7 @@ public abstract class ObjectUtils {
if (length == 0) {
return new Object[0];
}
Class wrapperType = Array.get(source, 0).getClass();
Class<?> wrapperType = Array.get(source, 0).getClass();
Object[] newArray = (Object[]) Array.newInstance(wrapperType, length);
for (int i = 0; i < length; i++) {
newArray[i] = Array.get(source, i);
@ -286,7 +280,7 @@ public abstract class ObjectUtils { @@ -286,7 +280,7 @@ public abstract class ObjectUtils {
/**
* Return as hash code for the given object; typically the value of
* {@code {@link Object#hashCode()}}. If the object is an array,
* {@code Object#hashCode()}}. If the object is an array,
* this method will delegate to any of the {@code nullSafeHashCode}
* methods for arrays in this class. If the object is {@code null},
* this method returns 0.
@ -345,9 +339,8 @@ public abstract class ObjectUtils { @@ -345,9 +339,8 @@ public abstract class ObjectUtils {
return 0;
}
int hash = INITIAL_HASH;
int arraySize = array.length;
for (int i = 0; i < arraySize; i++) {
hash = MULTIPLIER * hash + nullSafeHashCode(array[i]);
for (Object element : array) {
hash = MULTIPLIER * hash + nullSafeHashCode(element);
}
return hash;
}
@ -361,9 +354,8 @@ public abstract class ObjectUtils { @@ -361,9 +354,8 @@ public abstract class ObjectUtils {
return 0;
}
int hash = INITIAL_HASH;
int arraySize = array.length;
for (int i = 0; i < arraySize; i++) {
hash = MULTIPLIER * hash + hashCode(array[i]);
for (boolean element : array) {
hash = MULTIPLIER * hash + hashCode(element);
}
return hash;
}
@ -377,9 +369,8 @@ public abstract class ObjectUtils { @@ -377,9 +369,8 @@ public abstract class ObjectUtils {
return 0;
}
int hash = INITIAL_HASH;
int arraySize = array.length;
for (int i = 0; i < arraySize; i++) {
hash = MULTIPLIER * hash + array[i];
for (byte element : array) {
hash = MULTIPLIER * hash + element;
}
return hash;
}
@ -393,9 +384,8 @@ public abstract class ObjectUtils { @@ -393,9 +384,8 @@ public abstract class ObjectUtils {
return 0;
}
int hash = INITIAL_HASH;
int arraySize = array.length;
for (int i = 0; i < arraySize; i++) {
hash = MULTIPLIER * hash + array[i];
for (char element : array) {
hash = MULTIPLIER * hash + element;
}
return hash;
}
@ -409,9 +399,8 @@ public abstract class ObjectUtils { @@ -409,9 +399,8 @@ public abstract class ObjectUtils {
return 0;
}
int hash = INITIAL_HASH;
int arraySize = array.length;
for (int i = 0; i < arraySize; i++) {
hash = MULTIPLIER * hash + hashCode(array[i]);
for (double element : array) {
hash = MULTIPLIER * hash + hashCode(element);
}
return hash;
}
@ -425,9 +414,8 @@ public abstract class ObjectUtils { @@ -425,9 +414,8 @@ public abstract class ObjectUtils {
return 0;
}
int hash = INITIAL_HASH;
int arraySize = array.length;
for (int i = 0; i < arraySize; i++) {
hash = MULTIPLIER * hash + hashCode(array[i]);
for (float element : array) {
hash = MULTIPLIER * hash + hashCode(element);
}
return hash;
}
@ -441,9 +429,8 @@ public abstract class ObjectUtils { @@ -441,9 +429,8 @@ public abstract class ObjectUtils {
return 0;
}
int hash = INITIAL_HASH;
int arraySize = array.length;
for (int i = 0; i < arraySize; i++) {
hash = MULTIPLIER * hash + array[i];
for (int element : array) {
hash = MULTIPLIER * hash + element;
}
return hash;
}
@ -457,9 +444,8 @@ public abstract class ObjectUtils { @@ -457,9 +444,8 @@ public abstract class ObjectUtils {
return 0;
}
int hash = INITIAL_HASH;
int arraySize = array.length;
for (int i = 0; i < arraySize; i++) {
hash = MULTIPLIER * hash + hashCode(array[i]);
for (long element : array) {
hash = MULTIPLIER * hash + hashCode(element);
}
return hash;
}
@ -473,9 +459,8 @@ public abstract class ObjectUtils { @@ -473,9 +459,8 @@ public abstract class ObjectUtils {
return 0;
}
int hash = INITIAL_HASH;
int arraySize = array.length;
for (int i = 0; i < arraySize; i++) {
hash = MULTIPLIER * hash + array[i];
for (short element : array) {
hash = MULTIPLIER * hash + element;
}
return hash;
}
@ -485,7 +470,7 @@ public abstract class ObjectUtils { @@ -485,7 +470,7 @@ public abstract class ObjectUtils {
* @see Boolean#hashCode()
*/
public static int hashCode(boolean bool) {
return bool ? 1231 : 1237;
return (bool ? 1231 : 1237);
}
/**
@ -493,8 +478,7 @@ public abstract class ObjectUtils { @@ -493,8 +478,7 @@ public abstract class ObjectUtils {
* @see Double#hashCode()
*/
public static int hashCode(double dbl) {
long bits = Double.doubleToLongBits(dbl);
return hashCode(bits);
return hashCode(Double.doubleToLongBits(dbl));
}
/**

20
spring-core/src/test/java/org/springframework/util/ObjectUtilsTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2006 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,9 +16,6 @@ @@ -16,9 +16,6 @@
package org.springframework.util;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.sql.SQLException;
@ -26,6 +23,9 @@ import junit.framework.TestCase; @@ -26,6 +23,9 @@ import junit.framework.TestCase;
import org.springframework.core.task.TaskRejectedException;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
/**
* @author Rod Johnson
* @author Juergen Hoeller
@ -51,25 +51,25 @@ public final class ObjectUtilsTests extends TestCase { @@ -51,25 +51,25 @@ public final class ObjectUtilsTests extends TestCase {
Class<?>[] sqlAndIO = new Class[] {SQLException.class, IOException.class};
Class<?>[] throwable = new Class[] {Throwable.class};
assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new RuntimeException(), null));
assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new RuntimeException()));
assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new RuntimeException(), empty));
assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new RuntimeException(), exception));
assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new RuntimeException(), sqlAndIO));
assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new RuntimeException(), throwable));
assertFalse(ObjectUtils.isCompatibleWithThrowsClause(new Exception(), null));
assertFalse(ObjectUtils.isCompatibleWithThrowsClause(new Exception()));
assertFalse(ObjectUtils.isCompatibleWithThrowsClause(new Exception(), empty));
assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new Exception(), exception));
assertFalse(ObjectUtils.isCompatibleWithThrowsClause(new Exception(), sqlAndIO));
assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new Exception(), throwable));
assertFalse(ObjectUtils.isCompatibleWithThrowsClause(new SQLException(), null));
assertFalse(ObjectUtils.isCompatibleWithThrowsClause(new SQLException()));
assertFalse(ObjectUtils.isCompatibleWithThrowsClause(new SQLException(), empty));
assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new SQLException(), exception));
assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new SQLException(), sqlAndIO));
assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new SQLException(), throwable));
assertFalse(ObjectUtils.isCompatibleWithThrowsClause(new Throwable(), null));
assertFalse(ObjectUtils.isCompatibleWithThrowsClause(new Throwable()));
assertFalse(ObjectUtils.isCompatibleWithThrowsClause(new Throwable(), empty));
assertFalse(ObjectUtils.isCompatibleWithThrowsClause(new Throwable(), exception));
assertFalse(ObjectUtils.isCompatibleWithThrowsClause(new Throwable(), sqlAndIO));
@ -196,7 +196,7 @@ public final class ObjectUtilsTests extends TestCase { @@ -196,7 +196,7 @@ public final class ObjectUtilsTests extends TestCase {
Object obj = new Object();
String expected = obj.getClass().getName() + "@" + ObjectUtils.getIdentityHexString(obj);
String actual = ObjectUtils.identityToString(obj);
assertEquals(expected.toString(), actual);
assertEquals(expected, actual);
}
public void testIdentityToStringWithNullObject() {
@ -620,7 +620,7 @@ public final class ObjectUtilsTests extends TestCase { @@ -620,7 +620,7 @@ public final class ObjectUtilsTests extends TestCase {
assertEquals("null", ObjectUtils.nullSafeToString((String[]) null));
}
enum Tropes { FOO, BAR, baz };
enum Tropes { FOO, BAR, baz }
public void testContainsConstant() {
assertThat(ObjectUtils.containsConstant(Tropes.values(), "FOO"), is(true));

Loading…
Cancel
Save