Map nested Field without Wrapper-Getter in Source-Class

492 views
Skip to first unread message

Alex F

unread,
Feb 23, 2015, 4:29:20 AM2/23/15
to jmapper-...@googlegroups.com
Hello!

First of all: JMapper is a great Lib, Thank you for all your work and this Google Group!

My problem or feature request:

I want to map a nested field by using a conversion (i read that automatic mapping was requested earlier for nested fields). Here some sample code:

public class Source {
private String field;
private FieldWrapper fieldWrapper;

public Source(String field, FieldWrapper fieldWrapper) {
this.field = field;
this.fieldWrapper = fieldWrapper;
}
public String getField() {
return field;
}

public void setField(String field) {
this.field = field;
}

public Boolean getWrappedField() {
return fieldWrapper.getWrappedField();
}

public void setWrappedField(Boolean wrappedField) {
fieldWrapper.setWrappedField(wrappedField);
}
}

public class FieldWrapper {
protected Boolean wrappedField;
public FieldWrapper(Boolean wrappedField) {
this.wrappedField = wrappedField;
}

public Boolean getWrappedField() {
return wrappedField;
}

public void setWrappedField(Boolean wrappedField) {
this.wrappedField = wrappedField;
}
}

public class Destination {
@JMap
private String field;
@JMap("fieldWrapper")
private Boolean unwrappedField;
@JMapConversion(from={"fieldWrapper"}, to={"unwrappedField"})
public Boolean convert(FieldWrapper fieldWrapper) {
return fieldWrapper.getWrappedField();
}
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
public Boolean getUnwrappedField() {
return unwrappedField;
}
public void setUnwrappedField(Boolean unwrappedField) {
this.unwrappedField = unwrappedField;
}
}

Console:

com.googlecode.jmapper.exceptions.MalformedBeanException: getFieldWrapper method not found in Source Class for the fieldWrapper field, checks the signature.

Now the problem is exactly what the message says, although i have written a converter, it is not used since the fieldWrapper cannot be accessed. When i change my @JMap("fieldWrapper") to @JMap("wrappedField"), for which a getter exists, JMapper is complaining that the field does not exist in the source-class: com.googlecode.jmapper.exceptions.MappingErrorException: incorrect configuration of the unwrappedField field in Destination Class: the wrappedField field doesn't exist in Source Class

This is all correct behavior but i need to get around that since I am not allowed to (or shouldn't) make any changes to the source-class. What could i do besides manually mapping the field?

Best, Alex

Alessandro Vurro

unread,
Feb 23, 2015, 5:17:23 AM2/23/15
to jmapper-...@googlegroups.com
Hi Alex,

in the first case you must to add get method for fieldWrapper field in Source class.
This because the generated code need to that method to write the follow mapping code:

destination.setUnwrappedField(mapper.convert(source.getFieldWrapper())

At present is impossible to apply this mapping without add this get method.

I'm working on it, see next features:

Alex F

unread,
Feb 23, 2015, 5:32:38 AM2/23/15
to jmapper-...@googlegroups.com
Hello!

Thank you for you quick answer, I will add the accessors, just wanted to make sure if there was a way around it.

Best, Alex
Reply all
Reply to author
Forward
0 new messages