I’ve a simple generic class follows which accepts a generic type parameter, which is the same as the one declared as a type parameter of the class:
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
public abstract class SimpleClass<T extends AnnotatedElement>
{
public void operate()
{
Method m = null;
this.doSomething(m); // Error : SimpleClass.java:[34,10] doSomething(T) in SimpleClass<T> cannot be applied to (java.lang.reflect.Method)
}
protected abstract void doSomething(T annotatedElement);
}
This code fails to compile at the following line:
this.doSomething(m);
with this error:
Error : SimpleClass.java:[34,10] doSomething(T) in SimpleClass<T> cannot be applied to (java.lang.reflect.Method)
Am I missing something here? The type parameter T is marked as T extends AnnotatedElement. As such, I would expect the call to doSomething with a java.lang.reflect.Method argument to compile successfully.
Method implements AnnotatedElement, but that doesn’t require that T is a method. What if the class is declared as
SimpleClass<Constructor>? That satisfies<T extends AnnotatedElement>, but doesn’t support conversion fromMethod.