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 · 0 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.

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

1 participant