Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java反射中找不到方法_Java_Reflection - Fatal编程技术网

在java反射中找不到方法

在java反射中找不到方法,java,reflection,Java,Reflection,我正在尝试使用以下代码从对象中提取方法: Method[] methods = instance.getClass().getMethods(); for (Method m : methods) { System.out.println(">>> " + m.getName()); for (Class c : m.getParameterTypes()) { System.out.println("\t->>> " + c

我正在尝试使用以下代码从对象中提取方法:

Method[] methods = instance.getClass().getMethods();

for (Method m : methods) {
    System.out.println(">>> " + m.getName());
    for (Class c : m.getParameterTypes()) {
        System.out.println("\t->>> " + c.getName());
    }
}

Object method = instance.getClass().getMethod("initialize", ComponentContext.class);
它打印以下输出:

>>> initialize
->>> org.hive.lib.component.ComponentContext
java.lang.NoSuchMethodException: org.hive.sample.Calculator.initialize(org.hive.lib.component.ComponentContext)
at java.lang.Class.getMethod(Class.java:1624)
at org.hive.container.lib.Component.hasRightParent(Component.java:140)
at org.hive.container.lib.Component.<init>(Component.java:118)
at org.hive.container.lib.ComponentController$AppLoader.execute(ComponentController.java:107)
at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557)
怎么了

添加:我已将获取方法更改为:

Object method = instance.getClass().getMethod("initialize", org.hive.lib.component.ComponentContext.class);
但例外情况仍然出现


添加2:
实例
对象是用JCL从JAR实例化的,这可能是问题吗?

传递给
getMethod()
组件上下文
类可能与您的代码转储的类(在JVM眼中)不相同-如果它是由不同的类加载器加载的。检查关联的类加载器是否相等,以确定是否相等


这类似于

你的计算器类真的扩展了什么吗?是的,我的抽象类有一个抽象方法
initialize
奇怪!for循环也应该显示其余的方法,例如
toString
wait()
,…@user251414 OP修剪了它。
实例
是用JAR中的JCL实例化的,这可能是问题吗?是的,可能是问题。请输出
c.getClassLoader()
org.hive.lib.component.ComponentContext.class.getClassLoader()
以找出原因。+1此处可能存在不同的
ClassLoader
,因为导入是相同的。是的,你是对的-他们中的第一个拥有
org.xeustechnologies.jcl。JarClassLoader@3113c6f6
classloader和second具有
sun.misc.Launcher$AppClassLoader@36ddc581
。谢谢
Object method = instance.getClass().getMethod("initialize", org.hive.lib.component.ComponentContext.class);