Nomin uses Java reflection to retrieve type information about properties and methods. Though you can specify more accurate types using hints.
Suppose we have to map Employees owning Details and Kids to the following classes:
public class LegacyPerson { private Object name; // here should be string private Object lastName; // as the previous private Object details; // details actually refers to LegacyDetails instance // ... } public class LegacyDetails { private Date birthday; private Boolean sex; // identical to Details.sex private Object[] children; // children should contain Child instances // ... }
Nomin has a lack of type information to operate with desired types. We should provide that information as shown in the following mapping.
mappingFor a: LegacyPerson, b: Employee a.name = b.name hint a: String a.lastName = b.last hint a: String a.details.birthday = b.details.birth hint a: LegacyDetails a.details.sex = b.details.sex hint a: LegacyDetails a.details.children = b.details.kids hint a: [LegacyDetails, Array[Child]]
To specify type of elements of an array use org.nomin.core.Array type with desired element type in square brackets. The same technique is used for any collection as shown below.
a.list = b.set hint a: List[ElementType], b: HashSet[AnotherType] a.container.elements[0] = b.element hint a: [DEFAULT, List[ElementType]], b: AnotherType
If we want to leave a type inferred through the reflection we can use constant value DEFAULT.
Also you can specify a type with a dynamic hint, this means that a target type will be calculated dynamically during mapping. A dynamic hint is defined with a closure. The closure receives a value of the opposite side as the only parameter and can access a and b instances. Let's look at samples.
a.child = b.kid hint a: { kid -> if (kid.sex) Boy; else Girl } a.children = b.kids hint a: List[{ kid -> if (kid.sex) Boy; else Girl }]
This feature is quite simple but very helpful.