eclipse.jdt.ls icon indicating copy to clipboard operation
eclipse.jdt.ls copied to clipboard

[Feature Request]: "Convert Class to Record", "Convert Record to Class"

Open GavinRay97 opened this issue 4 years ago • 2 comments

JDK16 records are really nice to work with, it'd be great to be able to (at least in cases where it's supported) convert Class to Record to clean up code 😃

Example below -- the two being convertible between each other for instance:

How high is the barrier to entry for adding a new CodeAction refactoring suggestion so that they are available in VS Code? Is it reasonable for a random person to contribute the implementation?

record Person(String name, Integer age, String email) {
    void sayHello() {
        System.out.println("Hello");
    }
}

final class Person {
    private final String name;
    private final Integer age;
    private final String email;

    Person(String name, Integer age, String email) {
        this.name = name;
        this.age = age;
        this.email = email;
    }

    void sayHello() {
        System.out.println("Hello");
    }

    public String name() {
        return name;
    }

    public Integer age() {
        return age;
    }

    public String email() {
        return email;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) return true;
        if (obj == null || obj.getClass() != this.getClass()) return false;
        var that = (Person2) obj;
        return Objects.equals(this.name, that.name) &&
                Objects.equals(this.age, that.age) &&
                Objects.equals(this.email, that.email);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, email);
    }

    @Override
    public String toString() {
        return "Person[" +
                "name=" + name + ", " +
                "age=" + age + ", " +
                "email=" + email + ']';
    }
}

GavinRay97 avatar Aug 13 '21 17:08 GavinRay97

As per https://github.com/redhat-developer/vscode-java/issues/2053#issuecomment-899333351, I don't think this exists even in upstream JDT for us to just migrate over and use. So likely the work need to be done upstream first.

rgrunber avatar Nov 22 '22 16:11 rgrunber

Additionally, it would be nice to have the "Change method signature" refactoring be able to change the parameters of a record, similar to those of regular constructors.

Claudio140 avatar Jul 26 '24 08:07 Claudio140