You can use the methods Class.getDeclaredMethods()
and Class.getDeclaredFields()
from the Reflection API to list a class methods and attributes.
for (Method m : ExampleClass.class.getDeclaredMethods()) {
System.out.println(m.getName() + ": " + m.getGenericReturnType());
}
for (Field f : ExampleClass.class.getDeclaredFields()) {
System.out.println(f.getName() + ": " + f.getType());
}
But you can not normally access the local variables information. In Java 8, you have access to the methods parameters using the method Method.getParameters()
. So you could do:
for (Method m : ExampleClass.class.getDeclaredMethods()) {
for (Parameter p : m.getParameters()) {
System.out.println(p.getName() + ": " + p.getType());
}
}
The parameter names are not stored by default in the .class files though, so the parameters will be listed as arg0
, arg1
, etc. To get the real parameter names you need to compile the source file with the -parameters option to the javac compiler.