polishing
This commit is contained in:
parent
c97257b16f
commit
b9ebdd28fb
|
|
@ -34,6 +34,7 @@ public interface FormatterRegistry extends ConverterRegistry {
|
||||||
* The field type is implied by the parameterized Formatter instance.
|
* The field type is implied by the parameterized Formatter instance.
|
||||||
* @param formatter the formatter to add
|
* @param formatter the formatter to add
|
||||||
* @see #addFormatterForFieldType(Class, Formatter)
|
* @see #addFormatterForFieldType(Class, Formatter)
|
||||||
|
* @since 3.1
|
||||||
*/
|
*/
|
||||||
void addFormatter(Formatter<?> formatter);
|
void addFormatter(Formatter<?> formatter);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -65,10 +65,10 @@ public class FormattingConversionService extends GenericConversionService
|
||||||
|
|
||||||
|
|
||||||
public void addFormatter(Formatter<?> formatter) {
|
public void addFormatter(Formatter<?> formatter) {
|
||||||
final Class<?> fieldType = GenericTypeResolver.resolveTypeArgument(formatter.getClass(), Formatter.class);
|
Class<?> fieldType = GenericTypeResolver.resolveTypeArgument(formatter.getClass(), Formatter.class);
|
||||||
if (fieldType == null) {
|
if (fieldType == null) {
|
||||||
throw new IllegalArgumentException("Unable to extract parameterized field type argument from Formatter ["
|
throw new IllegalArgumentException("Unable to extract parameterized field type argument from Formatter [" +
|
||||||
+ formatter.getClass().getName() + "]; does the formatter parameterize the <T> generic type?");
|
formatter.getClass().getName() + "]; does the formatter parameterize the <T> generic type?");
|
||||||
}
|
}
|
||||||
addFormatterForFieldType(fieldType, formatter);
|
addFormatterForFieldType(fieldType, formatter);
|
||||||
}
|
}
|
||||||
|
|
@ -84,7 +84,7 @@ public class FormattingConversionService extends GenericConversionService
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||||
public void addFormatterForFieldAnnotation(final AnnotationFormatterFactory annotationFormatterFactory) {
|
public void addFormatterForFieldAnnotation(AnnotationFormatterFactory annotationFormatterFactory) {
|
||||||
final Class<? extends Annotation> annotationType = (Class<? extends Annotation>)
|
final Class<? extends Annotation> annotationType = (Class<? extends Annotation>)
|
||||||
GenericTypeResolver.resolveTypeArgument(annotationFormatterFactory.getClass(), AnnotationFormatterFactory.class);
|
GenericTypeResolver.resolveTypeArgument(annotationFormatterFactory.getClass(), AnnotationFormatterFactory.class);
|
||||||
if (annotationType == null) {
|
if (annotationType == null) {
|
||||||
|
|
@ -101,6 +101,7 @@ public class FormattingConversionService extends GenericConversionService
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static class PrinterConverter implements GenericConverter {
|
private static class PrinterConverter implements GenericConverter {
|
||||||
|
|
||||||
private Class<?> fieldType;
|
private Class<?> fieldType;
|
||||||
|
|
@ -143,6 +144,7 @@ public class FormattingConversionService extends GenericConversionService
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static class ParserConverter implements GenericConverter {
|
private static class ParserConverter implements GenericConverter {
|
||||||
|
|
||||||
private Class<?> fieldType;
|
private Class<?> fieldType;
|
||||||
|
|
@ -188,7 +190,8 @@ public class FormattingConversionService extends GenericConversionService
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private final class AnnotationPrinterConverter implements ConditionalGenericConverter {
|
|
||||||
|
private class AnnotationPrinterConverter implements ConditionalGenericConverter {
|
||||||
|
|
||||||
private Class<? extends Annotation> annotationType;
|
private Class<? extends Annotation> annotationType;
|
||||||
|
|
||||||
|
|
@ -227,7 +230,8 @@ public class FormattingConversionService extends GenericConversionService
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private final class AnnotationParserConverter implements ConditionalGenericConverter {
|
|
||||||
|
private class AnnotationParserConverter implements ConditionalGenericConverter {
|
||||||
|
|
||||||
private Class<? extends Annotation> annotationType;
|
private Class<? extends Annotation> annotationType;
|
||||||
|
|
||||||
|
|
@ -265,8 +269,9 @@ public class FormattingConversionService extends GenericConversionService
|
||||||
return String.class.getName() + " -> @" + annotationType.getName() + " " + fieldType.getName() + ": " + annotationFormatterFactory;
|
return String.class.getName() + " -> @" + annotationType.getName() + " " + fieldType.getName() + ": " + annotationFormatterFactory;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final class AnnotationConverterKey {
|
|
||||||
|
private static class AnnotationConverterKey {
|
||||||
|
|
||||||
private final Annotation annotation;
|
private final Annotation annotation;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -69,18 +69,19 @@ public class FormattingConversionServiceFactoryBean
|
||||||
|
|
||||||
private Set<FormatterRegistrar> formatterRegistrars;
|
private Set<FormatterRegistrar> formatterRegistrars;
|
||||||
|
|
||||||
private FormattingConversionService conversionService;
|
private boolean registerDefaultFormatters = true;
|
||||||
|
|
||||||
private StringValueResolver embeddedValueResolver;
|
private StringValueResolver embeddedValueResolver;
|
||||||
|
|
||||||
private boolean registerDefaultFormatters = true;
|
private FormattingConversionService conversionService;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configure the set of custom converter objects that should be added.
|
* Configure the set of custom converter objects that should be added.
|
||||||
* @param converters instances of any of the following:
|
* @param converters instances of any of the following:
|
||||||
* {@link org.springframework.core.convert.converter.Converter},
|
* {@link org.springframework.core.convert.converter.Converter},
|
||||||
* {@link org.springframework.core.convert.converter.ConverterFactory},
|
* {@link org.springframework.core.convert.converter.ConverterFactory},
|
||||||
* {@link org.springframework.core.convert.converter.GenericConverter}.
|
* {@link org.springframework.core.convert.converter.GenericConverter}
|
||||||
*/
|
*/
|
||||||
public void setConverters(Set<?> converters) {
|
public void setConverters(Set<?> converters) {
|
||||||
this.converters = converters;
|
this.converters = converters;
|
||||||
|
|
@ -88,8 +89,7 @@ public class FormattingConversionServiceFactoryBean
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configure the set of custom formatter objects that should be added.
|
* Configure the set of custom formatter objects that should be added.
|
||||||
* @param formatters instances of {@link Formatter} or
|
* @param formatters instances of {@link Formatter} or {@link AnnotationFormatterFactory}
|
||||||
* {@link AnnotationFormatterFactory}.
|
|
||||||
*/
|
*/
|
||||||
public void setFormatters(Set<?> formatters) {
|
public void setFormatters(Set<?> formatters) {
|
||||||
this.formatters = formatters;
|
this.formatters = formatters;
|
||||||
|
|
@ -113,14 +113,10 @@ public class FormattingConversionServiceFactoryBean
|
||||||
this.formatterRegistrars = formatterRegistrars;
|
this.formatterRegistrars = formatterRegistrars;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setEmbeddedValueResolver(StringValueResolver embeddedValueResolver) {
|
|
||||||
this.embeddedValueResolver = embeddedValueResolver;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Indicates whether default formatters should be registered or not. By
|
* Indicate whether default formatters should be registered or not.
|
||||||
* default built-in formatters are registered. This flag can be used to
|
* <p>By default, built-in formatters are registered. This flag can be used
|
||||||
* turn that off and rely on explicitly registered formatters only.
|
* to turn that off and rely on explicitly registered formatters only.
|
||||||
* @see #setFormatters(Set)
|
* @see #setFormatters(Set)
|
||||||
* @see #setFormatterRegistrars(Set)
|
* @see #setFormatterRegistrars(Set)
|
||||||
*/
|
*/
|
||||||
|
|
@ -128,8 +124,10 @@ public class FormattingConversionServiceFactoryBean
|
||||||
this.registerDefaultFormatters = registerDefaultFormatters;
|
this.registerDefaultFormatters = registerDefaultFormatters;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setEmbeddedValueResolver(StringValueResolver embeddedValueResolver) {
|
||||||
|
this.embeddedValueResolver = embeddedValueResolver;
|
||||||
|
}
|
||||||
|
|
||||||
// implementing InitializingBean
|
|
||||||
|
|
||||||
public void afterPropertiesSet() {
|
public void afterPropertiesSet() {
|
||||||
this.conversionService = new DefaultFormattingConversionService(this.embeddedValueResolver, this.registerDefaultFormatters);
|
this.conversionService = new DefaultFormattingConversionService(this.embeddedValueResolver, this.registerDefaultFormatters);
|
||||||
|
|
@ -137,46 +135,16 @@ public class FormattingConversionServiceFactoryBean
|
||||||
registerFormatters();
|
registerFormatters();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// implementing FactoryBean
|
|
||||||
|
|
||||||
public FormattingConversionService getObject() {
|
|
||||||
return this.conversionService;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Class<? extends FormattingConversionService> getObjectType() {
|
|
||||||
return FormattingConversionService.class;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isSingleton() {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// subclassing hooks
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Subclasses may override this method to register formatters and/or converters.
|
|
||||||
* Starting with Spring 3.1 however the recommended way of doing that is to
|
|
||||||
* through FormatterRegistrars.
|
|
||||||
* @see #setFormatters(Set)
|
|
||||||
* @see #setFormatterRegistrars(Set)
|
|
||||||
* @deprecated since Spring 3.1 in favor of {@link #setFormatterRegistrars(Set)}
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
protected void installFormatters(FormatterRegistry registry) {
|
|
||||||
}
|
|
||||||
|
|
||||||
// private helper methods
|
|
||||||
|
|
||||||
private void registerFormatters() {
|
private void registerFormatters() {
|
||||||
if (this.formatters != null) {
|
if (this.formatters != null) {
|
||||||
for (Object formatter : this.formatters) {
|
for (Object formatter : this.formatters) {
|
||||||
if (formatter instanceof Formatter<?>) {
|
if (formatter instanceof Formatter<?>) {
|
||||||
this.conversionService.addFormatter((Formatter<?>) formatter);
|
this.conversionService.addFormatter((Formatter<?>) formatter);
|
||||||
} else if (formatter instanceof AnnotationFormatterFactory<?>) {
|
}
|
||||||
|
else if (formatter instanceof AnnotationFormatterFactory<?>) {
|
||||||
this.conversionService.addFormatterForFieldAnnotation((AnnotationFormatterFactory<?>) formatter);
|
this.conversionService.addFormatterForFieldAnnotation((AnnotationFormatterFactory<?>) formatter);
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"Custom formatters must be implementations of Formatter or AnnotationFormatterFactory");
|
"Custom formatters must be implementations of Formatter or AnnotationFormatterFactory");
|
||||||
}
|
}
|
||||||
|
|
@ -190,4 +158,29 @@ public class FormattingConversionServiceFactoryBean
|
||||||
installFormatters(this.conversionService);
|
installFormatters(this.conversionService);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Subclasses may override this method to register formatters and/or converters.
|
||||||
|
* Starting with Spring 3.1 however the recommended way of doing that is to
|
||||||
|
* through FormatterRegistrars.
|
||||||
|
* @see #setFormatters(Set)
|
||||||
|
* @see #setFormatterRegistrars(Set)
|
||||||
|
* @deprecated since Spring 3.1 in favor of {@link #setFormatterRegistrars(Set)}
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
protected void installFormatters(FormatterRegistry registry) {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public FormattingConversionService getObject() {
|
||||||
|
return this.conversionService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Class<? extends FormattingConversionService> getObjectType() {
|
||||||
|
return FormattingConversionService.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isSingleton() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,10 +15,6 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.format.support;
|
package org.springframework.format.support;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
import java.lang.annotation.ElementType;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
|
@ -29,6 +25,7 @@ import java.util.Locale;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.core.convert.ConversionFailedException;
|
import org.springframework.core.convert.ConversionFailedException;
|
||||||
import org.springframework.core.convert.TypeDescriptor;
|
import org.springframework.core.convert.TypeDescriptor;
|
||||||
import org.springframework.format.AnnotationFormatterFactory;
|
import org.springframework.format.AnnotationFormatterFactory;
|
||||||
|
|
@ -40,8 +37,9 @@ import org.springframework.format.Printer;
|
||||||
import org.springframework.format.annotation.NumberFormat;
|
import org.springframework.format.annotation.NumberFormat;
|
||||||
import org.springframework.format.annotation.NumberFormat.Style;
|
import org.springframework.format.annotation.NumberFormat.Style;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test fixture for FormattingConversionServiceFactoryBean.
|
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
*/
|
*/
|
||||||
public class FormattingConversionServiceFactoryBeanTests {
|
public class FormattingConversionServiceFactoryBeanTests {
|
||||||
|
|
@ -68,8 +66,9 @@ public class FormattingConversionServiceFactoryBeanTests {
|
||||||
try {
|
try {
|
||||||
fcs.convert("5%", TypeDescriptor.valueOf(String.class), descriptor);
|
fcs.convert("5%", TypeDescriptor.valueOf(String.class), descriptor);
|
||||||
fail("This format should not be parseable");
|
fail("This format should not be parseable");
|
||||||
} catch (ConversionFailedException e) {
|
}
|
||||||
assertTrue(e.getCause() instanceof NumberFormatException);
|
catch (ConversionFailedException ex) {
|
||||||
|
assertTrue(ex.getCause() instanceof NumberFormatException);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -117,7 +116,8 @@ public class FormattingConversionServiceFactoryBeanTests {
|
||||||
try {
|
try {
|
||||||
factory.afterPropertiesSet();
|
factory.afterPropertiesSet();
|
||||||
fail("Expected formatter to be rejected");
|
fail("Expected formatter to be rejected");
|
||||||
} catch (IllegalArgumentException e) {
|
}
|
||||||
|
catch (IllegalArgumentException ex) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2009 the original author or authors.
|
* Copyright 2002-2011 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -161,6 +161,7 @@ public class DefaultTransactionStatus extends AbstractTransactionStatus {
|
||||||
* Delegate the flushing to the transaction object,
|
* Delegate the flushing to the transaction object,
|
||||||
* provided that the latter implements the {@link SmartTransactionObject} interface.
|
* provided that the latter implements the {@link SmartTransactionObject} interface.
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public void flush() {
|
public void flush() {
|
||||||
if (this.transaction instanceof SmartTransactionObject) {
|
if (this.transaction instanceof SmartTransactionObject) {
|
||||||
((SmartTransactionObject) this.transaction).flush();
|
((SmartTransactionObject) this.transaction).flush();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue