4

I'm trying to extract the method from my object with this code:

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);

It prints the following otput:

>>> 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)

Original source code:

class Calculator {
    @Override
    public void initialize(ComponentContext context) {
        //  Do nothing
    }
}

What's wrong?

ADD: I've changed getting method to:

Object method = instance.getClass().getMethod("initialize", org.hive.lib.component.ComponentContext.class);

But exception still appears

ADD 2: instance object was instantiated from the JAR with JCL, could it be the problem?

skayred
  • 10,603
  • 10
  • 52
  • 94

1 Answers1

3

The ComponentContext class passed to getMethod() may not be identical (in the eyes of the JVM) to the one which is dumped by your code - if it is loaded by a different class loader. Check the associated class loaders for equality just to be sure.

This is similar to Java, getting class cast exception where both classes are exactly the same

Community
  • 1
  • 1
halfbit
  • 3,414
  • 1
  • 20
  • 26
  • `instance` was instantiated with JCL from JAR, could it be the problem? – skayred Oct 13 '13 at 16:27
  • Yes, it could be. Please output `c.getClassLoader()` and `org.hive.lib.component.ComponentContext.class.getClassLoader()` to find out. – halfbit Oct 13 '13 at 16:34
  • +1 A different `ClassLoader` is likely to be the issue here since the imports are same. – Ravi K Thapliyal Oct 13 '13 at 16:36
  • Yeap, you were right - first of them has `org.xeustechnologies.jcl.JarClassLoader@3113c6f6` classloader and second has `sun.misc.Launcher$AppClassLoader@36ddc581`. Thanks! – skayred Oct 13 '13 at 16:50