With a given java.lang.reflect.Method
.
I can call,
final Class<?> returnType = method.getReturnType();
But when I tried to call getTypeParameters()
with following statement,
final TypeVariable<Class<?>>[] typeParameters = returnType.getTypeParameters();
I got a compiler error.
required: java.lang.reflect.TypeVariable<java.lang.Class<?>>[]
found: java.lang.reflect.TypeVariable<java.lang.Class<capture#1 of ?>>[]
What's wrong this statement? I just followed apidocs.
the code is attempting to perform a safe operation.
Helper method needs to be created so that the wildcard can be captured through type inference. compiler is not able to confirm the type of object that is being inserted into the list, and an error is produced. When this type of error occurs it typically means that the compiler believes that you are assigning the wrong type to a variable. Generics were added to the Java language for this reason — to enforce type safety at compile time.
here is the java doc refrence that explains the same.
http://docs.oracle.com/javase/tutorial/java/generics/capture.html
while this will work
final TypeVariable<?>[] typeParameters =returnType.getTypeParameters();