abir
abir

Reputation: 1807

Is it possible to construct a derived class instance from a base class instance using lombok?

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);

Upvotes: 5

Views: 467

Answers (1)

Ruslan Akhundov
Ruslan Akhundov

Reputation: 2216

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

Upvotes: 2

Related Questions