简体   繁体   English

使用Java Reflection调用带有ActionEvent参数的方法的最干净方法是什么?

[英]What is the cleanest way to use Java Reflection to invoke a method with an ActionEvent parameter?

Here is a very simplified example of what I am trying to accomplish. 这是我要完成的工作的非常简单的示例。 I am maintaining code that was written a long time ago by someone else and do not have the ability to change it. 我维护的是很久以前由其他人编写的代码,无法更改它。

import java.awt.event.ActionEvent;
import java.lang.reflect.Method;




public class Main {
      public static void main(String[] args) throws Exception {

            Class cls = Class.forName("Main");
            Main obj = (Main) cls.newInstance();

            Method m = cls.getDeclaredMethod("test", ActionEvent.class);

            m.invoke(obj, null); <--- throws an IllegalArgumentException
       }

       public void test(ActionEvent x) {
          System.out.println("Yeah");
       }
}

The above code throws java.lang.IllegalArgumentException: wrong number of arguments . 上面的代码抛出java.lang.IllegalArgumentException: wrong number of arguments I know I could pass new ActionEvent(new Object(), 0, null) as a parameter, but I am not sure this is the best/cleanest way to accomplish this. 我知道我可以将new ActionEvent(new Object(), 0, null)作为参数传递,但是我不确定这是实现此目的的最佳/最简洁的方法。 Note, the method test doesn't actually use the ActionEvent parameter. 注意,方法测试实际上并没有使用ActionEvent参数。

The problem here is that the null in 这里的问题是in中的null

m.invoke(obj, null);

is inferred as an argument of type Object[] which represents the collection of arguments to be passed to the method being invoked. 推断为Object[]类型的参数,该参数表示要传递给要调用的方法的参数的集合。

Instead, cast the null to Object if your intention was to simulate the following invocation 相反,如果您打算模拟以下调用,则将nullObject

obj.test(null);

That is equivalent to 那相当于

m.invoke(obj, (Object) null);

Well, you will have to call the method with the right parameter in one way or the other. 好吧,您将不得不以一种或另一种方式使用正确的参数来调用该方法。

Note however, that some time ago, I've written a reflective action . 但是请注意,前一段时间,我写了一个反思性的动作

try {  
    Action test = new ReflectiveXAction(this, "test");  
} catch (NoSuchMethodException ex) {  
    ex.printStackTrace();  
}  

...

test.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "someId"));

or 要么

test.actionPerformed(null);

Usually, the actionPerformed method gets called by a component such JButton, however. 通常,actionPerformed方法由诸如JButton之类的组件调用。

Maybe you find it useful. 也许您觉得它有用。 The library is Open Source. 该库是开源的。

Tutorial: http://www.softsmithy.org/lib/current/docs/tutorial/swing/action/index.html#reflective 教程: http//www.softsmithy.org/lib/current/docs/tutorial/swing/action/index.html#reflective

You can get the library either directly from Maven Central: 您可以直接从Maven Central获取该库:

<dependency>
    <groupId>org.softsmithy.lib</groupId>
    <artifactId>softsmithy-lib-swing</artifactId>
    <version>0.5</version>
</dependency>

or you can download it from here: http://sourceforge.net/projects/softsmithy/files/softsmithy/v0.5/ 或者您可以从此处下载: http : //sourceforge.net/projects/softsmithy/files/softsmithy/v0.5/

If you're using Java SE 8 (recommended) you could also consider to use lambdas/ method references, eg (untested): 如果您正在使用Java SE 8(推荐),则还可以考虑使用lambdas /方法引用,例如(未测试):

ActionListener test = this::test;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM