繁体   English   中英

Java getMethod()找不到方法

[英]Java getMethod() not finding method

我正在尝试使用Java的反射来为项目创建模块加载器,但是getMethod()方法似乎拒绝定位存在的方法,即使该方法已明确定义。

在Module.class文件中: public final void load(org.clustermc.core.ClusterCore plugin);

我要求核心打印出在类中找到的方法。 结果:
Methods: [public void me.capit.clustersample.SampleModule.onLoad(), public void me.capit.clusterSample.SampleModule.onUnload(), public final void org.clustermc.core.Module.load(org.clustermc.core.ClusterCore), /*Other stuff from Object*/];
请注意,SampleModule扩展了Module。

在核心Method enable = c.getMethod("onLoad"); Method init = c.getMethod("load", org.clustermc.core.ClusterCore.class);Method enable = c.getMethod("onLoad"); Method init = c.getMethod("load", org.clustermc.core.ClusterCore.class); Method enable = c.getMethod("onLoad"); Method init = c.getMethod("load", org.clustermc.core.ClusterCore.class);

onLoad()的'enable'变量工作正常,但是在尝试查找load(ClusterCore.class)时,我得到了NoSuchMethodException。 为什么...?

检查加载方法的范围,如果它是私有的,则将无法访问它。 您需要设置atrueable。 注意getDeclaredMethod而不是getMethod。

Method init = c.getDeclaredMethod("load", org.clustermc.core.ClusterCore.class);

在调用之前,您需要执行以下行:

 init.setAccessible(true);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM