diff --git a/core/src/main/java/feign/ReflectiveFeign.java b/core/src/main/java/feign/ReflectiveFeign.java index 5d8fe068..6a39d6d8 100644 --- a/core/src/main/java/feign/ReflectiveFeign.java +++ b/core/src/main/java/feign/ReflectiveFeign.java @@ -83,27 +83,28 @@ public class ReflectiveFeign extends Feign { } catch (IllegalArgumentException e) { return false; } - } - if ("hashCode".equals(method.getName())) { + } else if ("hashCode".equals(method.getName())) { return hashCode(); + } else if ("toString".equals(method.getName())) { + return toString(); } return dispatch.get(method).invoke(args); } - @Override public int hashCode() { - return target.hashCode(); - } - - @Override public boolean equals(Object other) { - if (other instanceof FeignInvocationHandler) { - FeignInvocationHandler that = (FeignInvocationHandler) other; - return this.target.equals(that.target); + @Override public boolean equals(Object obj) { + if (obj instanceof FeignInvocationHandler) { + FeignInvocationHandler other = (FeignInvocationHandler) obj; + return target.equals(other.target); } return false; } + @Override public int hashCode() { + return target.hashCode(); + } + @Override public String toString() { - return "target(" + target + ")"; + return target.toString(); } } diff --git a/core/src/main/java/feign/Target.java b/core/src/main/java/feign/Target.java index 894855d4..474ed297 100644 --- a/core/src/main/java/feign/Target.java +++ b/core/src/main/java/feign/Target.java @@ -15,8 +15,6 @@ */ package feign; -import java.util.Arrays; - import static feign.Util.checkNotNull; import static feign.Util.emptyToNull; @@ -95,19 +93,29 @@ public interface Target { return input.request(); } + @Override public boolean equals(Object obj) { + if (obj instanceof HardCodedTarget) { + HardCodedTarget other = (HardCodedTarget) obj; + return type.equals(other.type) + && name.equals(other.name) + && url.equals(other.url); + } + return false; + } + @Override public int hashCode() { - return Arrays.hashCode(new Object[]{type, name, url}); + int result = 17; + result = 31 * result + type.hashCode(); + result = 31 * result + name.hashCode(); + result = 31 * result + url.hashCode(); + return result; } - @Override public boolean equals(Object obj) { - if (obj == null) - return false; - if (this == obj) - return true; - if (HardCodedTarget.class != obj.getClass()) - return false; - HardCodedTarget that = HardCodedTarget.class.cast(obj); - return this.type.equals(that.type) && this.name.equals(that.name) && this.url.equals(that.url); + @Override public String toString() { + if (name.equals(url)) { + return "HardCodedTarget(type=" + type.getSimpleName() + ", url=" + url + ")"; + } + return "HardCodedTarget(type=" + type.getSimpleName() + ", name=" + name + ", url=" + url + ")"; } } } diff --git a/core/src/test/java/feign/FeignTest.java b/core/src/test/java/feign/FeignTest.java index 801422e2..ac64568f 100644 --- a/core/src/test/java/feign/FeignTest.java +++ b/core/src/test/java/feign/FeignTest.java @@ -24,6 +24,7 @@ import com.google.mockwebserver.RecordedRequest; import com.google.mockwebserver.SocketPolicy; import dagger.Module; import dagger.Provides; +import feign.Target.HardCodedTarget; import feign.codec.Decoder; import feign.codec.Encoder; import feign.codec.ErrorDecoder; @@ -46,6 +47,7 @@ import static dagger.Provides.Type.SET; import static feign.Util.UTF_8; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertNotEquals; import static org.testng.Assert.assertNull; import static org.testng.Assert.assertTrue; @@ -489,19 +491,38 @@ public class FeignTest { } } - @Test public void equalsAndHashCodeWork() { - TestInterface i1 = Feign.create(TestInterface.class, "http://localhost:8080", new TestInterface.Module()); - TestInterface i2 = Feign.create(TestInterface.class, "http://localhost:8080", new TestInterface.Module()); - TestInterface i3 = Feign.create(TestInterface.class, "http://localhost:8888", new TestInterface.Module()); - OtherTestInterface i4 = Feign.create(OtherTestInterface.class, "http://localhost:8080", new TestInterface.Module()); - - assertTrue(i1.equals(i1)); - assertTrue(i1.equals(i2)); - assertFalse(i1.equals(i3)); - assertFalse(i1.equals(i4)); + @Test public void equalsHashCodeAndToStringWork() { + Target t1 = new HardCodedTarget(TestInterface.class, "http://localhost:8080"); + Target t2 = new HardCodedTarget(TestInterface.class, "http://localhost:8888"); + Target t3 = + new HardCodedTarget(OtherTestInterface.class, "http://localhost:8080"); + TestInterface i1 = Feign.builder().target(t1); + TestInterface i2 = Feign.builder().target(t1); + TestInterface i3 = Feign.builder().target(t2); + OtherTestInterface i4 = Feign.builder().target(t3); + + assertEquals(i1, i1); + assertEquals(i1, i2); + assertNotEquals(i1, i3); + assertNotEquals(i1, i4); assertEquals(i1.hashCode(), i1.hashCode()); assertEquals(i1.hashCode(), i2.hashCode()); + assertNotEquals(i1.hashCode(), i3.hashCode()); + assertNotEquals(i1.hashCode(), i4.hashCode()); + + assertEquals(i1.hashCode(), t1.hashCode()); + assertEquals(i3.hashCode(), t2.hashCode()); + assertEquals(i4.hashCode(), t3.hashCode()); + + assertEquals(i1.toString(), i1.toString()); + assertEquals(i1.toString(), i2.toString()); + assertNotEquals(i1.toString(), i3.toString()); + assertNotEquals(i1.toString(), i4.toString()); + + assertEquals(i1.toString(), t1.toString()); + assertEquals(i3.toString(), t2.toString()); + assertEquals(i4.toString(), t3.toString()); } @Test public void decodeLogicSupportsByteArray() throws Exception { diff --git a/ribbon/src/main/java/feign/ribbon/LoadBalancingTarget.java b/ribbon/src/main/java/feign/ribbon/LoadBalancingTarget.java index 0894ed48..efa18e92 100644 --- a/ribbon/src/main/java/feign/ribbon/LoadBalancingTarget.java +++ b/ribbon/src/main/java/feign/ribbon/LoadBalancingTarget.java @@ -15,7 +15,6 @@ */ package feign.ribbon; -import com.google.common.base.Objects; import com.netflix.loadbalancer.AbstractLoadBalancer; import com.netflix.loadbalancer.Server; @@ -25,7 +24,6 @@ import feign.Request; import feign.RequestTemplate; import feign.Target; -import static com.google.common.base.Objects.equal; import static com.netflix.client.ClientFactory.getNamedLoadBalancer; import static feign.Util.checkNotNull; import static java.lang.String.format; @@ -99,18 +97,23 @@ public class LoadBalancingTarget implements Target { } } + @Override public boolean equals(Object obj) { + if (obj instanceof LoadBalancingTarget) { + LoadBalancingTarget other = (LoadBalancingTarget) obj; + return type.equals(other.type) + && name.equals(other.name); + } + return false; + } + @Override public int hashCode() { - return Objects.hashCode(type, name); + int result = 17; + result = 31 * result + type.hashCode(); + result = 31 * result + name.hashCode(); + return result; } - @Override public boolean equals(Object obj) { - if (obj == null) - return false; - if (this == obj) - return true; - if (LoadBalancingTarget.class != obj.getClass()) - return false; - LoadBalancingTarget that = LoadBalancingTarget.class.cast(obj); - return equal(this.type, that.type) && equal(this.name, that.name); + @Override public String toString() { + return "LoadBalancingTarget(type=" + type.getSimpleName() + ", name=" + name + ")"; } }