From 2665d56209ed686518974a57d73e8a057404b2be Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 15 Jul 2014 15:13:35 +0200 Subject: [PATCH] Polishing --- .../support/DefaultSingletonBeanRegistry.java | 10 +-- .../format/AnnotationFormatterFactory.java | 16 ++-- .../support/FormattingConversionService.java | 87 +++++++++++-------- .../core/convert/TypeDescriptor.java | 16 ++-- .../support/ArrayToArrayConverter.java | 14 +-- .../support/ArrayToStringConverter.java | 7 +- 6 files changed, 85 insertions(+), 65 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java index e0dd5796b64..c5218e88d5d 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java @@ -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 @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 * @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 * @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"); } } diff --git a/spring-context/src/main/java/org/springframework/format/AnnotationFormatterFactory.java b/spring-context/src/main/java/org/springframework/format/AnnotationFormatterFactory.java index dd0f12dd967..aaa5e720e1b 100644 --- a/spring-context/src/main/java/org/springframework/format/AnnotationFormatterFactory.java +++ b/spring-context/src/main/java/org/springframework/format/AnnotationFormatterFactory.java @@ -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}. * *

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 { Set> getFieldTypes(); /** - * Get the Printer to print the value of a field of {@code fieldType} annotated with {@code annotation}. - * 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. + * Get the Printer to print the value of a field of {@code fieldType} annotated with + * {@code annotation}. + *

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 { 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}. + *

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 diff --git a/spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java b/spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java index fb79c71782f..3ddf7e6143e 100644 --- a/spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java +++ b/spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java @@ -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 @Override @SuppressWarnings({ "unchecked", "rawtypes" }) public void addFormatterForFieldAnnotation(AnnotationFormatterFactory annotationFormatterFactory) { - final Class annotationType = (Class) + Class annotationType = (Class) 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 ((EmbeddedValueResolverAware) annotationFormatterFactory).setEmbeddedValueResolver(this.embeddedValueResolver); } Set> 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 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 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 private class AnnotationPrinterConverter implements ConditionalGenericConverter { - private Class annotationType; + private final Class annotationType; @SuppressWarnings("rawtypes") - private AnnotationFormatterFactory annotationFormatterFactory; + private final AnnotationFormatterFactory annotationFormatterFactory; - private Class fieldType; + private final Class fieldType; public AnnotationPrinterConverter(Class annotationType, AnnotationFormatterFactory annotationFormatterFactory, Class fieldType) { @@ -220,24 +220,28 @@ public class FormattingConversionService extends GenericConversionService @Override public Set 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 @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 annotationType; + private final Class annotationType; @SuppressWarnings("rawtypes") - private AnnotationFormatterFactory annotationFormatterFactory; + private final AnnotationFormatterFactory annotationFormatterFactory; - private Class fieldType; + private final Class fieldType; public AnnotationParserConverter(Class annotationType, AnnotationFormatterFactory annotationFormatterFactory, Class fieldType) { @@ -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 @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 } 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()); } } diff --git a/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java b/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java index 89b7a4a6342..9dc23064611 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java +++ b/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java @@ -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. - *

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. + *

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. *

Returns primitive types as-is. - *

See {@link #getObjectType()} for a variation of this operation that resolves primitive types - * to their corresponding Object types if necessary. + *

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 { * @return true if the annotation is present */ public boolean hasAnnotation(Class annotationType) { - return getAnnotation(annotationType) != null; + return (getAnnotation(annotationType) != null); } /** diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java b/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java index 0fb2f4de179..bdad578fc5d 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java @@ -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 { private final ConversionService conversionService; + public ArrayToArrayConverter(ConversionService conversionService) { this.helperConverter = new CollectionToArrayConverter(conversionService); this.conversionService = conversionService; } + @Override public Set getConvertibleTypes() { return Collections.singleton(new ConvertiblePair(Object[].class, Object[].class)); @@ -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 sourceList = Arrays.asList(ObjectUtils.toObjectArray(source)); diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToStringConverter.java b/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToStringConverter.java index 85326e4b601..ff9db8ef3f2 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToStringConverter.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/ArrayToStringConverter.java @@ -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; /** * 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 { private final CollectionToStringConverter helperConverter; + public ArrayToStringConverter(ConversionService conversionService) { this.helperConverter = new CollectionToStringConverter(conversionService); } + @Override public Set getConvertibleTypes() { return Collections.singleton(new ConvertiblePair(Object[].class, String.class));