diff --git a/core/src/main/java/org/jclouds/internal/BaseView.java b/core/src/main/java/org/jclouds/internal/BaseView.java index 50c191e861..87e7d5fce0 100644 --- a/core/src/main/java/org/jclouds/internal/BaseView.java +++ b/core/src/main/java/org/jclouds/internal/BaseView.java @@ -70,11 +70,18 @@ public abstract class BaseView extends ForwardingObject implements View { public A unwrapApi(Class apiClass) { checkArgument(ApiContext.class.isAssignableFrom(backendType.getRawType()), "backend type: %s should be an ApiContext", backendType); - TypeToken> contextToken = new TypeToken>(delegate().getClass()) { + /* + * Revert back to ApiContext...where(A, apiClass) as soon as + * https://code.google.com/p/guava-libraries/issues/detail?id=1635 + * is resolved. + */ + TypeToken> contextToken = new TypeToken>() { private static final long serialVersionUID = 1L; - }.where(new TypeParameter() { - }, TypeToken.of(apiClass)); - return unwrap(contextToken).getApi(); + }; + Closeable api = unwrap(contextToken).getApi(); + checkArgument(apiClass.isAssignableFrom(api.getClass()), + "API type: %s not assignable to %s", api.getClass(), apiClass); + return apiClass.cast(api); } @Override diff --git a/core/src/main/java/org/jclouds/rest/config/BinderUtils.java b/core/src/main/java/org/jclouds/rest/config/BinderUtils.java index bf21dab495..6a79d5eaac 100644 --- a/core/src/main/java/org/jclouds/rest/config/BinderUtils.java +++ b/core/src/main/java/org/jclouds/rest/config/BinderUtils.java @@ -21,6 +21,9 @@ import com.google.common.reflect.TypeToken; import com.google.inject.Binder; import com.google.inject.TypeLiteral; +import org.jclouds.util.TypeToken2; +import org.jclouds.util.TypeToken2.TypeParameter2; + /** * * @author Adrian Cole @@ -99,13 +102,13 @@ public class BinderUtils { @Deprecated @SuppressWarnings({ "unchecked", "serial" }) private static void bindHttpApiProvider(Binder binder, Class sync, Class async) { - TypeToken> token = new TypeToken>() { - }.where(new TypeParameter() { - }, sync).where(new TypeParameter() { + TypeToken> token = new TypeToken2>() { + }.where(new TypeParameter2() { + }, sync, new TypeParameter2() { }, async); binder.bind(sync).toProvider(TypeLiteral.class.cast(TypeLiteral.get(token.getType()))); } - + /** * adds an explicit binding for an interface which synchronously blocks on * similar calls to an {@code async} type. @@ -139,9 +142,9 @@ public class BinderUtils { @Deprecated @SuppressWarnings({ "unchecked", "serial" }) private static void bindCallGetOnFutures(Binder binder, Class sync, Class async) { - TypeToken> token = new TypeToken>() { - }.where(new TypeParameter() { - }, sync).where(new TypeParameter() { + TypeToken> token = new TypeToken2>() { + }.where(new TypeParameter2() { + }, sync, new TypeParameter2() { }, async); binder.bind(sync).toProvider(TypeLiteral.class.cast(TypeLiteral.get(token.getType()))); } diff --git a/core/src/main/java/org/jclouds/rest/internal/BaseRestApiMetadata.java b/core/src/main/java/org/jclouds/rest/internal/BaseRestApiMetadata.java index efc3002248..8af312d14c 100644 --- a/core/src/main/java/org/jclouds/rest/internal/BaseRestApiMetadata.java +++ b/core/src/main/java/org/jclouds/rest/internal/BaseRestApiMetadata.java @@ -24,10 +24,11 @@ import java.util.Properties; import org.jclouds.apis.ApiMetadata; import org.jclouds.apis.internal.BaseApiMetadata; import org.jclouds.rest.RestApiMetadata; +import org.jclouds.util.TypeToken2; +import org.jclouds.util.TypeToken2.TypeParameter2; import com.google.common.annotations.Beta; import com.google.common.base.Objects.ToStringHelper; -import com.google.common.reflect.TypeParameter; import com.google.common.reflect.TypeToken; /** @@ -56,10 +57,10 @@ public abstract class BaseRestApiMetadata extends BaseApiMetadata implements Res } public static TypeToken> contextToken(TypeToken apiToken, TypeToken asyncApiToken) { - return new TypeToken>() { + return new TypeToken2>() { private static final long serialVersionUID = 1L; - }.where(new TypeParameter() { - }, apiToken).where(new TypeParameter() { + }.where(new TypeParameter2() { + }, apiToken, new TypeParameter2() { }, asyncApiToken); } diff --git a/core/src/main/java/org/jclouds/util/TypeToken2.java b/core/src/main/java/org/jclouds/util/TypeToken2.java new file mode 100644 index 0000000000..99e47e5c38 --- /dev/null +++ b/core/src/main/java/org/jclouds/util/TypeToken2.java @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jclouds.util; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.lang.reflect.TypeVariable; +import java.util.Map; + +import org.jclouds.reflect.Reflection2; + +import com.google.common.collect.ImmutableMap; +import com.google.common.reflect.Invokable; +import com.google.common.reflect.TypeParameter; +import com.google.common.reflect.TypeResolver; +import com.google.common.reflect.TypeToken; + +/* + * FIXME: remove this class ASAP! + * + * Evil stuff, adapted from https://code.google.com/p/guava-libraries/source/browse/guava/src/com/google/common/reflect/TypeToken.java#236. + * See https://issues.apache.org/jira/browse/JCLOUDS-427 and + * https://code.google.com/p/guava-libraries/issues/detail?id=1635 + */ +public class TypeToken2 extends TypeToken { + private static final long serialVersionUID = 1L; + + @SuppressWarnings("unchecked") + public TypeToken where(TypeParameter2 typeParam1, + TypeToken typeArg1, TypeParameter2 typeParam2, TypeToken typeArg2) { + // resolving both parameters in one shot seems to work around 1635 + TypeResolver resolver = new TypeResolver(); + // where(Map) is package-private in TypeResolver + Invokable whereWithMap = + Reflection2.method(TypeResolver.class, "where", + Map.class); + try { + resolver = whereWithMap.invoke(resolver, ImmutableMap.of( + typeParam1.getTypeVariable(), typeArg1.getType(), + typeParam2.getTypeVariable(), typeArg2.getType())); + } catch (IllegalAccessException exception) { + // should never happen + throw new IllegalStateException(exception); + } catch (InvocationTargetException exception) { + // should never happen + throw new IllegalStateException(exception); + } + return (TypeToken) TypeToken.of(resolver.resolveType(getType())); + } + + public TypeToken where(TypeParameter2 typeParam1, Class typeArg1, + TypeParameter2 typeParam2, Class typeArg2) { + return where(typeParam1, of(typeArg1), typeParam2, of(typeArg2)); + } + + public static abstract class TypeParameter2 extends TypeParameter { + TypeVariable getTypeVariable() { + // duplicated from TypeCapture, where it's package-private + Type superclass = getClass().getGenericSuperclass(); + return (TypeVariable) ((ParameterizedType) superclass).getActualTypeArguments()[0]; + } + } +} \ No newline at end of file