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

Option to ignore non-final fields expected to be used for equals #200

Closed
abika opened this issue Jul 12, 2018 · 3 comments
Closed

Option to ignore non-final fields expected to be used for equals #200

abika opened this issue Jul 12, 2018 · 3 comments
Labels

Comments

@abika
Copy link

abika commented Jul 12, 2018

Considering this simple example (almost auto-generated by IDE):

public class Foo {

    private final long id;
    private final String name;
    private String description = "";

    public Foo(long id, String name, String description) {
        this.id = id;
        this.name = name;
        this.description = description;
    }

    public long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Foo foo = (Foo) o;
        return id == foo.id && Objects.equals(name, foo.name);
    }

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

EqualsVerifier.forClass(Foo.class).usingGetClass().verify(); does fail with

java.lang.AssertionError: Significant fields: equals does not use description, or it is stateless.

I follow the common practice to not use non-final fields for equals (insert foos into a set and later modify them -> ugh!).
Now there is the option to explicitly tell EqualsVerifier the fields used in equals -> not practical if EqualsVerifier should be used for 100+ already existing classes. Or suppress(Warning.ALL_FIELDS_SHOULD_BE_USED), but this does not differentiate between final and non-final fields.

So i kindly propose a new suppress(Warning.ALL_NON_FINAL_FIELDS_SHOULD_BE_USED).

@jqno
Copy link
Owner

jqno commented Jul 13, 2018 via email

@jqno
Copy link
Owner

jqno commented Jul 30, 2018

I just released version 2.5 with a Warning.ALL_NONFINAL_FIELDS_SHOULD_BE_USED flag.

@jqno jqno closed this as completed Jul 30, 2018
@abika
Copy link
Author

abika commented Jul 31, 2018

Thanks for the fast implementation!

akhalikov pushed a commit to akhalikov/equalsverifier that referenced this issue Nov 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants