Revision: 1358
Author: sberlin
Date: Tue Nov 2 19:23:23 2010
Log: never call hashcode on the underlying binding instances. continue to
call equals as lazily as possible.
http://code.google.com/p/google-guice/source/detail?r=1358
Modified:
/trunk/core/src/com/google/inject/internal/InstanceBindingImpl.java
/trunk/core/src/com/google/inject/internal/ProviderInstanceBindingImpl.java
/trunk/core/test/com/google/inject/DuplicateBindingsTest.java
/trunk/core/test/com/google/inject/OverrideModuleTest.java
=======================================
--- /trunk/core/src/com/google/inject/internal/InstanceBindingImpl.java Sat
Jul 3 08:51:31 2010
+++ /trunk/core/src/com/google/inject/internal/InstanceBindingImpl.java Tue
Nov 2 19:23:23 2010
@@ -110,6 +110,6 @@
@Override
public int hashCode() {
- return Objects.hashCode(getKey(), getScoping(), instance);
+ return Objects.hashCode(getKey(), getScoping());
}
}
=======================================
---
/trunk/core/src/com/google/inject/internal/ProviderInstanceBindingImpl.java
Sun Aug 22 11:12:59 2010
+++
/trunk/core/src/com/google/inject/internal/ProviderInstanceBindingImpl.java
Tue Nov 2 19:23:23 2010
@@ -114,6 +114,6 @@
@Override
public int hashCode() {
- return Objects.hashCode(getKey(), getScoping(), providerInstance);
+ return Objects.hashCode(getKey(), getScoping());
}
}
=======================================
--- /trunk/core/test/com/google/inject/DuplicateBindingsTest.java Sat Jul
3 08:51:31 2010
+++ /trunk/core/test/com/google/inject/DuplicateBindingsTest.java Tue Nov
2 19:23:23 2010
@@ -20,6 +20,8 @@
import static com.google.inject.name.Names.named;
import com.google.inject.internal.util.Lists;
+import com.google.inject.internal.util.Objects;
+
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.util.Arrays;
@@ -231,6 +233,66 @@
bind(A.class);
bind(A.class).to(RealA.class);
}
+ });
+ }
+
+ public void testEqualsNotCalledByDefaultOnInstance() {
+ final HashEqualsTester a = new HashEqualsTester();
+ a.throwOnEquals = true;
+ Guice.createInjector(new AbstractModule() {
+ @Override
+ protected void configure() {
+ bind(String.class);
+ bind(HashEqualsTester.class).toInstance(a);
+ }
+ });
+ }
+
+ public void testEqualsNotCalledByDefaultOnProvider() {
+ final HashEqualsTester a = new HashEqualsTester();
+ a.throwOnEquals = true;
+ Guice.createInjector(new AbstractModule() {
+ @Override
+ protected void configure() {
+ bind(String.class);
+ bind(Object.class).toProvider(a);
+ }
+ });
+ }
+
+ public void testHashcodeNeverCalledOnInstance() {
+ final HashEqualsTester a = new HashEqualsTester();
+ a.throwOnHashcode = true;
+ a.equality = "test";
+
+ final HashEqualsTester b = new HashEqualsTester();
+ b.throwOnHashcode = true;
+ b.equality = "test";
+ Guice.createInjector(new AbstractModule() {
+ @Override
+ protected void configure() {
+ bind(String.class);
+ bind(HashEqualsTester.class).toInstance(a);
+ bind(HashEqualsTester.class).toInstance(b);
+ }
+ });
+ }
+
+ public void testHashcodeNeverCalledOnProviderInstance() {
+ final HashEqualsTester a = new HashEqualsTester();
+ a.throwOnHashcode = true;
+ a.equality = "test";
+
+ final HashEqualsTester b = new HashEqualsTester();
+ b.throwOnHashcode = true;
+ b.equality = "test";
+ Guice.createInjector(new AbstractModule() {
+ @Override
+ protected void configure() {
+ bind(String.class);
+ bind(Object.class).toProvider(a);
+ bind(Object.class).toProvider(b);
+ }
});
}
@@ -417,6 +479,44 @@
public Foo get() {
return new Bar();
}
- }
+ }
+
+ private static class HashEqualsTester implements Provider<Object> {
+ private String equality;
+ private boolean throwOnEquals;
+ private boolean throwOnHashcode;
+
+ @Override
+ public boolean equals(Object obj) {
+ if (throwOnEquals) {
+ throw new RuntimeException();
+ } else if (obj instanceof HashEqualsTester) {
+ HashEqualsTester o = (HashEqualsTester)obj;
+ if(o.throwOnEquals) {
+ throw new RuntimeException();
+ }
+ if(equality == null && o.equality == null) {
+ return this == o;
+ } else {
+ return Objects.equal(equality, o.equality);
+ }
+ } else {
+ return false;
+ }
+ }
+
+ @Override
+ public int hashCode() {
+ if(throwOnHashcode) {
+ throw new RuntimeException();
+ } else {
+ return super.hashCode();
+ }
+ }
+
+ public Object get() {
+ return new Object();
+ }
+ }
}
=======================================
--- /trunk/core/test/com/google/inject/OverrideModuleTest.java Sun Aug 1
09:15:50 2010
+++ /trunk/core/test/com/google/inject/OverrideModuleTest.java Tue Nov 2
19:23:23 2010
@@ -20,6 +20,7 @@
import static com.google.inject.Guice.createInjector;
import static com.google.inject.name.Names.named;
+import com.google.inject.internal.util.Objects;
import com.google.inject.name.Named;
import com.google.inject.util.Modules;
import static java.lang.annotation.ElementType.TYPE;
@@ -509,4 +510,103 @@
@Override protected void configure() {
}
}
-}
+
+ public void testEqualsNotCalledByDefaultOnInstance() {
+ final HashEqualsTester a = new HashEqualsTester();
+ a.throwOnEquals = true;
+ Guice.createInjector(Modules.override(new AbstractModule() {
+ @Override
+ protected void configure() {
+ bind(String.class);
+ bind(HashEqualsTester.class).toInstance(a);
+ }
+ }).with());
+ }
+
+ public void testEqualsNotCalledByDefaultOnProvider() {
+ final HashEqualsTester a = new HashEqualsTester();
+ a.throwOnEquals = true;
+ Guice.createInjector(Modules.override(new AbstractModule() {
+ @Override
+ protected void configure() {
+ bind(String.class);
+ bind(Object.class).toProvider(a);
+ }
+ }).with());
+ }
+
+ public void testHashcodeNeverCalledOnInstance() {
+ final HashEqualsTester a = new HashEqualsTester();
+ a.throwOnHashcode = true;
+ a.equality = "test";
+
+ final HashEqualsTester b = new HashEqualsTester();
+ b.throwOnHashcode = true;
+ b.equality = "test";
+ Guice.createInjector(Modules.override(new AbstractModule() {
+ @Override
+ protected void configure() {
+ bind(String.class);
+ bind(HashEqualsTester.class).toInstance(a);
+ bind(HashEqualsTester.class).toInstance(b);
+ }
+ }).with());
+ }
+
+ public void testHashcodeNeverCalledOnProviderInstance() {
+ final HashEqualsTester a = new HashEqualsTester();
+ a.throwOnHashcode = true;
+ a.equality = "test";
+
+ final HashEqualsTester b = new HashEqualsTester();
+ b.throwOnHashcode = true;
+ b.equality = "test";
+ Guice.createInjector(Modules.override(new AbstractModule() {
+ @Override
+ protected void configure() {
+ bind(String.class);
+ bind(Object.class).toProvider(a);
+ bind(Object.class).toProvider(b);
+ }
+ }).with());
+ }
+
+ private static class HashEqualsTester implements Provider<Object> {
+ private String equality;
+ private boolean throwOnEquals;
+ private boolean throwOnHashcode;
+
+ @Override
+ public boolean equals(Object obj) {
+ if (throwOnEquals) {
+ throw new RuntimeException();
+ } else if (obj instanceof HashEqualsTester) {
+ HashEqualsTester o = (HashEqualsTester)obj;
+ if(o.throwOnEquals) {
+ throw new RuntimeException();
+ }
+ if(equality == null && o.equality == null) {
+ return this == o;
+ } else {
+ return Objects.equal(equality, o.equality);
+ }
+ } else {
+ return false;
+ }
+ }
+
+ @Override
+ public int hashCode() {
+ if(throwOnHashcode) {
+ throw new RuntimeException();
+ } else {
+ return super.hashCode();
+ }
+ }
+
+ public Object get() {
+ return new Object();
+ }
+ }
+
+}
--
You received this message because you are subscribed to the Google Groups
"google-guice-dev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice-dev?hl=en.