Java 动态调用方法名
假设我有一个名为testMethod的类和方法(String test1,String Test2)。 我还有另一个类,它使用不同的方法调用它所在的方法。请参见下面的示例Java 动态调用方法名,java,reflection,Java,Reflection,假设我有一个名为testMethod的类和方法(String test1,String Test2)。 我还有另一个类,它使用不同的方法调用它所在的方法。请参见下面的示例 public class functional { testMethod(String test1, String test2) { reCallMethod(); } } reCallMethod(){ testMethod(test1, test2); // ------&g
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
}
}
}
您可以使用反射来确定在运行时调用哪个方法 有关如何做到这一点的信息,请参阅本文:
看一看 您可以使用Java反射来查找类上的方法。 因此,clazz.getMethods()将返回一个方法对象数组。 当您确定您感兴趣的方法时,您将调用method.invoke(…)
见此:在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);
}
尽管“执行”上的返回值和参数可能因您的需要而有所不同
然后,您的代码如下所示:
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);
}
如果你不想使用反射,还有另一种可能性,那就是策略模式。它不提供与反射完全相同的功能,但您确实可以更改在运行时调用的方法。您可以使用将在
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);
}
然后有两种不同的策略实施;每个要调用的方法对应一个。例如:
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);
}
}
之后您需要做的就是根据当前环境使用适当的策略。也许您可以这样做,做同样的事情:
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次尝试或其他尝试后增加并中断的东西
最大的好处是,如果您已经编写了其他代码。您只需复制并通过方法顶部的前四行,复制并通过底部的最后五行,并检查现有代码中是否没有捕获和吃掉任何异常。看起来您正在寻找。为什么它必须是动态的?请注意,在java中,根据惯例,名称以大写字母开头,方法名称以小写字母开头。大多数人都遵循这些约定。如果使用反射,则必须以任何方式将参数传递到重试函数中。这是因为参数只在其自己的函数中有作用域,而反射会将函数的重新调用移到该作用域之外。类应该以大写字母开头,方法应该以小写字母开头。谢谢!但在我的示例中,我有一个接受两个参数的“testMethod”。但是,如果我有另一个名为“testMethod2”的方法,它接受三个参数,而我仍然想使用“reCallMethod”?您可以通过反射来实现这一点,看看该方法的方法,它接受可变数量的参数。在运行时,您当然需要知道调用哪个方法以及需要多少参数。您现在可以检查我编辑的信息,看看是否可以帮助我吗?谢谢在retryLogic方法中,需要传递要调用的方法及其参数数组。调用retryLogic时,可以使用以下.getClass().getMethod(“TestCase2”,String.class,String.class,String.class)获得正确的方法。将此方法以及调用它的对象及其参数传递给retryLogic。在这里,您执行myMethod.invoke(obj、param1、param2);你现在能检查一下我编辑的信息,看看你能不能帮我?谢谢你现在能检查一下我编辑的信息,看看你能不能帮我?谢谢