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

Replace commons-lang equals, hashCode, and toString with generated code #812

Merged

Conversation

ehrmann
Copy link
Contributor

@ehrmann ehrmann commented Dec 8, 2017

commons-lang is a relatively large library for users to include just for adding toString(), equals(), and hashCode(). This removes that dependency and uses generated code instead.

It avoids calls like Objects.hash(), Double.hashCode(), and Objects.equals() that were added in later versions of Java.

Generated code looks something like this:

public class ComplexTypesArray {
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(ComplexTypesArray.class.getName())
                .append('@')
                .append(Integer.toHexString(System.identityHashCode(this)))
                .append('[');
        sb.append("additionalProperties");
        sb.append('=');
        sb.append(((this.additionalProperties == null)?"<null>":this.additionalProperties));
        sb.append(',');
        if (sb.charAt((sb.length()- 1)) == ',') {
            sb.setCharAt((sb.length()- 1), ']');
        } else {
            sb.append(']');
        }
        return sb.toString();
    }

    @Override
    public int hashCode() {
        int result = 1;
        result = ((result* 31)+((this.additionalProperties == null)? 0 :this.additionalProperties.hashCode()));
        return result;
    }

    @Override
    public boolean equals(Object other) {
        if (other == this) {
            return true;
        }
        if ((other instanceof ComplexTypesArray) == false) {
            return false;
        }
        ComplexTypesArray rhs = ((ComplexTypesArray) other);
        return ((this.additionalProperties == rhs.additionalProperties)||((this.additionalProperties!= null)
                && this.additionalProperties.equals(rhs.additionalProperties)));
    }
}

@ehrmann
Copy link
Contributor Author

ehrmann commented Dec 8, 2017

I wasn't able to get jsonschema2pojo-gradle-plugin to pass tests. It looks like

classpath 'org.jsonschema2pojo:jsonschema2pojo-gradle-plugin:latest.integration' in jsonschema2pojo-gradle-plugin/example/java/build.gradle actually pulls in the latest repository version of jsonschema2pojo-* rather than the one from the project.

@joelittlejohn
Copy link
Owner

Great stuff. Thanks for this! Are you aware of any functional difference between the commons-lang implementations and the code you have generated?

@ehrmann
Copy link
Contributor Author

ehrmann commented Dec 8, 2017

I should look into in more (or add more tests). I tried to match the behavior of toString. Equals should generally do the right thing.

HashCode is probably different. The only time I can think of a change to hashCode making a difference is a serialized HashMap. Luckily, OpenJDK's HashMap rebuilds the map on deserialization. Since this doesn't change any fields or method signatures, these classes will have the same SerialVersionUID.

@joelittlejohn
Copy link
Owner

Great. I don't think identical behaviour is required, here (particularly, as you say, for hashCode). I only ask so that we can understand any difference that may exist, and also preempt support questions.

I'll merge this for the next release.

@ehrmann
Copy link
Contributor Author

ehrmann commented Dec 11, 2017

Cool! I might add a few tests to check the string format.

@ehrmann ehrmann force-pushed the remove-commons-lang-from-pojos branch from 7444424 to 111cc9d Compare December 12, 2017 06:40
@ehrmann
Copy link
Contributor Author

ehrmann commented Dec 12, 2017

I added a test class for toString as a separate commit. It looks like the output is generally compatible. There might be an edge case, but it's really close.

@ehrmann
Copy link
Contributor Author

ehrmann commented Jan 1, 2018

@joelittlejohn Any idea when you'll make the next release?

@joelittlejohn joelittlejohn merged commit 248a1c6 into joelittlejohn:master Jan 1, 2018
@joelittlejohn joelittlejohn added this to the 1.0.0-alpha1 milestone Feb 1, 2018
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

Successfully merging this pull request may close these issues.

2 participants