Mapping inner classes

1,787 views
Skip to first unread message

Natalia Dimitrova

unread,
Dec 23, 2014, 11:08:00 AM12/23/14
to mapstru...@googlegroups.com
Hi,

I want to map properties of a class to another, which appears to be inner one. When the Impl class is generated, there is an error initializing the inner class object. I can not think of a work around this issue.

Regards,
Natalia

Andreas Gudian

unread,
Dec 23, 2014, 11:58:49 AM12/23/14
to Natalia Dimitrova, mapstru...@googlegroups.com
Hi Natalia,

Can you give us a code example?

Thanks,
Andreas
--
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/d/optout.

Natalia Dimitrova

unread,
Dec 23, 2014, 1:41:52 PM12/23/14
to mapstru...@googlegroups.com
public class A {

public void methodA(){

}

public class B {
public void methodB(){

}
}
}



public class C {
public void methodC(){

}

}

@Mapper
public interface Test {
@Mapping(some mapping here)
public A.B mappingMethod(C c);
}

Natalia Dimitrova

unread,
Jan 6, 2015, 12:50:29 PM1/6/15
to mapstru...@googlegroups.com
Actually this could be achieved with object factories :)

Gunnar Morling

unread,
Jan 6, 2015, 4:38:32 PM1/6/15
to mapstru...@googlegroups.com
Hi Natalia,

Sorry for the late reply, we have been in holiday mode basically for a while :)

Glad to hear you found a work around; Out of interest, how does it exactly look like? Could you share some code?

Nevertheless we should add the import, I've filed https://github.com/mapstruct/mapstruct/issues/411 for this. For the time being, you also could add the class via @Mapper#imports(), this only works though if the inner and outer class live in a different package than the mapper interface (that's another glitch I just noticed).

One thing I'm wondering though, is your inner class actually a *static* inner class? I think that's the only case we can support in MapStruct, as otherwise we wouldn't have an instance of the outer class at hand which we'd need for instantiating the non-static inner class.

--Gunnar
Message has been deleted

Natalia Dimitrova

unread,
Jan 10, 2015, 3:39:58 PM1/10/15
to mapstru...@googlegroups.com
Hi,

the nested class is not static and basically that is the problem.
The code looks something like that:


public class A {

    public class B {
        private String fieldB;

        //getter and setter
    }
}

public class C {
    private String fieldC;

    //getter and setter
}

public class ObjectFactory {
    private static final A a = new A();

    public A getA() {
        return a;
    }

    public A.B createB() {

        return a.new B();
    }

}

@Mapper(uses = { ObjectFactory.class })
public interface BMapper {

    BMapper INSTANCE = Mappers.getMapper(BMapper.class);

    @Mappings({ @Mapping(source = "fieldC", target = "fieldB") })
    A.B mapB(C c);
Reply all
Reply to author
Forward
0 new messages