使用 Java Reflections 调用 invoke 方法时出现 IllegalArgumentException

发布于 2024-11-24 02:25:27 字数 761 浏览 1 评论 0原文

我有一个具有如下方法的类:-

public void setCurrencyCode(List<String> newCurrencycode){
    this.currencycode = newCurrencycode;
}

我正在使用 Java Relections 来调用此方法,如下所示:-

try {
    List<String> value = new ArrayList<String>();
    value.add("GB");

    Class<?> clazz = Class.forName( "com.xxx.Currency" );
    Object obj = clazz.newInstance();
    Class param[] = { List.class };
    Method method = obj.getClass().getDeclaredMethod( "setCurrencyCode", param );
    method.invoke( value );
} catch(Exception e) {
    System.out.println( "Exception : " + e.getMessage() );
}

但是,“调用”调用时引发异常:- java.lang.IllegalArgumentException:对象不是声明类的实例

有什么想法吗?

谢谢莎拉

I have a class that has a method as follows :-

public void setCurrencyCode(List<String> newCurrencycode){
    this.currencycode = newCurrencycode;
}

I am using Java Relections to invoke this method as follows :-

try {
    List<String> value = new ArrayList<String>();
    value.add("GB");

    Class<?> clazz = Class.forName( "com.xxx.Currency" );
    Object obj = clazz.newInstance();
    Class param[] = { List.class };
    Method method = obj.getClass().getDeclaredMethod( "setCurrencyCode", param );
    method.invoke( value );
} catch(Exception e) {
    System.out.println( "Exception : " + e.getMessage() );
}

However, an exception is raised on the "invoke" call :-
java.lang.IllegalArgumentException: object is not an instance of declaring class

Any ideas?

Thanks

Sarah

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戒ㄋ 2024-12-01 02:25:27

您没有调用 invoke() 正确:invoke() 期望目标对象作为第一个参数,然后是方法调用的参数作为以下参数(因为 java 1.5,它是一个 varargs 参数)

试试这个:

try 
    {
        List<String> value = new ArrayList<String>();
        value.add("GB");

        Class<?> clazz = Class.forName( "com.xxx.Currency" );
        Object obj = clazz.newInstance();
        // Since java 1.5, you don't need Class[] for params: it's a varargs now 
        Method method = clazz.getDeclaredMethod( "setCurrencyCode", List.class ); // you already have a reference to the class - no need for obj.getClass()
        method.invoke( obj, value ); // invoke expects the target object, then the parameters
    }
    catch(Exception e)
    {
        System.out.println( "Exception : " + e.getMessage() );
    }
}

You are not calling invoke() correctly: invoke() expects the target object as the first parameter, then the parameters to the method call as the following parameters (since java 1.5, it's a varargs parameter)

Try this:

try 
    {
        List<String> value = new ArrayList<String>();
        value.add("GB");

        Class<?> clazz = Class.forName( "com.xxx.Currency" );
        Object obj = clazz.newInstance();
        // Since java 1.5, you don't need Class[] for params: it's a varargs now 
        Method method = clazz.getDeclaredMethod( "setCurrencyCode", List.class ); // you already have a reference to the class - no need for obj.getClass()
        method.invoke( obj, value ); // invoke expects the target object, then the parameters
    }
    catch(Exception e)
    {
        System.out.println( "Exception : " + e.getMessage() );
    }
}
终遇你 2024-12-01 02:25:27

这意味着您传递给 invokevalue 对象不是定义 method 的类的实例。这是因为 invoke 的第一个参数是要调用的对象,后续参数是被调用方法的参数。 (在这种情况下,值看起来需要是 com.xxx.Currency 的实例 - 当然它不是,因为它是一个 List

) '正在调用一个非静态方法(并且会遇到创建新实例的麻烦),然后在 try 块结束时调用 obj.setCurrencyCode(value) 的反射等效方法' d 需要打电话

method.invoke(obj, value)

代替您当前的单身单参数调用。

This means that the value object you pass into invoke is not an instance of the class on which the method is defined. This is because the first argument of invoke is the object on which to make the call, and the subsequent arguments are the parameters to the invoked method. (In this case it looks like value needs to be an instance of com.xxx.Currency - which of course it isn't, because it's a List.)

Since you're calling a non-static method (and going to to trouble of creating a new instance), then for the reflective equivalent of obj.setCurrencyCode(value), at the end of your try block you'd need to call

method.invoke(obj, value)

instead of your current single one-arg call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文