The problem: To get parameterized constructor reference of subclasses of A (such as class H shown below) in class C.
Class A<T extends B> {
public A(T objectT, D objectD, E objectE, F objectF) {
}
public T aMethodWithTAsReturnType();
}
Class B { }
Class C<T extends A<?>> {
private Constructor<?> classAConstructor;
public C(Class<T> classA) {
classAConstructor=classA.getConstructor(B.class, D.class,E.class,F.class)
}
}
Class H extends A<X> {
public H(X objectX, D objectD, E objectE, F objectF) {}
}
Class X extends B {}
new C(new H(objectX,objectD,objectE,objectF));
The above code configuration would result in a NoSuchMethodException when a new Class C object is created because it cannot find the constructor for Class A
I am now trying to use:
Method methodInA = classA.getMethod('aMethodWithTAsReturnType')
Class<?> TClassInA = methodInA.getReturnType();
as a replacement for B.class in the classA.getConstructor line as I'm guessing that's the issue here because class B is a super type of T (in Class A).
However... when I used methodInA.getReturnType() it returns B.class! rather than a class that has extended B. I then found a getGenericReturnType() method but it returns a Type object.
C c = new C(A.class);. Everything runs fine.