-
Notifications
You must be signed in to change notification settings - Fork 41
Custom Accessor methods
since 1.3.0 version
It is not always possible to comply with the get and set prefixes, for this reason we have introduced this new important feature.
We have two possibility: with annotation @JMapAccessor
and in XML with get
and set
attributes.
@JMapAccessor
is possibile to use it 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 (since 1.3.1 version): (Optional) allows to limit the configuration to a range of classes
@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.<br>You can use `@JMapAccessor` to define custom methods for the opposite field, follow an example:
```java
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.
© 2016 Alessandro Vurro
- Home
- How to map
- Relations
- Conversions
- creation/enrichment
- XML
- Annotation
- API
- Configurations
- Utilities
- Examples
- Articles
- More information
- Performance tests
- Release Notes