使用反射调用 PumpEvents 时出现 java.lang.illegalArgumentException

发布于 2024-12-14 02:34:47 字数 2195 浏览 1 评论 0原文

我正在尝试在 Java Applet 中制作模态框架,如下所示: http:// /www.java2s.com/Tutorial/Java/0240__Swing/Showthegivenframeasmodaltothespecifiedowner.htm。这段代码有 start() 函数,看起来像

public void start() throws Exception {
  Class<?> clazz = Class.forName("java.awt.Conditional");
  Object conditional = Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz },
      this);
  Method pumpMethod = Class.forName("java.awt.EventDispatchThread").getDeclaredMethod(
      "pumpEvents", new Class[] { clazz });
  pumpMethod.setAccessible(true);
  pumpMethod.invoke(Thread.currentThread(), new Object[] { conditional });
}.

当我调用时

 pumpMethod.invoke(Thread.currentThread(), new Object[] { conditional });

出现以下异常:

    java.lang.RuntimeException: java.lang.IllegalArgumentException: object is not an instance of declaring class
    at wizard.ModalFrameUtil.showAsModal(ModalFrameUtil.java:136)
    at wizard.WizardCore.showWizardFrame(WizardCore.java:206)
    at SelfRegistrationApplet$1.run(SelfRegistrationApplet.java:55)
    at SelfRegistrationApplet$1.run(SelfRegistrationApplet.java:35)
    at java.security.AccessController.doPrivileged(Native Method)
    at SelfRegistrationApplet.RunSelfRegistrationApplet(SelfRegistrationApplet.java:32)
    at SelfRegistrationApplet.init(SelfRegistrationApplet.java:26)
    at sun.applet.AppletPanel.run(AppletPanel.java:424)
    at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at wizard.ModalFrameUtil$EventPump.start(ModalFrameUtil.java:80)
    at wizard.ModalFrameUtil.showAsModal(ModalFrameUtil.java:133)
    ... 8 more

您能告诉我此调用中出了什么问题以及如何避免此异常吗?

I'm trying to make modal frame in Java Applet such like shown here: http://www.java2s.com/Tutorial/Java/0240__Swing/Showthegivenframeasmodaltothespecifiedowner.htm. This code have start() function, that looks like

public void start() throws Exception {
  Class<?> clazz = Class.forName("java.awt.Conditional");
  Object conditional = Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz },
      this);
  Method pumpMethod = Class.forName("java.awt.EventDispatchThread").getDeclaredMethod(
      "pumpEvents", new Class[] { clazz });
  pumpMethod.setAccessible(true);
  pumpMethod.invoke(Thread.currentThread(), new Object[] { conditional });
}.

When I call

 pumpMethod.invoke(Thread.currentThread(), new Object[] { conditional });

I have following exception:

    java.lang.RuntimeException: java.lang.IllegalArgumentException: object is not an instance of declaring class
    at wizard.ModalFrameUtil.showAsModal(ModalFrameUtil.java:136)
    at wizard.WizardCore.showWizardFrame(WizardCore.java:206)
    at SelfRegistrationApplet$1.run(SelfRegistrationApplet.java:55)
    at SelfRegistrationApplet$1.run(SelfRegistrationApplet.java:35)
    at java.security.AccessController.doPrivileged(Native Method)
    at SelfRegistrationApplet.RunSelfRegistrationApplet(SelfRegistrationApplet.java:32)
    at SelfRegistrationApplet.init(SelfRegistrationApplet.java:26)
    at sun.applet.AppletPanel.run(AppletPanel.java:424)
    at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at wizard.ModalFrameUtil$EventPump.start(ModalFrameUtil.java:80)
    at wizard.ModalFrameUtil.showAsModal(ModalFrameUtil.java:133)
    ... 8 more

Could You please tell what is wrong in this invocation and how to avoid this exception?

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

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

发布评论

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

评论(2

喜爱纠缠 2024-12-21 02:34:47

它的意思是,Thread.currentThread() 返回的 Thread 对象不是 EventDispatchThread 的实例。

避免这个问题的方法是找出该对象的真正类是什么,并使用那个类来获取Method对象。 (您应该能够通过在尝试调用该方法的位置打印从 Thread.currentThread().getClass() 获取的对象来找出它是什么。


<代码>调用这表示:

“抛出IllegalArgumentException - 如果该方法是实例方法并且指定的对象参数不是声明底层方法(或其子类或其实现者)的类或接口的实例;如果实际参数和形式参数的数量不同;如果原始参数的展开转换失败,或者在可能的展开之后,参数值无法通过方法调用转换转换为相应的形式参数类型。”

我对您的代码的解读是,您拥有正确数量和类型的实际参数,因此这一定是线程类的问题。

What it is saying is that the Thread object returned by Thread.currentThread() is not an instance of EventDispatchThread.

The way to avoid the problem is to find out what the class of that object really is, and use that class to obtain the Method object. (You should be able to find out what it is by printing the object you get from Thread.currentThread().getClass() at the place where you are trying to invoke the method.


The Javadoc for invoke this says this:

"Throws IllegalArgumentException - if the method is an instance method and the specified object argument is not an instance of the class or interface declaring the underlying method (or of a subclass or implementor thereof); if the number of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal parameter type by a method invocation conversion."

My reading of your code is that you have the right number and type of actual arguments, so it must be an issue with the thread class.

白况 2024-12-21 02:34:47

AWT 和 swing GUI 是单线程的,事件调度线程是一个特殊的线程,所有 GUI 操作都应该在其中运行。您的方法很可能没有在 GUI 线程上调用。确保您在事件分派线程上调用您的方法,您可以通过像这样调用它来执行此

SwingUtilities.invokeAndWait(new Runnable(){public void run(){mymethod();}})

操作。 注意:javadocs 不包含 java.awt.EventDispatchThread,因此您可能依赖于某些实现细节。您可以改用 java.awt.EventQueueToolkit.getSystemEventQueue() 的子类。

AWT and swing GUIs are single-threaded the event dispatch thread is a special thread where all GUI operations should be run. It's likely that your method is not invoked on the GUI Thread. Make sure that you invoke your method on the event dispatch thread, you can do so by calling it like this

SwingUtilities.invokeAndWait(new Runnable(){public void run(){mymethod();}})

Note: The javadocs do not contain java.awt.EventDispatchThread so you likely depend on some implementation detail. You can use a subclass of java.awt.EventQueue and Toolkit.getSystemEventQueue() instead.

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