Browse Source

polishing

pull/23217/head
Juergen Hoeller 15 years ago
parent
commit
cc0bd730eb
  1. 4
      org.springframework.aop/src/main/java/org/springframework/aop/framework/ReflectiveMethodInvocation.java
  2. 1
      org.springframework.core/src/main/java/org/springframework/core/convert/ConversionException.java
  3. 14
      org.springframework.core/src/main/java/org/springframework/core/convert/ConversionFailedException.java
  4. 127
      org.springframework.core/src/main/java/org/springframework/core/convert/TypeDescriptor.java
  5. 28
      org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java
  6. 15
      org.springframework.core/src/main/java/org/springframework/util/CollectionUtils.java
  7. 36
      org.springframework.expression/src/main/java/org/springframework/expression/spel/support/StandardTypeConverter.java
  8. 5
      org.springframework.orm/src/main/java/org/springframework/orm/hibernate3/LocalDataSourceConnectionProvider.java
  9. 4
      org.springframework.orm/src/main/java/org/springframework/orm/hibernate3/SessionFactoryUtils.java

4
org.springframework.aop/src/main/java/org/springframework/aop/framework/ReflectiveMethodInvocation.java

@ -102,8 +102,8 @@ public class ReflectiveMethodInvocation implements ProxyMethodInvocation, Clonea @@ -102,8 +102,8 @@ public class ReflectiveMethodInvocation implements ProxyMethodInvocation, Clonea
* but would complicate the code. And it would work only for static pointcuts.
*/
protected ReflectiveMethodInvocation(
Object proxy, Object target, Method method, Object[] arguments,
Class targetClass, List<Object> interceptorsAndDynamicMethodMatchers) {
Object proxy, Object target, Method method, Object[] arguments,
Class targetClass, List<Object> interceptorsAndDynamicMethodMatchers) {
this.proxy = proxy;
this.target = target;

1
org.springframework.core/src/main/java/org/springframework/core/convert/ConversionException.java

@ -24,7 +24,6 @@ import org.springframework.core.NestedRuntimeException; @@ -24,7 +24,6 @@ import org.springframework.core.NestedRuntimeException;
* @author Keith Donald
* @since 3.0
*/
@SuppressWarnings("serial")
public abstract class ConversionException extends NestedRuntimeException {
/**

14
org.springframework.core/src/main/java/org/springframework/core/convert/ConversionFailedException.java

@ -22,13 +22,13 @@ package org.springframework.core.convert; @@ -22,13 +22,13 @@ package org.springframework.core.convert;
* @author Keith Donald
* @since 3.0
*/
@SuppressWarnings("serial")
public final class ConversionFailedException extends ConversionException {
private final TypeDescriptor sourceType;
private final TypeDescriptor targetType;
/**
* Create a new conversion exception.
* @param value the value we tried to convert
@ -37,11 +37,13 @@ public final class ConversionFailedException extends ConversionException { @@ -37,11 +37,13 @@ public final class ConversionFailedException extends ConversionException {
* @param cause the cause of the conversion failure
*/
public ConversionFailedException(TypeDescriptor sourceType, TypeDescriptor targetType, Object value, Throwable cause) {
super(buildDefaultMessage(value, sourceType, targetType, cause), cause);
super("Unable to convert value " + value + " from type [" + sourceType.getName() + "] to type [" +
targetType.getName() + "]; reason = '" + cause.getMessage() + "'", cause);
this.sourceType = sourceType;
this.targetType = targetType;
}
/**
* Return the source type we tried to convert the value from.
*/
@ -56,10 +58,4 @@ public final class ConversionFailedException extends ConversionException { @@ -56,10 +58,4 @@ public final class ConversionFailedException extends ConversionException {
return this.targetType;
}
private static String buildDefaultMessage(Object value, TypeDescriptor sourceType, TypeDescriptor targetType,
Throwable cause) {
return "Unable to convert value " + value + " from type [" + sourceType.getName() + "] to type ["
+ targetType.getName() + "]; reason = '" + cause.getMessage() + "'";
}
}
}

127
org.springframework.core/src/main/java/org/springframework/core/convert/TypeDescriptor.java

@ -26,6 +26,7 @@ import org.springframework.core.GenericCollectionTypeResolver; @@ -26,6 +26,7 @@ import org.springframework.core.GenericCollectionTypeResolver;
import org.springframework.core.MethodParameter;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
/**
* Context about a type to convert to.
@ -42,6 +43,7 @@ public class TypeDescriptor { @@ -42,6 +43,7 @@ public class TypeDescriptor {
*/
public static final TypeDescriptor NULL = new TypeDescriptor();
private Class<?> type;
private TypeDescriptor elementType;
@ -52,6 +54,7 @@ public class TypeDescriptor { @@ -52,6 +54,7 @@ public class TypeDescriptor {
private Annotation[] cachedFieldAnnotations;
/**
* Create a new descriptor for the given type.
* <p>Use this constructor when a conversion point comes from a source such as a Map
@ -99,6 +102,7 @@ public class TypeDescriptor { @@ -99,6 +102,7 @@ public class TypeDescriptor {
this.elementType = elementType;
}
/**
* Return the wrapped MethodParameter, if any.
* <p>Note: Either MethodParameter or Field is available.
@ -124,11 +128,14 @@ public class TypeDescriptor { @@ -124,11 +128,14 @@ public class TypeDescriptor {
public Class<?> getType() {
if (this.type != null) {
return this.type;
} else if (this.field != null) {
}
else if (this.field != null) {
return this.field.getType();
} else if (this.methodParameter != null) {
}
else if (this.methodParameter != null) {
return this.methodParameter.getParameterType();
} else {
}
else {
return null;
}
}
@ -139,7 +146,7 @@ public class TypeDescriptor { @@ -139,7 +146,7 @@ public class TypeDescriptor {
*/
public Class<?> getObjectType() {
Class<?> type = getType();
return type != null ? ClassUtils.resolvePrimitiveIfNecessary(type) : type;
return (type != null ? ClassUtils.resolvePrimitiveIfNecessary(type) : type);
}
/**
@ -147,8 +154,7 @@ public class TypeDescriptor { @@ -147,8 +154,7 @@ public class TypeDescriptor {
* @param type the type to test against
*/
public boolean typeEquals(Class<?> type) {
Class<?> thisType = getType();
return thisType != null ? thisType.equals(type) : false;
return ObjectUtils.nullSafeEquals(getType(), type);
}
/**
@ -158,7 +164,8 @@ public class TypeDescriptor { @@ -158,7 +164,8 @@ public class TypeDescriptor {
Class<?> type = getType();
if (type != null) {
return getType().getName();
} else {
}
else {
return null;
}
}
@ -198,14 +205,17 @@ public class TypeDescriptor { @@ -198,14 +205,17 @@ public class TypeDescriptor {
* Return the element type as a type descriptor.
*/
public TypeDescriptor getElementTypeDescriptor() {
if (elementType != null) {
return elementType;
} else {
if (this.elementType != null) {
return this.elementType;
}
else {
if (isArray()) {
return TypeDescriptor.valueOf(getArrayComponentType());
} else if (isCollection()) {
}
else if (isCollection()) {
return TypeDescriptor.valueOf(getCollectionElementType());
} else {
}
else {
return TypeDescriptor.NULL;
}
}
@ -232,9 +242,11 @@ public class TypeDescriptor { @@ -232,9 +242,11 @@ public class TypeDescriptor {
public Class<?> getMapKeyType() {
if (this.field != null) {
return GenericCollectionTypeResolver.getMapKeyFieldType(field);
} else if (this.methodParameter != null) {
}
else if (this.methodParameter != null) {
return GenericCollectionTypeResolver.getMapKeyParameterType(this.methodParameter);
} else {
}
else {
return null;
}
}
@ -246,9 +258,11 @@ public class TypeDescriptor { @@ -246,9 +258,11 @@ public class TypeDescriptor {
public Class<?> getMapValueType() {
if (this.field != null) {
return GenericCollectionTypeResolver.getMapValueFieldType(this.field);
} else if (this.methodParameter != null) {
}
else if (this.methodParameter != null) {
return GenericCollectionTypeResolver.getMapValueParameterType(this.methodParameter);
} else {
}
else {
return null;
}
}
@ -276,9 +290,11 @@ public class TypeDescriptor { @@ -276,9 +290,11 @@ public class TypeDescriptor {
this.cachedFieldAnnotations = this.field.getAnnotations();
}
return this.cachedFieldAnnotations;
} else if (this.methodParameter != null) {
}
else if (this.methodParameter != null) {
return this.methodParameter.getParameterAnnotations();
} else {
}
else {
return new Annotation[0];
}
}
@ -324,6 +340,28 @@ public class TypeDescriptor { @@ -324,6 +340,28 @@ public class TypeDescriptor {
return type != null && ClassUtils.isAssignable(targetType.getType(), type);
}
private Class<?> getArrayComponentType() {
return getType().getComponentType();
}
@SuppressWarnings("unchecked")
private Class<?> getCollectionElementType() {
if (this.type != null) {
return GenericCollectionTypeResolver.getCollectionType((Class<? extends Collection>) this.type);
}
else if (this.field != null) {
return GenericCollectionTypeResolver.getCollectionFieldType(this.field);
}
else {
return GenericCollectionTypeResolver.getCollectionParameterType(this.methodParameter);
}
}
private boolean isTypeAssignableTo(Class<?> clazz) {
Class<?> type = getType();
return (type != null && ClassUtils.isAssignable(clazz, type));
}
/**
* @return a textual representation of the type descriptor (eg. Map<String,Foo>) for use in messages
*/
@ -332,7 +370,8 @@ public class TypeDescriptor { @@ -332,7 +370,8 @@ public class TypeDescriptor {
if (isArray()) {
// TODO should properly handle multi dimensional arrays
stringValue.append(getArrayComponentType().getName()).append("[]");
} else {
}
else {
Class<?> clazz = getType();
if (clazz == null) {
return "null";
@ -355,27 +394,23 @@ public class TypeDescriptor { @@ -355,27 +394,23 @@ public class TypeDescriptor {
return stringValue.toString();
}
// internal helpers
private Class<?> getArrayComponentType() {
return getType().getComponentType();
}
@SuppressWarnings("unchecked")
private Class<?> getCollectionElementType() {
if (this.type != null) {
return GenericCollectionTypeResolver.getCollectionType((Class<? extends Collection>) this.type);
} else if (this.field != null) {
return GenericCollectionTypeResolver.getCollectionFieldType(this.field);
} else {
return GenericCollectionTypeResolver.getCollectionParameterType(this.methodParameter);
public String toString() {
if (this == TypeDescriptor.NULL) {
return "[TypeDescriptor.NULL]";
}
else {
StringBuilder builder = new StringBuilder();
builder.append("[TypeDescriptor ");
Annotation[] anns = getAnnotations();
for (Annotation ann : anns) {
builder.append("@").append(ann.annotationType().getName()).append(' ');
}
builder.append(getType().getName());
builder.append("]");
return builder.toString();
}
}
private boolean isTypeAssignableTo(Class<?> clazz) {
Class<?> type = getType();
return (type != null && ClassUtils.isAssignable(clazz, type));
}
// static factory methods
@ -406,21 +441,5 @@ public class TypeDescriptor { @@ -406,21 +441,5 @@ public class TypeDescriptor {
public static TypeDescriptor collection(Class<?> type, TypeDescriptor elementType) {
return new TypeDescriptor(type, elementType);
}
public String toString() {
if (this == TypeDescriptor.NULL) {
return "[TypeDescriptor.NULL]";
} else {
StringBuilder builder = new StringBuilder();
builder.append("[TypeDescriptor ");
Annotation[] anns = getAnnotations();
for (Annotation ann : anns) {
builder.append("@" + ann.annotationType().getName()).append(' ');
}
builder.append(getType().getName());
builder.append("]");
return builder.toString();
}
}
}

28
org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java

@ -62,8 +62,7 @@ public class GenericConversionService implements ConversionService, ConverterReg @@ -62,8 +62,7 @@ public class GenericConversionService implements ConversionService, ConverterReg
}
};
private final Map<Class<?>, Map<Class<?>, MatchableConverters>> converters = new HashMap<Class<?>, Map<Class<?>, MatchableConverters>>(
36);
private final Map<Class<?>, Map<Class<?>, MatchableConverters>> converters = new HashMap<Class<?>, Map<Class<?>, MatchableConverters>>(36);
// implementing ConverterRegistry
@ -182,7 +181,8 @@ public class GenericConversionService implements ConversionService, ConverterReg @@ -182,7 +181,8 @@ public class GenericConversionService implements ConversionService, ConverterReg
GenericConverter converter = findConverterForClassPair(sourceType, targetType);
if (converter != null) {
return converter;
} else {
}
else {
return getDefaultConverter(sourceType, targetType);
}
}
@ -199,11 +199,13 @@ public class GenericConversionService implements ConversionService, ConverterReg @@ -199,11 +199,13 @@ public class GenericConversionService implements ConversionService, ConverterReg
protected GenericConverter getDefaultConverter(TypeDescriptor sourceType, TypeDescriptor targetType) {
if (sourceType.isAssignableTo(targetType)) {
return NO_OP_CONVERTER;
} else {
}
else {
return null;
}
}
// internal helpers
private Class<?>[] getRequiredTypeInfo(Object converter, Class<?> genericIfc) {
@ -256,7 +258,8 @@ public class GenericConversionService implements ConversionService, ConverterReg @@ -256,7 +258,8 @@ public class GenericConversionService implements ConversionService, ConverterReg
}
Map<Class<?>, MatchableConverters> objectConverters = getTargetConvertersForSource(Object.class);
return getMatchingConverterForTarget(sourceType, targetType, objectConverters);
} else {
}
else {
LinkedList<Class<?>> classQueue = new LinkedList<Class<?>>();
classQueue.addFirst(sourceObjectType);
while (!classQueue.isEmpty()) {
@ -271,7 +274,8 @@ public class GenericConversionService implements ConversionService, ConverterReg @@ -271,7 +274,8 @@ public class GenericConversionService implements ConversionService, ConverterReg
if (componentType.getSuperclass() != null) {
classQueue.addFirst(Array.newInstance(componentType.getSuperclass(), 0).getClass());
}
} else {
}
else {
Class<?>[] interfaces = currentClass.getInterfaces();
for (Class<?> ifc : interfaces) {
classQueue.addFirst(ifc);
@ -295,6 +299,7 @@ public class GenericConversionService implements ConversionService, ConverterReg @@ -295,6 +299,7 @@ public class GenericConversionService implements ConversionService, ConverterReg
private GenericConverter getMatchingConverterForTarget(TypeDescriptor sourceType, TypeDescriptor targetType,
Map<Class<?>, MatchableConverters> converters) {
Class<?> targetObjectType = targetType.getObjectType();
if (targetObjectType.isInterface()) {
LinkedList<Class<?>> classQueue = new LinkedList<Class<?>>();
@ -312,7 +317,8 @@ public class GenericConversionService implements ConversionService, ConverterReg @@ -312,7 +317,8 @@ public class GenericConversionService implements ConversionService, ConverterReg
}
}
return matchConverter(converters.get(Object.class), sourceType, targetType);
} else {
}
else {
LinkedList<Class<?>> classQueue = new LinkedList<Class<?>>();
classQueue.addFirst(targetObjectType);
while (!classQueue.isEmpty()) {
@ -343,9 +349,11 @@ public class GenericConversionService implements ConversionService, ConverterReg @@ -343,9 +349,11 @@ public class GenericConversionService implements ConversionService, ConverterReg
private GenericConverter matchConverter(MatchableConverters matchable, TypeDescriptor sourceFieldType,
TypeDescriptor targetFieldType) {
return matchable != null ? matchable.matchConverter(sourceFieldType, targetFieldType) : null;
}
@SuppressWarnings("unchecked")
private final class ConverterAdapter implements GenericConverter {
@ -374,6 +382,7 @@ public class GenericConversionService implements ConversionService, ConverterReg @@ -374,6 +382,7 @@ public class GenericConversionService implements ConversionService, ConverterReg
}
}
@SuppressWarnings("unchecked")
private final class ConverterFactoryAdapter implements GenericConverter {
@ -400,9 +409,9 @@ public class GenericConversionService implements ConversionService, ConverterReg @@ -400,9 +409,9 @@ public class GenericConversionService implements ConversionService, ConverterReg
public String toString() {
return this.typeInfo[0].getName() + " -> " + this.typeInfo[1].getName() + " : " + this.converterFactory.toString();
}
}
private static class MatchableConverters {
private LinkedList<ConditionalGenericConverter> conditionalConverters;
@ -458,7 +467,6 @@ public class GenericConversionService implements ConversionService, ConverterReg @@ -458,7 +467,6 @@ public class GenericConversionService implements ConversionService, ConverterReg
return this.defaultConverter.toString();
}
}
}
}
}

15
org.springframework.core/src/main/java/org/springframework/util/CollectionUtils.java

@ -213,7 +213,7 @@ public abstract class CollectionUtils { @@ -213,7 +213,7 @@ public abstract class CollectionUtils {
* or <code>null</code> if none or more than one such value found
*/
@SuppressWarnings("unchecked")
public static <T> T findValueOfType(Collection collection, Class<T> type) {
public static <T> T findValueOfType(Collection<?> collection, Class<T> type) {
if (isEmpty(collection)) {
return null;
}
@ -239,11 +239,11 @@ public abstract class CollectionUtils { @@ -239,11 +239,11 @@ public abstract class CollectionUtils {
* @return a value of one of the given types found if there is a clear match,
* or <code>null</code> if none or more than one such value found
*/
public static Object findValueOfType(Collection collection, Class[] types) {
public static Object findValueOfType(Collection<?> collection, Class<?>[] types) {
if (isEmpty(collection) || ObjectUtils.isEmpty(types)) {
return null;
}
for (Class type : types) {
for (Class<?> type : types) {
Object value = findValueOfType(collection, type);
if (value != null) {
return value;
@ -264,8 +264,7 @@ public abstract class CollectionUtils { @@ -264,8 +264,7 @@ public abstract class CollectionUtils {
}
boolean hasCandidate = false;
Object candidate = null;
for (Iterator it = collection.iterator(); it.hasNext();) {
Object elem = it.next();
for (Object elem : collection) {
if (!hasCandidate) {
hasCandidate = true;
candidate = elem;
@ -286,6 +285,7 @@ public abstract class CollectionUtils { @@ -286,6 +285,7 @@ public abstract class CollectionUtils {
return new EnumerationIterator<E>(enumeration);
}
/**
* Iterator wrapping an Enumeration.
*/
@ -298,15 +298,16 @@ public abstract class CollectionUtils { @@ -298,15 +298,16 @@ public abstract class CollectionUtils {
}
public boolean hasNext() {
return enumeration.hasMoreElements();
return this.enumeration.hasMoreElements();
}
public E next() {
return enumeration.nextElement();
return this.enumeration.nextElement();
}
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException("Not supported");
}
}
}

36
org.springframework.expression/src/main/java/org/springframework/expression/spel/support/StandardTypeConverter.java

@ -28,7 +28,8 @@ import org.springframework.expression.spel.SpelMessage; @@ -28,7 +28,8 @@ import org.springframework.expression.spel.SpelMessage;
import org.springframework.util.Assert;
/**
* Default implementation of the {@link TypeConverter} interface, delegating to a core Spring {@link ConversionService}.
* Default implementation of the {@link TypeConverter} interface,
* delegating to a core Spring {@link ConversionService}.
*
* @author Juergen Hoeller
* @author Andy Clement
@ -37,12 +38,18 @@ import org.springframework.util.Assert; @@ -37,12 +38,18 @@ import org.springframework.util.Assert;
*/
public class StandardTypeConverter implements TypeConverter {
private static ConversionService DEFAULT_INSTANCE;
private static ConversionService defaultConversionService;
private final ConversionService conversionService;
public StandardTypeConverter() {
this.conversionService = getDefaultConversionService();
synchronized (this) {
if (defaultConversionService == null) {
defaultConversionService = ConversionServiceFactory.createDefaultConversionService();
}
}
this.conversionService = defaultConversionService;
}
public StandardTypeConverter(ConversionService conversionService) {
@ -50,6 +57,7 @@ public class StandardTypeConverter implements TypeConverter { @@ -50,6 +57,7 @@ public class StandardTypeConverter implements TypeConverter {
this.conversionService = conversionService;
}
public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
return this.conversionService.canConvert(sourceType, targetType);
}
@ -57,21 +65,15 @@ public class StandardTypeConverter implements TypeConverter { @@ -57,21 +65,15 @@ public class StandardTypeConverter implements TypeConverter {
public Object convertValue(Object value, TypeDescriptor typeDescriptor) throws EvaluationException {
try {
return this.conversionService.convert(value, TypeDescriptor.forObject(value), typeDescriptor);
} catch (ConverterNotFoundException cenfe) {
throw new SpelEvaluationException(cenfe, SpelMessage.TYPE_CONVERSION_ERROR, value != null ? value
.getClass() : null, typeDescriptor.asString());
} catch (ConversionException ce) {
throw new SpelEvaluationException(ce, SpelMessage.TYPE_CONVERSION_ERROR, value != null ? value.getClass()
: null, typeDescriptor.asString());
}
}
private ConversionService getDefaultConversionService() {
synchronized(this) {
if (DEFAULT_INSTANCE == null) {
DEFAULT_INSTANCE = ConversionServiceFactory.createDefaultConversionService();
}
catch (ConverterNotFoundException cenfe) {
throw new SpelEvaluationException(cenfe, SpelMessage.TYPE_CONVERSION_ERROR,
(value != null ? value.getClass() : null), typeDescriptor.asString());
}
catch (ConversionException ce) {
throw new SpelEvaluationException(ce, SpelMessage.TYPE_CONVERSION_ERROR,
(value != null ? value.getClass() : null), typeDescriptor.asString());
}
return DEFAULT_INSTANCE;
}
}

5
org.springframework.orm/src/main/java/org/springframework/orm/hibernate3/LocalDataSourceConnectionProvider.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2009 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.
@ -19,7 +19,6 @@ package org.springframework.orm.hibernate3; @@ -19,7 +19,6 @@ package org.springframework.orm.hibernate3;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.HibernateException;
@ -70,7 +69,7 @@ public class LocalDataSourceConnectionProvider implements ConnectionProvider { @@ -70,7 +69,7 @@ public class LocalDataSourceConnectionProvider implements ConnectionProvider {
* Return the DataSource that this ConnectionProvider wraps.
*/
public DataSource getDataSource() {
return dataSource;
return this.dataSource;
}
/**

4
org.springframework.orm/src/main/java/org/springframework/orm/hibernate3/SessionFactoryUtils.java

@ -194,7 +194,7 @@ public abstract class SessionFactoryUtils { @@ -194,7 +194,7 @@ public abstract class SessionFactoryUtils {
* @see HibernateTemplate
*/
public static Session getSession(SessionFactory sessionFactory, boolean allowCreate)
throws DataAccessResourceFailureException, IllegalStateException {
throws DataAccessResourceFailureException, IllegalStateException {
try {
return doGetSession(sessionFactory, null, null, allowCreate);
@ -251,7 +251,7 @@ public abstract class SessionFactoryUtils { @@ -251,7 +251,7 @@ public abstract class SessionFactoryUtils {
* @throws IllegalStateException if no thread-bound Session found and allowCreate false
*/
public static Session doGetSession(SessionFactory sessionFactory, boolean allowCreate)
throws HibernateException, IllegalStateException {
throws HibernateException, IllegalStateException {
return doGetSession(sessionFactory, null, null, allowCreate);
}

Loading…
Cancel
Save