Skip to content

Commit e3445b1

Browse files
committed
Improve Conversion section of ref docs
1 parent 40f9ac5 commit e3445b1

File tree

1 file changed

+25
-25
lines changed
  • framework-docs/modules/ROOT/pages/core/validation

1 file changed

+25
-25
lines changed

framework-docs/modules/ROOT/pages/core/validation/convert.adoc

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ When you need to centralize the conversion logic for an entire class hierarchy
7272
}
7373
----
7474

75-
Parameterize S to be the type you are converting from and R to be the base type defining
75+
Parameterize `S` to be the type you are converting from and `R` to be the base type defining
7676
the __range__ of classes you can convert to. Then implement `getConverter(Class<T>)`,
77-
where T is a subclass of R.
77+
where `T` is a subclass of `R`.
7878

7979
Consider the `StringToEnumConverterFactory` as an example:
8080

@@ -107,13 +107,15 @@ Consider the `StringToEnumConverterFactory` as an example:
107107
[[core-convert-GenericConverter-SPI]]
108108
== Using `GenericConverter`
109109

110-
When you require a sophisticated `Converter` implementation, consider using the
111-
`GenericConverter` interface. With a more flexible but less strongly typed signature
112-
than `Converter`, a `GenericConverter` supports converting between multiple source and
113-
target types. In addition, a `GenericConverter` makes available source and target field
114-
context that you can use when you implement your conversion logic. Such context lets a
115-
type conversion be driven by a field annotation or by generic information declared on a
116-
field signature. The following listing shows the interface definition of `GenericConverter`:
110+
When you require a more sophisticated `Converter` implementation, consider using the
111+
`GenericConverter` interface. With a more flexible but less strongly typed signature than
112+
`Converter`, a `GenericConverter` supports converting between multiple source and target
113+
types. In addition, a `GenericConverter` is provided source and target type descriptors
114+
that you can use when you implement your conversion logic. Such type descriptors enable
115+
type conversion to be driven by an annotation on the source of the descriptor (such as a
116+
field or method) or by generic information declared in a field signature, method
117+
signature, etc. The following listing shows the definition of the `GenericConverter`
118+
interface:
117119

118120
[source,java,indent=0,subs="verbatim,quotes",chomp="-packages"]
119121
----
@@ -128,16 +130,17 @@ field signature. The following listing shows the interface definition of `Generi
128130
----
129131

130132
To implement a `GenericConverter`, have `getConvertibleTypes()` return the supported
131-
source->target type pairs. Then implement `convert(Object, TypeDescriptor,
133+
source &#8594; target type pairs. Then implement `convert(Object, TypeDescriptor,
132134
TypeDescriptor)` to contain your conversion logic. The source `TypeDescriptor` provides
133-
access to the source field that holds the value being converted. The target `TypeDescriptor`
134-
provides access to the target field where the converted value is to be set.
135+
access to the source field or method that holds the value being converted. The target
136+
`TypeDescriptor` provides access to the target field or method where the converted value
137+
is to be set.
135138

136139
A good example of a `GenericConverter` is a converter that converts between a Java array
137-
and a collection. Such an `ArrayToCollectionConverter` introspects the field that declares
138-
the target collection type to resolve the collection's element type. This lets each
139-
element in the source array be converted to the collection element type before the
140-
collection is set on the target field.
140+
and a collection. Such an `ArrayToCollectionConverter` introspects the field or method
141+
that declares the target collection type to resolve the collection's element type. This
142+
lets each element in the source array be converted to the collection element type before
143+
the collection is set on the target field or supplied to the target method or constructor.
141144

142145
NOTE: Because `GenericConverter` is a more complex SPI interface, you should use
143146
it only when you need it. Favor `Converter` or `ConverterFactory` for basic type
@@ -148,9 +151,9 @@ conversion needs.
148151
=== Using `ConditionalGenericConverter`
149152

150153
Sometimes, you want a `Converter` to run only if a specific condition holds true. For
151-
example, you might want to run a `Converter` only if a specific annotation is present
152-
on the target field, or you might want to run a `Converter` only if a specific method
153-
(such as a `static valueOf` method) is defined on the target class.
154+
example, you might want to run a `Converter` only if a specific annotation is present on
155+
the target field or method, or you might want to run a `Converter` only if a specific
156+
method (such as a `static valueOf` method) is defined on the target type.
154157
`ConditionalGenericConverter` is the union of the `GenericConverter` and
155158
`ConditionalConverter` interfaces that lets you define such custom matching criteria:
156159

@@ -212,7 +215,7 @@ creating common `ConversionService` configurations.
212215
A `ConversionService` is a stateless object designed to be instantiated at application
213216
startup and then shared between multiple threads. In a Spring application, you typically
214217
configure a `ConversionService` instance for each Spring container (or `ApplicationContext`).
215-
Spring picks up that `ConversionService` and uses it whenever a type
218+
Spring picks up that `ConversionService` and uses it whenever type
216219
conversion needs to be performed by the framework. You can also inject this
217220
`ConversionService` into any of your beans and invoke it directly.
218221

@@ -249,7 +252,8 @@ It is also common to use a `ConversionService` within a Spring MVC application.
249252
xref:web/webmvc/mvc-config/conversion.adoc[Conversion and Formatting] in the Spring MVC chapter.
250253

251254
In certain situations, you may wish to apply formatting during conversion. See
252-
xref:core/validation/format.adoc#format-FormatterRegistry-SPI[The `FormatterRegistry` SPI] for details on using `FormattingConversionServiceFactoryBean`.
255+
xref:core/validation/format.adoc#format-FormatterRegistry-SPI[The `FormatterRegistry` SPI]
256+
for details on using `FormattingConversionServiceFactoryBean`.
253257

254258

255259

@@ -338,7 +342,3 @@ method on the `DefaultConversionService` class.
338342
Converters for value types are reused for arrays and collections, so there is
339343
no need to create a specific converter to convert from a `Collection` of `S` to a
340344
`Collection` of `T`, assuming that standard collection handling is appropriate.
341-
342-
343-
344-

0 commit comments

Comments
 (0)