0

example:

class A{
    String aa;
    String bb;
    String cc;
    String dd;
}

class B{
    String aa;
    String bb;
}

OF course my real classes are bigger with more than 15 fields, that are also complex Object on their own. by now I am doing

B parseToB(A a){
    B b = new b();
    b.setAa(a.getAa());
    b.setBb(a.getBb());
    return b;
}

Is there an easy way to parse A to B? A Library or a builtin mapper class? It looks kind of like a weird spot in my code where I am just getsett-ing 15 lines of code.

Penguin74
  • 480
  • 6
  • 19
SG Tech Edge
  • 477
  • 4
  • 16
  • you can't parse the two, since there is no hierarchical relation. Just because those variables have the same name, does not mean they are related. You can map them, though. – Stultuske Feb 11 '20 at 07:45
  • At the very least you should provide a copy constructor or similar instead of having a dozen public setters. Or for an automatic solution search for "object to object mapper". https://stackoverflow.com/questions/3319002/automapper-for-java – Voo Feb 11 '20 at 07:48

3 Answers3

1

I personally use MapStruct in my projects for this sort of thing, it provides a nice flexible way to declare simple mappings and also provides good extensions for complex mapping types.

So for your example you could simply define an interface (or abstract class)

@Mapper
public interface ObjectMapper {

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

    A mapToA(B b);

    B mapToB(A a);
}

You will need to configure an annotation processor during your build cycle (if you are using maven), and then you can simply call the mapper in your code

A firstObject = new A(); //Set your variables

B secondObject = ObjectMapper.INSTANCE.mapToB(firstObject);
Brendon Randall
  • 1,436
  • 3
  • 14
  • 25
0

You can for example provide a constructor in B class that accept A as parameter:

class B {
    String aa;
    String bb;

    public B(A a) {
        this.aa = a.getAa();
        this.bb = a.getAa();
    }
 }

And then you can create a new instance of B starting from A simply calling the constructor:

B newObject = new B(a);

Remember that if you want to create a new instance of B class without parameter you have to add also the empty constructor (which can be omitted if there are no other constructors):

class B {
    String aa;
    String bb;


    public B() {
    }

    public B(A a) {
        this.aa = a.getAa();
        this.bb = a.getAa();
    }
 }
A. Wolf
  • 1,309
  • 17
  • 39
0

You can use reflection. Try this:

static class A {

        String aa, bb;
        Integer cc;
        Boolean dd;
    }

    static class B {

        String aa, bb;
        Integer cc;
        Integer dd;

        @Override
        public String toString() {
            return "B{" + "aa=" + aa + ", bb=" + bb + ", cc=" + cc + ", dd=" + dd + '}';
        }


    }

    public static void main(String []args) throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
        A a = new A();
        a.aa = "aa";
        a.bb = "bb";
        a.cc = 10;
        a.dd = true;
        B b = new B();
        for (Field declaredField : b.getClass().getDeclaredFields()) {
            if (a.getClass().getDeclaredField(declaredField.getName()) != null // both class have a field with the same name      
              && a.getClass().getDeclaredField(declaredField.getName()).getType()
                  .equals(declaredField.getType())) { // and the fields are of the same type
                declaredField.set(b, a.getClass().getDeclaredField(declaredField.getName()).get(a));
            }
        }
        System.out.println(b);

    }
Renato
  • 2,077
  • 1
  • 11
  • 22