here's my code. And it throws
can not access a member of class sun.management.ClassLoadingImpl with modifiers "public"
I first reflect the method, then call it. And as you see, getObjectName is a public method, but why cannot I invoke it?
import java.lang.management.ManagementFactory;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Hello world!
*/
public class App {
public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
Method[] methods = ManagementFactory.class.getMethods();
System.out.println(ManagementFactory.getClassLoadingMXBean().getObjectName());
for (Method method : methods) {
if (method.getName().startsWith("get") && method.getName().endsWith("Bean")) {
System.out.println(method.getName());
Object bean = method.invoke(null);
printBean(bean);
}
}
}
private static void printBean(Object bean) throws InvocationTargetException, IllegalAccessException {
Method[] methods = bean.getClass().getMethods();
for (Method method : methods) {
if (method.getName().startsWith("get")) {
System.out.println(method.getName());
System.out.println(method.getName() + ":" + method.invoke(bean).toString());
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire