Transform to work with SaxParser

This commit is contained in:
Adrian Cole 2012-07-15 22:44:07 -07:00
parent 872046ed4c
commit 248c6dcb39
3 changed files with 46 additions and 40 deletions

View File

@ -109,7 +109,7 @@ public class ClassMethodArgs {
return method;
}
public Object[] getArgs() {
public @Nullable Object[] getArgs() {
return args;
}

View File

@ -28,9 +28,11 @@ import java.util.List;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpRequestFilter;
import org.jclouds.internal.ClassMethodArgs;
import org.jclouds.io.Payload;
import org.jclouds.javax.annotation.Nullable;
import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
@ -53,7 +55,8 @@ public class GeneratedHttpRequest extends HttpRequest {
protected Method javaMethod;
// args can be null, so cannot use immutable list
protected List<Object> args = Lists.newArrayList();
protected Optional<ClassMethodArgs> caller = Optional.absent();
/**
* @see GeneratedHttpRequest#getDeclaring()
*/
@ -92,17 +95,26 @@ public class GeneratedHttpRequest extends HttpRequest {
this.args.add(arg);
return self();
}
/**
* @see GeneratedHttpRequest#getCaller()
*/
public T caller(@Nullable ClassMethodArgs caller) {
this.caller = Optional.fromNullable(caller);
return self();
}
public GeneratedHttpRequest build() {
return new GeneratedHttpRequest(method, endpoint, headers.build(), payload, declaring, javaMethod,
args, skips.build(), filters.build());
args, skips.build(), filters.build(), caller);
}
public T fromGeneratedHttpRequest(GeneratedHttpRequest in) {
return super.fromHttpRequest(in)
.declaring(in.getDeclaring())
.javaMethod(in.getJavaMethod())
.args(in.getArgs());
.args(in.getArgs())
.caller(in.getCaller().orNull());
}
}
@ -116,15 +128,17 @@ public class GeneratedHttpRequest extends HttpRequest {
private final Class<?> declaring;
private final Method javaMethod;
private final List<Object> args;
private final Optional<ClassMethodArgs> caller;
protected GeneratedHttpRequest(String method, URI endpoint, Multimap<String, String> headers, @Nullable Payload payload,
Class<?> declaring, Method javaMethod, Iterable<Object> args, Iterable<Character> skips,
Iterable<HttpRequestFilter> filters) {
Iterable<HttpRequestFilter> filters, Optional<ClassMethodArgs> caller) {
super(method, endpoint, headers, payload, skips, filters);
this.declaring = checkNotNull(declaring, "declaring");
this.javaMethod = checkNotNull(javaMethod, "javaMethod");
// TODO make immutable. ImmutableList.of() doesn't accept nulls
this.args = Lists.newArrayList(checkNotNull(args, "args"));
this.caller = checkNotNull(caller, "caller");
}
public Class<?> getDeclaring() {
@ -139,4 +153,7 @@ public class GeneratedHttpRequest extends HttpRequest {
return Collections.unmodifiableList(args);
}
public Optional<ClassMethodArgs> getCaller() {
return caller;
}
}

View File

@ -138,6 +138,7 @@ import org.jclouds.util.Strings2;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
@ -269,6 +270,7 @@ public class RestAnnotationProcessor<T> {
return createResponseParser(parserFactory, injector, method, request);
}
@SuppressWarnings("unchecked")
@VisibleForTesting
public static Function<HttpResponse, ?> createResponseParser(ParseSax.Factory parserFactory, Injector injector,
Method method, HttpRequest request) {
@ -282,6 +284,13 @@ public class RestAnnotationProcessor<T> {
if (transformer instanceof InvocationContext<?>) {
((InvocationContext<?>) transformer).setContext(request);
}
if (method.isAnnotationPresent(Transform.class)) {
Function<?, ?> wrappingTransformer = injector.getInstance(method.getAnnotation(Transform.class).value());
if (wrappingTransformer instanceof InvocationContext<?>) {
((InvocationContext<?>) wrappingTransformer).setContext(request);
}
transformer = Functions.compose(Function.class.cast(wrappingTransformer), transformer);
}
return transformer;
}
@ -299,11 +308,6 @@ public class RestAnnotationProcessor<T> {
} else {
transformer = injector.getInstance(getParserOrThrowException(method));
}
if (method.isAnnotationPresent(Transform.class)) {
transformer = Functions
.compose(Function.class.cast(injector.getInstance(method.getAnnotation(Transform.class).value())),
transformer);
}
return transformer;
}
@ -352,38 +356,19 @@ public class RestAnnotationProcessor<T> {
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((declaringClass == null) ? 0 : declaringClass.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + parametersTypeHashCode;
return result;
return Objects.hashCode(declaringClass, name, parametersTypeHashCode);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MethodKey other = (MethodKey) obj;
if (declaringClass == null) {
if (other.declaringClass != null)
return false;
} else if (!declaringClass.equals(other.declaringClass))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (parametersTypeHashCode != other.parametersTypeHashCode)
return false;
return true;
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
MethodKey that = MethodKey.class.cast(obj);
return Objects.equal(this.declaringClass, that.declaringClass)
&& Objects.equal(this.name, that.name)
&& Objects.equal(this.parametersTypeHashCode, that.parametersTypeHashCode);
}
private final String name;
private final int parametersTypeHashCode;
private final Class<?> declaringClass;
@ -450,8 +435,12 @@ public class RestAnnotationProcessor<T> {
requestBuilder.method(getHttpMethodOrConstantOrThrowException(method));
}
requestBuilder.declaring(declaring).javaMethod(method).args(args).skips(skips);
requestBuilder.filters(getFiltersIfAnnotated(method));
requestBuilder.declaring(declaring)
.javaMethod(method)
.args(args)
.caller(caller)
.skips(skips)
.filters(getFiltersIfAnnotated(method));
UriBuilder builder = uriBuilderProvider.get().uri(endpoint);