Polishing

This commit is contained in:
Juergen Hoeller
2014-07-29 10:10:48 +02:00
parent daaeeaa8e2
commit c0a4631fd1
56 changed files with 845 additions and 794 deletions

View File

@@ -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.
@@ -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 &lt;T&gt; the printer accepts is not assignable to {@code fieldType}, a coersion 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> {
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 coersion 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

View File

@@ -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.
@@ -85,7 +85,7 @@ public class FormattingConversionService extends GenericConversionService
@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 [" +
@@ -95,7 +95,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));
}
@@ -104,14 +104,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;
@@ -139,6 +139,7 @@ public class FormattingConversionService extends GenericConversionService
return GenericTypeResolver.resolveTypeArgument(printer.getClass(), Printer.class);
}
@Override
public String toString() {
return this.fieldType.getName() + " -> " + String.class.getName() + " : " + this.printer;
}
@@ -147,11 +148,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;
@@ -185,6 +186,7 @@ public class FormattingConversionService extends GenericConversionService
return result;
}
@Override
public String toString() {
return String.class.getName() + " -> " + this.fieldType.getName() + ": " + this.parser;
}
@@ -193,55 +195,56 @@ public class FormattingConversionService extends GenericConversionService
private class AnnotationPrinterConverter implements ConditionalGenericConverter {
private Class<? extends Annotation> annotationType;
private final Class<? extends Annotation> annotationType;
private AnnotationFormatterFactory annotationFormatterFactory;
private final AnnotationFormatterFactory annotationFormatterFactory;
private Class<?> fieldType;
private final Class<?> fieldType;
public AnnotationPrinterConverter(Class<? extends Annotation> annotationType,
AnnotationFormatterFactory annotationFormatterFactory, Class<?> fieldType) {
AnnotationFormatterFactory<?> annotationFormatterFactory, Class<?> fieldType) {
this.annotationType = annotationType;
this.annotationFormatterFactory = annotationFormatterFactory;
this.fieldType = fieldType;
}
public Set<ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(fieldType, String.class));
return Collections.singleton(new ConvertiblePair(this.fieldType, String.class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return sourceType.hasAnnotation(annotationType);
return sourceType.hasAnnotation(this.annotationType);
}
@SuppressWarnings("unchecked")
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
AnnotationConverterKey converterKey =
new AnnotationConverterKey(sourceType.getAnnotation(annotationType), sourceType.getObjectType());
new AnnotationConverterKey(sourceType.getAnnotation(this.annotationType), 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);
}
@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;
private AnnotationFormatterFactory annotationFormatterFactory;
private final AnnotationFormatterFactory annotationFormatterFactory;
private Class<?> fieldType;
private final Class<?> fieldType;
public AnnotationParserConverter(Class<? extends Annotation> annotationType,
AnnotationFormatterFactory<?> annotationFormatterFactory, Class<?> fieldType) {
@@ -255,26 +258,27 @@ public class FormattingConversionService extends GenericConversionService
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return targetType.hasAnnotation(annotationType);
return targetType.hasAnnotation(this.annotationType);
}
@SuppressWarnings("unchecked")
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
AnnotationConverterKey converterKey =
new AnnotationConverterKey(targetType.getAnnotation(annotationType), targetType.getObjectType());
new AnnotationConverterKey(targetType.getAnnotation(this.annotationType), 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);
}
@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;
}
}
@@ -291,23 +295,28 @@ public class FormattingConversionService extends GenericConversionService
}
public Annotation getAnnotation() {
return annotation;
return this.annotation;
}
public Class<?> getFieldType() {
return fieldType;
return this.fieldType;
}
public boolean equals(Object o) {
if (!(o instanceof AnnotationConverterKey)) {
@Override
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());
}
}

View File

