Skip to content

Commit 2665d56

Browse files
committed
Polishing
1 parent a4968b9 commit 2665d56

File tree

6 files changed

+85
-65
lines changed

6 files changed

+85
-65
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -277,7 +277,7 @@ protected void removeSingleton(String beanName) {
277277

278278
@Override
279279
public boolean containsSingleton(String beanName) {
280-
return (this.singletonObjects.containsKey(beanName));
280+
return this.singletonObjects.containsKey(beanName);
281281
}
282282

283283
@Override
@@ -330,8 +330,7 @@ public boolean isSingletonCurrentlyInCreation(String beanName) {
330330
* @see #isSingletonCurrentlyInCreation
331331
*/
332332
protected void beforeSingletonCreation(String beanName) {
333-
if (!this.inCreationCheckExclusions.contains(beanName) &&
334-
!this.singletonsCurrentlyInCreation.add(beanName)) {
333+
if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.add(beanName)) {
335334
throw new BeanCurrentlyInCreationException(beanName);
336335
}
337336
}
@@ -343,8 +342,7 @@ protected void beforeSingletonCreation(String beanName) {
343342
* @see #isSingletonCurrentlyInCreation
344343
*/
345344
protected void afterSingletonCreation(String beanName) {
346-
if (!this.inCreationCheckExclusions.contains(beanName) &&
347-
!this.singletonsCurrentlyInCreation.remove(beanName)) {
345+
if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.remove(beanName)) {
348346
throw new IllegalStateException("Singleton '" + beanName + "' isn't currently in creation");
349347
}
350348
}

spring-context/src/main/java/org/springframework/format/AnnotationFormatterFactory.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.format;
1718

1819
import java.lang.annotation.Annotation;
1920
import java.util.Set;
2021

2122
/**
22-
* A factory that creates formatters to format values of fields annotated with a particular {@link Annotation}.
23+
* A factory that creates formatters to format values of fields annotated with a particular
24+
* {@link Annotation}.
2325
*
2426
* <p>For example, a {@code DateTimeFormatAnnotationFormatterFactory} might create a formatter
2527
* that formats {@code Date} values set on fields annotated with {@code @DateTimeFormat}.
@@ -36,17 +38,21 @@ public interface AnnotationFormatterFactory<A extends Annotation> {
3638
Set<Class<?>> getFieldTypes();
3739

3840
/**
39-
* Get the Printer to print the value of a field of {@code fieldType} annotated with {@code annotation}.
40-
* If the type &lt;T&gt; the printer accepts is not assignable to {@code fieldType}, a coercion from {@code fieldType} to &lt;T&gt; will be attempted before the Printer is invoked.
41+
* Get the Printer to print the value of a field of {@code fieldType} annotated with
42+
* {@code annotation}.
43+
* <p>If the type T the printer accepts is not assignable to {@code fieldType}, a
44+
* coercion from {@code fieldType} to T will be attempted before the Printer is invoked.
4145
* @param annotation the annotation instance
4246
* @param fieldType the type of field that was annotated
4347
* @return the printer
4448
*/
4549
Printer<?> getPrinter(A annotation, Class<?> fieldType);
4650

4751
/**
48-
* Get the Parser to parse a submitted value for a field of {@code fieldType} annotated with {@code annotation}.
49-
* 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.
52+
* Get the Parser to parse a submitted value for a field of {@code fieldType}
53+
* annotated with {@code annotation}.
54+
* <p>If the object the parser returns is not assignable to {@code fieldType},
55+
* a coercion to {@code fieldType} will be attempted before the field is set.
5056
* @param annotation the annotation instance
5157
* @param fieldType the type of field that was annotated
5258
* @return the parser

spring-context/src/main/java/org/springframework/format/support/FormattingConversionService.java

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -90,7 +90,7 @@ public void addFormatterForFieldType(Class<?> fieldType, Printer<?> printer, Par
9090
@Override
9191
@SuppressWarnings({ "unchecked", "rawtypes" })
9292
public void addFormatterForFieldAnnotation(AnnotationFormatterFactory annotationFormatterFactory) {
93-
final Class<? extends Annotation> annotationType = (Class<? extends Annotation>)
93+
Class<? extends Annotation> annotationType = (Class<? extends Annotation>)
9494
GenericTypeResolver.resolveTypeArgument(annotationFormatterFactory.getClass(), AnnotationFormatterFactory.class);
9595
if (annotationType == null) {
9696
throw new IllegalArgumentException("Unable to extract parameterized Annotation type argument from AnnotationFormatterFactory [" +
@@ -100,7 +100,7 @@ public void addFormatterForFieldAnnotation(AnnotationFormatterFactory annotation
100100
((EmbeddedValueResolverAware) annotationFormatterFactory).setEmbeddedValueResolver(this.embeddedValueResolver);
101101
}
102102
Set<Class<?>> fieldTypes = annotationFormatterFactory.getFieldTypes();
103-
for (final Class<?> fieldType : fieldTypes) {
103+
for (Class<?> fieldType : fieldTypes) {
104104
addConverter(new AnnotationPrinterConverter(annotationType, annotationFormatterFactory, fieldType));
105105
addConverter(new AnnotationParserConverter(annotationType, annotationFormatterFactory, fieldType));
106106
}
@@ -109,14 +109,14 @@ public void addFormatterForFieldAnnotation(AnnotationFormatterFactory annotation
109109

110110
private static class PrinterConverter implements GenericConverter {
111111

112-
private Class<?> fieldType;
112+
private final Class<?> fieldType;
113113

114-
private TypeDescriptor printerObjectType;
114+
private final TypeDescriptor printerObjectType;
115115

116116
@SuppressWarnings("rawtypes")
117-
private Printer printer;
117+
private final Printer printer;
118118

119-
private ConversionService conversionService;
119+
private final ConversionService conversionService;
120120

121121
public PrinterConverter(Class<?> fieldType, Printer<?> printer, ConversionService conversionService) {
122122
this.fieldType = fieldType;
@@ -155,11 +155,11 @@ public String toString() {
155155

156156
private static class ParserConverter implements GenericConverter {
157157

158-
private Class<?> fieldType;
158+
private final Class<?> fieldType;
159159

160-
private Parser<?> parser;
160+
private final Parser<?> parser;
161161

162-
private ConversionService conversionService;
162+
private final ConversionService conversionService;
163163

164164
public ParserConverter(Class<?> fieldType, Parser<?> parser, ConversionService conversionService) {
165165
this.fieldType = fieldType;
@@ -204,12 +204,12 @@ public String toString() {
204204

205205
private class AnnotationPrinterConverter implements ConditionalGenericConverter {
206206

207-
private Class<? extends Annotation> annotationType;
207+
private final Class<? extends Annotation> annotationType;
208208

209209
@SuppressWarnings("rawtypes")
210-
private AnnotationFormatterFactory annotationFormatterFactory;
210+
private final AnnotationFormatterFactory annotationFormatterFactory;
211211

212-
private Class<?> fieldType;
212+
private final Class<?> fieldType;
213213

214214
public AnnotationPrinterConverter(Class<? extends Annotation> annotationType,
215215
AnnotationFormatterFactory<?> annotationFormatterFactory, Class<?> fieldType) {
@@ -220,45 +220,49 @@ public AnnotationPrinterConverter(Class<? extends Annotation> annotationType,
220220

221221
@Override
222222
public Set<ConvertiblePair> getConvertibleTypes() {
223-
return Collections.singleton(new ConvertiblePair(fieldType, String.class));
223+
return Collections.singleton(new ConvertiblePair(this.fieldType, String.class));
224224
}
225225

226226
@Override
227227
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
228-
return sourceType.hasAnnotation(annotationType);
228+
return sourceType.hasAnnotation(this.annotationType);
229229
}
230230

231231
@Override
232232
@SuppressWarnings("unchecked")
233233
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
234-
AnnotationConverterKey converterKey =
235-
new AnnotationConverterKey(sourceType.getAnnotation(annotationType), sourceType.getObjectType());
234+
Annotation ann = sourceType.getAnnotation(this.annotationType);
235+
if (ann == null) {
236+
throw new IllegalStateException(
237+
"Expected [" + this.annotationType.getName() + "] to be present on " + sourceType);
238+
}
239+
AnnotationConverterKey converterKey = new AnnotationConverterKey(ann, sourceType.getObjectType());
236240
GenericConverter converter = cachedPrinters.get(converterKey);
237241
if (converter == null) {
238-
Printer<?> printer = annotationFormatterFactory.getPrinter(
242+
Printer<?> printer = this.annotationFormatterFactory.getPrinter(
239243
converterKey.getAnnotation(), converterKey.getFieldType());
240-
converter = new PrinterConverter(fieldType, printer, FormattingConversionService.this);
244+
converter = new PrinterConverter(this.fieldType, printer, FormattingConversionService.this);
241245
cachedPrinters.put(converterKey, converter);
242246
}
243247
return converter.convert(source, sourceType, targetType);
244248
}
245249

246250
@Override
247251
public String toString() {
248-
return "@" + annotationType.getName() + " " + fieldType.getName() + " -> " +
249-
String.class.getName() + ": " + annotationFormatterFactory;
252+
return "@" + this.annotationType.getName() + " " + this.fieldType.getName() + " -> " +
253+
String.class.getName() + ": " + this.annotationFormatterFactory;
250254
}
251255
}
252256

253257

254258
private class AnnotationParserConverter implements ConditionalGenericConverter {
255259

256-
private Class<? extends Annotation> annotationType;
260+
private final Class<? extends Annotation> annotationType;
257261

258262
@SuppressWarnings("rawtypes")
259-
private AnnotationFormatterFactory annotationFormatterFactory;
263+
private final AnnotationFormatterFactory annotationFormatterFactory;
260264

261-
private Class<?> fieldType;
265+
private final Class<?> fieldType;
262266

263267
public AnnotationParserConverter(Class<? extends Annotation> annotationType,
264268
AnnotationFormatterFactory<?> annotationFormatterFactory, Class<?> fieldType) {
@@ -274,28 +278,32 @@ public Set<ConvertiblePair> getConvertibleTypes() {
274278

275279
@Override
276280
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
277-
return targetType.hasAnnotation(annotationType);
281+
return targetType.hasAnnotation(this.annotationType);
278282
}
279283

280284
@Override
281285
@SuppressWarnings("unchecked")
282286
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
283-
AnnotationConverterKey converterKey =
284-
new AnnotationConverterKey(targetType.getAnnotation(annotationType), targetType.getObjectType());
287+
Annotation ann = targetType.getAnnotation(this.annotationType);
288+
if (ann == null) {
289+
throw new IllegalStateException(
290+
"Expected [" + this.annotationType.getName() + "] to be present on " + targetType);
291+
}
292+
AnnotationConverterKey converterKey = new AnnotationConverterKey(ann, targetType.getObjectType());
285293
GenericConverter converter = cachedParsers.get(converterKey);
286294
if (converter == null) {
287-
Parser<?> parser = annotationFormatterFactory.getParser(
295+
Parser<?> parser = this.annotationFormatterFactory.getParser(
288296
converterKey.getAnnotation(), converterKey.getFieldType());
289-
converter = new ParserConverter(fieldType, parser, FormattingConversionService.this);
297+
converter = new ParserConverter(this.fieldType, parser, FormattingConversionService.this);
290298
cachedParsers.put(converterKey, converter);
291299
}
292300
return converter.convert(source, sourceType, targetType);
293301
}
294302

295303
@Override
296304
public String toString() {
297-
return String.class.getName() + " -> @" + annotationType.getName() + " " +
298-
fieldType.getName() + ": " + annotationFormatterFactory;
305+
return String.class.getName() + " -> @" + this.annotationType.getName() + " " +
306+
this.fieldType.getName() + ": " + this.annotationFormatterFactory;
299307
}
300308
}
301309

@@ -312,25 +320,28 @@ public AnnotationConverterKey(Annotation annotation, Class<?> fieldType) {
312320
}
313321

314322
public Annotation getAnnotation() {
315-
return annotation;
323+
return this.annotation;
316324
}
317325

318326
public Class<?> getFieldType() {
319-
return fieldType;
327+
return this.fieldType;
320328
}
321329

322330
@Override
323-
public boolean equals(Object o) {
324-
if (!(o instanceof AnnotationConverterKey)) {
331+
public boolean equals(Object other) {
332+
if (this == other) {
333+
return true;
334+
}
335+
if (!(other instanceof AnnotationConverterKey)) {
325336
return false;
326337
}
327-
AnnotationConverterKey key = (AnnotationConverterKey) o;
328-
return this.annotation.equals(key.annotation) && this.fieldType.equals(key.fieldType);
338+
AnnotationConverterKey otherKey = (AnnotationConverterKey) other;
339+
return (this.annotation.equals(otherKey.annotation) && this.fieldType.equals(otherKey.fieldType));
329340
}
330341

331342
@Override
332343
public int hashCode() {
333-
return this.annotation.hashCode() + 29 * this.fieldType.hashCode();
344+
return (this.annotation.hashCode() + 29 * this.fieldType.hashCode());
334345
}
335346
}
336347

spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,21 @@ private Annotation[] nullSafeAnnotations(Annotation[] annotations) {
129129
}
130130

131131
/**
132-
* Variation of {@link #getType()} that accounts for a primitive type by returning its object wrapper type.
133-
* <p>This is useful for conversion service implementations that wish to normalize to object-based types
134-
* and not work with primitive types directly.
132+
* Variation of {@link #getType()} that accounts for a primitive type by
133+
* returning its object wrapper type.
134+
* <p>This is useful for conversion service implementations that wish to
135+
* normalize to object-based types and not work with primitive types directly.
135136
*/
136137
public Class<?> getObjectType() {
137138
return ClassUtils.resolvePrimitiveIfNecessary(getType());
138139
}
139140

140141
/**
141-
* The type of the backing class, method parameter, field, or property described by this TypeDescriptor.
142+
* The type of the backing class, method parameter, field, or property
143+
* described by this TypeDescriptor.
142144
* <p>Returns primitive types as-is.
143-
* <p>See {@link #getObjectType()} for a variation of this operation that resolves primitive types
144-
* to their corresponding Object types if necessary.
145+
* <p>See {@link #getObjectType()} for a variation of this operation that
146+
* resolves primitive types to their corresponding Object types if necessary.
145147
* @return the type, or {@code null}
146148
* @see #getObjectType()
147149
*/
@@ -234,7 +236,7 @@ public Annotation[] getAnnotations() {
234236
* @return <tt>true</tt> if the annotation is present
235237
*/
236238
public boolean hasAnnotation(Class<? extends Annotation> annotationType) {
237-
return getAnnotation(annotationType) != null;
239+
return (getAnnotation(annotationType) != null);
238240
}
239241

240242
/**

spring-core/src/main/java/org/springframework/core/convert/support/ArrayToArrayConverter.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,11 +40,13 @@ final class ArrayToArrayConverter implements ConditionalGenericConverter {
4040

4141
private final ConversionService conversionService;
4242

43+
4344
public ArrayToArrayConverter(ConversionService conversionService) {
4445
this.helperConverter = new CollectionToArrayConverter(conversionService);
4546
this.conversionService = conversionService;
4647
}
4748

49+
4850
@Override
4951
public Set<ConvertiblePair> getConvertibleTypes() {
5052
return Collections.singleton(new ConvertiblePair(Object[].class, Object[].class));
@@ -56,12 +58,10 @@ public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
5658
}
5759

5860
@Override
59-
public Object convert(Object source, TypeDescriptor sourceType,
60-
TypeDescriptor targetType) {
61-
if ((conversionService instanceof GenericConversionService)
62-
&& ((GenericConversionService) conversionService).canBypassConvert(
63-
sourceType.getElementTypeDescriptor(),
64-
targetType.getElementTypeDescriptor())) {
61+
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
62+
if (this.conversionService instanceof GenericConversionService &&
63+
((GenericConversionService) this.conversionService).canBypassConvert(
64+
sourceType.getElementTypeDescriptor(), targetType.getElementTypeDescriptor())) {
6565
return source;
6666
}
6767
List<Object> sourceList = Arrays.asList(ObjectUtils.toObjectArray(source));

spring-core/src/main/java/org/springframework/core/convert/support/ArrayToStringConverter.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,7 +27,8 @@
2727

2828
/**
2929
* Converts an Array to a comma-delimited String.
30-
* This implementation first adapts the source Array to a List, then delegates to {@link CollectionToStringConverter} to perform the target String conversion.
30+
* This implementation first adapts the source Array to a List,
31+
* then delegates to {@link CollectionToStringConverter} to perform the target String conversion.
3132
*
3233
* @author Keith Donald
3334
* @since 3.0
@@ -36,10 +37,12 @@ final class ArrayToStringConverter implements ConditionalGenericConverter {
3637

3738
private final CollectionToStringConverter helperConverter;
3839

40+
3941
public ArrayToStringConverter(ConversionService conversionService) {
4042
this.helperConverter = new CollectionToStringConverter(conversionService);
4143
}
4244

45+
4346
@Override
4447
public Set<ConvertiblePair> getConvertibleTypes() {
4548
return Collections.singleton(new ConvertiblePair(Object[].class, String.class));

0 commit comments

Comments
 (0)