diff options
author | C. Sean Young <csyoung@google.com> | 2015-08-05 14:31:50 -0500 |
---|---|---|
committer | Steve Kondik <steve@cyngn.com> | 2016-04-03 13:19:10 -0700 |
commit | 99b0ee6f5b3a3cd22f340e4e84581fbedc81423a (patch) | |
tree | 8f9fb57cfb403a5325c8f1a6831dba8d4d23c1d1 | |
parent | 5b9b416a211d8d2ac2012aaad1b77f79ffe7617c (diff) | |
download | android_external_guava-stable/cm-13.0-ZNH5Y.tar.gz android_external_guava-stable/cm-13.0-ZNH5Y.tar.bz2 android_external_guava-stable/cm-13.0-ZNH5Y.zip |
Backport Java 8 compile time break workaround.HEADreplicant-6.0-0004-transitionreplicant-6.0-0004-rc6replicant-6.0-0004-rc5-transitionreplicant-6.0-0004-rc5replicant-6.0-0004-rc4replicant-6.0-0004-rc3replicant-6.0-0004-rc2replicant-6.0-0004-rc1replicant-6.0-0004replicant-6.0-0003replicant-6.0-0002replicant-6.0-0001stable/cm-13.0-ZNH5Ycm-13.0
Required for building host libraries with OpenJDK 8.
Without this, trying to build with Java 8 fails with:
external/guava/guava/src/com/google/common/reflect/Types.java:317:
error: TypeVariableImpl is not abstract and does not override abstract method getAnnotatedBounds() in TypeVariable
private static final class TypeVariableImpl<D extends GenericDeclaration>
Backport of https://github.com/google/guava/commit/f4aa25e74a9466c2f93a2147a7cf9b01850dd41f
See https://github.com/google/guava/issues/1738
Bug: 23074347
Change-Id: I35adc64a339055e1e426af2aa3a41058f9948984
-rw-r--r-- | guava/src/com/google/common/reflect/Types.java | 98 |
1 files changed, 90 insertions, 8 deletions
diff --git a/guava/src/com/google/common/reflect/Types.java b/guava/src/com/google/common/reflect/Types.java index 0f05f78..be358c7 100644 --- a/guava/src/com/google/common/reflect/Types.java +++ b/guava/src/com/google/common/reflect/Types.java @@ -26,16 +26,22 @@ import com.google.common.base.Joiner; import com.google.common.base.Objects; import com.google.common.base.Predicates; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; import java.io.Serializable; import java.lang.reflect.Array; import java.lang.reflect.GenericArrayType; import java.lang.reflect.GenericDeclaration; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Proxy; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.lang.reflect.WildcardType; +import java.security.AccessControlException; import java.util.Arrays; import java.util.Collection; import java.util.concurrent.atomic.AtomicReference; @@ -146,7 +152,7 @@ final class Types { */ static <D extends GenericDeclaration> TypeVariable<D> newArtificialTypeVariable( D declaration, String name, Type... bounds) { - return new TypeVariableImpl<D>( + return newTypeVariableImpl( declaration, name, (bounds.length == 0) @@ -314,8 +320,76 @@ final class Types { private static final long serialVersionUID = 0; } - private static final class TypeVariableImpl<D extends GenericDeclaration> - implements TypeVariable<D> { + private static <D extends GenericDeclaration> TypeVariable<D> newTypeVariableImpl( + D genericDeclaration, String name, Type[] bounds) { + TypeVariableImpl<D> typeVariableImpl = + new TypeVariableImpl<D>(genericDeclaration, name, bounds); + @SuppressWarnings("unchecked") + TypeVariable<D> typeVariable = Reflection.newProxy( + TypeVariable.class, new TypeVariableInvocationHandler(typeVariableImpl)); + return typeVariable; + } + + /** + * Invocation handler to work around a compatibility problem between Java 7 and Java 8. + * + * <p>Java 8 introduced a new method {@code getAnnotatedBounds()} in the {@link TypeVariable} + * interface, whose return type {@code AnnotatedType[]} is also new in Java 8. That means that we + * cannot implement that interface in source code in a way that will compile on both Java 7 and + * Java 8. If we include the {@code getAnnotatedBounds()} method then its return type means + * it won't compile on Java 7, while if we don't include the method then the compiler will + * complain that an abstract method is unimplemented. So instead we use a dynamic proxy to + * get an implementation. If the method being called on the {@code TypeVariable} instance has + * the same name as one of the public methods of {@link TypeVariableImpl}, the proxy calls + * the same method on its instance of {@code TypeVariableImpl}. Otherwise it throws {@link + * UnsupportedOperationException}; this should only apply to {@code getAnnotatedBounds()}. This + * does mean that users on Java 8 who obtain an instance of {@code TypeVariable} from {@link + * TypeResolver#resolveType} will not be able to call {@code getAnnotatedBounds()} on it, but that + * should hopefully be rare. + * + * <p>This workaround should be removed at a distant future time when we no longer support Java + * versions earlier than 8. + */ + private static final class TypeVariableInvocationHandler implements InvocationHandler { + private static final ImmutableMap<String, Method> typeVariableMethods; + static { + ImmutableMap.Builder<String, Method> builder = ImmutableMap.builder(); + for (Method method : TypeVariableImpl.class.getMethods()) { + if (method.getDeclaringClass().equals(TypeVariableImpl.class)) { + try { + method.setAccessible(true); + } catch (AccessControlException e) { + // OK: the method is accessible to us anyway. The setAccessible call is only for + // unusual execution environments where that might not be true. + } + builder.put(method.getName(), method); + } + } + typeVariableMethods = builder.build(); + } + + private final TypeVariableImpl<?> typeVariableImpl; + + TypeVariableInvocationHandler(TypeVariableImpl<?> typeVariableImpl) { + this.typeVariableImpl = typeVariableImpl; + } + + @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + String methodName = method.getName(); + Method typeVariableMethod = typeVariableMethods.get(methodName); + if (typeVariableMethod == null) { + throw new UnsupportedOperationException(methodName); + } else { + try { + return typeVariableMethod.invoke(typeVariableImpl, args); + } catch (InvocationTargetException e) { + throw e.getCause(); + } + } + } + } + + private static final class TypeVariableImpl<D extends GenericDeclaration> { private final D genericDeclaration; private final String name; @@ -328,15 +402,19 @@ final class Types { this.bounds = ImmutableList.copyOf(bounds); } - @Override public Type[] getBounds() { + public Type[] getBounds() { return toArray(bounds); } - @Override public D getGenericDeclaration() { + public D getGenericDeclaration() { return genericDeclaration; } - @Override public String getName() { + public String getName() { + return name; + } + + public String getTypeName() { return name; } @@ -351,8 +429,12 @@ final class Types { @Override public boolean equals(Object obj) { if (NativeTypeVariableEquals.NATIVE_TYPE_VARIABLE_ONLY) { // equal only to our TypeVariable implementation with identical bounds - if (obj instanceof TypeVariableImpl) { - TypeVariableImpl<?> that = (TypeVariableImpl<?>) obj; + if (obj != null + && Proxy.isProxyClass(obj.getClass()) + && Proxy.getInvocationHandler(obj) instanceof TypeVariableInvocationHandler) { + TypeVariableInvocationHandler typeVariableInvocationHandler = + (TypeVariableInvocationHandler) Proxy.getInvocationHandler(obj); + TypeVariableImpl<?> that = typeVariableInvocationHandler.typeVariableImpl; return name.equals(that.getName()) && genericDeclaration.equals(that.getGenericDeclaration()) && bounds.equals(that.bounds); |