动态调用方法名称

时间:2013-01-23 20:24:41

标签: java reflection

假设我有一个名为testMethod的类和方法(String test1,String test 2)。 我还有另一个使用不同方法的类,它将调用它所使用的方法。请参阅下面的示例

public class functional {

    testMethod(String test1, String test2) {

        reCallMethod();

    }
}

reCallMethod(){
    testMethod(test1, test2); // ------> This has to be dynamic. I've written the method name as "testMEthod" here. But I want it generalized so that I can use this in any method and not just in "testMethod"
}

更多信息:-------------------------------

public class test1 {
public void TestCase1(String param1, String param2, String param3) {
        try {
            //Bla Bla Bla
        }
        catch (Throwable t) {
                TestCase_Store_Locator_Verify_Page_Name(param1,param2,param3); //Retry running this method

        }
    }
}

public class test2 {
    public void TestCase2(String param1, String param2, String param3, String param4, String Param5) {
        try {
            //Bla Bla Bla
        }
        catch (Throwable t) {
                TestCase2(param1,param2,param3,param4,param5); //Retry running this method

        }
    }
}

与TestCase1和TestCase2一样,我有500个测试。而不是上面我将有一个名为retryLogic的常见方法,如下面的

public void retryLogic(){
 //Call the test method in the class which this method is placed.
}


So my TestCase1 will look like

    public class test1 {
public void TestCase1(String param1, String param2, String param3) {
        try {
            //Bla Bla Bla
        }
        catch (Throwable t) {
                retryLogic(); //Retry running this method

        }
    }
}


    public void TestCase2(String param1, String param2, String param3) {
        try {
            //Bla Bla Bla
        }
        catch (Throwable t) {
                retryLogic(); //Retry running this method

        }
    }
}

6 个答案:

答案 0 :(得分:1)

您可以使用Reflection来确定在运行时调用哪个方法。

有关如何执行此操作的信息,请参阅此帖子: How do I invoke a Java method when given the method name as a string?

答案 1 :(得分:1)

答案 2 :(得分:0)

您可以使用Java反射来查找类上的方法。 因此,clazz.getMethods()将返回一个Method对象Array。 当你确定你感兴趣的那个时,你会调用method.invoke(...) 请参阅:http://docs.oracle.com/javase/tutorial/reflect/class/classMembers.html

答案 3 :(得分:0)

在java中,没有办法让变量持有对方法的引用。其他一些语言,例如javascript,允许这样做。

如果将对象传递到实现具有已知方法名称的接口的“reCallMethod()”,则会有一个详细的解决方法。通常界面看起来像这样:

public interface CallMe() {
    public Object execute(Object parm1, Object parm2);
}

虽然返回值和'执行'上的参数可能会根据您的需要而有所不同。

然后你的代码看起来像这样:

public class functional {

    final OtherClass otherInstance = new OtherClass();

    testMethod(String test1, String test2) {

        reCallMethod(new CallMe() {
            public Object execute(Object parm1, Object par2) {
                return otherInstance.varyingMethod((String)parm1, (String)parm2); // This is the varying method
            }
        }, text1, text2);
    });
}

reCallMethod(CallMe func, Object parm1, Object parm2){
    func.execute(parm1, parm2);
}

答案 4 :(得分:0)

如果你不想使用反思,还有另一种可能性,即策略模式。它不提供与反射完全相同的功能,但您可以更改在运行时调用的方法。您可以使用将在functional类中调用正确方法的类。

例如,如果您的functional类具有以下定义:

public class functional {
    public void testMethod (String test1, String test2) {
        reCallMethod();
    }

    public void anotherMethod (String test1, String test2) {
        reCallMethod();
    }
}

你可以有一个界面来定义你的战略界面:

public interface MyStrategy {
    void callMethod (String param1, String param2);
}

然后有2种不同的策略实现;每个要调用的方法一个。例如:

public class TestMethodStrategy implements MyStrategy {
    private functional myFunctional;

    public TestMethodStrategy (functional myFunctional) {
        this.myFunctional = myFunctional;
    }

    public void callMethod (String test1, String test2) {
        myFunctional.testMethod (test1, test2);
    }
}

之后您需要做的就是根据当前情况使用适当的策略。

答案 5 :(得分:0)

也许你可以做同样的事情:

  public void TestCase2(String param1, String param2, String param3) {
        boolean success;
        do {
            success = true;
            try {
                //Bla Bla Bla
            }
            catch (Throwable t) {
                success = false;
            }
        } while (!success);
    }

你甚至可以添加一个计数器来防止它永远运行。只是在20次尝试之后递增并执行break的东西。

关于它的一大优点是,如果你已经编写了其他代码。您只需复制并经过方法顶部的前4行,然后复制并通过底部的最后5行,并检查是否在现有代码中没有捕获和吃掉任何异常。