Created
March 21, 2013 17:22
-
-
Save elaatifi/5214847 to your computer and use it in GitHub Desktop.
Create a logical property to use within constructor mappings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Constructors { | |
public static void main(String[] args) { | |
ConfigurableMapper mapper = new ConfigurableMapper() { | |
@Override | |
protected void configure(MapperFactory factory) { | |
factory.classMap(A.class, B.class) | |
.fieldMap("name", logical("name", String.class), false) | |
/* Should not be included when generating mappers */ | |
.exclude() | |
.add() | |
.register(); | |
} | |
/** | |
* a Logical property used within constructor mappings may need other options (ex : converterId) | |
* May be we need to enhance ClassMapBuilder API to give easy way to create logical property | |
*/ | |
protected Property.Builder logical(String name, Class<?> type) { | |
final Property.Builder builder = new Property.Builder().name(name).type(String.class.getName()).getter("/*fake*/"); | |
return builder; | |
} | |
}; | |
A source = new A(); | |
source.name = "Khalil Gebran"; | |
System.out.println(mapper.map(source, B.class)); | |
} | |
public static class A { | |
public String name; | |
} | |
public static class B { | |
private String firstName; | |
private String lastName; | |
public B(String name) { | |
super(); | |
String[] s = name.split(" "); | |
firstName = s[0]; | |
lastName = s[1]; | |
} | |
public String getFirstName() { | |
return firstName; | |
} | |
public String getLastName() { | |
return lastName; | |
} | |
@Override | |
public String toString() { | |
return "B [firstName=" + firstName + ", lastName=" + lastName + "]"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment