public interface JTransfo
jTransfo can be used to convert between transfer objects (TO ot DTO) and domain objects.
You just have to annotate the transfer object with DomainClass
to indicate the domain object
(either the class or the fully qualified class name).
By default all fields are converted, but you can use annotations to configure this:
You can also add PreConvert
or PostConvert
tags on the transfer object to define code to be executed
before or after converting the fields.
When converting, the fields on the transfer objects are always used directly (as the annotations are on the fields). For domain objects, the getters and setters are used when they exist (even when private).
When jTransfo needs to determine the object to convert to (when using convertTo(Object, Class, String...)
or when converting a nested object which is null in the target) then the ObjectFinder
will be used.
By default this always creates new objects, but you can add custom object finders to allow objects to be retrieved
from the database or whatever behaviour you need.
The conversion itself can be controlled in various ways:
MapOnly
and in the type converters, interceptors etcMappedBy
or MapOnly
annotations. See TypeConverter
.ConvertInterceptor
.
ObjectReplacer
. This can replace objects just before they are
converted. This can for example be useful to detect lazy objects and replace them with proper
implementations. See ObjectReplacer
.
ClassReplacer
. This can replace classes which are used as conversion
target. It affects both DomainClass
target classes and convertTo(Object, Class, String...)
target classes.
This is very useful to allow replacing interfaces to implementation classes for domain objects or replacing
transfer object classes to enhanced transfer objects (with more fields).
You should only replace classes by child classes. See ClassReplacer
.
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_TAG_WHEN_NO_TAGS
Default tag which is activated when no tags are specified in the convert call.
|
static String |
TAG_WHEN_READ_ONLY_DOMAIN
Default tag which is automatically added when calling
findTarget(Object, Class, String...)
in the context of a ReadOnlyDomain type converter. |
Modifier and Type | Method and Description |
---|---|
Object |
convert(Object source)
Create a new domain object from the source transfer object.
|
<T> T |
convert(Object source,
T target,
String... tags)
Fill the target object with the values from the source object.
|
<T> List<T> |
convertList(List<?> source,
Class<T> targetClass,
String... tags)
Convert a list of object to the given type.
|
<T> T |
convertTo(Object source,
Class<T> targetClass,
String... tags)
Create a new domain object from the source transfer object.
|
<T> T |
findTarget(Object source,
Class<T> targetClass,
String... tags)
Get the base target (domain) object for the source (transfer) object.
|
Class<?> |
getDomainClass(Class<?> toClass)
Get domain class for transfer object.
|
Class<?> |
getToSubType(Class<?> toType,
Object domainObject)
Get the correct transfer object type for the given domain object.
|
boolean |
isToClass(Class<?> toClass)
Is the given class a transfer object class?
|
static final String DEFAULT_TAG_WHEN_NO_TAGS
static final String TAG_WHEN_READ_ONLY_DOMAIN
findTarget(Object, Class, String...)
in the context of a ReadOnlyDomain type converter.<T> T convert(Object source, T target, String... tags)
This will write all values from the transfer object, other fields are not touched.
T
- type of object for targetsource
- source object. Should not be null.target
- target object. Should not be null.tags
- tags which indicate which fields can be converted based on MapOnly
annotations.
Tags are processed from left to right.Object convert(Object source)
When the source is null, the result is also null.
The object finders are used to build the object to copy to.
source
- source transfer object<T> T convertTo(Object source, Class<T> targetClass, String... tags)
When the source is null, the result is also null.
The object finders are used to build the object to copy to.
T
- type of object for targetsource
- source transfer objecttargetClass
- target class to convert totags
- tags which indicate which fields can be converted based on MapOnly
annotations.
Tags are processed from left to right.<T> List<T> convertList(List<?> source, Class<T> targetClass, String... tags)
convertTo(Object, Class, String...)
on each object.
When the source is null, the result is also null.
T
- type of object for targetsource
- source list of objectstargetClass
- target class to convert each object totags
- tags which indicate which fields can be converted based on MapOnly
annotations.
Tags are processed from left to right.<T> T findTarget(Object source, Class<T> targetClass, String... tags)
When the source is null, the result is also null.
The object finders are used to build the object to copy to.
T
- type of object for targetsource
- source transfer objecttargetClass
- target class to convert totags
- tags which indicate how objects need to be foundClass<?> getDomainClass(Class<?> toClass)
toClass
- transfer object classboolean isToClass(Class<?> toClass)
True when there is a DomainClass
annotation on the class.
toClass
- object class to testClass<?> getToSubType(Class<?> toType, Object domainObject)
This searches the DomainClassDelegates (if present) to see of there is a better matching transfer object than the one given as parameter.
toType
- base transfer object typedomainObject
- domain object (instance)Copyright © 2022. All rights reserved.