Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add @DeepClone Annotation for Generating Deep Cloning Methods #3794

Open
volodya-lombrozo opened this issue Dec 9, 2024 · 3 comments

Comments

@volodya-lombrozo
Copy link

Describe the feature

A @DeepClone annotation that, when placed on a class, generates a deepClone() method. This method would create and return a new object instance with all its fields deeply copied. The goal is to reduce boilerplate code where developers currently must manually write clone logic, especially in classes with nested objects.

Lomboked Version (Conceptual Example)

import lombok.DeepClone;

@DeepClone
public class Person {
    private String name;
    private Address address;
}

What Lombok Would Generate Under the Hood

public class Person {
    private String name;
    private Address address;

    public Person deepClone() {
        Person clone = new Person();
        clone.name = this.name;
        // In case if address doesn't have deepClone method, we can clone the reference.
        clone.address = (this.address == null) ? null : this.address.deepClone();
        return clone;
    }
}

// For completeness, the Address class would need its own deepClone logic:
public class Address {
    private String street;
    private String city;

    public Address deepClone() {
        Address clone = new Address();
        clone.street = this.street;
        clone.city = this.city;
        return clone;
    }
}

Describe the target audience
This feature would benefit Java developers who work with complex, nested object graphs and frequently need a deep copy of their objects. For instance, developers building domain models with hierarchical structures, DTOs, or configuration objects that must be safely cloned to avoid unintended mutations. Such developers currently must manually implement deep cloning or rely on external utilities, which leads to boilerplate and potential errors.

Additional context
Deep cloning is a common requirement in many applications but is often repetitive and error-prone if done manually. While libraries like Apache Commons Lang provide a reflection-based cloning approach, a Lombok-generated method would be more efficient, type-safe, and integrated with existing Lombok features. Implementing @DeepClone would align with Lombok’s goal of reducing boilerplate while maintaining performance and clarity.

@AndersonInc
Copy link

Hello Volodya, I would like to take up this issue, however I am new to this but I have atleast studied some cloning before, is there any thing I would need to know to get started with this issue other than what you have stated thank you very much.

@janrieke
Copy link
Contributor

janrieke commented Dec 28, 2024

You should not start working on this before you get an approval from the maintainers. Otherwise you'd risk getting your PR rejected, either because you chose a wrong approach, or the feature or solution in itself is flawed, or too difficult to maintain.

In this case, I'm quite sure the maintainers will consider the whole feature flawed. It was already discussed a few times, and it will simply never meet the expectations Lombok users would have: You would need deep-cloning enabled on all referenced classes, and you can neither ensure that nor even detect that one of the referenced classes does not support it.

Furthermore, @With serves a similar purpose.

@AndersonInc
Copy link

Thank you very much, oh yes about the referenced classes it's something I was also looking at intently. Thank you very much please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants