引用同一类中的方法时发生Java反射NoSuchMethodException

引用同一类中的方法时发生Java反射NoSuchMethodException,java,reflection,invoke,nosuchmethod,Java,Reflection,Invoke,Nosuchmethod,我在尝试对同一类中的方法调用getMethod时遇到NoSuchMethodException,该方法没有从hashmap中提取的字符串名称的参数。是否有任何建议,或者只给出方法的字符串名就可以调用同一类中的方法? 获取方法的调用如下所示: if (testChoices.containsKey(K)) { String method = testChoices.get(K); System.out.println(method); try {

我在尝试对同一类中的方法调用getMethod时遇到NoSuchMethodException,该方法没有从hashmap中提取的字符串名称的参数。是否有任何建议,或者只给出方法的字符串名就可以调用同一类中的方法? 获取方法的调用如下所示:

if (testChoices.containsKey(K)) {
        String method = testChoices.get(K);
        System.out.println(method);

        try {
            java.lang.reflect.Method m = TST.getClass().getMethod(method);
            m.invoke(testChoices.getClass());
        } catch (NoSuchMethodException e1) {
            // TODO Auto-generated catch block
            System.out.println("No method found");
            e1.printStackTrace();
        } catch (SecurityException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();


        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
testChoices.put(1, "testgetDomainLic");
我尝试调用的方法之一是:

private static void testgetDomainLic() throws IOException {
正在调用的地图条目如下所示:

if (testChoices.containsKey(K)) {
        String method = testChoices.get(K);
        System.out.println(method);

        try {
            java.lang.reflect.Method m = TST.getClass().getMethod(method);
            m.invoke(testChoices.getClass());
        } catch (NoSuchMethodException e1) {
            // TODO Auto-generated catch block
            System.out.println("No method found");
            e1.printStackTrace();
        } catch (SecurityException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();


        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
testChoices.put(1, "testgetDomainLic");

我不是专家,但是试着改变你的方法,这样它就不是私人的了


私有方法可以由反射调用,但还有额外的步骤。请参见

我不是专家,但请尝试更改您的方法,使其不是私有的


私有方法可以由反射调用,但还有额外的步骤。看

我想你可以换成
getMethod
仅返回公共方法

这里的问题在于,除了是否返回非公共方法之外,它们实际上具有不同的语义
getDeclaredMethod
仅包括声明的而非继承的方法

例如:

class Foo { protected void m() {} }
class Bar extends Foo {}
Foo actuallyBar = new Bar();
// This will throw NoSuchMethodException
// because m() is declared by Foo, not Bar:
actuallyBar.getClass().getDeclaredMethod("m");
在这种最坏的情况下,您必须遍历所有声明的方法,如下所示:

Class<?> c = obj.getClass();
do {
    for (Method m : c.getDeclaredMethods())
        if (isAMatch(m))
            return m;
} while ((c = c.getSuperclass()) != null);
Class c=obj.getClass();
做{
对于(方法m:c.getDeclaredMethods())
如果(匹配(m))
返回m;
}while((c=c.getSuperclass())!=null);
或者考虑接口(主要是因为它们现在可以声明静态方法):


List我想在你的情况下,你可以改成
getMethod
仅返回公共方法

这里的问题在于,除了是否返回非公共方法之外,它们实际上具有不同的语义
getDeclaredMethod
仅包括声明的而非继承的方法

例如:

class Foo { protected void m() {} }
class Bar extends Foo {}
Foo actuallyBar = new Bar();
// This will throw NoSuchMethodException
// because m() is declared by Foo, not Bar:
actuallyBar.getClass().getDeclaredMethod("m");
在这种最坏的情况下,您必须遍历所有声明的方法,如下所示:

Class<?> c = obj.getClass();
do {
    for (Method m : c.getDeclaredMethods())
        if (isAMatch(m))
            return m;
} while ((c = c.getSuperclass()) != null);
Class c=obj.getClass();
做{
对于(方法m:c.getDeclaredMethods())
如果(匹配(m))
返回m;
}while((c=c.getSuperclass())!=null);
或者考虑接口(主要是因为它们现在可以声明静态方法):


list是在TST类中定义的静态方法
testgetDomainLic()
,还是在它的超级接口中?TST只是testgetDomainLic()所在的类的一个实例。我将其更改为使用class.forName调用直接定义的类,但它仍然找不到该方法。静态方法是
testgetDomainLic()
是在TST类中定义的,还是在它的超级接口中定义的?TST只是testgetDomainLic()所在的类的一个实例。我将其更改为使用class.forName调用直接定义的类,但它仍然找不到方法。OP得到的是NoSuchMethodException。问题在于
getMethod()
——而不是
invoke()
。此外,我尝试删除Private,但仍然会出现相同的错误OK。所以我正在解决他们还没有解决的问题!OP得到一个NoSuchMethod异常。问题在于
getMethod()
——而不是
invoke()
。此外,我尝试删除Private,但仍然会出现相同的错误OK。所以我正在解决他们还没有解决的问题!