@@ -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.
@@ -39,10 +39,11 @@ class WebLogicClassPreProcessorAdapter implements InvocationHandler {
private final ClassLoader loader;
/**
* Creates a new {@link WebLogicClassPreProcessorAdapter}.
* @param transformer the {@link ClassFileTransformer} to be adapted (must
* not be {@code null})
* @param transformer the {@link ClassFileTransformer} to be adapted
* (must not be {@code null})
*/
public WebLogicClassPreProcessorAdapter(ClassFileTransformer transformer, ClassLoader loader) {
this.transformer = transformer;
@@ -51,40 +52,43 @@ class WebLogicClassPreProcessorAdapter implements InvocationHandler {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String name = method.getName();
if ("equals".equals(name)) {
return (Boolean.valueOf(proxy == args[0]));
} else if ("hashCode".equals(name)) {
return (proxy == args[0]);
}
else if ("hashCode".equals(name)) {
return hashCode();
} else if ("toString".equals(name)) {
}
else if ("toString".equals(name)) {
return toString();
} else if ("initialize".equals(name)) {
initialize((Hashtable) args[0]);
}
else if ("initialize".equals(name)) {
initialize((Hashtable<?, ?>) args[0]);
return null;
} else if ("preProcess".equals(name)) {
}
else if ("preProcess".equals(name)) {
return preProcess((String) args[0], (byte[]) args[1]);
} else {
}
else {
throw new IllegalArgumentException("Unknown method: " + method);
}
}
public void initialize(Hashtable params) {
public void initialize(Hashtable<?, ?> params) {
}
public byte[] preProcess(String className, byte[] classBytes) {
try {
byte[] result = this.transformer.transform(this.loader, className, null, null, classBytes);
return (result != null ? result : classBytes);
} catch (IllegalClassFormatException ex) {
}
catch (IllegalClassFormatException ex) {
throw new IllegalStateException("Cannot transform due to illegal class format", ex);
}
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder(getClass().getName());
builder.append(" for transformer: ");
builder.append(this.transformer);
return builder.toString();
return getClass().getName() + " for transformer: " + this.transformer;
}
}

View File

@@ -141,7 +141,7 @@ public class AnnotationJmxAttributeSource implements JmxAttributeSource, BeanFac
public ManagedNotification[] getManagedNotifications(Class<?> clazz) throws InvalidMetadataException {
ManagedNotifications notificationsAnn = clazz.getAnnotation(ManagedNotifications.class);
if(notificationsAnn == null) {
if (notificationsAnn == null) {
return new ManagedNotification[0];
}
Annotation[] notifications = notificationsAnn.value();

View File

@@ -434,7 +434,7 @@ public abstract class AbstractReflectiveMBeanInfoAssembler extends AbstractMBean
* @see #getClassToExpose(Class)
* @see org.springframework.aop.framework.AopProxyUtils#proxiedUserInterfaces(Object)
*/
protected Class getClassForDescriptor(Object managedBean) {
protected Class<?> getClassForDescriptor(Object managedBean) {
if (AopUtils.isJdkDynamicProxy(managedBean)) {
return AopProxyUtils.proxiedUserInterfaces(managedBean)[0];
}
@@ -514,7 +514,7 @@ public abstract class AbstractReflectiveMBeanInfoAssembler extends AbstractMBean
MBeanParameterInfo[] info = new MBeanParameterInfo[paramNames.length];
Class<?>[] typeParameters = method.getParameterTypes();
for(int i = 0; i < info.length; i++) {
for (int i = 0; i < info.length; i++) {
info[i] = new MBeanParameterInfo(paramNames[i], typeParameters[i].getName(), paramNames[i]);
}

View File

@@ -151,7 +151,7 @@ public class MetadataMBeanInfoAssembler extends AbstractReflectiveMBeanInfoAssem
protected boolean includeOperation(Method method, String beanKey) {
PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
if (pd != null) {
if(hasManagedAttribute(method)) {
if (hasManagedAttribute(method)) {
return true;
}
}
@@ -261,7 +261,7 @@ public class MetadataMBeanInfoAssembler extends AbstractReflectiveMBeanInfoAssem
}
MBeanParameterInfo[] parameterInfo = new MBeanParameterInfo[params.length];
Class[] methodParameters = method.getParameterTypes();
Class<?>[] methodParameters = method.getParameterTypes();
for (int i = 0; i < params.length; i++) {
ManagedOperationParameter param = params[i];
parameterInfo[i] =
@@ -332,7 +332,7 @@ public class MetadataMBeanInfoAssembler extends AbstractReflectiveMBeanInfoAssem
*/
@Override
protected void populateAttributeDescriptor(Descriptor desc, Method getter, Method setter, String beanKey) {
if(getter != null && hasManagedMetric(getter)) {
if (getter != null && hasManagedMetric(getter)) {
populateMetricDescriptor(desc, this.attributeSource.getManagedMetric(getter));
}
else {
@@ -374,11 +374,11 @@ public class MetadataMBeanInfoAssembler extends AbstractReflectiveMBeanInfoAssem
desc.setField(FIELD_DISPLAY_NAME, metric.getDisplayName());
}
if(StringUtils.hasLength(metric.getUnit())) {
if (StringUtils.hasLength(metric.getUnit())) {
desc.setField(FIELD_UNITS, metric.getUnit());
}
if(StringUtils.hasLength(metric.getCategory())) {
if (StringUtils.hasLength(metric.getCategory())) {
desc.setField(FIELD_METRIC_CATEGORY, metric.getCategory());
}

View File

@@ -243,7 +243,7 @@ public class DefaultMessageCodesResolver implements MessageCodesResolver, Serial
public static String toDelimitedString(String... elements) {
StringBuilder rtn = new StringBuilder();
for (String element : elements) {
if(StringUtils.hasLength(element)) {
if (StringUtils.hasLength(element)) {
rtn.append(rtn.length() == 0 ? "" : CODE_SEPARATOR);
rtn.append(element);
}