5

Using lombok, I am interested to copy all the fields from a base class instance to the derived class via its constructor, very similar to what C++ copy constructor does. Presently concern is not so much on whether the copy is deep or shallow. I have a base class as shown below,

class Parent {
  .... fields
}

and I am interested to automatically generate a derived class constructor which takes base class instance and copy (either shallow or deep) all the fields to the derived one. e.g.

class Child extends Parent {
   ... derived fields
   Child(Parent p) { // can be implemented as super(p); 
   }
}

I have the flexibility to annotate both Parent and Child class as needed, however do not want to handcraft the constructor, which copies each fields one by one. Example usage

Parent parent = Parent.of(....);
Child child = new Child(parent);
abir
  • 1,797
  • 14
  • 26

1 Answers1

2

It looks like the functionality for copy constructor isn't there yet(github issue)

And also its not possible to generate constructors calling super(stated here and github issue), because:

getting to the parent class required resolution, it is simply not possible.

So based on this I think it's not possible to do that, currently

Ruslan Akhundov
  • 2,178
  • 4
  • 20
  • 38
  • 1
    @vmrvictor It was opened few years ago when I was answering question above. Now it seems they've eventually decided not to implement this, please refer to this comment: https://github.com/rzwitserloot/lombok/issues/908#issuecomment-430001916 – Ruslan Akhundov Nov 15 '20 at 16:45