Browse Source

Polishing

pull/592/head
Juergen Hoeller 12 years ago
parent
commit
2665d56209
  1. 10
      spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java
  2. 16
      spring-context/src/main/java/org/springframework/format/AnnotationFormatterFactory.java
  3. 87
      spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java
  4. 16
      spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java
  5. 14
      spring-core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java
  6. 7
      spring-core/src/main/java/org/springframework/core/convert/support/ArrayToStringConverter.java

10
spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 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.
@ -277,7 +277,7 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements @@ -277,7 +277,7 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements
@Override
public boolean containsSingleton(String beanName) {
return (this.singletonObjects.containsKey(beanName));
return this.singletonObjects.containsKey(beanName);
}
@Override
@ -330,8 +330,7 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements @@ -330,8 +330,7 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements
* @see #isSingletonCurrentlyInCreation
*/
protected void beforeSingletonCreation(String beanName) {
if (!this.inCreationCheckExclusions.contains(beanName) &&
!this.singletonsCurrentlyInCreation.add(beanName)) {
if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.add(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
}
@ -343,8 +342,7 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements @@ -343,8 +342,7 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements
* @see #isSingletonCurrentlyInCreation
*/
protected void afterSingletonCreation(String beanName) {
if (!this.inCreationCheckExclusions.contains(beanName) &&
!this.singletonsCurrentlyInCreation.remove(beanName)) {
if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.remove(beanName)) {
throw new IllegalStateException("Singleton '" + beanName + "' isn't currently in creation");
}
}

16
spring-context/src/main/java/org/springframework/format/AnnotationFormatterFactory.java

@ -13,13 +13,15 @@ @@ -13,13 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.format;
import java.lang.annotation.Annotation;
import java.util.Set;
/**
* A factory that creates formatters to format values of fields annotated with a particular {@link Annotation}.
* A factory that creates formatters to format values of fields annotated with a particular
* {@link Annotation}.
*
* <p>For example, a {@code DateTimeFormatAnnotationFormatterFactory} might create a formatter
* that formats {@code Date} values set on fields annotated with {@code @DateTimeFormat}.
@ -36,8 +38,10 @@ public interface AnnotationFormatterFactory<A extends Annotation> { @@ -36,8 +38,10 @@ public interface AnnotationFormatterFactory<A extends Annotation> {
Set<Class<?>> getFieldTypes();
/**
* Get the Printer to print the value of a field of {@code fieldType} annotated with {@code annotation}.
* If the type &lt;T&gt; the printer accepts is not assignable to {@code fieldType}, a coercion from {@code fieldType} to &lt;T&gt; will be attempted before the Printer is invoked.
* Get the Printer to print the value of a field of {@code fieldType} annotated with
* {@code annotation}.
* <p>If the type T the printer accepts is not assignable to {@code fieldType}, a
* coercion from {@code fieldType} to T will be attempted before the Printer is invoked.
* @param annotation the annotation instance
* @param fieldType the type of field that was annotated
* @return the printer
@ -45,8 +49,10 @@ public interface AnnotationFormatterFactory<A extends Annotation> { @@ -45,8 +49,10 @@ public interface AnnotationFormatterFactory<A extends Annotation> {
Printer<?> getPrinter(A annotation, Class<?> fieldType);
/**
* Get the Parser to parse a submitted value for a field of {@code fieldType} annotated with {@code annotation}.
* If the object the parser returns is not assignable to {@code fieldType}, a coercion to {@code fieldType} will be attempted before the field is set.
* Get the Parser to parse a submitted value for a field of {@code fieldType}
* annotated with {@code annotation}.
* <p>If the object the parser returns is not assignable to {@code fieldType},
* a coercion to {@code fieldType} will be attempted before the field is set.
* @param annotation the annotation instance
* @param fieldType the type of field that was annotated
* @return the parser

87
spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 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.
@ -90,7 +90,7 @@ public class FormattingConversionService extends GenericConversionService @@ -90,7 +90,7 @@ public class FormattingConversionService extends GenericConversionService
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void addFormatterForFieldAnnotation(AnnotationFormatterFactory annotationFormatterFactory) {
final Class<? extends Annotation> annotationType = (Class<? extends Annotation>)
Class<? extends Annotation> annotationType = (Class<? extends Annotation>)
GenericTypeResolver.resolveTypeArgument(annotationFormatterFactory.getClass(), AnnotationFormatterFactory.class);
if (annotationType == null) {
throw new IllegalArgumentException("Unable to extract parameterized Annotation type argument from AnnotationFormatterFactory [" +
@ -100,7 +100,7 @@ public class FormattingConversionService extends GenericConversionService @@ -100,7 +100,7 @@ public class FormattingConversionService extends GenericConversionService
((EmbeddedValueResolverAware) annotationFormatterFactory).setEmbeddedValueResolver(this.embeddedValueResolver);
}
Set<Class<?>> fieldTypes = annotationFormatterFactory.getFieldTypes();
for (final Class<?> fieldType : fieldTypes) {
for (Class<?> fieldType : fieldTypes) {
addConverter(new AnnotationPrinterConverter(annotationType, annotationFormatterFactory, fieldType));
addConverter(new AnnotationParserConverter(annotationType, annotationFormatterFactory, fieldType));
}
@ -109,14 +109,14 @@ public class FormattingConversionService extends GenericConversionService @@ -109,14 +109,14 @@ public class FormattingConversionService extends GenericConversionService
private static class PrinterConverter implements GenericConverter {
private Class<?> fieldType;
private final Class<?> fieldType;
private TypeDescriptor printerObjectType;
private final TypeDescriptor printerObjectType;
@SuppressWarnings("rawtypes")
private Printer printer;
private final Printer printer;
private ConversionService conversionService;
private final ConversionService conversionService;
public PrinterConverter(Class<?> fieldType, Printer<?> printer, ConversionService conversionService) {
this.fieldType = fieldType;
@ -155,11 +155,11 @@ public class FormattingConversionService extends GenericConversionService @@ -155,11 +155,11 @@ public class FormattingConversionService extends GenericConversionService
private static class ParserConverter implements GenericConverter {
private Class<?> fieldType;
private final Class<?> fieldType;
private Parser<?> parser;
private final Parser<?> parser;
private ConversionService conversionService;
private final ConversionService conversionService;
public ParserConverter(Class<?> fieldType, Parser<?> parser, ConversionService conversionService) {
this.fieldType = fieldType;
@ -204,12 +204,12 @@ public class FormattingConversionService extends GenericConversionService @@ -204,12 +204,12 @@ public class FormattingConversionService extends GenericConversionService
private class AnnotationPrinterConverter implements ConditionalGenericConverter {
private Class<? extends Annotation> annotationType;
private final Class<? extends Annotation> annotationType;
@SuppressWarnings("rawtypes")
private AnnotationFormatterFactory annotationFormatterFactory;
private final AnnotationFormatterFactory annotationFormatterFactory;
private Class<?> fieldType;
private final Class<?> fieldType;
public AnnotationPrinterConverter(Class<? extends Annotation> annotationType,
AnnotationFormatterFactory<?> annotationFormatterFactory, Class<?> fieldType) {
@ -220,24 +220,28 @@ public class FormattingConversionService extends GenericConversionService @@ -220,24 +220,28 @@ public class FormattingConversionService extends GenericConversionService
@Override
public Set<ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(fieldType, String.class));
return Collections.singleton(new ConvertiblePair(this.fieldType, String.class));
}
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return sourceType.hasAnnotation(annotationType);
return sourceType.hasAnnotation(this.annotationType);
}
@Override
@SuppressWarnings("unchecked")
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
AnnotationConverterKey converterKey =
new AnnotationConverterKey(sourceType.getAnnotation(annotationType), sourceType.getObjectType());
Annotation ann = sourceType.getAnnotation(this.annotationType);
if (ann == null) {
throw new IllegalStateException(
"Expected [" + this.annotationType.getName() + "] to be present on " + sourceType);
}
AnnotationConverterKey converterKey = new AnnotationConverterKey(ann, sourceType.getObjectType());
GenericConverter converter = cachedPrinters.get(converterKey);
if (converter == null) {
Printer<?> printer = annotationFormatterFactory.getPrinter(
Printer<?> printer = this.annotationFormatterFactory.getPrinter(
converterKey.getAnnotation(), converterKey.getFieldType());
converter = new PrinterConverter(fieldType, printer, FormattingConversionService.this);
converter = new PrinterConverter(this.fieldType, printer, FormattingConversionService.this);
cachedPrinters.put(converterKey, converter);
}
return converter.convert(source, sourceType, targetType);
@ -245,20 +249,20 @@ public class FormattingConversionService extends GenericConversionService @@ -245,20 +249,20 @@ public class FormattingConversionService extends GenericConversionService
@Override
public String toString() {
return "@" + annotationType.getName() + " " + fieldType.getName() + " -> " +
String.class.getName() + ": " + annotationFormatterFactory;
return "@" + this.annotationType.getName() + " " + this.fieldType.getName() + " -> " +
String.class.getName() + ": " + this.annotationFormatterFactory;
}
}
private class AnnotationParserConverter implements ConditionalGenericConverter {
private Class<? extends Annotation> annotationType;
private final Class<? extends Annotation> annotationType;
@SuppressWarnings("rawtypes")
private AnnotationFormatterFactory annotationFormatterFactory;
private final AnnotationFormatterFactory annotationFormatterFactory;
private Class<?> fieldType;
private final Class<?> fieldType;
public AnnotationParserConverter(Class<? extends Annotation> annotationType,
AnnotationFormatterFactory<?> annotationFormatterFactory, Class<?> fieldType) {
@ -274,19 +278,23 @@ public class FormattingConversionService extends GenericConversionService @@ -274,19 +278,23 @@ public class FormattingConversionService extends GenericConversionService
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return targetType.hasAnnotation(annotationType);
return targetType.hasAnnotation(this.annotationType);
}
@Override
@SuppressWarnings("unchecked")
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
AnnotationConverterKey converterKey =
new AnnotationConverterKey(targetType.getAnnotation(annotationType), targetType.getObjectType());
Annotation ann = targetType.getAnnotation(this.annotationType);
if (ann == null) {
throw new IllegalStateException(
"Expected [" + this.annotationType.getName() + "] to be present on " + targetType);
}
AnnotationConverterKey converterKey = new AnnotationConverterKey(ann, targetType.getObjectType());
GenericConverter converter = cachedParsers.get(converterKey);
if (converter == null) {
Parser<?> parser = annotationFormatterFactory.getParser(
Parser<?> parser = this.annotationFormatterFactory.getParser(
converterKey.getAnnotation(), converterKey.getFieldType());
converter = new ParserConverter(fieldType, parser, FormattingConversionService.this);
converter = new ParserConverter(this.fieldType, parser, FormattingConversionService.this);
cachedParsers.put(converterKey, converter);
}
return converter.convert(source, sourceType, targetType);
@ -294,8 +302,8 @@ public class FormattingConversionService extends GenericConversionService @@ -294,8 +302,8 @@ public class FormattingConversionService extends GenericConversionService
@Override
public String toString() {
return String.class.getName() + " -> @" + annotationType.getName() + " " +
fieldType.getName() + ": " + annotationFormatterFactory;
return String.class.getName() + " -> @" + this.annotationType.getName() + " " +
this.fieldType.getName() + ": " + this.annotationFormatterFactory;
}
}
@ -312,25 +320,28 @@ public class FormattingConversionService extends GenericConversionService @@ -312,25 +320,28 @@ public class FormattingConversionService extends GenericConversionService
}
public Annotation getAnnotation() {
return annotation;
return this.annotation;
}
public Class<?> getFieldType() {
return fieldType;
return this.fieldType;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof AnnotationConverterKey)) {
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof AnnotationConverterKey)) {
return false;
}
AnnotationConverterKey key = (AnnotationConverterKey) o;
return this.annotation.equals(key.annotation) && this.fieldType.equals(key.fieldType);
AnnotationConverterKey otherKey = (AnnotationConverterKey) other;
return (this.annotation.equals(otherKey.annotation) && this.fieldType.equals(otherKey.fieldType));
}
@Override
public int hashCode() {
return this.annotation.hashCode() + 29 * this.fieldType.hashCode();
return (this.annotation.hashCode() + 29 * this.fieldType.hashCode());
}
}

16
spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java

@ -129,19 +129,21 @@ public class TypeDescriptor implements Serializable { @@ -129,19 +129,21 @@ public class TypeDescriptor implements Serializable {
}
/**
* Variation of {@link #getType()} that accounts for a primitive type by returning its object wrapper type.
* <p>This is useful for conversion service implementations that wish to normalize to object-based types
* and not work with primitive types directly.
* Variation of {@link #getType()} that accounts for a primitive type by
* returning its object wrapper type.
* <p>This is useful for conversion service implementations that wish to
* normalize to object-based types and not work with primitive types directly.
*/
public Class<?> getObjectType() {
return ClassUtils.resolvePrimitiveIfNecessary(getType());
}
/**
* The type of the backing class, method parameter, field, or property described by this TypeDescriptor.
* The type of the backing class, method parameter, field, or property
* described by this TypeDescriptor.
* <p>Returns primitive types as-is.
* <p>See {@link #getObjectType()} for a variation of this operation that resolves primitive types
* to their corresponding Object types if necessary.
* <p>See {@link #getObjectType()} for a variation of this operation that
* resolves primitive types to their corresponding Object types if necessary.
* @return the type, or {@code null}
* @see #getObjectType()
*/
@ -234,7 +236,7 @@ public class TypeDescriptor implements Serializable { @@ -234,7 +236,7 @@ public class TypeDescriptor implements Serializable {
* @return <tt>true</tt> if the annotation is present
*/
public boolean hasAnnotation(Class<? extends Annotation> annotationType) {
return getAnnotation(annotationType) != null;
return (getAnnotation(annotationType) != null);
}
/**

14
spring-core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 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.
@ -40,11 +40,13 @@ final class ArrayToArrayConverter implements ConditionalGenericConverter { @@ -40,11 +40,13 @@ final class ArrayToArrayConverter implements ConditionalGenericConverter {
private final ConversionService conversionService;
public ArrayToArrayConverter(ConversionService conversionService) {
this.helperConverter = new CollectionToArrayConverter(conversionService);
this.conversionService = conversionService;
}
@Override
public Set<ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(Object[].class, Object[].class));
@ -56,12 +58,10 @@ final class ArrayToArrayConverter implements ConditionalGenericConverter { @@ -56,12 +58,10 @@ final class ArrayToArrayConverter implements ConditionalGenericConverter {
}
@Override
public Object convert(Object source, TypeDescriptor sourceType,
TypeDescriptor targetType) {
if ((conversionService instanceof GenericConversionService)
&& ((GenericConversionService) conversionService).canBypassConvert(
sourceType.getElementTypeDescriptor(),
targetType.getElementTypeDescriptor())) {
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (this.conversionService instanceof GenericConversionService &&
((GenericConversionService) this.conversionService).canBypassConvert(
sourceType.getElementTypeDescriptor(), targetType.getElementTypeDescriptor())) {
return source;
}
List<Object> sourceList = Arrays.asList(ObjectUtils.toObjectArray(source));

7
spring-core/src/main/java/org/springframework/core/convert/support/ArrayToStringConverter.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 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.
@ -27,7 +27,8 @@ import org.springframework.util.ObjectUtils; @@ -27,7 +27,8 @@ import org.springframework.util.ObjectUtils;
/**
* Converts an Array to a comma-delimited String.
* This implementation first adapts the source Array to a List, then delegates to {@link CollectionToStringConverter} to perform the target String conversion.
* This implementation first adapts the source Array to a List,
* then delegates to {@link CollectionToStringConverter} to perform the target String conversion.
*
* @author Keith Donald
* @since 3.0
@ -36,10 +37,12 @@ final class ArrayToStringConverter implements ConditionalGenericConverter { @@ -36,10 +37,12 @@ final class ArrayToStringConverter implements ConditionalGenericConverter {
private final CollectionToStringConverter helperConverter;
public ArrayToStringConverter(ConversionService conversionService) {
this.helperConverter = new CollectionToStringConverter(conversionService);
}
@Override
public Set<ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(Object[].class, String.class));

Loading…
Cancel
Save