如何调用字符串数组中的 Java 方法? (java.lang.NoSuchMethodException)

How to invoke a Java method that is in a Array of Strings? (java.lang.NoSuchMethodException)

我有一个 ArrayStringsMethod names 我想在循环中调用它。我检索了 class 中所有变量的名称并添加了所需的文本以填充整个函数并将整个字符串添加到列表中:

String[] methodNames= new String[16];
Class c = telegramm.class;
Field[] fields = c.getDeclaredFields();

int x=0;
for (Field field : fields) 
{               
    String str = field.getName();
    String variableName = str.substring(0, 1).toUpperCase() + str.substring(1);
    // This is how I initially intended to call the method
    // methodNames[x] = "set" + variableName + "(parts[" + x + "]);";

    // but with the method I found I used this
    methodNames[x] = "set" + variableName";

    Method gs1Method = tClass.getMethod("getString1", new Class[] {});
    //String str1 = (String) gs1Method.invoke(t, new Object[] {});
    //System.out.println("getString1 returned: " + str1);
    x++;
}

因此,我将方法调用 String 添加到数组 methodNames

而且我找到了调用方法的方法:

Method gs1Method = tClass.getMethod("getString1", new Class[] {});
String str1 = (String) gs1Method.invoke(t, new Object[] {});
System.out.println("getString1 returned: " + str1);

这有效,但仅当您对方法名称进行硬编码时 ("getString1")。

现在我想实现一种通过 methodNames 数组调用这些函数的方法。

当我将 methodNames 数组的对象作为参数传递时:

getMethod(methodNames[x], new Class[] {});

我收到错误:

Exception in thread "main" java.lang.NoSuchMethodException: 
kin.gateway.adient.Testing.setBoolean1(java.lang.String)
at java.lang.Class.getMethod(Class.java:1786)
at kin.gateway.adient.ClassMethodTest.main(ClassMethodTest.java:31)

我也尝试将字符串添加到一个单一变量 String variableName = "set" + variableName; 但得到了相同的结果。

为什么它不接受字符串作为变量?

尝试这个示例应该可以帮助您找到错误,之后您可以根据您的方法在其上添加一个循环:

1 - 你的电报 class :

public class Telegramm {

    String value1;
    String value2;

    public String getValue1() {
        return this.value1;
    }

    public void setValue1(String value) {
        this.value1 = value;
    }

    public String getValue2() {
        return this.value2;
    }

    public String setValue2(String value) {
        this.value2 = value;
        return this.value2;
    }

}

2 - 调用方法:getValue1() 和 setValue2()

try {
            // Create our Telegramm instance
            Telegramm telegramm = new Telegramm();
            telegramm.setValue1("value1");
            telegramm.setValue2("value2");

            // Invoke public static String getValue1()
            Method getValue1Method = telegramm.getClass().getMethod("getValue1", null);
            String result = (String)getValue1Method.invoke(telegramm);
            System.out.println("result invocation getValue1() : " + result);

            // Invoke public static String setValue2()
            getValue1Method = telegramm.getClass().getMethod("setValue2", new Class[] {String.class});
            result = (String)getValue1Method.invoke(telegramm, "ValueX");
            System.out.println("result invocation setValue2() : " + result);

        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }