Automatic generate code - add getters/setters for method invocation, not object creation [closed]

StackOverflow https://stackoverflow.com/questions/18013111

  •  04-06-2022
  •  | 
  •  

I need to fill up some Java business or entity objects in a method.

User user = new User();

user.setFirstName();
user.setLastName();
user.setCountry();
user.setLanguage();
user.setStreet();
user.setHouseNumber();
user.setTown();
user.setUserName();

Is there a way to generate those setters automatically? I already have this object with getters/setters, now I need to create this instance. A solutions is needed for method invocation, not object creation.

I use the eclipse IDE. Something like:

User user = new User();

Right click -> Add Source -> Add all setters (ALT+CTRL+exotic-key-combination) Or an alternative way?

Just to say that this will same me a lot of plotting code.

有帮助吗?

解决方案

Personally I would recommend Project Lombock for getter/setter generation.

It can also generate equals, hashCode and toString.

It uses an annotation processor to generate the code at compile time so that it doesn't clutter up your classes.

So a simple class :

public class Example {

    private String firstName;
    private String lastName;
}

With getters,setters,equals and hashCode becomes:

public class Example {

    private String firstName;
    private String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 67 * hash + Objects.hashCode(this.firstName);
        hash = 67 * hash + Objects.hashCode(this.lastName);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Example other = (Example) obj;
        if (!Objects.equals(this.firstName, other.firstName)) {
            return false;
        }
        if (!Objects.equals(this.lastName, other.lastName)) {
            return false;
        }
        return true;
    }
}

Which is a right mess. With Lombock this can be done like so:

@Data
public class Example {

    private String firstName;
    private String lastName;
}

And if we decompile the compiled class we get:

public class Example {

    public Example() {
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof Example)) {
            return false;
        }
        Example other = (Example) o;
        if (!other.canEqual(this)) {
            return false;
        }
        Object this$firstName = getFirstName();
        Object other$firstName = other.getFirstName();
        if (this$firstName != null ? !this$firstName.equals(other$firstName) : other$firstName != null) {
            return false;
        }
        Object this$lastName = getLastName();
        Object other$lastName = other.getLastName();
        return this$lastName != null ? this$lastName.equals(other$lastName) : other$lastName == null;
    }

    public boolean canEqual(Object other) {
        return other instanceof Example;
    }

    public int hashCode() {
        int PRIME = 31;
        int result = 1;
        Object $firstName = getFirstName();
        result = result * 31 + ($firstName != null ? $firstName.hashCode() : 0);
        Object $lastName = getLastName();
        result = result * 31 + ($lastName != null ? $lastName.hashCode() : 0);
        return result;
    }

    public String toString() {
        return (new StringBuilder()).append("Example(firstName=").append(getFirstName()).append(", lastName=").append(getLastName()).append(")").toString();
    }
    private String firstName;
    private String lastName;
}

So all the code is there, including a toString.

其他提示

There is Project Lombok. Using this you can annotate your class with @Data and library will generate getters and setters.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top