public class JaxbSource
{
private JAXBElement<String> test;
private JAXBElement<BigDecimal> test2;
@Mapper(uses = JaxbMapper.class)
public interface Convertor {
TargetDto transform(JaxbSource survey);
}
public class JaxbMapper{
public BigDecimal asBigDecimal(JAXBElement<BigDecimal> t)
{
return t.getValue();
}
public String asString(JAXBElement<String> t)
{
return t.getValue();
}
}
Hi,First of all: Thanks. This is really a good initiative. I like to have control and having the generated code available gives a lot more insight in what's happening than in competing (runtime frameworks).
One of the things I see a use case is the following:I'm trying to use Mapstruct to map JAXB generated java beans to DTO's. Because I need element properties on my JAXB mapping, I've included a global binding that generates wrappers (JAXBElement) around each generated JAXB class. So a source object would typically look like this:public class JaxbSource
{
private JAXBElement<String> test;
private JAXBElement<BigDecimal> test2;And a target DTO like this:public class TargetDto{private String test;private BigDecimal test2;Now, ideally, I would like to have nested properties in my mapper (in the form of Mapping(source = "test.value" , target = "test")) as is addressed elsewhere already.
But instead, I chose to use a custom mapper.@Mapper(uses = JaxbMapper.class)
public interface Convertor {
TargetDto transform(JaxbSource survey);
}Custom mapper:public class JaxbMapper{
public BigDecimal asBigDecimal(JAXBElement<BigDecimal> t)
{
return t.getValue();
}
public String asString(JAXBElement<String> t)
{
return t.getValue();
}
}The problem is that I keep on adding types (and I have a lot of them). I would rather make use of generics to solve this:public <T> T getValue(JAXBElement<T> t)
{
return t.getValue();
}
Unfortunately, mapstruct does not support this. Instead, it fails:diagnostic /Users/sjaak/Development/java/NetBeansProjects/ogc/MapStruct/src/main/java/com/mycompany/mapstruct/Convertor.java:8: error: Can't map property "javax.xml.bind.JAXBElement<java.lang.String> test" to "java.lang.String test".TargetDto transform(JaxbSource survey);^Is it possible that mapstruct could support generics in this way in the future?
Best regards & many thanks,SjaakPs. I know that Dozer offers support for JAXB Ojbectfactories as well. That might be a good addition for the future as well.
--
You received this message because you are subscribed to the Google Groups "mapstruct-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapstruct-use...@googlegroups.com.
To post to this group, send email to mapstru...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Hi,First of all: Thanks. This is really a good initiative. I like to have control and having the generated code available gives a lot more insight in what's happening than in competing (runtime frameworks).
One of the things I see a use case is the following:I'm trying to use Mapstruct to map JAXB generated java beans to DTO's. Because I need element properties on my JAXB mapping, I've included a global binding that generates wrappers (JAXBElement) around each generated JAXB class. So a source object would typically look like this:public class JaxbSource
{
private JAXBElement<String> test;
private JAXBElement<BigDecimal> test2;And a target DTO like this:public class TargetDto{private String test;private BigDecimal test2;Now, ideally, I would like to have nested properties in my mapper (in the form of Mapping(source = "test.value" , target = "test")) as is addressed elsewhere already.
But instead, I chose to use a custom mapper.@Mapper(uses = JaxbMapper.class)
public interface Convertor {
TargetDto transform(JaxbSource survey);
}Custom mapper:public class JaxbMapper{
public BigDecimal asBigDecimal(JAXBElement<BigDecimal> t)
{
return t.getValue();
}
public String asString(JAXBElement<String> t)
{
return t.getValue();
}
}The problem is that I keep on adding types (and I have a lot of them). I would rather make use of generics to solve this:public <T> T getValue(JAXBElement<T> t)
{
return t.getValue();
}Unfortunately, mapstruct does not support this. Instead, it fails:diagnostic /Users/sjaak/Development/java/NetBeansProjects/ogc/MapStruct/src/main/java/com/mycompany/mapstruct/Convertor.java:8: error: Can't map property "javax.xml.bind.JAXBElement<java.lang.String> test" to "java.lang.String test".TargetDto transform(JaxbSource survey);^Is it possible that mapstruct could support generics in this way in the future?
Best regards & many thanks,SjaakPs. I know that Dozer offers support for JAXB Ojbectfactories as well. That might be a good addition for the future as well.
Hi Gunnar,
I've spent some time on the issue and just checked in some code. Took me some time to get my Netbeans configured though (with Eclipse code formatter and check-style plugin). The solution makes the check in the MapCreationProcessor, method getMappingMethodReference a bit more accurate by introducing a MethodMatcher, which is in essence a visitor that check if the provided arguments and return type match the candidate methods. It takes the TypeParameters in the candidate method into consideration.
I've added unit test as well. It would be good to do a good check on my solution for the wild-cards. I'm always a bit struggling with the super / extends criteria.
Have a nice sylvester & good 2014,
Best regards,
Sjaak