Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 12 additions & 11 deletions core/src/main/java/feign/ReflectiveFeign.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,28 @@ static class FeignInvocationHandler implements InvocationHandler {
} 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();
}
}

Expand Down
32 changes: 20 additions & 12 deletions core/src/main/java/feign/Target.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package feign;

import java.util.Arrays;

import static feign.Util.checkNotNull;
import static feign.Util.emptyToNull;

Expand Down Expand Up @@ -95,19 +93,29 @@ public HardCodedTarget(Class<T> type, String name, String url) {
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 + ")";
}
}
}
41 changes: 31 additions & 10 deletions core/src/test/java/feign/FeignTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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;
Expand All @@ -46,6 +47,7 @@
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;

Expand Down Expand Up @@ -489,19 +491,38 @@ static class DisableHostnameVerification {
}
}

@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<TestInterface> t1 = new HardCodedTarget<TestInterface>(TestInterface.class, "http://localhost:8080");
Target<TestInterface> t2 = new HardCodedTarget<TestInterface>(TestInterface.class, "http://localhost:8888");
Target<OtherTestInterface> t3 =
new HardCodedTarget<OtherTestInterface>(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 {
Expand Down
27 changes: 15 additions & 12 deletions ribbon/src/main/java/feign/ribbon/LoadBalancingTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package feign.ribbon;

import com.google.common.base.Objects;
import com.netflix.loadbalancer.AbstractLoadBalancer;
import com.netflix.loadbalancer.Server;

Expand All @@ -25,7 +24,6 @@
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;
Expand Down Expand Up @@ -99,18 +97,23 @@ public AbstractLoadBalancer lb() {
}
}

@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 + ")";
}
}