Reputation: 3185
is it possible to get a generic (parametrized) method from a non-generic class through reflection? Here's a sample of what i want to do:
public interface GenericInterface<T> {
public T publicMethod(T arg);
}
public class NonGenericClassWithGenericMethods {
private <T> void privateMethod(GenericInterface<T> arg) {
}
}
public class Generics {
public static void main(String[] args) {
try {
NonGenericClassWithGenericMethods.class.getMethod("privateMethod", GenericInterface.class).setAccessible(true);
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}
If i run Generics, i get:
java.lang.NoSuchMethodException: NonGenericClassWithGenericMethods.privateMethod(GenericInterface)
Thanks everybody
Upvotes: 0
Views: 514
Reputation: 597402
.getDeclaredMethod()
should be used instead of .getMethod()
, which returns only public ones.
Upvotes: 7