Skip to content

Commit 32c32e7

Browse files
committed
Merge pull request OpenFeign#142 from Netflix/adrian.target-tostring
Adds toString to Targets. Normalizes equals/hashCode.
2 parents 246e01b + 96fa779 commit 32c32e7

4 files changed

Lines changed: 78 additions & 45 deletions

File tree

‎core/src/main/java/feign/ReflectiveFeign.java‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,27 +83,28 @@ static class FeignInvocationHandler implements InvocationHandler {
8383
} catch (IllegalArgumentException e) {
8484
return false;
8585
}
86-
}
87-
if ("hashCode".equals(method.getName())) {
86+
} else if ("hashCode".equals(method.getName())) {
8887
return hashCode();
88+
} else if ("toString".equals(method.getName())) {
89+
return toString();
8990
}
9091
return dispatch.get(method).invoke(args);
9192
}
9293

93-
@Override public int hashCode() {
94-
return target.hashCode();
95-
}
96-
97-
@Override public boolean equals(Object other) {
98-
if (other instanceof FeignInvocationHandler) {
99-
FeignInvocationHandler that = (FeignInvocationHandler) other;
100-
return this.target.equals(that.target);
94+
@Override public boolean equals(Object obj) {
95+
if (obj instanceof FeignInvocationHandler) {
96+
FeignInvocationHandler other = (FeignInvocationHandler) obj;
97+
return target.equals(other.target);
10198
}
10299
return false;
103100
}
104101

102+
@Override public int hashCode() {
103+
return target.hashCode();
104+
}
105+
105106
@Override public String toString() {
106-
return "target(" + target + ")";
107+
return target.toString();
107108
}
108109
}
109110

‎core/src/main/java/feign/Target.java‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package feign;
1717

18-
import java.util.Arrays;
19-
2018
import static feign.Util.checkNotNull;
2119
import static feign.Util.emptyToNull;
2220

@@ -95,19 +93,29 @@ public HardCodedTarget(Class<T> type, String name, String url) {
9593
return input.request();
9694
}
9795

96+
@Override public boolean equals(Object obj) {
97+
if (obj instanceof HardCodedTarget) {
98+
HardCodedTarget<?> other = (HardCodedTarget) obj;
99+
return type.equals(other.type)
100+
&& name.equals(other.name)
101+
&& url.equals(other.url);
102+
}
103+
return false;
104+
}
105+
98106
@Override public int hashCode() {
99-
return Arrays.hashCode(new Object[]{type, name, url});
107+
int result = 17;
108+
result = 31 * result + type.hashCode();
109+
result = 31 * result + name.hashCode();
110+
result = 31 * result + url.hashCode();
111+
return result;
100112
}
101113

102-
@Override public boolean equals(Object obj) {
103-
if (obj == null)
104-
return false;
105-
if (this == obj)
106-
return true;
107-
if (HardCodedTarget.class != obj.getClass())
108-
return false;
109-
HardCodedTarget<?> that = HardCodedTarget.class.cast(obj);
110-
return this.type.equals(that.type) && this.name.equals(that.name) && this.url.equals(that.url);
114+
@Override public String toString() {
115+
if (name.equals(url)) {
116+
return "HardCodedTarget(type=" + type.getSimpleName() + ", url=" + url + ")";
117+
}
118+
return "HardCodedTarget(type=" + type.getSimpleName() + ", name=" + name + ", url=" + url + ")";
111119
}
112120
}
113121
}

‎core/src/test/java/feign/FeignTest.java‎

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.mockwebserver.SocketPolicy;
2525
import dagger.Module;
2626
import dagger.Provides;
27+
import feign.Target.HardCodedTarget;
2728
import feign.codec.Decoder;
2829
import feign.codec.Encoder;
2930
import feign.codec.ErrorDecoder;
@@ -46,6 +47,7 @@
4647
import static feign.Util.UTF_8;
4748
import static org.testng.Assert.assertEquals;
4849
import static org.testng.Assert.assertFalse;
50+
import static org.testng.Assert.assertNotEquals;
4951
import static org.testng.Assert.assertNull;
5052
import static org.testng.Assert.assertTrue;
5153

@@ -489,19 +491,38 @@ static class DisableHostnameVerification {
489491
}
490492
}
491493

