Issue 609:support same arity operations on rest services

This commit is contained in:
Adrian Cole 2011-06-27 02:23:14 -07:00
parent 3e80c0433c
commit a9277558cd
2 changed files with 29 additions and 14 deletions

View File

@ -178,7 +178,7 @@ public class RestAnnotationProcessor<T> {
static Map<Method, Map<Integer, Set<Annotation>>> createMethodToIndexOfParamToAnnotation(
final Class<? extends Annotation> annotation) {
return new MapMaker().makeComputingMap(new Function<Method, Map<Integer, Set<Annotation>>>() {
public Map<Integer, Set<Annotation>> apply(final Method method) {
public Map<Integer, Set<Annotation>> apply(Method method) {
return new MapMaker().makeComputingMap(new GetAnnotationsForMethodParameterIndex(method, annotation));
}
});
@ -218,7 +218,7 @@ public class RestAnnotationProcessor<T> {
static final Map<Method, Set<Integer>> methodToIndexesOfOptions = new MapMaker()
.makeComputingMap(new Function<Method, Set<Integer>>() {
public Set<Integer> apply(final Method method) {
public Set<Integer> apply(Method method) {
Builder<Integer> toReturn = ImmutableSet.<Integer> builder();
for (int index = 0; index < method.getParameterTypes().length; index++) {
Class<?> type = method.getParameterTypes()[index];
@ -307,7 +307,7 @@ public class RestAnnotationProcessor<T> {
int result = 1;
result = prime * result + ((declaringPackage == null) ? 0 : declaringPackage.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + parameterCount;
result = prime * result + parametersTypeHashCode;
return result;
}
@ -330,19 +330,22 @@ public class RestAnnotationProcessor<T> {
return false;
} else if (!name.equals(other.name))
return false;
if (parameterCount != other.parameterCount)
if (parametersTypeHashCode != other.parametersTypeHashCode)
return false;
return true;
}
private final String name;
private final int parameterCount;
private final int parametersTypeHashCode;
private final Package declaringPackage;
public MethodKey(Method method) {
this.name = method.getName();
this.declaringPackage = method.getDeclaringClass().getPackage();
this.parameterCount = method.getParameterTypes().length;
int parametersTypeHashCode = 0;
for (Class<?> param: method.getParameterTypes())
parametersTypeHashCode +=param.hashCode();
this.parametersTypeHashCode = parametersTypeHashCode;
}
}

View File

@ -307,8 +307,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
}
Caller caller = child.getInstance(Caller.class);
expect(mock.submit(requestLineEquals("GET http://howdyboys/client/1/foo HTTP/1.1"), eq(function)))
.andReturn(Futures.<Void> immediateFuture(null)).atLeastOnce();
expect(mock.submit(requestLineEquals("GET http://howdyboys/client/1/foo HTTP/1.1"), eq(function))).andReturn(
Futures.<Void> immediateFuture(null)).atLeastOnce();
replay(mock);
caller.getCallee(URI.create("http://howdyboys")).onePath("foo");
@ -854,8 +854,12 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
}
@ROWDY
@Path("/objects/{id}")
@Path("/strings/{id}")
ListenableFuture<Boolean> rowdy(@PathParam("id") String path);
@ROWDY
@Path("/ints/{id}")
ListenableFuture<Boolean> rowdy(@PathParam("id") int path);
}
static class Wrapper {
@ -866,7 +870,16 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
Method method = TestPut.class.getMethod("rowdy", String.class);
HttpRequest request = factory(TestPut.class).createRequest(method, "data");
assertRequestLineEquals(request, "ROWDY http://localhost:9999/objects/data HTTP/1.1");
assertRequestLineEquals(request, "ROWDY http://localhost:9999/strings/data HTTP/1.1");
assertNonPayloadHeadersEqual(request, "");
assertPayloadEquals(request, null, null, false);
}
public void testAlternateHttpMethodSameArity() throws SecurityException, NoSuchMethodException, IOException {
Method method = TestPut.class.getMethod("rowdy", int.class);
HttpRequest request = factory(TestPut.class).createRequest(method, "data");
assertRequestLineEquals(request, "ROWDY http://localhost:9999/ints/data HTTP/1.1");
assertNonPayloadHeadersEqual(request, "");
assertPayloadEquals(request, null, null, false);
}
@ -1051,8 +1064,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
Function<HttpResponse, Map<String, String>> parser = (Function<HttpResponse, Map<String, String>>) RestAnnotationProcessor
.createResponseParser(parserFactory, injector, method, request);
assertEquals(parser.apply(new HttpResponse(200, "ok",
newStringPayload("{\"runit\":[\"0.7.0\"]}"))), "0.7.0");
assertEquals(parser.apply(new HttpResponse(200, "ok", newStringPayload("{\"runit\":[\"0.7.0\"]}"))), "0.7.0");
assertEquals(parser.apply(new HttpResponse(200, "ok", newStringPayload("{\"runit\":[]}"))), null);
}