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

Use EqualsVerifier to test equals and hashCode, modified implementati… #244

Merged
merged 6 commits into from
Dec 29, 2021
Merged

Use EqualsVerifier to test equals and hashCode, modified implementati… #244

merged 6 commits into from
Dec 29, 2021

Conversation

tracylynne99
Copy link
Collaborator

Use EqualsVerifier to test equals and hashCode, modified implementations of equals and hashCode as needed to comply with EV. Including, but not limited to:

do not include transient variables in either function, 
added missing hashCode where needed, 
use Objects.equals to compare object types, 
use Double.doubleToLongBits to compare double values.

…ons of equals and hashCode as needed to comply with EV. Including, but not limited to:

    do not include transient variables in either function, 
    added missing hashCode where needed, 
    use Objects.equals to compare object types, 
    use Double.doubleToLongBits to compare double values.
Copy link
Owner

@jfree jfree left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The removal of transient fields from the equals() tests causes some strange results. For example, add this to LegendItemTest.java, it should pass but does not:

    public void testEquals() {
        LegendItem item1 = new LegendItem("Series 1", Color.RED);
        LegendItem item2 = new LegendItem("Series 1", Color.BLUE);
        assertNotEquals(item1, item2);
    }

@tracylynne99
Copy link
Collaborator Author

tracylynne99 commented Nov 15, 2021

The removal of transient fields from the equals() tests causes some strange results. For example, add this to LegendItemTest.java, it should pass but does not:

    public void testEquals() {
        LegendItem item1 = new LegendItem("Series 1", Color.RED);
        LegendItem item2 = new LegendItem("Series 1", Color.BLUE);
        assertNotEquals(item1, item2);
    }

I removed those fields from the equals test because I had read (from many many places) that they should not be included. Why, exactly, was a mystery, but I figured that since the origin of that advice was "Effective Java", it had to be pretty solid.

Then I read your email about the questions I'd had about the classes whose only variables were transient and thought "Oh. Yeah ok, we do need to know if those members are equal or not"

So this morning I did some extensive Google searching, and could not find the why for this generally accepted rule. And then I came across an article in InfoWorld (https://www.infoworld.com/article/2072762/object-equality.html) that said

"transient fields are not compared because the transient keyword's purpose is to prevent fields from being written during serialization; and these fields should therefore not play a part in equality testing. Otherwise, if an object is serialized and then de-serialized, then it will not be equal to itself."

Ah, but we DO serialize the transient fields! JFC just does it manually rather than rely on Java's built in serialization - I think you had said in your email that GradientPaint doesn't serialize well, so JFC has to do it manually. But the manual serialization can only be done if the variable is transient.

So, long story short (LOL too late! :-)), when I removed the transient variables from equals, I messed up! I will fix that mistake and commit the fix.

transient variables back into eq/hc where needed, fixed up more eq/hc
methods to comply with EV. Added suppression of transient warning to
EV calls in junit tests, added previous tests for eq/hc back into junit
tests (they can be removed later if needed).
@tracylynne99
Copy link
Collaborator Author

@jfree I finally finished adding the transient fields back! (And EV'ing more classes)

I realize there's still some conflicts and I'll resolve those for sure - just let me know if this 2nd commit looks better. I reran all the JUnit tests and they pass!

@jfree
Copy link
Owner

jfree commented Nov 30, 2021

Thanks Tracy. I didn't get a chance to review everything but so far it is looking good. If I run 'mvn clean verify' on your branch I get just one test failure:

[ERROR] CategoryPointerAnnotationTest.testEqualsHashCode:76 EqualsVerifier found a problem in class org.jfree.chart.annotations.CategoryPointerAnnotation.
-> Non-nullity: hashCode throws NullPointerException on field arrowStroke.

Do you see the same?

@tracylynne99
Copy link
Collaborator Author

tracylynne99 commented Nov 30, 2021 via email

Use Objects.hashCode for arrowStroke
@tracylynne99
Copy link
Collaborator Author

Ok, I finally fixed CategoryPointerAnnotation. Then I resolved the conflicts in the other files, but I did so offline because I wasn't clear on how to complete the process using the website.

Ran into a bunch of other problems which turned out to be a problem in my local environment.

So hopefully it's clear where my fix/conflict resolution is! If you want me to ditch this pull request and create a brand new one so there's no conflicts I can sure do that - just let me know.

Fixed Conflicts With:
	src/main/java/org/jfree/chart/entity/CategoryItemEntity.java
	src/main/java/org/jfree/chart/entity/ChartEntity.java
	src/main/java/org/jfree/chart/entity/FlowEntity.java
	src/main/java/org/jfree/chart/entity/PieSectionEntity.java
	src/test/java/org/jfree/chart/entity/CategoryItemEntityTest.java
	src/test/java/org/jfree/chart/entity/CategoryLabelEntityTest.java
	src/test/java/org/jfree/chart/entity/FlowEntityTest.java
	src/test/java/org/jfree/chart/entity/LegendItemEntityTest.java
	src/test/java/org/jfree/chart/entity/PieSectionEntityTest.java
Fixed Conflicts With:
	src/main/java/org/jfree/chart/entity/CategoryItemEntity.java
	src/main/java/org/jfree/chart/entity/ChartEntity.java
	src/main/java/org/jfree/chart/entity/FlowEntity.java
	src/main/java/org/jfree/chart/entity/PieSectionEntity.java
	src/test/java/org/jfree/chart/entity/CategoryItemEntityTest.java
	src/test/java/org/jfree/chart/entity/CategoryLabelEntityTest.java
	src/test/java/org/jfree/chart/entity/FlowEntityTest.java
	src/test/java/org/jfree/chart/entity/LegendItemEntityTest.java
	src/test/java/org/jfree/chart/entity/PieSectionEntityTest.java
@tracylynne99
Copy link
Collaborator Author

tracylynne99 commented Dec 15, 2021 via email

@jfree jfree merged commit ea36f93 into jfree:v1.5.x Dec 29, 2021
@tracylynne99 tracylynne99 deleted the v1.5.x-stuff2 branch May 5, 2022 17:44
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.

None yet

2 participants