Copy pojo fields to another pojo's setters
Asked Answered
H

3

7

Let's say I have class A with public fields x and y. And let's say I have another pojo class B but that uses setters and getters, so it has setX() and setY().

I'd like to use some automatic way to copy from instance of A to B and back.

With default settings at least, Dozer's

   Mapper mapper = new DozerBeanMapper();
   B b = mapper.map(a, B.class);

does not copy the fields correctly.

So is there a simple configuration change that allows me to accomplish the above with Dozer, or another library that would do this for me?

Homeopathy answered 4/12, 2013 at 8:54 Comment(3)
have you tried using JAXB, ObjectMapper? or you want to user dozer only?Curculio
No I haven't. I'm happy to use any lib that offers "one line conversion" in the above situation.Homeopathy
you would like to use jaxb, it does annotation based binding so i think it should work for you. checkout wiki.fasterxml.com/JacksonDocumentation and tutorial in the same pageCurculio
L
4

I'd suggest you use:

http://modelmapper.org/

Or take a look at this question:

Copy all values from fields in one class to another through reflection

I'd say that both API's (BeanUtils) and ModelMapper provide one-liners for copy pojos' values to another pojos. Take a look @ this:

http://modelmapper.org/getting-started/

Logy answered 4/12, 2013 at 9:24 Comment(1)
Ok, ModelMapper does the job but there's one gotcha, you have to enable field mapping: mapper.getConfiguration().setFieldMatchingEnabled(true);.Homeopathy
H
1

Not actually a one-liner but this approach doesn't require any libs.

I was testing it using these classes:

  private class A {
    public int x;
    public String y;

    @Override
    public String toString() {
      return "A [x=" + x + ", y=" + y + "]";
    }
  }

  private class B {
    private int x;
    private String y;

    public int getX() {
      return x;
    }

    public void setX(int x) {
      System.out.println("setX");
      this.x = x;
    }

    public String getY() {
      return y;
    }

    public void setY(String y) {
      System.out.println("setY");
      this.y = y;
    }

    @Override
    public String toString() {
      return "B [x=" + x + ", y=" + y + "]";
    }
  }

To get public field we can use reflection, as for setters it's better to use bean utils:

public static <X, Y> void copyPublicFields(X donor, Y recipient) throws Exception {
    for (Field field : donor.getClass().getFields()) {
      for (PropertyDescriptor descriptor : Introspector.getBeanInfo(recipient.getClass()).getPropertyDescriptors()) {
        if (field.getName().equals(descriptor.getName())) {
          descriptor.getWriteMethod().invoke(recipient, field.get(donor));
          break;
        }
      }
    }
  }

The test:

final A a = new A();
a.x = 5;
a.y = "10";
System.out.println(a);
final B b = new B();
copyPublicFields(a, b);
System.out.println(b);

And its output is:

A [x=5, y=10]
setX
setY
B [x=5, y=10]
Homunculus answered 4/12, 2013 at 9:54 Comment(0)
O
1

For someone who is still looking for, You could try this using Gson

Gson gson = new Gson();
Type type = new TypeToken<YourPOJOClass>(){}.getType();
String data = gson.toJson(workingPOJO);
coppiedPOJO = gson.fromJson(data, type);
Ogilvy answered 8/12, 2020 at 17:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.