492-
@Test public void equalsAndHashCodeWork() {
493-
TestInterface i1 = Feign.create(TestInterface.class, "http://localhost:8080", new TestInterface.Module());
494-
TestInterface i2 = Feign.create(TestInterface.class, "http://localhost:8080", new TestInterface.Module());
495-
TestInterface i3 = Feign.create(TestInterface.class, "http://localhost:8888", new TestInterface.Module());
496-
OtherTestInterface i4 = Feign.create(OtherTestInterface.class, "http://localhost:8080", new TestInterface.Module());
497-
498-
assertTrue(i1.equals(i1));
499-
assertTrue(i1.equals(i2));
500-
assertFalse(i1.equals(i3));
501-
assertFalse(i1.equals(i4));
494+
@Test public void equalsHashCodeAndToStringWork() {
495+
Target<TestInterface> t1 = new HardCodedTarget<TestInterface>(TestInterface.class, "http://localhost:8080");
496+
Target<TestInterface> t2 = new HardCodedTarget<TestInterface>(TestInterface.class, "http://localhost:8888");
497+
Target<OtherTestInterface> t3 =
498+
new HardCodedTarget<OtherTestInterface>(OtherTestInterface.class, "http://localhost:8080");
499+
TestInterface i1 = Feign.builder().target(t1);
500+
TestInterface i2 = Feign.builder().target(t1);
501+
TestInterface i3 = Feign.builder().target(t2);
502+
OtherTestInterface i4 = Feign.builder().target(t3);
503+
504+
assertEquals(i1, i1);
505+
assertEquals(i1, i2);
506+
assertNotEquals(i1, i3);
507+
assertNotEquals(i1, i4);
502508

503509
assertEquals(i1.hashCode(), i1.hashCode());
504510
assertEquals(i1.hashCode(), i2.hashCode());
511+
assertNotEquals(i1.hashCode(), i3.hashCode());
512+
assertNotEquals(i1.hashCode(), i4.hashCode());
513+
514+
assertEquals(i1.hashCode(), t1.hashCode());
515+
assertEquals(i3.hashCode(), t2.hashCode());
516+
assertEquals(i4.hashCode(), t3.hashCode());
517+
518+
assertEquals(i1.toString(), i1.toString());
519+
assertEquals(i1.toString(), i2.toString());
520+
assertNotEquals(i1.toString(), i3.toString());
521+
assertNotEquals(i1.toString(), i4.toString());
522+
523+
assertEquals(i1.toString(), t1.toString());
524+
assertEquals(i3.toString(), t2.toString());
525+
assertEquals(i4.toString(), t3.toString());
505526
}
506527

507528
@Test public void decodeLogicSupportsByteArray() throws Exception {

‎ribbon/src/main/java/feign/ribbon/LoadBalancingTarget.java‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package feign.ribbon;
1717

18-
import com.google.common.base.Objects;
1918
import com.netflix.loadbalancer.AbstractLoadBalancer;
2019
import com.netflix.loadbalancer.Server;
2120

@@ -25,7 +24,6 @@
2524
import feign.RequestTemplate;
2625
import feign.Target;
2726

28-
import static com.google.common.base.Objects.equal;
2927
import static com.netflix.client.ClientFactory.getNamedLoadBalancer;
3028
import static feign.Util.checkNotNull;
3129
import static java.lang.String.format;
@@ -99,18 +97,23 @@ public AbstractLoadBalancer lb() {
9997
}
10098
}
10199

100+
@Override public boolean equals(Object obj) {
101+
if (obj instanceof LoadBalancingTarget) {
102+
LoadBalancingTarget<?> other = (LoadBalancingTarget) obj;
103+
return type.equals(other.type)
104+
&& name.equals(other.name);
105+
}
106+
return false;
107+
}
108+
102109
@Override public int hashCode() {
103-
return Objects.hashCode(type, name);
110+
int result = 17;
111+
result = 31 * result + type.hashCode();
112+
result = 31 * result + name.hashCode();
113+
return result;
104114
}
105115

106-
@Override public boolean equals(Object obj) {
107-
if (obj == null)
108-
return false;
109-
if (this == obj)
110-
return true;
111-
if (LoadBalancingTarget.class != obj.getClass())
112-
return false;
113-
LoadBalancingTarget<?> that = LoadBalancingTarget.class.cast(obj);
114-
return equal(this.type, that.type) && equal(this.name, that.name);
116+
@Override public String toString() {
117+
return "LoadBalancingTarget(type=" + type.getSimpleName() + ", name=" + name + ")";
115118
}
116119
}

0 commit comments

Comments
 (0)