dimanche 3 janvier 2016

Java, GetMethod - public method is not found - Eclipse RCP

I encountered strange behaviour when using function java.lang.Class.getMethod. If i call it from inside the class (with given method), method is found but when i do it from another class, NoSuchMethodException is thrown.

This code works fine:

private void testMethod()
{
    System.out.println("testMethod");
    try {
        Method lMethod = this.getClass().getMethod("onCompleteExecutionOrder", new Class[]{BaseDelegate.class});
        System.out.println("lMethod: " + lMethod);
        lMethod.invoke(this, new Object[]{new CmdDelegate(null, null, "")});
    } catch (Exception e) {
        System.out.println("ERROR");
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Console log:

testMethod lMethod: public void views.TransactionManagerView.onCompleteExecutionOrder(app.delegates.BaseDelegate) WORKS!


but this one called from another class (the same object is passed as above) throws IllegalArgumentException:

private void testMethodFromOutside(Object pObject)
{
    System.out.println("testMethodFromOutside: " + pObject);
    try {
        Method lMethod = pObject.getClass().getMethod("onCompleteExecutionOrder", new Class[]{BaseDelegate.class});
        System.out.println("lMethod: " + lMethod);
        lMethod.invoke(pObject, new Object[]{new CmdDelegate(null, null, "")});
    } catch (Exception e) {
        System.out.println("ERROR");
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Console log:

testMethodFromOutside: views.TransactionManagerView@1fed1ec lMethod: public void views.TransactionManagerView.onCompleteExecutionOrder(app.delegates.BaseDelegate)

And the method i try to get:

public void onCompleteExecutionOrder(BaseDelegate pDelegate)
{
    System.out.println("WORKS!");
}

It is Eclipse RCP project and Java 1.8 and the class from which i try to get the method is declared as:

public class TransactionManagerView extends ViewPart

EDIT: I added there function to debug all the methods:

private void debugMethodsName()
{
    Class lClass = this.getClass();
    System.out.println("Class: " + lClass);
    System.out.println(lClass.getMethods().length);
    for(int i = 0; i < lClass.getMethods().length; ++i)
    {
        System.out.println(lClass.getMethods()[i].getName());
    }
}

and i have noticed that when: i call it from inside the class, there is Console log: Class: class views.TransactionManagerView and this class has 34 functions

when i call it from another class the log: Class: class views.TransactionManagerView$1 and this class has 11 functions

The second call is executed fromo another thread but what is the ($1 on the end of the class in log?)





Aucun commentaire:

Enregistrer un commentaire