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

@EqualsAndHashCode/@ToString(onlyExplicitlyIncluded = true) - still takes all fields, when no fields are annotated with @...Include. #1772

Closed
wheredevel opened this issue Jul 10, 2018 · 7 comments

Comments

@wheredevel
Copy link

wheredevel commented Jul 10, 2018

@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@ToString(onlyExplicitlyIncluded = true)

Doesn't work as expected. Instead, if nothing is annotated with @...Incude - all is included.

This is the workaround that worked for me:

@EqualsAndHashCode(of = {}, onlyExplicitlyIncluded = true)
@ToString(of = {}, onlyExplicitlyIncluded = true)

Please, fix.

@wheredevel wheredevel changed the title @EqualsAndHashCode/@ToString(onlyExplicitlyIncluded = true) - still takes all fields, when no fields are annotated with @.Include. @EqualsAndHashCode/@ToString(onlyExplicitlyIncluded = true) - still takes all fields, when no fields are annotated with @...Include. Jul 10, 2018
@Maaartinus
Copy link
Contributor

I'd bet this has been fixed in 1.18, just can't see it in the changelog.

@wheredevel
Copy link
Author

wheredevel commented Jul 10, 2018

I stopped upgrading from 1.16.20 because of this.
I'd be happy to see tests covering such new features (as well as regression).

@wheredevel
Copy link
Author

@rspilker , please, test before closing this issue.

Here's the PROOF that it DOESN'T WORK in 1.18.2.
===> testLombok_OnlyExplicitlyIncluded() - fails.

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import lombok.Data;
import lombok.EqualsAndHashCode;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class EqualsAndHashCodeTest2 {

	static class Super {

		private String field;
	}

	static class DummyVanilla extends Super {

		private final String field;

		public DummyVanilla(String field) {
			super();
			this.field = field;
		}

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

		@Override
		public boolean equals(Object obj) {
			if (this == obj) {
				return true;
			}
			if (obj == null) {
				return false;
			}
			if (getClass() != obj.getClass()) {
				return false;
			}
			DummyVanilla other = (DummyVanilla) obj;
			if (field == null) {
				if (other.field != null) {
					return false;
				}
			} else if (!field.equals(other.field)) {
				return false;
			}
			return true;
		}
	}

	static class DummyVanilla_OnlyExplicitlyIncluded extends Super {

		private final String field;

		public DummyVanilla_OnlyExplicitlyIncluded(String field) {
			super();
			this.field = field;
		}

	}

	@Data
	@EqualsAndHashCode(callSuper = false)
	static class DummyLombok extends Super {

		private final String field;
	}

	@Data
	@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
	static class DummyLombok_OnlyExplicitlyIncluded extends Super {

		private final String field;
	}

	//////////////// Equals //////////////////////
	
	@Test
	public void testVanilla() {
		assertEquals(new DummyVanilla("test"), new DummyVanilla("test"));
	}

	@Test
	public void testLombok() {
		assertEquals(new DummyLombok("test"), new DummyLombok("test"));
	}

	//////////////// NotEquals //////////////////////

	@Test
	public void testObject() {
		assertNotEquals(new Object(), new Object());
	}

	@Test
	public void testSuper() {
		assertNotEquals(new Super(), new Super());
	}

	@Test
	public void testVanilla_OnlyExplicitlyIncluded() {
		assertNotEquals(new DummyVanilla_OnlyExplicitlyIncluded("test"), new DummyVanilla_OnlyExplicitlyIncluded("test"));
	}

	@Test
	public void testLombok_OnlyExplicitlyIncluded() {
		assertNotEquals(new DummyLombok_OnlyExplicitlyIncluded("test"), new DummyLombok_OnlyExplicitlyIncluded("test"));
	}
}

@randakar
Copy link

randakar commented Oct 23, 2018 via email

@wheredevel
Copy link
Author

@randakar it's not on the Edge list either.

@rspilker
Copy link
Collaborator

I did add a test in the changeset I mentioned

@rspilker
Copy link
Collaborator

We might have forgotten to mention it in change logs.

@wheredevel, did you test it against the latest edge release.

Can you otherwise tell me what I did wrong in my test code?

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

No branches or pull requests

4 participants