mardi 6 mars 2018

Accessing methods form nested classes using Reflection

I have the below class which is auto-generated by LeanFT Automation tool when an Object repository is created. Below is a condensed version of the file just to give an idea:

public class Calculator extends AppModelBase {
    private CalculatorWindow CalculatorWindow;

    public Calculator() throws GeneralLeanFtException {
        CalculatorWindow = new CalculatorWindow(this);
    }

    public CalculatorWindow CalculatorWindow() {
        return CalculatorWindow;
    }

    //Inner class   
    public class CalculatorWindow extends WindowNodeBase {
        private Button4 Button4;

        public CalculatorWindow(AppModelBase applicationModel) throws GeneralLeanFtException {
            Button4 = new Button4(this, applicationModel);
        }

        public Button4 Button4() {
            return Button4;
        }

        //Inner class
        public class Button4 extends ButtonNodeBase {
            public Button4(TestObject parent, AppModelBase applicationModel) throws GeneralLeanFtException {
                super(parent, applicationModel);
                setDisplayName("Button4");
            }
        }
    }   
}

As you can see, the Calculator class contains an inner class CalculatorWindow, which in turn contains an inner class Button4.

Within this project, in another class I can do something like -

Calculator caclModel = new Calculator();
caclModel.CalculatorWindow().Button4().click();

Now, I need to access the members of this class from a different project using Reflection. So far below is what I have:

        File file = new File("C:/TestProj/target/classes");
        URL classUrl = file.toURL();
        URL[] cUrls = new URL[] { classUrl };

        File sdkJar = new File("C:/Jars/14.0.0/sdk-14.0.0.JAR");
        URL parentUrl = sdkJar.toURL();
        URL[] pUrls = new URL[] { parentUrl };

        // Load jar
        URLClassLoader parentCL = new URLClassLoader(pUrls, LoaderTest.class.getClassLoader());

        // Load target class
        ClassLoader cl = new URLClassLoader(cUrls, parentCL);
        Class<?> clsCalc = cl.loadClass("Calculator");
        Object oCalc = clsCalc.newInstance();

        // --> Works fine so far. After this, I should be able to do something like -
        //     oCalc.CalculatorWindow().Button4().click();
        //     Tried the below
        Class<?> clsWin = cl.loadClass("Calculator$CalculatorWindow");
        Constructor<?> cWin = clsWin.getDeclaredConstructor(clsCalc); //-->This throws java.lang.NoSuchMethodException: Calculator$CalculatorWindow.<init>(Calculator)
        Object oWin = cWin.newInstance(oCalc);

        // Class<?> clsBut = cl.loadClass("Calculator$CalculatorWindow$Button4");
        // Constructor<?> cBut = clsBut.getDeclaredConstructor(clsWin);
        //
        // Method click = clsBut.getDeclaredMethod("click");
        // click.invoke(cBut, null);

What is the correct approach to access the method in my inner most nested class so that I achieve something like oCalc.CalculatorWindow().Button4().click();





Aucun commentaire:

Enregistrer un commentaire