M
Magno Machado
It's possible to use reflection with a generic type?
For example:
// MyClass.java
import java.lang.reflect.*;
public class MyClass<E> {
public MyClass() {
Method[] methods = <E>.getClass().getMethods();
for (int i = 0; i < methods.length; ++i)
System.out.println(methods.getName());
}
}
// Test.java
public class Test {
public void methodA() {}
public void methodB() {}
public void methodC() {}
public static void main(String[] args) {
new MyClass<Test>();
}
}
This will print all methods from class Test. Basically, I need only
obtain the Class object that represents the class passed to the generic
argument E of the class MyClass
For example:
// MyClass.java
import java.lang.reflect.*;
public class MyClass<E> {
public MyClass() {
Method[] methods = <E>.getClass().getMethods();
for (int i = 0; i < methods.length; ++i)
System.out.println(methods.getName());
}
}
// Test.java
public class Test {
public void methodA() {}
public void methodB() {}
public void methodC() {}
public static void main(String[] args) {
new MyClass<Test>();
}
}
This will print all methods from class Test. Basically, I need only
obtain the Class object that represents the class passed to the generic
argument E of the class MyClass