我正在尝试使用以下代码从我的对象中提取方法:
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)
原始源代码:
class Calculator {
@Override
public void initialize(ComponentContext context) {
// Do nothing
}
}
怎么了?
ADD:我已将方法改为:
Object method = instance.getClass().getMethod("initialize", org.hive.lib.component.ComponentContext.class);
但异常仍然出现
ADD 2: instance
对象是使用JCL从JAR实例化的,可能是问题吗?
答案 0 :(得分:3)
传递给ComponentContext
的{{1}}类可能与您的代码转储的getMethod()
类不一致(在JVM的眼中) - 如果它由不同的类加载器加载。检查相关的类加载器是否相等,以确保。
这类似于Java, getting class cast exception where both classes are exactly the same