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.
|
||||
* @param formatter the formatter to add
|
||||
* @see #addFormatterForFieldType(Class, Formatter)
|
||||
* @since 3.1
|
||||
*/
|
||||
void addFormatter(Formatter<?> formatter);
|
||||
|
||||
|
|
|
|||
|
|
@ -65,10 +65,10 @@ public class FormattingConversionService extends GenericConversionService
|
|||
|
||||
|
||||
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) {
|
||||
throw new IllegalArgumentException("Unable to extract parameterized field type argument from Formatter ["
|
||||
+ formatter.getClass().getName() + "]; does the formatter parameterize the <T> generic type?");
|
||||
throw new IllegalArgumentException("Unable to extract parameterized field type argument from Formatter [" +
|
||||
formatter.getClass().getName() + "]; does the formatter parameterize the <T> generic type?");
|
||||
}
|
||||
addFormatterForFieldType(fieldType, formatter);
|
||||
}
|
||||
|
|
@ -84,7 +84,7 @@ public class FormattingConversionService extends GenericConversionService
|
|||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public void addFormatterForFieldAnnotation(final AnnotationFormatterFactory annotationFormatterFactory) {
|
||||
public void addFormatterForFieldAnnotation(AnnotationFormatterFactory annotationFormatterFactory) {
|
||||
final Class<? extends Annotation> annotationType = (Class<? extends Annotation>)
|
||||
GenericTypeResolver.resolveTypeArgument(annotationFormatterFactory.getClass(), AnnotationFormatterFactory.class);
|
||||
if (annotationType == null) {
|
||||
|
|
@ -101,6 +101,7 @@ public class FormattingConversionService extends GenericConversionService
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
private static class PrinterConverter implements GenericConverter {
|
||||
|
||||
private Class<?> fieldType;
|
||||
|
|
@ -143,6 +144,7 @@ public class FormattingConversionService extends GenericConversionService
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
private static class ParserConverter implements GenericConverter {
|
||||
|
||||
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;
|
||||
|
||||
|
|
@ -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;
|
||||
|
||||
|
|
@ -265,8 +269,9 @@ public class FormattingConversionService extends GenericConversionService
|
|||
return String.class.getName() + " -> @" + annotationType.getName() + " " + fieldType.getName() + ": " + annotationFormatterFactory;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class AnnotationConverterKey {
|
||||
|
||||
|
||||
private static class AnnotationConverterKey {
|
||||
|
||||
private final Annotation annotation;
|
||||
|
||||
|
|
|
|||
|
|
@ -69,18 +69,19 @@ public class FormattingConversionServiceFactoryBean
|
|||
|
||||
private Set<FormatterRegistrar> formatterRegistrars;
|
||||
|
||||
private FormattingConversionService conversionService;
|
||||
private boolean registerDefaultFormatters = true;
|
||||
|
||||
private StringValueResolver embeddedValueResolver;
|
||||
|
||||
private boolean registerDefaultFormatters = true;
|
||||
private FormattingConversionService conversionService;
|
||||
|
||||
|
||||
/**
|
||||
* Configure the set of custom converter objects that should be added.
|
||||
* @param converters instances of any of the following:
|
||||
* {@link org.springframework.core.convert.converter.Converter},
|
||||
* {@link org.springframework.core.convert.converter.ConverterFactory},
|
||||
* {@link org.springframework.core.convert.converter.GenericConverter}.
|
||||
* {@link org.springframework.core.convert.converter.Converter},
|
||||
* {@link org.springframework.core.convert.converter.ConverterFactory},
|
||||
* {@link org.springframework.core.convert.converter.GenericConverter}
|
||||
*/
|
||||
public void setConverters(Set<?> converters) {
|
||||
this.converters = converters;
|
||||
|
|
@ -88,8 +89,7 @@ public class FormattingConversionServiceFactoryBean
|
|||
|
||||
/**
|
||||
* Configure the set of custom formatter objects that should be added.
|
||||
* @param formatters instances of {@link Formatter} or
|
||||
* {@link AnnotationFormatterFactory}.
|
||||
* @param formatters instances of {@link Formatter} or {@link AnnotationFormatterFactory}
|
||||
*/
|
||||
public void setFormatters(Set<?> formatters) {
|
||||
this.formatters = formatters;
|
||||
|
|
@ -113,14 +113,10 @@ public class FormattingConversionServiceFactoryBean
|
|||
this.formatterRegistrars = formatterRegistrars;
|
||||
}
|
||||
|
||||
public void setEmbeddedValueResolver(StringValueResolver embeddedValueResolver) {
|
||||
this.embeddedValueResolver = embeddedValueResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether default formatters should be registered or not. By
|
||||
* default built-in formatters are registered. This flag can be used to
|
||||
* turn that off and rely on explicitly registered formatters only.
|
||||
* Indicate whether default formatters should be registered or not.
|
||||
* <p>By default, built-in formatters are registered. This flag can be used
|
||||
* to turn that off and rely on explicitly registered formatters only.
|
||||
* @see #setFormatters(Set)
|
||||
* @see #setFormatterRegistrars(Set)
|
||||
*/
|
||||
|
|
@ -128,8 +124,10 @@ public class FormattingConversionServiceFactoryBean
|
|||
this.registerDefaultFormatters = registerDefaultFormatters;
|
||||
}
|
||||
|
||||
public void setEmbeddedValueResolver(StringValueResolver embeddedValueResolver) {
|
||||
this.embeddedValueResolver = embeddedValueResolver;
|
||||
}
|
||||
|
||||
// implementing InitializingBean
|
||||
|
||||
public void afterPropertiesSet() {
|
||||
this.conversionService = new DefaultFormattingConversionService(this.embeddedValueResolver, this.registerDefaultFormatters);
|
||||
|
|
@ -137,46 +135,16 @@ public class FormattingConversionServiceFactoryBean
|
|||
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() {
|
||||
if (this.formatters != null) {
|
||||
for (Object formatter : this.formatters) {
|
||||
if (formatter instanceof Formatter<?>) {
|
||||
this.conversionService.addFormatter((Formatter<?>) formatter);
|
||||
} else if (formatter instanceof AnnotationFormatterFactory<?>) {
|
||||
}
|
||||
else if (formatter instanceof AnnotationFormatterFactory<?>) {
|
||||
this.conversionService.addFormatterForFieldAnnotation((AnnotationFormatterFactory<?>) formatter);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException(
|
||||
"Custom formatters must be implementations of Formatter or AnnotationFormatterFactory");
|
||||
}
|
||||
|
|
@ -190,4 +158,29 @@ public class FormattingConversionServiceFactoryBean
|
|||
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;
|
||||
|
||||
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.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
|
@ -29,6 +25,7 @@ import java.util.Locale;
|
|||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.convert.ConversionFailedException;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
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.Style;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Test fixture for FormattingConversionServiceFactoryBean.
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class FormattingConversionServiceFactoryBeanTests {
|
||||
|
|
@ -68,8 +66,9 @@ public class FormattingConversionServiceFactoryBeanTests {
|
|||
try {
|
||||
fcs.convert("5%", TypeDescriptor.valueOf(String.class), descriptor);
|
||||
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 {
|
||||
factory.afterPropertiesSet();
|
||||
fail("Expected formatter to be rejected");
|
||||
} catch (IllegalArgumentException e) {
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// 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");
|
||||
* 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,
|
||||
* provided that the latter implements the {@link SmartTransactionObject} interface.
|
||||
*/
|
||||
@Override
|
||||
public void flush() {
|
||||
if (this.transaction instanceof SmartTransactionObject) {
|
||||
((SmartTransactionObject) this.transaction).flush();
|
||||
|
|
|
|||
Loading…
Reference in New Issue