-
Notifications
You must be signed in to change notification settings - Fork 41
Custom Accessor methods
It is not always possible to comply with the get and set prefixes, for this reason we have introduced this new important feature.
We can do this in:
- Annotation with
@JMapAccessor
, - XML with
get
andset
attributes in theattribute
andvalue
nodes, - API with
customGet
andcustomSet
methods that belong to theTargetAttribute
andLocalAttribute
classes.
It's possible to use @JMapAccessor
on field and on class.
It containts three parameters:
- name: (Optional) the name of the field that has custom accessor methods, if you not define it, JMapper assumes that the custom methods refer to the field itself
- get: (Optional) Permits to define a custom get method
- set: (Optional) Permits to define a custom set method
- classes: (Optional) allows to limit the configuration to a range of classes
Class Bean {
@JMapAccessor(get="SpecialGetField",set="SpecialSetField")
@JMap("targetField")
String field;
public String SpecialGetField(){
return field;
}
public void SpecialSetField(String field){
this.field = field;
}
}
As you can see custom accessor methods are easy to use, in this case we want to define get/set methods for that field (for this reason the name is optional), by default Jmapper associates custom methods to the field that declares it.
You can use @JMapAccessor
to define custom methods for the opposite field, follow an example:
Class DestBean {
@JMapAccessor(name="srcField",get="SpecialGetField",set="SpecialSetField")
@JMap("srcField")
String destField;
}
Class SrcBean {
String srcField;
public String SpecialGetField(){
return srcField;
}
public void SpecialSetField(String field){
this.srcField= field;
}
}
You can do the two operations together, with @JMapAccessors
:
Class DestBean {
@JMapAccessors({
@JMapAccessor(name="srcField",get="SpecialGetField",set="SpecialSetField")
@JMapAccessor(get="SpecialGetField",set="SpecialSetField")
})
@JMap("srcField")
String destField;
public String SpecialGetField(){
return destField;
}
public void SpecialSetField(String field){
this.destField= field;
}
}
Class SrcBean {
String srcField;
public String SpecialGetField(){
return srcField;
}
public void SpecialSetField(String field){
this.srcField= field;
}
}
IMPORTANT! The definition of custom accessor methods for the opposite field has less visibility of the same definition on the field itself.
Example:
Class DestBean {
@JMapAccessors({
@JMapAccessor(name="srcField",get="UNREAD",set="SpecialSetField")
@JMapAccessor(get="SpecialGetField",set="SpecialSetField")
})
@JMap("srcField")
String destField;
public String SpecialGetField(){
return destField;
}
public void SpecialSetField(String field){
this.destField= field;
}
}
Class SrcBean {
@JMapAccessor(get="SpecialGetField")
String srcField;
public String SpecialGetField(){
return srcField;
}
public void SpecialSetField(String field){
this.srcField= field;
}
}
You can do the same things on class:
@JMapAccessors({
@JMapAccessor(name="srcField",get="UNREAD",set="SpecialSetField")
@JMapAccessor(name="destField",get="SpecialGetField",set="SpecialSetField")
})
Class DestBean {
@JMap("srcField")
String destField;
public String SpecialGetField(){
return destField;
}
public void SpecialSetField(String field){
this.destField= field;
}
}
Class SrcBean {
@JMapAccessor(get="SpecialGetField")
String srcField;
public String SpecialGetField(){
return srcField;
}
public void SpecialSetField(String field){
this.srcField= field;
}
}
classes parameter permits to avoid cases where target and mapped fields have the same name for example:
public class Destination{
@JMapAccessors({
@JMapAccessor(name="field",get="get",set="set", classes={Source.class}),
@JMapAccessor(name="field",get="get",set="set", classes={Destination.class})
})
private String field;
}
IMPORTANT! @JMapAccessor on class has major visibility of @JMapAccessor on field.
In xml configuration you can add the get/set custom methods in the attribute node, both in the current attribute and in the target attribute:
...
<class name="destBean">
<attribute name="dField" get="getDestField" set="setDestField">
<value name="sField" get="UNREAD" set="setSrcField"/>
</attribute>
</class>
<class name="srcBean">
<attribute name="sField" get="getsField" />
</class>
...
IMPORTANT! The definition of custom accessor methods for the opposite field has less visibility of the same definition on the field itself.
You can do the same work with global node:
...
<class name="DestBean">
<global>
<value name="sField" get="getSrcField" />
<attributes>
<attribute name="dField" get="getDestField" set="setDestField"/>
</attributes>
</global>
<attribute name="dField" get="UNREAD" set="UNREAD"/>
</class>
...
IMPORTANT! The custom methods definition on global node has major visibility of it on attribute node.
The methods that permit to define custom get/set methods are customGet
and customSet
that belong to Attribute
, LocalAttribute
and TargetAttribute
classes.
An example:
...
import static com.googlecode.jmapper.api.JMapperAPI.*;
...
JMapperAPI jmapperAPI = new JMapperAPI()
.add(mappedClass(MyClass.class)
.add(global()
.excludedAttributes(
localAttribute("localField")
.customGet("customGetMethod")
.customSet("customSetMethod")))
.add(attribute("sField1")
.customSet("customSetMethod")
.customGet(""customGetMethod")
.value(targetAttribute("targetField")
.customGet("customGetMethod")
.customSet("customSetMethod"))
the methods that permit to use customGet and customSet are attribute
, localAttribute
and targetAttribute
.
It's possibile to use a signature with the three parameters: name and the custom get/set methods, as shown below:
.attribute("attribute", "customGetMethod", "customSetMethod)
.localAttribute("attribute", "customGetMethod", "customSetMethod)
.targetAttribute("attribute", "customGetMethod", "customSetMethod)
© 2016 Alessandro Vurro
- Home
- How to map
- Relations
- Conversions
- creation/enrichment
- XML
- Annotation
- API
- Configurations
- Utilities
- Examples
- Articles
- More information
- Performance tests
- Release Notes