使用反射调用 PumpEvents 时出现 java.lang.illegalArgumentException
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它的意思是,
Thread.currentThread()
返回的Thread
对象不是EventDispatchThread
的实例。避免这个问题的方法是找出该对象的真正类是什么,并使用那个类来获取
Method
对象。 (您应该能够通过在尝试调用该方法的位置打印从 Thread.currentThread().getClass() 获取的对象来找出它是什么。<代码>调用这表示:
我对您的代码的解读是,您拥有正确数量和类型的实际参数,因此这一定是线程类的问题。
What it is saying is that the
Thread
object returned byThread.currentThread()
is not an instance ofEventDispatchThread
.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 fromThread.currentThread().getClass()
at the place where you are trying to invoke the method.The Javadoc for
invoke
this says this: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.
AWT 和 swing GUI 是单线程的,事件调度线程是一个特殊的线程,所有 GUI 操作都应该在其中运行。您的方法很可能没有在 GUI 线程上调用。确保您在事件分派线程上调用您的方法,您可以通过像这样调用它来执行此
操作。 注意:javadocs 不包含 java.awt.EventDispatchThread,因此您可能依赖于某些实现细节。您可以改用
java.awt.EventQueue
和Toolkit.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
Note: The javadocs do not contain
java.awt.EventDispatchThread
so you likely depend on some implementation detail. You can use a subclass ofjava.awt.EventQueue
andToolkit.getSystemEventQueue()
instead.