Polishing
This commit is contained in:
@@ -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> {
|
||||
Set<Class<?>> 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}.
|
||||
* <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> {
|
||||
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
|
||||
|
||||
@@ -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<? 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
|
||||
((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
|
||||
|
||||
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<? 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
|
||||
|
||||
@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
|
||||
|
||||
@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
|
||||
|
||||
@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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user