我试图使用Java的反射来为项目创建模块加载器,但是getMethod()方法似乎拒绝定位存在的方法,即使该方法已明确定义。
在module. class文件中:公共最终无效加载(org.clustermc.core.ClusterCore插件);
我要求核心打印出它在Class中找到的方法。结果:方法:[public void me. cb.clustersample.SampleModule.onLoad(),public void me.cb.clusterSample.SampleModule.onUnload(),public最终void org.groustermc.core.Module.load(org.clustermc.core.ClusterCore),/*Object中的其他东西*/];
注意SampleModule扩展了模块。
在核心:方法启用=c. getMethod("onLoad");方法init=c.getMethod("load",org.clustermc.core.ClusterCore.class);
onLoad()的“启用”变量工作得很好,但是在尝试查找load(ClusterCore. class)时,我得到了一个NoSuchMEodException。为什么…?
检查load方法的范围,如果它是私有的,那么它将不会被访问。您需要设置acesable true。注意getDeclaredMethod而不是getmethod。
Method init = c.getDeclaredMethod("load", org.clustermc.core.ClusterCore.class);
在调用之前,您需要执行以下行:-
init.setAccessible(true);