Class Types.TypeVariableInvocationHandler
- java.lang.Object
-
- com.google.common.reflect.Types.TypeVariableInvocationHandler
-
- All Implemented Interfaces:
java.lang.reflect.InvocationHandler
- Enclosing class:
- Types
private static final class Types.TypeVariableInvocationHandler extends java.lang.Object implements java.lang.reflect.InvocationHandler
Invocation handler to work around a compatibility problem between Java 7 and Java 8.Java 8 introduced a new method
getAnnotatedBounds()
in theTypeVariable
interface, whose return typeAnnotatedType[]
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 thegetAnnotatedBounds()
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 theTypeVariable
instance has the same name as one of the public methods ofTypes.TypeVariableImpl
, the proxy calls the same method on its instance ofTypeVariableImpl
. Otherwise it throwsUnsupportedOperationException
; this should only apply togetAnnotatedBounds()
. This does mean that users on Java 8 who obtain an instance ofTypeVariable
fromTypeResolver.resolveType(java.lang.reflect.Type)
will not be able to callgetAnnotatedBounds()
on it, but that should hopefully be rare.TODO(b/147144588): We are currently also missing the methods inherited from
AnnotatedElement
, whichTypeVariable
began to extend only in Java 8. Those methods refer only to types present in Java 7, so we could implement them inTypeVariableImpl
today. (We could probably then makeTypeVariableImpl
implementAnnotatedElement
so that we get partial compile-time checking.)This workaround should be removed at a distant future time when we no longer support Java versions earlier than 8.
-
-
Field Summary
Fields Modifier and Type Field Description private Types.TypeVariableImpl<?>
typeVariableImpl
private static ImmutableMap<java.lang.String,java.lang.reflect.Method>
typeVariableMethods
-
Constructor Summary
Constructors Constructor Description TypeVariableInvocationHandler(Types.TypeVariableImpl<?> typeVariableImpl)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Object
invoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)
-
-
-
Field Detail
-
typeVariableMethods
private static final ImmutableMap<java.lang.String,java.lang.reflect.Method> typeVariableMethods
-
typeVariableImpl
private final Types.TypeVariableImpl<?> typeVariableImpl
-
-
Constructor Detail
-
TypeVariableInvocationHandler
TypeVariableInvocationHandler(Types.TypeVariableImpl<?> typeVariableImpl)
-
-