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

Make Message.hashCode Consistent with Message.equals #941

Merged
merged 2 commits into from
Dec 22, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Make Message.hashCode Consistent with .equals
Fixes #940
  • Loading branch information
tlantz committed Jul 3, 2015
commit d4bac74d446a63ce76c6b5b8303026c93f96eaec
6 changes: 3 additions & 3 deletions core/src/com/google/inject/spi/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.google.inject.spi;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.Objects;
import com.google.common.collect.ImmutableList;
import com.google.inject.Binder;
Expand All @@ -28,6 +26,8 @@
import java.io.Serializable;
import java.util.List;

import static com.google.common.base.Preconditions.checkNotNull;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why'd you move this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Inadvertent (IntelliJ defaults). I will fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in d20bf5b


/**
* An error message and the context in which it occured. Messages are usually created internally by
* Guice and its extensions. Messages can be created explicitly in a module using {@link
Expand Down Expand Up @@ -108,7 +108,7 @@ public Throwable getCause() {
}

@Override public int hashCode() {
return message.hashCode();
return Objects.hashCode(message, cause, sources);
}

@Override public boolean equals(Object o) {
Expand Down
2 changes: 2 additions & 0 deletions core/test/com/google/inject/AllTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.inject.spi.HasDependenciesTest;
import com.google.inject.spi.InjectionPointTest;
import com.google.inject.spi.InjectorSpiTest;
import com.google.inject.spi.MessageTest;
import com.google.inject.spi.ModuleAnnotatedMethodScannerTest;
import com.google.inject.spi.ModuleRewriterTest;
import com.google.inject.spi.ModuleSourceTest;
Expand Down Expand Up @@ -134,6 +135,7 @@ public static Test suite() {
suite.addTestSuite(ToolStageInjectorTest.class);
suite.addTestSuite(ModuleSourceTest.class);
suite.addTestSuite(ElementSourceTest.class);
suite.addTestSuite(MessageTest.class);

// tools
// suite.addTestSuite(JmxTest.class); not a testcase
Expand Down
29 changes: 29 additions & 0 deletions core/test/com/google/inject/spi/MessageTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.google.inject.spi;

import com.google.common.collect.Lists;
import junit.framework.TestCase;

import java.util.List;

/**
* Tests for {@link Message}.
*/
public class MessageTest extends TestCase {

public void testMessageHashCodeVariesWithSource() {
String innerMessage = "This is the message.";
Message firstMessage = new Message(1, innerMessage);
Message secondMessage = new Message(2, innerMessage);
assertFalse(firstMessage.hashCode() == secondMessage.hashCode());
}

public void testMessageHashCodeVariesWithCause() {
String innerMessage = "This is the message.";
List<Object> sourceList = Lists.newArrayList(new Object());
// the throwable argument of each Message below do not have value equality
Message firstMessage = new Message(sourceList, innerMessage, new Exception(innerMessage));
Message secondMessage = new Message(sourceList, innerMessage, new Exception(innerMessage));
assertFalse(firstMessage.hashCode() == secondMessage.hashCode());
}
}