Skip to content

Commit 222eec5

Browse files
committed
Review and polish pull request #132
Content: - Rename Conditional{Conversion=>Converter} - Add @SInCE tags where appropriate - Update Apache date headers to read 2002-2012 (not just 2012) - Correct minor Javadoc typo Style: - Polish line breaks / whitespace - Use wildcard static imports where appropriate Issue: SPR-9566, SPR-9692, SPR-9928, SPR-9927
1 parent f82c6ed commit 222eec5

File tree

12 files changed

+69
-72
lines changed

12 files changed

+69
-72
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public interface ConversionService {
6363
* @param targetType context about the target type to convert to (required)
6464
* @return true if conversion can be bypassed
6565
* @throws IllegalArgumentException if targetType is null
66+
* @since 3.2
6667
*/
6768
boolean canBypassConvert(Class<?> sourceType, Class<?> targetType);
6869

@@ -74,6 +75,7 @@ public interface ConversionService {
7475
* @param targetType context about the target type to convert to (required)
7576
* @return true if conversion can be bypassed
7677
* @throws IllegalArgumentException if targetType is null
78+
* @since 3.2
7779
*/
7880
boolean canBypassConvert(TypeDescriptor sourceType, TypeDescriptor targetType);
7981

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ public TypeDescriptor narrow(Object value) {
257257
* @param superType the super type to cast to (can be {@code null}
258258
* @return a new TypeDescriptor for the up-cast type
259259
* @throws IllegalArgumentException if this type is not assignable to the super-type
260+
* @since 3.2
260261
*/
261262
public TypeDescriptor upcast(Class<?> superType) {
262263
if (superType == null) {

spring-core/src/main/java/org/springframework/core/convert/converter/ConditionalConversion.java renamed to spring-core/src/main/java/org/springframework/core/convert/converter/ConditionalConverter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012 the original author or authors.
2+
* Copyright 2002-2012 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.
@@ -33,18 +33,18 @@
3333
* implementation might return {@code true} if the target Account class defines a
3434
* {@code public static findAccount(String)} method.
3535
*
36-
* @author Keith Donald
3736
* @author Phillip Webb
37+
* @author Keith Donald
3838
* @since 3.2
3939
* @see Converter
4040
* @see GenericConverter
4141
* @see ConverterFactory
4242
* @see ConditionalGenericConverter
4343
*/
44-
public interface ConditionalConversion {
44+
public interface ConditionalConverter {
4545

4646
/**
47-
* Should the converter from {@code sourceType} to {@code targetType} currently under
47+
* Should the conversion from {@code sourceType} to {@code targetType} currently under
4848
* consideration be selected?
4949
*
5050
* @param sourceType the type descriptor of the field we are converting from

spring-core/src/main/java/org/springframework/core/convert/converter/ConditionalGenericConverter.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,18 @@
1818

1919
import org.springframework.core.convert.TypeDescriptor;
2020

21-
2221
/**
2322
* A {@link GenericConverter} that may conditionally execute based on attributes of the
2423
* {@code source} and {@code target} {@link TypeDescriptor}. See
25-
* {@link ConditionalConversion} for details.
24+
* {@link ConditionalConverter} for details.
2625
*
2726
* @author Keith Donald
2827
* @author Phillip Webb
2928
* @since 3.0
3029
* @see GenericConverter
31-
* @see ConditionalConversion
30+
* @see ConditionalConverter
3231
*/
3332
public interface ConditionalGenericConverter extends GenericConverter,
34-
ConditionalConversion {
33+
ConditionalConverter {
3534

3635
}

spring-core/src/main/java/org/springframework/core/convert/converter/Converter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
* A converter converts a source object of type S to a target of type T.
2121
* Implementations of this interface are thread-safe and can be shared.
2222
*
23-
* <p>Implementations may additionally implement {@link ConditionalConversion}.
23+
* <p>Implementations may additionally implement {@link ConditionalConverter}.
2424
*
2525
* @author Keith Donald
2626
* @since 3.0
27-
* @see ConditionalConversion
27+
* @see ConditionalConverter
2828
* @param <S> The source type
2929
* @param <T> The target type
3030
*/

spring-core/src/main/java/org/springframework/core/convert/converter/ConverterFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
/**
2020
* A factory for "ranged" converters that can convert objects from S to subtypes of R.
2121
*
22-
* <p>Implementations may additionally implement {@link ConditionalConversion}.
22+
* <p>Implementations may additionally implement {@link ConditionalConverter}.
2323
*
2424
* @author Keith Donald
2525
* @since 3.0
26-
* @see ConditionalConversion
26+
* @see ConditionalConverter
2727
* @param <S> The source type converters created by this factory can convert from
2828
* @param <R> The target range (or base) type converters created by this factory can convert to;
2929
* for example {@link Number} for a set of number subtypes.

spring-core/src/main/java/org/springframework/core/convert/converter/GenericConverter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,23 @@
3434
* <p>This interface should generally not be used when the simpler {@link Converter} or
3535
* {@link ConverterFactory} interfaces are sufficient.
3636
*
37-
* <p>Implementations may additionally implement {@link ConditionalConversion}.
37+
* <p>Implementations may additionally implement {@link ConditionalConverter}.
3838
*
3939
* @author Keith Donald
4040
* @author Juergen Hoeller
4141
* @since 3.0
4242
* @see TypeDescriptor
4343
* @see Converter
4444
* @see ConverterFactory
45-
* @see ConditionalConversion
45+
* @see ConditionalConverter
4646
*/
4747
public interface GenericConverter {
4848

4949
/**
5050
* Return the source and target types which this converter can convert between. Each
5151
* entry is a convertible source-to-target type pair.
5252
* <p>
53-
* For {@link ConditionalConversion conditional} converters this method may return
53+
* For {@link ConditionalConverter conditional} converters this method may return
5454
* {@code null} to indicate all source-to-target pairs should be considered. *
5555
*/
5656
Set<ConvertiblePair> getConvertibleTypes();

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2012 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.
@@ -18,10 +18,9 @@
1818

1919
import org.springframework.core.convert.ConversionService;
2020
import org.springframework.core.convert.TypeDescriptor;
21-
import org.springframework.core.convert.converter.ConditionalConversion;
21+
import org.springframework.core.convert.converter.ConditionalConverter;
2222
import org.springframework.core.convert.converter.Converter;
2323
import org.springframework.util.ClassUtils;
24-
import org.springframework.util.ReflectionUtils;
2524

2625
/**
2726
* Calls {@link Enum#name()} to convert a source Enum to a String. This converter will
@@ -30,7 +29,7 @@
3029
* @author Phillip Webb
3130
* @since 3.0
3231
*/
33-
final class EnumToStringConverter implements Converter<Enum<?>, String>, ConditionalConversion {
32+
final class EnumToStringConverter implements Converter<Enum<?>, String>, ConditionalConverter {
3433

3534
private final ConversionService conversionService;
3635

@@ -40,8 +39,7 @@ public EnumToStringConverter(ConversionService conversionService) {
4039

4140
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
4241
for (Class<?> interfaceType : ClassUtils.getAllInterfacesForClass(sourceType.getType())) {
43-
if (conversionService.canConvert(TypeDescriptor.valueOf(interfaceType),
44-
targetType)) {
42+
if (conversionService.canConvert(TypeDescriptor.valueOf(interfaceType), targetType)) {
4543
return false;
4644
}
4745
}

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

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import org.springframework.core.convert.ConversionService;
3535
import org.springframework.core.convert.ConverterNotFoundException;
3636
import org.springframework.core.convert.TypeDescriptor;
37-
import org.springframework.core.convert.converter.ConditionalConversion;
37+
import org.springframework.core.convert.converter.ConditionalConverter;
3838
import org.springframework.core.convert.converter.ConditionalGenericConverter;
3939
import org.springframework.core.convert.converter.Converter;
4040
import org.springframework.core.convert.converter.ConverterFactory;
@@ -81,8 +81,8 @@ public class GenericConversionService implements ConfigurableConversionService {
8181

8282
public void addConverter(Converter<?, ?> converter) {
8383
GenericConverter.ConvertiblePair typeInfo = getRequiredTypeInfo(converter, Converter.class);
84-
Assert.notNull(typeInfo, "Unable to the determine sourceType <S> and targetType <T> which " +
85-
"your Converter<S, T> converts between; declare these generic types.");
84+
Assert.notNull(typeInfo, "Unable to the determine sourceType <S> and targetType " +
85+
"<T> which your Converter<S, T> converts between; declare these generic types.");
8686
addConverter(new ConverterAdapter(typeInfo, converter));
8787
}
8888

@@ -99,8 +99,9 @@ public void addConverter(GenericConverter converter) {
9999
public void addConverterFactory(ConverterFactory<?, ?> converterFactory) {
100100
GenericConverter.ConvertiblePair typeInfo = getRequiredTypeInfo(converterFactory, ConverterFactory.class);
101101
if (typeInfo == null) {
102-
throw new IllegalArgumentException("Unable to the determine sourceType <S> and targetRangeType R which " +
103-
"your ConverterFactory<S, R> converts between; declare these generic types.");
102+
throw new IllegalArgumentException("Unable to the determine sourceType <S> and " +
103+
"targetRangeType R which your ConverterFactory<S, R> converts between; " +
104+
"declare these generic types.");
104105
}
105106
addConverter(new ConverterFactoryAdapter(typeInfo, converterFactory));
106107
}
@@ -114,7 +115,9 @@ public void removeConvertible(Class<?> sourceType, Class<?> targetType) {
114115

115116
public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
116117
Assert.notNull(targetType, "The targetType to convert to cannot be null");
117-
return canConvert(sourceType != null ? TypeDescriptor.valueOf(sourceType) : null, TypeDescriptor.valueOf(targetType));
118+
return canConvert(sourceType != null ?
119+
TypeDescriptor.valueOf(sourceType) : null,
120+
TypeDescriptor.valueOf(targetType));
118121
}
119122

120123
public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
@@ -128,8 +131,9 @@ public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType)
128131

129132
public boolean canBypassConvert(Class<?> sourceType, Class<?> targetType) {
130133
Assert.notNull(targetType, "The targetType to convert to cannot be null");
131-
return canBypassConvert(sourceType != null ? TypeDescriptor.valueOf(sourceType)
132-
: null, TypeDescriptor.valueOf(targetType));
134+
return canBypassConvert(sourceType != null ?
135+
TypeDescriptor.valueOf(sourceType) : null,
136+
TypeDescriptor.valueOf(targetType));
133137
}
134138

135139
public boolean canBypassConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
@@ -166,8 +170,11 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
166170
}
167171

168172
/**
169-
* Convenience operation for converting a source object to the specified targetType, where the targetType is a descriptor that provides additional conversion context.
170-
* Simply delegates to {@link #convert(Object, TypeDescriptor, TypeDescriptor)} and encapsulates the construction of the sourceType descriptor using {@link TypeDescriptor#forObject(Object)}.
173+
* Convenience operation for converting a source object to the specified targetType,
174+
* where the targetType is a descriptor that provides additional conversion context.
175+
* Simply delegates to {@link #convert(Object, TypeDescriptor, TypeDescriptor)} and
176+
* encapsulates the construction of the sourceType descriptor using
177+
* {@link TypeDescriptor#forObject(Object)}.
171178
* @param source the source object
172179
* @param targetType the target type
173180
* @return the converted value
@@ -206,7 +213,8 @@ protected Object convertNullSource(TypeDescriptor sourceType, TypeDescriptor tar
206213
* Subclasses may override.
207214
* @param sourceType the source type to convert from
208215
* @param targetType the target type to convert to
209-
* @return the generic converter that will perform the conversion, or {@code null} if no suitable converter was found
216+
* @return the generic converter that will perform the conversion, or {@code null} if
217+
* no suitable converter was found
210218
* @see #getDefaultConverter(TypeDescriptor, TypeDescriptor)
211219
*/
212220
protected GenericConverter getConverter(TypeDescriptor sourceType, TypeDescriptor targetType) {
@@ -305,9 +313,8 @@ public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
305313
if(!this.typeInfo.getTargetType().equals(targetType.getObjectType())) {
306314
return false;
307315
}
308-
if (this.converter instanceof ConditionalConversion) {
309-
return ((ConditionalConversion) this.converter).matches(sourceType,
310-
targetType);
316+
if (this.converter instanceof ConditionalConverter) {
317+
return ((ConditionalConverter) this.converter).matches(sourceType, targetType);
311318
}
312319
return true;
313320
}
@@ -320,8 +327,9 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
320327
}
321328

322329
public String toString() {
323-
return this.typeInfo.getSourceType().getName() + " -> " + this.typeInfo.getTargetType().getName() +
324-
" : " + this.converter.toString();
330+
return this.typeInfo.getSourceType().getName() + " -> " +
331+
this.typeInfo.getTargetType().getName() + " : " +
332+
this.converter.toString();
325333
}
326334
}
327335

@@ -349,14 +357,13 @@ public Set<ConvertiblePair> getConvertibleTypes() {
349357

350358
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
351359
boolean matches = true;
352-
if (this.converterFactory instanceof ConditionalConversion) {
353-
matches = ((ConditionalConversion) this.converterFactory).matches(
354-
sourceType, targetType);
360+
if (this.converterFactory instanceof ConditionalConverter) {
361+
matches = ((ConditionalConverter) this.converterFactory).matches(sourceType, targetType);
355362
}
356363
if(matches) {
357-
Converter<?, ?> converter = converterFactory.getConverter(targetType.getType());
358-
if(converter instanceof ConditionalConversion) {
359-
matches = ((ConditionalConversion) converter).matches(sourceType, targetType);
364+
Converter<?, ?> converter = this.converterFactory.getConverter(targetType.getType());
365+
if(converter instanceof ConditionalConverter) {
366+
matches = ((ConditionalConverter) converter).matches(sourceType, targetType);
360367
}
361368
}
362369
return matches;
@@ -370,8 +377,9 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
370377
}
371378

372379
public String toString() {
373-
return this.typeInfo.getSourceType().getName() + " -> " + this.typeInfo.getTargetType().getName() +
374-
" : " + this.converterFactory.toString();
380+
return this.typeInfo.getSourceType().getName() + " -> " +
381+
this.typeInfo.getTargetType().getName() + " : " +
382+
this.converterFactory.toString();
375383
}
376384
}
377385

@@ -437,7 +445,7 @@ private static class Converters {
437445
public void add(GenericConverter converter) {
438446
Set<ConvertiblePair> convertibleTypes = converter.getConvertibleTypes();
439447
if (convertibleTypes == null) {
440-
Assert.state(converter instanceof ConditionalConversion,
448+
Assert.state(converter instanceof ConditionalConverter,
441449
"Only conditional converters may return null convertible types");
442450
globalConverters.add(converter);
443451
} else {
@@ -476,7 +484,8 @@ public GenericConverter find(TypeDescriptor sourceType, TypeDescriptor targetTyp
476484
List<TypeDescriptor> targetCandidates = getTypeHierarchy(targetType);
477485
for (TypeDescriptor sourceCandidate : sourceCandidates) {
478486
for (TypeDescriptor targetCandidate : targetCandidates) {
479-
GenericConverter converter = getRegisteredConverter(sourceType, targetType, sourceCandidate, targetCandidate);
487+
GenericConverter converter = getRegisteredConverter(
488+
sourceType, targetType, sourceCandidate, targetCandidate);
480489
if(converter != null) {
481490
return converter;
482491
}
@@ -499,9 +508,8 @@ private GenericConverter getRegisteredConverter(TypeDescriptor sourceType, TypeD
499508

500509
// Check ConditionalGenericConverter that match all types
501510
for (GenericConverter globalConverter : this.globalConverters) {
502-
if (((ConditionalConversion)globalConverter).matches(
503-
sourceCandidate,
504-
targetCandidate)) {
511+
if (((ConditionalConverter)globalConverter).matches(
512+
sourceCandidate, targetCandidate)) {
505513
return globalConverter;
506514
}
507515
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.springframework.core.convert.support;
1818

1919
import org.springframework.core.convert.TypeDescriptor;
20-
import org.springframework.core.convert.converter.ConditionalConversion;
20+
import org.springframework.core.convert.converter.ConditionalConverter;
2121
import org.springframework.core.convert.converter.Converter;
2222
import org.springframework.core.convert.converter.ConverterFactory;
2323
import org.springframework.util.NumberUtils;
@@ -41,7 +41,7 @@
4141
* @see NumberUtils
4242
*/
4343
final class NumberToNumberConverterFactory implements ConverterFactory<Number, Number>,
44-
ConditionalConversion {
44+
ConditionalConverter {
4545

4646
public <T extends Number> Converter<Number, T> getConverter(Class<T> targetType) {
4747
return new NumberToNumber<T>(targetType);

spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@
1616

1717
package org.springframework.core.convert;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertFalse;
21-
import static org.junit.Assert.assertNotNull;
22-
import static org.junit.Assert.assertNull;
23-
import static org.junit.Assert.assertTrue;
24-
import static org.junit.Assert.fail;
25-
2619
import java.lang.annotation.ElementType;
2720
import java.lang.annotation.Retention;
2821
import java.lang.annotation.RetentionPolicy;
@@ -37,6 +30,8 @@
3730
import org.junit.Test;
3831
import org.springframework.core.MethodParameter;
3932

33+
import static org.junit.Assert.*;
34+
4035
/**
4136
* @author Keith Donald
4237
* @author Andy Clement

0 commit comments

Comments
 (0)