【问题标题】:java.lang.NoSuchMethodException: [Ljava.lang.reflect.Method;.myMethod(java.lang.String, boolean)java.lang.NoSuchMethodException: [Ljava.lang.reflect.Method;.myMethod(java.lang.String, boolean)
【发布时间】:2019-06-28 07:56:04
【问题描述】:

我正在尝试从使用 Java 1.7 的类中获取方法。

非常奇怪的是,如果我打印 methodName 及其参数,与我使用的相同,但我总是得到: java.lang.NoSuchMethodException:

这是我的代码:

    public void invokeMethod(String className, String myMethod, List<Object> parametersMethod) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException{
    Class<?> cls = Class.forName(className);

    Method[] allMethods = cls.getDeclaredMethods();
    for(Method m : cls.getDeclaredMethods()){

        Type[] types = m.getParameterTypes();
        String tmp="";

        for (Type type : types) {
            tmp+=type+" ";
        }

        log.info(" " +m.getName()+" "+tmp); // 
    }
    Object obj = cls.newInstance();
    log.info("myMethod "+myMethod);

    Method m= allMethods.getClass().getMethod(myMethod, String.class, boolean.class); 
    log.info("m "+m.getName()+ "  "+m.getParameterTypes()+ "  "+m.getDefaultValue());
    m.invoke(obj, parametersMethod); }

这里是我试图调用的方法:

public void tryIt(String myString, boolean mybool) throws Exception {
       //Do something
}

log.info 打印:tryIt class java.lang.String boolean

但我明白了(当我尝试使用 Method m= allMethods.getClass().getMethod(myMethod, String.class, boolean.class);) 时:

java.lang.NoSuchMethodException: [Ljava.lang.reflect.Method;.tryIt(java.lang.String, boolean)

我尝试使用布尔值而不是布尔值,但没有任何变化。

invokeMethod 位于使用 Jboss 7 的 Web 服务上,我的所有课程都是 @StateLess

【问题讨论】:

    标签: java jboss stateless


    【解决方案1】:

    allMethodsMethod[] 类型,它没有方法tryIt(String, boolean)。你想打电话给getMethod()cls

    另外你调用的方法是错误的,因为Method.invoke() 需要一个参数数组而不是List,你可能需要这样的方法:

    public void invokeMethod(String className, String myMethod, Object... parametersMethod) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
        Class<?> cls = Class.forName(className);
    
        Object obj = cls.newInstance();
    
        Method m = cls.getMethod(myMethod, String.class, boolean.class);
        m.invoke(obj, parametersMethod);
    }
    

    可以这样调用:

    invokeMethod("com.example.MyClass", "tryIt", "SomeString", true);
    

    【讨论】:

    • 如果 allMethods 没有方法 tryIt,为什么 log.info(" " +m.getName()+" "+tmp);使用正确的参数打印方法 tryit?
    • @LizLamperouge 因为你正在迭代cls.getDeclaredMethods(),但后来你调用了这个cls.getDeclaredMethods().getClass().getMethod("tryIt", String.class, boolean.class) 的等价物。你想省略部分.getDeclaredMethods().getClass()
    • 好吧,它的工作有点......但不完全,事实上,如果在我的方法 tryIt 中,我使用像@EJB Otherclass otClass 这样的 EJB。 public void tryIt(String myString, boolean mybool) 抛出异常 { otClass,doOther();我得到一个 nullPointer.... 有什么我可以做的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    相关资源
    最近更新 更多