Skip to content

Commit 8bd202d

Browse files
committed
Merge pull request #11 from cretz/issue-9
Support Object methods and minor spacing changes. Fixes issue #9
2 parents 68fa7ce + fdc7456 commit 8bd202d

4 files changed

Lines changed: 32 additions & 4 deletions

File tree

gen/src/main/java/com/softlayer/api/gen/ClassWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,9 @@ public ClassWriter emitServiceMethod(TypeClass.Property property, boolean async)
324324

325325
// Instance is only required if it's not an account property
326326
if ("SoftLayer_Account".equals(type.meta.name)) {
327-
emitAnnotation(TYPE_API_METHOD);
327+
emitAnnotation(TYPE_API_METHOD);
328328
} else {
329-
emitAnnotationWithAttrs(TYPE_API_METHOD, "instanceRequired", true);
329+
emitAnnotationWithAttrs(TYPE_API_METHOD, "instanceRequired", true);
330330
}
331331
} else {
332332
// Otherwise, just a javadoc link

src/main/java/com/softlayer/api/RestApiClient.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,10 +459,25 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
459459
return invokeService(method, args);
460460
} else if (ServiceAsync.class.isAssignableFrom(method.getDeclaringClass())) {
461461
return invokeServiceAsync(method, args);
462+
} else if (method.getDeclaringClass() == Object.class) {
463+
return method.invoke(this, args);
462464
} else {
463465
// Should not be possible
464466
throw new RuntimeException("Unrecognized method: " + method);
465467
}
466468
}
469+
470+
@Override
471+
public boolean equals(Object obj) {
472+
return Proxy.isProxyClass(obj.getClass()) && obj.hashCode() == hashCode();
473+
}
474+
475+
@Override
476+
public String toString() {
477+
if (id == null) {
478+
return "Service: " + serviceClass.getAnnotation(ApiService.class).value();
479+
}
480+
return "Service: " + serviceClass.getAnnotation(ApiService.class).value() + " with ID " + id;
481+
}
467482
}
468483
}

src/test/java/com/softlayer/api/RestApiClientTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.io.ByteArrayOutputStream;
66
import java.io.PrintStream;
7+
import java.lang.reflect.Proxy;
78
import java.net.URLEncoder;
89
import java.util.Collections;
910
import java.util.GregorianCalendar;
@@ -428,4 +429,16 @@ public void onSuccess(String value) {
428429
assertTrue(http.invokeAsyncCallbackCalled);
429430
assertTrue(successCalled.get());
430431
}
432+
433+
@Test
434+
public void testNormalObjectMethodsOnService() {
435+
RestApiClient client = new RestApiClient("http://example.com/");
436+
TestEntity.Service service = TestEntity.service(client);
437+
assertEquals("Service: SoftLayer_TestEntity", service.toString());
438+
assertEquals("Service: SoftLayer_TestEntity with ID 5", TestEntity.service(client, 5L).toString());
439+
assertTrue(Proxy.isProxyClass(service.getClass()));
440+
assertEquals(service.hashCode(), service.hashCode());
441+
assertTrue(service.equals(service));
442+
443+
}
431444
}

src/test/java/com/softlayer/api/json/GsonJsonMarshallerFactoryTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ public void testWrite() throws Exception {
105105
expected.put("baz", null);
106106
int offsetMinutes = TimeZone.getDefault().getOffset(obj.getDate().getTimeInMillis()) / 60000;
107107
String expectedTimeZone =
108-
(offsetMinutes < 0 ? '-' : '+') +
109-
String.format("%1$02d:%2$02d", Math.abs(offsetMinutes / 60), Math.abs(offsetMinutes % 60));
108+
(offsetMinutes < 0 ? '-' : '+') +
109+
String.format("%1$02d:%2$02d", Math.abs(offsetMinutes / 60), Math.abs(offsetMinutes % 60));
110110
expected.put("date", "1984-02-25T20:15:25" + expectedTimeZone);
111111
Map<String, Object> childMap = new HashMap<String, Object>();
112112
childMap.put("complexType", "SoftLayer_TestEntity");

0 commit comments

Comments
 (0)