Target object without default constructor

1,181 views
Skip to first unread message

Massimo Biancalani

unread,
Jul 10, 2015, 5:50:32 AM7/10/15
to mapstru...@googlegroups.com
Dear all,
I spent some time on trying to map and object to a target without default constructor.. like

Enter code here...
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class ValidationFieldDetails {

   
private final String field;
   
private final String message;
}

Could someone give me an hint... I tried with

Object factories


but MapStruct will look for a parameterless method, or a method with only one @TargetType parameter that returns the required target type and invoke this method instead of calling the default constructor. So I failed to use my constructor

 

Thanks
Massimo

Massimo Biancalani

unread,
Jul 10, 2015, 6:18:38 AM7/10/15
to mapstru...@googlegroups.com
Dear all,
I used this work around

First I create a fake Factory class

public class ValidationFieldDetailsMapperFactory {

   
public ValidationFieldDetails fakeConstructorToCheatDefaultConstructor(){
     
return null;
   
}
Enter code here...

and then I created a decorator for the real mapping

very inelegant...but at least working

Ciao
Massimo

Gunnar Morling

unread,
Jul 11, 2015, 2:14:33 AM7/11/15
to mapstru...@googlegroups.com, massimo.b...@gmail.com
Hi Massimo,

At the moment there is no direct support for parameterized constructors yet. There is an issue logged for this (https://github.com/mapstruct/mapstruct/issues/73) and we plan to address it in MapStruct 1.1. Maybe you'd like to help out with implementing this feature?

As a work-around, you could create a builder class with a default constructor for your target type, let MapStruct map to this builder instead of the actual target type and then manually invoke the build() method of that builder returned by the mapping method. The build() method would invoke the proper constructor.

Cheers,

--Gunnar
Reply all
Reply to author
Forward
0 new messages