mirror of https://github.com/apache/jclouds.git
JCLOUDS-427: Avoiding Guava reflection code broken in Java 7u51
- Adding a version of TypeToken.where that replaces two parameters in one go - Avoiding TypeToken type parameter replacement with wildcard bounds Works around https://code.google.com/p/guava-libraries/issues/detail?id=1635
This commit is contained in:
parent
859ca920c6
commit
597615977e
|
@ -70,11 +70,18 @@ public abstract class BaseView extends ForwardingObject implements View {
|
||||||
public <A extends Closeable> A unwrapApi(Class<A> apiClass) {
|
public <A extends Closeable> A unwrapApi(Class<A> apiClass) {
|
||||||
checkArgument(ApiContext.class.isAssignableFrom(backendType.getRawType()),
|
checkArgument(ApiContext.class.isAssignableFrom(backendType.getRawType()),
|
||||||
"backend type: %s should be an ApiContext", backendType);
|
"backend type: %s should be an ApiContext", backendType);
|
||||||
TypeToken<ApiContext<? extends A>> contextToken = new TypeToken<ApiContext<? extends A>>(delegate().getClass()) {
|
/*
|
||||||
|
* Revert back to ApiContext<? extends A>...where(A, apiClass) as soon as
|
||||||
|
* https://code.google.com/p/guava-libraries/issues/detail?id=1635
|
||||||
|
* is resolved.
|
||||||
|
*/
|
||||||
|
TypeToken<ApiContext<? extends Closeable>> contextToken = new TypeToken<ApiContext<? extends Closeable>>() {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
}.where(new TypeParameter<A>() {
|
};
|
||||||
}, TypeToken.of(apiClass));
|
Closeable api = unwrap(contextToken).getApi();
|
||||||
return unwrap(contextToken).getApi();
|
checkArgument(apiClass.isAssignableFrom(api.getClass()),
|
||||||
|
"API type: %s not assignable to %s", api.getClass(), apiClass);
|
||||||
|
return apiClass.cast(api);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -21,6 +21,9 @@ import com.google.common.reflect.TypeToken;
|
||||||
import com.google.inject.Binder;
|
import com.google.inject.Binder;
|
||||||
import com.google.inject.TypeLiteral;
|
import com.google.inject.TypeLiteral;
|
||||||
|
|
||||||
|
import org.jclouds.util.TypeToken2;
|
||||||
|
import org.jclouds.util.TypeToken2.TypeParameter2;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Adrian Cole
|
* @author Adrian Cole
|
||||||
|
@ -99,9 +102,9 @@ public class BinderUtils {
|
||||||
@Deprecated
|
@Deprecated
|
||||||
@SuppressWarnings({ "unchecked", "serial" })
|
@SuppressWarnings({ "unchecked", "serial" })
|
||||||
private static <S, A> void bindHttpApiProvider(Binder binder, Class<S> sync, Class<A> async) {
|
private static <S, A> void bindHttpApiProvider(Binder binder, Class<S> sync, Class<A> async) {
|
||||||
TypeToken<SyncToAsyncHttpApiProvider<S, A>> token = new TypeToken<SyncToAsyncHttpApiProvider<S, A>>() {
|
TypeToken<SyncToAsyncHttpApiProvider<S, A>> token = new TypeToken2<SyncToAsyncHttpApiProvider<S, A>>() {
|
||||||
}.where(new TypeParameter<S>() {
|
}.where(new TypeParameter2<S>() {
|
||||||
}, sync).where(new TypeParameter<A>() {
|
}, sync, new TypeParameter2<A>() {
|
||||||
}, async);
|
}, async);
|
||||||
binder.bind(sync).toProvider(TypeLiteral.class.cast(TypeLiteral.get(token.getType())));
|
binder.bind(sync).toProvider(TypeLiteral.class.cast(TypeLiteral.get(token.getType())));
|
||||||
}
|
}
|
||||||
|
@ -139,9 +142,9 @@ public class BinderUtils {
|
||||||
@Deprecated
|
@Deprecated
|
||||||
@SuppressWarnings({ "unchecked", "serial" })
|
@SuppressWarnings({ "unchecked", "serial" })
|
||||||
private static <S, A> void bindCallGetOnFutures(Binder binder, Class<S> sync, Class<A> async) {
|
private static <S, A> void bindCallGetOnFutures(Binder binder, Class<S> sync, Class<A> async) {
|
||||||
TypeToken<CallGetOnFuturesProvider<S, A>> token = new TypeToken<CallGetOnFuturesProvider<S, A>>() {
|
TypeToken<CallGetOnFuturesProvider<S, A>> token = new TypeToken2<CallGetOnFuturesProvider<S, A>>() {
|
||||||
}.where(new TypeParameter<S>() {
|
}.where(new TypeParameter2<S>() {
|
||||||
}, sync).where(new TypeParameter<A>() {
|
}, sync, new TypeParameter2<A>() {
|
||||||
}, async);
|
}, async);
|
||||||
binder.bind(sync).toProvider(TypeLiteral.class.cast(TypeLiteral.get(token.getType())));
|
binder.bind(sync).toProvider(TypeLiteral.class.cast(TypeLiteral.get(token.getType())));
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,10 +24,11 @@ import java.util.Properties;
|
||||||
import org.jclouds.apis.ApiMetadata;
|
import org.jclouds.apis.ApiMetadata;
|
||||||
import org.jclouds.apis.internal.BaseApiMetadata;
|
import org.jclouds.apis.internal.BaseApiMetadata;
|
||||||
import org.jclouds.rest.RestApiMetadata;
|
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.annotations.Beta;
|
||||||
import com.google.common.base.Objects.ToStringHelper;
|
import com.google.common.base.Objects.ToStringHelper;
|
||||||
import com.google.common.reflect.TypeParameter;
|
|
||||||
import com.google.common.reflect.TypeToken;
|
import com.google.common.reflect.TypeToken;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -56,10 +57,10 @@ public abstract class BaseRestApiMetadata extends BaseApiMetadata implements Res
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <S, A> TypeToken<org.jclouds.rest.RestContext<S, A>> contextToken(TypeToken<S> apiToken, TypeToken<A> asyncApiToken) {
|
public static <S, A> TypeToken<org.jclouds.rest.RestContext<S, A>> contextToken(TypeToken<S> apiToken, TypeToken<A> asyncApiToken) {
|
||||||
return new TypeToken<org.jclouds.rest.RestContext<S, A>>() {
|
return new TypeToken2<org.jclouds.rest.RestContext<S, A>>() {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
}.where(new TypeParameter<S>() {
|
}.where(new TypeParameter2<S>() {
|
||||||
}, apiToken).where(new TypeParameter<A>() {
|
}, apiToken, new TypeParameter2<A>() {
|
||||||
}, asyncApiToken);
|
}, asyncApiToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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<T> extends TypeToken<T> {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public <X, Y> TypeToken<T> where(TypeParameter2<X> typeParam1,
|
||||||
|
TypeToken<X> typeArg1, TypeParameter2<Y> typeParam2, TypeToken<Y> typeArg2) {
|
||||||
|
// resolving both parameters in one shot seems to work around 1635
|
||||||
|
TypeResolver resolver = new TypeResolver();
|
||||||
|
// where(Map) is package-private in TypeResolver
|
||||||
|
Invokable<TypeResolver, TypeResolver> whereWithMap =
|
||||||
|
Reflection2.<TypeResolver, TypeResolver>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<T>) TypeToken.of(resolver.resolveType(getType()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <X, Y> TypeToken<T> where(TypeParameter2<X> typeParam1, Class<X> typeArg1,
|
||||||
|
TypeParameter2<Y> typeParam2, Class<Y> typeArg2) {
|
||||||
|
return where(typeParam1, of(typeArg1), typeParam2, of(typeArg2));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static abstract class TypeParameter2<T> extends TypeParameter<T> {
|
||||||
|
TypeVariable<?> getTypeVariable() {
|
||||||
|
// duplicated from TypeCapture, where it's package-private
|
||||||
|
Type superclass = getClass().getGenericSuperclass();
|
||||||
|
return (TypeVariable<?>) ((ParameterizedType) superclass).getActualTypeArguments()[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue