[issue 795] De-generified HttpMessage.Builder and HttpRequest.Builder, prevented the "ambiguous method" compile errors caused by GeneratedHttpRequest.builder() and from() (see [issue 461]) and added a convenience requestBuilder() and fromRequest() methods with the former signatures of builder() and from() resp.

This commit is contained in:
Andrew Phillips 2011-12-24 16:16:31 +00:00
parent 53563179a8
commit 12cfbfe55c
7 changed files with 68 additions and 54 deletions

View File

@ -36,31 +36,30 @@ import com.google.common.collect.Multimap;
* @author Adrian Cole
*/
public class HttpMessage extends PayloadEnclosingImpl {
public static Builder<? extends HttpMessage> builder() {
return new Builder<HttpMessage>();
public static Builder builder() {
return new Builder();
}
public static class Builder<T extends HttpMessage> {
public static class Builder {
protected Payload payload;
protected Multimap<String, String> headers = ImmutableMultimap.of();
public Builder<T> payload(Payload payload) {
public Builder payload(Payload payload) {
this.payload = payload;
return this;
}
public Builder<T> headers(Multimap<String, String> headers) {
public Builder headers(Multimap<String, String> headers) {
this.headers = ImmutableMultimap.copyOf(checkNotNull(headers, "headers"));
return this;
}
@SuppressWarnings("unchecked")
public T build() {
return (T) new HttpMessage(payload, headers);
public HttpMessage build() {
return new HttpMessage(payload, headers);
}
public static <X extends HttpMessage> Builder<X> from(X input) {
return new Builder<X>().payload(input.getPayload()).headers(input.getHeaders());
public static Builder from(HttpMessage input) {
return new Builder().payload(input.getPayload()).headers(input.getHeaders());
}
}
@ -85,7 +84,7 @@ public class HttpMessage extends PayloadEnclosingImpl {
return (values.size() >= 1) ? values.iterator().next() : null;
}
public Builder<? extends HttpMessage> toBuilder() {
public Builder toBuilder() {
return Builder.from(this);
}

View File

@ -39,32 +39,32 @@ import com.google.common.collect.Multimap;
* @author Adrian Cole
*/
public class HttpRequest extends HttpMessage {
public static Builder<? extends HttpRequest> builder() {
return new Builder<HttpRequest>();
public static Builder builder() {
return new Builder();
}
public static class Builder<T extends HttpRequest> extends HttpMessage.Builder<T> {
public static class Builder extends HttpMessage.Builder {
protected String method;
protected URI endpoint;
protected char[] skips = new char[] {};
protected List<HttpRequestFilter> requestFilters = ImmutableList.of();
public Builder<T> filters(List<HttpRequestFilter> requestFilters) {
public Builder filters(List<HttpRequestFilter> requestFilters) {
this.requestFilters = ImmutableList.copyOf(checkNotNull(requestFilters, "requestFilters"));
return this;
}
public Builder<T> method(String method) {
public Builder method(String method) {
this.method = checkNotNull(method, "method");
return this;
}
public Builder<T> endpoint(URI endpoint) {
public Builder endpoint(URI endpoint) {
this.endpoint = checkNotNull(endpoint, "endpoint");
return this;
}
public Builder<T> skips(char[] skips) {
public Builder skips(char[] skips) {
char[] retval = new char[checkNotNull(skips, "skips").length];
System.arraycopy(skips, 0, retval, 0, skips.length);
this.skips = retval;
@ -72,23 +72,22 @@ public class HttpRequest extends HttpMessage {
}
@Override
public Builder<T> payload(Payload payload) {
return (Builder<T>) super.payload(payload);
public Builder payload(Payload payload) {
return (Builder) super.payload(payload);
}
@Override
public Builder<T> headers(Multimap<String, String> headers) {
return (Builder<T>) super.headers(headers);
public Builder headers(Multimap<String, String> headers) {
return (Builder) super.headers(headers);
}
@Override
@SuppressWarnings("unchecked")
public T build() {
return (T) new HttpRequest(method, endpoint, skips, requestFilters, payload, headers);
public HttpRequest build() {
return new HttpRequest(method, endpoint, skips, requestFilters, payload, headers);
}
public static <X extends HttpRequest> Builder<X> from(X input) {
return new Builder<X>().method(input.getMethod()).endpoint(input.getEndpoint()).skips(input.getSkips())
public static Builder from(HttpRequest input) {
return new Builder().method(input.getMethod()).endpoint(input.getEndpoint()).skips(input.getSkips())
.filters(input.getFilters()).payload(input.getPayload()).headers(input.getHeaders());
}
@ -189,7 +188,7 @@ public class HttpRequest extends HttpMessage {
}
@Override
public Builder<? extends HttpRequest> toBuilder() {
public Builder toBuilder() {
return Builder.from(this);
}

View File

@ -35,7 +35,7 @@ public class HttpResponse extends HttpMessage {
return new Builder();
}
public static class Builder extends HttpMessage.Builder<HttpResponse> {
public static class Builder extends HttpMessage.Builder {
private int statusCode;
private String message;

View File

@ -40,11 +40,21 @@ import com.google.common.collect.Multimap;
* @author Adrian Cole
*/
public class GeneratedHttpRequest<T> extends HttpRequest {
public static <T> Builder<T> builder() {
return new Builder<T>();
public static Builder<?> builder() {
// empty builder, so can be safely cast to Builder<T> by the caller
return new Builder<Object>();
}
public static class Builder<T> extends HttpRequest.Builder<GeneratedHttpRequest<T>> {
/*
* Convenience method - cannot have the same signature as builder() - see
* http://code.google.com/p/jclouds/issues/detail?id=795
*/
@SuppressWarnings("unchecked")
public static <T> Builder<T> requestBuilder() {
return (Builder<T>) builder();
}
public static class Builder<T> extends HttpRequest.Builder {
protected Class<T> declaring;
protected Method javaMethod;
protected List<Object> args;
@ -69,31 +79,37 @@ public class GeneratedHttpRequest<T> extends HttpRequest {
return this;
}
@SuppressWarnings("unchecked")
@Override
public Builder<T> filters(List<HttpRequestFilter> requestFilters) {
return (Builder<T>) super.filters(requestFilters);
}
@SuppressWarnings("unchecked")
@Override
public Builder<T> method(String method) {
return (Builder<T>) super.method(method);
}
@SuppressWarnings("unchecked")
@Override
public Builder<T> endpoint(URI endpoint) {
return (Builder<T>) super.endpoint(endpoint);
}
@SuppressWarnings("unchecked")
@Override
public Builder<T> skips(char[] skips) {
return (Builder<T>) super.skips(skips);
}
@SuppressWarnings("unchecked")
@Override
public Builder<T> payload(Payload payload) {
return (Builder<T>) super.payload(payload);
}
@SuppressWarnings("unchecked")
@Override
public Builder<T> headers(Multimap<String, String> headers) {
return (Builder<T>) super.headers(headers);
@ -105,11 +121,24 @@ public class GeneratedHttpRequest<T> extends HttpRequest {
javaMethod, args);
}
public static <Y> Builder<Y> from(HttpRequest input) {
return new Builder<Y>().method(input.getMethod()).endpoint(input.getEndpoint()).skips(input.getSkips())
public static Builder<?> from(HttpRequest input) {
/*
* State added to builder will not conflict with return type so caller can
* safely cast result to Builder<T>
*/
return new Builder<Object>().method(input.getMethod()).endpoint(input.getEndpoint()).skips(input.getSkips())
.filters(input.getFilters()).payload(input.getPayload()).headers(input.getHeaders());
}
/*
* Convenience method - cannot have the same signature as from(HttpRequest) - see
* http://code.google.com/p/jclouds/issues/detail?id=795
*/
@SuppressWarnings("unchecked")
public static <Y> Builder<Y> fromRequest(HttpRequest input) {
return (Builder<Y>) from(input);
}
public static <Y> Builder<Y> from(GeneratedHttpRequest<Y> input) {
return new Builder<Y>().method(input.getMethod()).endpoint(input.getEndpoint()).skips(input.getSkips())
.filters(input.getFilters()).payload(input.getPayload()).headers(input.getHeaders())

View File

@ -420,18 +420,10 @@ public class RestAnnotationProcessor<T> {
GeneratedHttpRequest.Builder<T> requestBuilder;
HttpRequest r = RestAnnotationProcessor.findHttpRequestInArgs(args);
if (r != null) {
requestBuilder = GeneratedHttpRequest.Builder.<T> from(r);
requestBuilder = GeneratedHttpRequest.Builder.fromRequest(r);
endpoint = r.getEndpoint();
} else {
/*
* Can't use GeneratedHttpRequest.<T>builder() because the T is too
* general for the compiler to be able to distinguish between
* GeneratedHttpRequest.builder() and HttpMessage.builder() - the
* latter is available because GHR inherits from HM.
*
* See http://code.google.com/p/jclouds/issues/detail?id=461
*/
requestBuilder = new GeneratedHttpRequest.Builder<T>();
requestBuilder = GeneratedHttpRequest.requestBuilder();
requestBuilder.method(getHttpMethodOrConstantOrThrowException(method));
}

View File

@ -54,8 +54,7 @@ public class BindMapToStringPayloadTest {
@Test
public void testCorrect() throws SecurityException, NoSuchMethodException {
Method testPayload = TestPayload.class.getMethod("testPayload", String.class);
// can't use GHR.builder() - see http://code.google.com/p/jclouds/issues/detail?id=461
GeneratedHttpRequest<TestPayload> request = new GeneratedHttpRequest.Builder<TestPayload>()
GeneratedHttpRequest<TestPayload> request = GeneratedHttpRequest.<TestPayload>requestBuilder()
.declaring(TestPayload.class).javaMethod(testPayload).args(ImmutableList.<Object> of("robot"))
.method(HttpMethod.POST).endpoint(URI.create("http://localhost")).build();
@ -69,8 +68,7 @@ public class BindMapToStringPayloadTest {
@Test(expectedExceptions = IllegalArgumentException.class)
public void testMustHavePayloadAnnotation() throws SecurityException, NoSuchMethodException {
Method noPayload = TestPayload.class.getMethod("noPayload", String.class);
// can't use GHR.builder() - see http://code.google.com/p/jclouds/issues/detail?id=461
GeneratedHttpRequest<TestPayload> request = new GeneratedHttpRequest.Builder<TestPayload>()
GeneratedHttpRequest<TestPayload> request = GeneratedHttpRequest.<TestPayload>requestBuilder()
.declaring(TestPayload.class).javaMethod(noPayload).args(ImmutableList.<Object> of("robot"))
.method(HttpMethod.POST).endpoint(URI.create("http://localhost")).build();
binder().bindToRequest(request, ImmutableMap.of("fooble", "robot"));

View File

@ -1816,8 +1816,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void oneTransformerWithContext() throws SecurityException, NoSuchMethodException {
RestAnnotationProcessor<TestTransformers> processor = factory(TestTransformers.class);
Method method = TestTransformers.class.getMethod("oneTransformerWithContext");
// can't use GHR.builder() - see http://code.google.com/p/jclouds/issues/detail?id=461
GeneratedHttpRequest<TestTransformers> request = new GeneratedHttpRequest.Builder<TestTransformers>().method("GET")
GeneratedHttpRequest<TestTransformers> request = GeneratedHttpRequest.<TestTransformers>requestBuilder().method("GET")
.endpoint(URI.create("http://localhost")).declaring(TestTransformers.class).javaMethod(method)
.args(new Object[] {}).build();
Function<HttpResponse, ?> transformer = processor.createResponseParser(method, request);
@ -2349,8 +2348,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testCreateJAXBResponseParserWithAnnotation() throws SecurityException, NoSuchMethodException {
RestAnnotationProcessor<TestJAXBResponseParser> processor = factory(TestJAXBResponseParser.class);
Method method = TestJAXBResponseParser.class.getMethod("jaxbGetWithAnnotation");
// can't use GHR.builder() - see http://code.google.com/p/jclouds/issues/detail?id=461
GeneratedHttpRequest<TestJAXBResponseParser> request = new GeneratedHttpRequest.Builder<TestJAXBResponseParser>()
GeneratedHttpRequest<TestJAXBResponseParser> request = GeneratedHttpRequest.<TestJAXBResponseParser>requestBuilder()
.method("GET").endpoint(URI.create("http://localhost")).declaring(TestJAXBResponseParser.class)
.javaMethod(method).args(new Object[] {}).build();
Function<HttpResponse, ?> transformer = processor.createResponseParser(method, request);
@ -2361,8 +2359,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testCreateJAXBResponseParserWithAcceptHeader() throws SecurityException, NoSuchMethodException {
RestAnnotationProcessor<TestJAXBResponseParser> processor = factory(TestJAXBResponseParser.class);
Method method = TestJAXBResponseParser.class.getMethod("jaxbGetWithAcceptHeader");
// can't use GHR.builder() - see http://code.google.com/p/jclouds/issues/detail?id=461
GeneratedHttpRequest<TestJAXBResponseParser> request = new GeneratedHttpRequest.Builder<TestJAXBResponseParser>()
GeneratedHttpRequest<TestJAXBResponseParser> request = GeneratedHttpRequest.<TestJAXBResponseParser>requestBuilder()
.method("GET").endpoint(URI.create("http://localhost")).declaring(TestJAXBResponseParser.class)
.javaMethod(method).args(new Object[] {}).build();
Function<HttpResponse, ?> transformer = processor.createResponseParser(method, request);