Sebi
Sebi

Reputation: 2584

Dozer: Mapping of class with no default constructor

Lets say I want to map the following two classes:

public class A {

    String member;

    public void setMember(String member) { this.member = member }
    public String getMember() { return member }
}

public class B {

    String member;

    public B(String member) { this.member = member }

    public String getMember() { return member }
}

Now when I want Dozer to do the following conversion: dozerBeanMapper.map( a, B.class ); I get an error because of the missing default constructor of class B.

What's the best way to solve that problem? Use a custom converter?

Upvotes: 10

Views: 8677

Answers (4)

David
David

Reputation: 1101

I ran into this issue while trying to map a java.util.Locale. To solve my problem I did as follows :

I created a class called LocaleMapper which would match dumb LocaleToLocaleConversion

public class LocaleMapper extends DozerConverter<Locale, Locale> {
    public LocaleMapper() {
        super(Locale.class, Locale.class);
    }

    @Override
    public Locale convertTo(Locale localeA, Locale localeB) {
        return localeA;
    }

    @Override
    public Locale convertFrom(Locale localeA, Locale localeB) {
        return localeA;
    }
}

Then I modified the mapping xml of the project :

<converter type="LocaleMapper">
            <class-a>java.util.Locale</class-a>
            <class-b>java.util.Locale</class-b>
 </converter>

Now I can add Locale objects to my classes that are mapped with Dozer. My Dozer knowledge is somewhat limited, so I can't explain the indepth details of how it works underneath the hood, but it worked for my project.

Upvotes: 3

ktorn
ktorn

Reputation: 88

You can either create a default constructor for B, or alternatively use a custom BeanFactory, so that Dozer can create the instance it needs.

Upvotes: 0

Strelok
Strelok

Reputation: 51421

If class B is not your API and you have no control over it and you intend to map member property anyway, you can get away with a custom bean factory that can perhaps pass a default value to the costructor:

<mapping>
  <class-a>com.example.A</class-a>
  <class-b bean-factory="com.example.factories.BFactory">
    com.example.B
  </class-b>
</mapping>

Your factory will implement org.dozer.BeanFactory interface:

public interface BeanFactory {
  public Object createBean(Object source, Class sourceClass, String targetBeanId);
}

Upvotes: 9

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340708

From Dozer FAQ:

Some of my data objects don't have public constructors. Does Dozer support this use case?

Yes. When creating a new instance of the destination object if a public no-arg constructor is not found, Dozer will auto detect a private constructor and use that. If the data object does not have a private constructor, you can specify a custom BeanFactory for creating new instances of the destination object.

Here is a documentation of Custom Bean Factories

Upvotes: 4

Related Questions