Package ch.ivyteam.ivy.scripting.util
Class IvyBeanUtil
- java.lang.Object
-
- ch.ivyteam.ivy.scripting.util.IvyBeanUtil
-
public final class IvyBeanUtil extends Object
Utility class to convert java beans from one type to another type.- API:
- This is a public API.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <E> E
toType(Object sourceBean, Class<E> returnType)
Converts the given source bean to the given type by copying all properties that have the same name in the source and target type.
This method makes a hierarchical deep conversion, so complex properties (for example, nested ones) will be converted/copied too.
The conversion is done as follows: If the types are assignable then the original property value is copied and no conversion is performed. If the types are not assignable then a conversions is performed if available.static <E> List<E>
toTypedList(Collection<?> sourceCollection, Class<E> listMemberType)
Converts the given source collection to a list of the given type.
Each list entry is converted by using the methodtoType(Object, Class)
.
-
-
-
Method Detail
-
toType
public static <E> E toType(Object sourceBean, Class<E> returnType) throws IllegalArgumentException
Converts the given source bean to the given type by copying all properties that have the same name in the source and target type.
This method makes a hierarchical deep conversion, so complex properties (for example, nested ones) will be converted/copied too.
The conversion is done as follows:- If the types are assignable then the original property value is copied and no conversion is performed.
- If the types are not assignable then a conversions is performed if available.
- If not conversion is available then the property is not copied.
Supported conversions:
- BeanX to BeanY (if they have common fields, see above)
- Ivy types to Java types and vice versa (e.g.
Date
<->DateTime
) - Arrays or Collections of different types (e.g. List<TypeX> <-> Array<TypeY>
To convert collections (e.g. a List) instead of beans, use the method
toTypedList(Collection, Class)
.Example:
All properties which exists on both class are copied from TypeX to TypeY:
TypeX typeX = ...;
TypeY typeY = IvyBeanUtil.toType(typeX, TypeY.class);
- Type Parameters:
E
- type of the result- Parameters:
sourceBean
- the object to convert to thereturnType
. Could be null.returnType
- the type of the resulting object.- Returns:
- The converted
sourceBean
with the givenreturnType
or null, if the sourceBean was null - Throws:
IllegalArgumentException
- if conversion failed- API:
- This public API is available in IvyScript and Java. It has the visibility EXPERT.
-
toTypedList
public static <E> List<E> toTypedList(Collection<?> sourceCollection, Class<E> listMemberType) throws IllegalArgumentException
Converts the given source collection to a list of the given type.
Each list entry is converted by using the methodtoType(Object, Class)
.Example:
All entries in the list are converted from TypeX to TypeY:
List<TypeX> listX = ...;
List<TypeY> listY = IvyBeanUtil.toTypedList(listX, TypeY.class);
- Type Parameters:
E
- Type of the list- Parameters:
sourceCollection
- Collection to convert, could be null.listMemberType
- The member type of the resulting list.- Returns:
- the converted
sourceCollection
as List of typedestListMemberType
- Throws:
IllegalArgumentException
- if the conversion fails- API:
- This public API is available in IvyScript and Java. It has the visibility EXPERT.
-
-