Class identity comparisons wherever possible

Issue: SPR-12926
This commit is contained in:
Juergen Hoeller
2015-05-20 14:34:09 +02:00
parent 57e0c789a8
commit b4095c3e1d
108 changed files with 317 additions and 322 deletions

View File

@@ -141,7 +141,7 @@ public abstract class BridgeMethodResolver {
private static Method findGenericDeclaration(Method bridgeMethod) {
// Search parent types for method that has same signature as bridge.
Class<?> superclass = bridgeMethod.getDeclaringClass().getSuperclass();
while (superclass != null && !Object.class.equals(superclass)) {
while (superclass != null && Object.class != superclass) {
Method method = searchForMatch(superclass, bridgeMethod);
if (method != null && !method.isBridge()) {
return method;

View File

@@ -178,20 +178,20 @@ public abstract class CollectionFactory {
public static <E> Collection<E> createCollection(Class<?> collectionType, Class<?> elementType, int capacity) {
Assert.notNull(collectionType, "Collection type must not be null");
if (collectionType.isInterface()) {
if (Set.class.equals(collectionType) || Collection.class.equals(collectionType)) {
if (Set.class == collectionType || Collection.class == collectionType) {
return new LinkedHashSet<E>(capacity);
}
else if (List.class.equals(collectionType)) {
else if (List.class == collectionType) {
return new ArrayList<E>(capacity);
}
else if (SortedSet.class.equals(collectionType) || NavigableSet.class.equals(collectionType)) {
else if (SortedSet.class == collectionType || NavigableSet.class == collectionType) {
return new TreeSet<E>();
}
else {
throw new IllegalArgumentException("Unsupported Collection interface: " + collectionType.getName());
}
}
else if (EnumSet.class.equals(collectionType)) {
else if (EnumSet.class == collectionType) {
Assert.notNull(elementType, "Cannot create EnumSet for unknown element type");
// Cast is necessary for compilation in Eclipse 4.4.1.
return (Collection<E>) EnumSet.noneOf(asEnumType(elementType));
@@ -294,20 +294,20 @@ public abstract class CollectionFactory {
public static <K, V> Map<K, V> createMap(Class<?> mapType, Class<?> keyType, int capacity) {
Assert.notNull(mapType, "Map type must not be null");
if (mapType.isInterface()) {
if (Map.class.equals(mapType)) {
if (Map.class == mapType) {
return new LinkedHashMap<K, V>(capacity);
}
else if (SortedMap.class.equals(mapType) || NavigableMap.class.equals(mapType)) {
else if (SortedMap.class == mapType || NavigableMap.class == mapType) {
return new TreeMap<K, V>();
}
else if (MultiValueMap.class.equals(mapType)) {
else if (MultiValueMap.class == mapType) {
return new LinkedMultiValueMap();
}
else {
throw new IllegalArgumentException("Unsupported Map interface: " + mapType.getName());
}
}
else if (EnumMap.class.equals(mapType)) {
else if (EnumMap.class == mapType) {
Assert.notNull(keyType, "Cannot create EnumMap for unknown key type");
return new EnumMap(asEnumType(keyType));
}

View File

@@ -168,7 +168,7 @@ public abstract class Conventions {
public static String getVariableNameForReturnType(Method method, Class<?> resolvedType, Object value) {
Assert.notNull(method, "Method must not be null");
if (Object.class.equals(resolvedType)) {
if (Object.class == resolvedType) {
if (value == null) {
throw new IllegalArgumentException("Cannot generate variable name for an Object return type with null value");
}

View File

@@ -63,12 +63,12 @@ public class ExceptionDepthComparator implements Comparator<Class<? extends Thro
}
private int getDepth(Class<?> declaredException, Class<?> exceptionToMatch, int depth) {
if (declaredException.equals(exceptionToMatch)) {
if (exceptionToMatch.equals(declaredException)) {
// Found it!
return depth;
}
// If we've gone as far as we can go and haven't found it...
if (Throwable.class.equals(exceptionToMatch)) {
if (exceptionToMatch == Throwable.class) {
return Integer.MAX_VALUE;
}
return getDepth(declaredException, exceptionToMatch.getSuperclass(), depth + 1);

View File

@@ -77,10 +77,10 @@ public abstract class ParameterizedTypeReference<T> {
private static Class<?> findParameterizedTypeReferenceSubclass(Class<?> child) {
Class<?> parent = child.getSuperclass();
if (Object.class.equals(parent)) {
if (Object.class == parent) {
throw new IllegalStateException("Expected ParameterizedTypeReference superclass");
}
else if (ParameterizedTypeReference.class.equals(parent)) {
else if (ParameterizedTypeReference.class == parent) {
return child;
}
else {

View File

@@ -527,7 +527,7 @@ public class ResolvableType implements Serializable {
WildcardType wt = (WildcardType) this.type;
if (wt.getLowerBounds().length == 0) {
Type[] upperBounds = wt.getUpperBounds();
if (upperBounds.length == 0 || (upperBounds.length == 1 && Object.class.equals(upperBounds[0]))) {
if (upperBounds.length == 0 || (upperBounds.length == 1 && Object.class == upperBounds[0])) {
return true;
}
}
@@ -770,7 +770,7 @@ public class ResolvableType implements Serializable {
}
private Type resolveBounds(Type[] bounds) {
if (ObjectUtils.isEmpty(bounds) || Object.class.equals(bounds[0])) {
if (ObjectUtils.isEmpty(bounds) || Object.class == bounds[0]) {
return null;
}
return bounds[0];

View File

@@ -244,10 +244,10 @@ abstract class SerializableTypeWrapper {
}
return this.provider.getType().equals(other);
}
if (Type.class.equals(method.getReturnType()) && args == null) {
if (Type.class == method.getReturnType() && args == null) {
return forTypeProvider(new MethodInvokeTypeProvider(this.provider, method, -1));
}
if (Type[].class.equals(method.getReturnType()) && args == null) {
if (Type[].class == method.getReturnType() && args == null) {
Type[] result = new Type[((Type[]) method.invoke(this.provider.getType(), args)).length];
for (int i = 0; i < result.length; i++) {
result[i] = forTypeProvider(new MethodInvokeTypeProvider(this.provider, method, i));

View File

@@ -694,7 +694,7 @@ public class AnnotatedElementUtils {
Class<?> clazz = method.getDeclaringClass();
while (true) {
clazz = clazz.getSuperclass();
if (clazz == null || clazz.equals(Object.class)) {
if (clazz == null || Object.class == clazz) {
break;
}
@@ -744,7 +744,7 @@ public class AnnotatedElementUtils {
// Search on superclass
if (searchOnSuperclasses) {
Class<?> superclass = clazz.getSuperclass();
if (superclass != null && !superclass.equals(Object.class)) {
if (superclass != null && Object.class != superclass) {
T result = searchWithFindSemantics(superclass, annotationType, searchOnInterfaces,
searchOnSuperclasses, searchOnMethodsInInterfaces, searchOnMethodsInSuperclasses,
processor, visited, metaDepth);

View File

@@ -368,7 +368,7 @@ public abstract class AnnotationUtils {
Class<?> clazz = method.getDeclaringClass();
while (result == null) {
clazz = clazz.getSuperclass();
if (clazz == null || clazz.equals(Object.class)) {
if (clazz == null || Object.class == clazz) {
break;
}
try {
@@ -511,7 +511,7 @@ public abstract class AnnotationUtils {
}
Class<?> superclass = clazz.getSuperclass();
if (superclass == null || superclass.equals(Object.class)) {
if (superclass == null || Object.class == superclass) {
return null;
}
return findAnnotation(superclass, annotationType, visited);
@@ -541,7 +541,7 @@ public abstract class AnnotationUtils {
*/
public static Class<?> findAnnotationDeclaringClass(Class<? extends Annotation> annotationType, Class<?> clazz) {
Assert.notNull(annotationType, "Annotation type must not be null");
if (clazz == null || clazz.equals(Object.class)) {
if (clazz == null || Object.class == clazz) {
return null;
}
if (isAnnotationDeclaredLocally(annotationType, clazz)) {
@@ -576,7 +576,7 @@ public abstract class AnnotationUtils {
*/
public static Class<?> findAnnotationDeclaringClassForTypes(List<Class<? extends Annotation>> annotationTypes, Class<?> clazz) {
Assert.notEmpty(annotationTypes, "The list of annotation types must not be empty");
if (clazz == null || clazz.equals(Object.class)) {
if (clazz == null || Object.class == clazz) {
return null;
}
for (Class<? extends Annotation> annotationType : annotationTypes) {

View File

@@ -677,7 +677,7 @@ public class TypeDescriptor implements Serializable {
private static TypeDescriptor nested(TypeDescriptor typeDescriptor, int nestingLevel) {
ResolvableType nested = typeDescriptor.resolvableType;
for (int i = 0; i < nestingLevel; i++) {
if (Object.class.equals(nested.getType())) {
if (Object.class == nested.getType()) {
// Could be a collection type but we don't know about its element type,
// so let's just assume there is an element type of type Object...
}

View File

@@ -50,7 +50,7 @@ final class FallbackObjectToStringConverter implements ConditionalGenericConvert
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
Class<?> sourceClass = sourceType.getObjectType();
if (String.class.equals(sourceClass)) {
if (String.class == sourceClass) {
// no conversion required
return false;
}

View File

@@ -71,7 +71,7 @@ final class ObjectToObjectConverter implements ConditionalGenericConverter {
// no conversion required
return false;
}
return (String.class.equals(targetType.getType()) ?
return (String.class == targetType.getType() ?
hasFactoryConstructor(String.class, sourceType.getType()) :
hasToMethodOrFactoryMethodOrConstructor(targetType.getType(), sourceType.getType()));
}
@@ -85,7 +85,7 @@ final class ObjectToObjectConverter implements ConditionalGenericConverter {
Class<?> targetClass = targetType.getType();
try {
// Do not invoke a toString() method
if (!String.class.equals(targetClass)) {
if (String.class != targetClass) {
Method method = getToMethod(targetClass, sourceClass);
if (method != null) {
ReflectionUtils.makeAccessible(method);

View File

@@ -759,7 +759,7 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String methodName = method.getName();
if (Object.class.equals(method.getDeclaringClass())) {
if (Object.class == method.getDeclaringClass()) {
if (methodName.equals("equals")) {
// Only consider equal when proxies are identical.
return (proxy == args[0]);

View File

@@ -27,6 +27,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
@@ -34,7 +35,7 @@ import java.util.Set;
/**
* Miscellaneous class utility methods.
* <p>Mainly for internal use within the framework.
* Mainly for internal use within the framework.
*
* @author Juergen Hoeller
* @author Keith Donald
@@ -75,13 +76,13 @@ public abstract class ClassUtils {
* Map with primitive wrapper type as key and corresponding primitive
* type as value, for example: Integer.class -> int.class.
*/
private static final Map<Class<?>, Class<?>> primitiveWrapperTypeMap = new HashMap<Class<?>, Class<?>>(8);
private static final Map<Class<?>, Class<?>> primitiveWrapperTypeMap = new IdentityHashMap<Class<?>, Class<?>>(8);
/**
* Map with primitive type as key and corresponding wrapper
* type as value, for example: int.class -> Integer.class.
*/
private static final Map<Class<?>, Class<?>> primitiveTypeToWrapperMap = new HashMap<Class<?>, Class<?>>(8);
private static final Map<Class<?>, Class<?>> primitiveTypeToWrapperMap = new IdentityHashMap<Class<?>, Class<?>>(8);
/**
* Map with primitive type name as key and corresponding primitive
@@ -352,9 +353,9 @@ public abstract class ClassUtils {
*/
public static Class<?> getUserClass(Class<?> clazz) {
if (clazz != null && clazz.getName().contains(CGLIB_CLASS_SEPARATOR)) {
Class<?> superClass = clazz.getSuperclass();
if (superClass != null && !Object.class.equals(superClass)) {
return superClass;
Class<?> superclass = clazz.getSuperclass();
if (superclass != null && Object.class != superclass) {
return superclass;
}
}
return clazz;
@@ -1186,7 +1187,7 @@ public abstract class ClassUtils {
Class<?> ancestor = clazz1;
do {
ancestor = ancestor.getSuperclass();
if (ancestor == null || Object.class.equals(ancestor)) {
if (ancestor == null || Object.class == ancestor) {
return null;
}
}

View File

@@ -86,28 +86,28 @@ public abstract class NumberUtils {
if (targetClass.isInstance(number)) {
return (T) number;
}
else if (targetClass.equals(Byte.class)) {
else if (Byte.class == targetClass) {
long value = number.longValue();
if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
raiseOverflowException(number, targetClass);
}
return (T) new Byte(number.byteValue());
}
else if (targetClass.equals(Short.class)) {
else if (Short.class == targetClass) {
long value = number.longValue();
if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
raiseOverflowException(number, targetClass);
}
return (T) new Short(number.shortValue());
}
else if (targetClass.equals(Integer.class)) {
else if (Integer.class == targetClass) {
long value = number.longValue();
if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
raiseOverflowException(number, targetClass);
}
return (T) new Integer(number.intValue());
}
else if (targetClass.equals(Long.class)) {
else if (Long.class == targetClass) {
BigInteger bigInt = null;
if (number instanceof BigInteger) {
bigInt = (BigInteger) number;
@@ -121,7 +121,7 @@ public abstract class NumberUtils {
}
return (T) new Long(number.longValue());
}
else if (targetClass.equals(BigInteger.class)) {
else if (BigInteger.class == targetClass) {
if (number instanceof BigDecimal) {
// do not lose precision - use BigDecimal's own conversion
return (T) ((BigDecimal) number).toBigInteger();
@@ -131,13 +131,13 @@ public abstract class NumberUtils {
return (T) BigInteger.valueOf(number.longValue());
}
}
else if (targetClass.equals(Float.class)) {
else if (Float.class == targetClass) {
return (T) new Float(number.floatValue());
}
else if (targetClass.equals(Double.class)) {
else if (Double.class == targetClass) {
return (T) new Double(number.doubleValue());
}
else if (targetClass.equals(BigDecimal.class)) {
else if (BigDecimal.class == targetClass) {
// always use BigDecimal(String) here to avoid unpredictability of BigDecimal(double)
// (see BigDecimal javadoc for details)
return (T) new BigDecimal(number.toString());
@@ -183,28 +183,28 @@ public abstract class NumberUtils {
Assert.notNull(targetClass, "Target class must not be null");
String trimmed = StringUtils.trimAllWhitespace(text);
if (targetClass.equals(Byte.class)) {
if (Byte.class == targetClass) {
return (T) (isHexNumber(trimmed) ? Byte.decode(trimmed) : Byte.valueOf(trimmed));
}
else if (targetClass.equals(Short.class)) {
else if (Short.class == targetClass) {
return (T) (isHexNumber(trimmed) ? Short.decode(trimmed) : Short.valueOf(trimmed));
}
else if (targetClass.equals(Integer.class)) {
else if (Integer.class == targetClass) {
return (T) (isHexNumber(trimmed) ? Integer.decode(trimmed) : Integer.valueOf(trimmed));
}
else if (targetClass.equals(Long.class)) {
else if (Long.class == targetClass) {
return (T) (isHexNumber(trimmed) ? Long.decode(trimmed) : Long.valueOf(trimmed));
}
else if (targetClass.equals(BigInteger.class)) {
else if (BigInteger.class == targetClass) {
return (T) (isHexNumber(trimmed) ? decodeBigInteger(trimmed) : new BigInteger(trimmed));
}
else if (targetClass.equals(Float.class)) {
else if (Float.class == targetClass) {
return (T) Float.valueOf(trimmed);
}
else if (targetClass.equals(Double.class)) {
else if (Double.class == targetClass) {
return (T) Double.valueOf(trimmed);
}
else if (targetClass.equals(BigDecimal.class) || targetClass.equals(Number.class)) {
else if (BigDecimal.class == targetClass || Number.class == targetClass) {
return (T) new BigDecimal(trimmed);
}
else {
@@ -236,7 +236,7 @@ public abstract class NumberUtils {
boolean resetBigDecimal = false;
if (numberFormat instanceof DecimalFormat) {
decimalFormat = (DecimalFormat) numberFormat;
if (BigDecimal.class.equals(targetClass) && !decimalFormat.isParseBigDecimal()) {
if (BigDecimal.class == targetClass && !decimalFormat.isParseBigDecimal()) {
decimalFormat.setParseBigDecimal(true);
resetBigDecimal = true;
}

View File

@@ -89,7 +89,7 @@ public abstract class ReflectionUtils {
Assert.notNull(clazz, "Class must not be null");
Assert.isTrue(name != null || type != null, "Either name or type of the field must be specified");
Class<?> searchType = clazz;
while (!Object.class.equals(searchType) && searchType != null) {
while (Object.class != searchType && searchType != null) {
Field[] fields = getDeclaredFields(searchType);
for (Field field : fields) {
if ((name == null || name.equals(field.getName())) &&

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 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.
@@ -44,7 +44,7 @@ public abstract class TypeUtils {
Assert.notNull(rhsType, "Right-hand side type must not be null");
// all types are assignable to themselves and to class Object
if (lhsType.equals(rhsType) || lhsType.equals(Object.class)) {
if (lhsType.equals(rhsType) || Object.class == lhsType) {
return true;
}