JCLOUDS-432: Revert "JCLOUDS-427: Avoiding Guava reflection code broken in Java 7u51"

This reverts commit 8ff60020f8.
This commit is contained in:
Andrew Phillips 2014-02-05 00:04:55 +01:00
parent 02615adc4f
commit 4c07702d29
4 changed files with 14 additions and 103 deletions

View File

@ -70,18 +70,11 @@ 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>() {
Closeable api = unwrap(contextToken).getApi(); }, TypeToken.of(apiClass));
checkArgument(apiClass.isAssignableFrom(api.getClass()), return unwrap(contextToken).getApi();
"API type: %s not assignable to %s", api.getClass(), apiClass);
return apiClass.cast(api);
} }
@Override @Override

View File

@ -21,9 +21,6 @@ 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
@ -102,9 +99,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 TypeToken2<SyncToAsyncHttpApiProvider<S, A>>() { TypeToken<SyncToAsyncHttpApiProvider<S, A>> token = new TypeToken<SyncToAsyncHttpApiProvider<S, A>>() {
}.where(new TypeParameter2<S>() { }.where(new TypeParameter<S>() {
}, sync, new TypeParameter2<A>() { }, sync).where(new TypeParameter<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())));
} }
@ -142,9 +139,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 TypeToken2<CallGetOnFuturesProvider<S, A>>() { TypeToken<CallGetOnFuturesProvider<S, A>> token = new TypeToken<CallGetOnFuturesProvider<S, A>>() {
}.where(new TypeParameter2<S>() { }.where(new TypeParameter<S>() {
}, sync, new TypeParameter2<A>() { }, sync).where(new TypeParameter<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())));
} }

View File

@ -24,11 +24,10 @@ 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;
/** /**
@ -57,10 +56,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 TypeToken2<org.jclouds.rest.RestContext<S, A>>() { return new TypeToken<org.jclouds.rest.RestContext<S, A>>() {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
}.where(new TypeParameter2<S>() { }.where(new TypeParameter<S>() {
}, apiToken, new TypeParameter2<A>() { }, apiToken).where(new TypeParameter<A>() {
}, asyncApiToken); }, asyncApiToken);
} }

View File

@ -1,78 +0,0 @@
/*
* 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];
}
}
}