get(Object obj)字段不与实例变量一起使用

时间:2011-07-30 12:07:56

标签: java class reflection object illegalargumentexception

我很困惑为什么get(Object obj)不能与实例变量(实例字段)一起使用,并抛出IllegalArgumentException。

我有以下课程:

public class InfoSender {

String testInt = "hey";

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    InfoSender test = new InfoSender();
    test.asd();
    //        script g = new script();
    //        mobile h = new mobile();
}

private void asd() {
    new StatusGUI(this);
}

}

我有StatusGUI类(不包括initComponents):

public class StatusGUI extends javax.swing.JFrame {

Class clazz;

/** Creates new form StatusGUI */
public StatusGUI(Object invokerObject) {
    initComponents();
    setVisible(true);
    setLocationRelativeTo(null);
    clazz = invokerObject.getClass();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    try {
        Field[] fields = clazz.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            jTextArea1.append(fields[i].getName() + " | " + fields[i].getType() + " | " + fields[i].get(clazz));

        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}                                        
}

按下按钮,它将成功写入字段名称和类型。

当我想添加公共字段的值时,我很快就知道如果不将它们声明为静态(至少不是我所知),这是不可能的。

然而,根据Java Field Class文档,该字段可以成为实例字段: http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Field.html

异常的堆栈跟踪

java.lang.IllegalArgumentException: Can not set java.lang.String field infosender.InfoSender.testInt to java.lang.Class
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:150)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:37)
at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:18)
at java.lang.reflect.Field.get(Field.java:358)
at infosender.StatusGUI.jButton1ActionPerformed(StatusGUI.java:93)
at infosender.StatusGUI.access$000(StatusGUI.java:22)
at infosender.StatusGUI$1.actionPerformed(StatusGUI.java:54)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6289)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6054)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4652)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4482)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4482)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:644)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:603)
at java.awt.EventQueue$1.run(EventQueue.java:601)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:617)
at java.awt.EventQueue$2.run(EventQueue.java:615)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:614)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

感谢任何帮助:) 麦克

2 个答案:

答案 0 :(得分:10)

简而言之,问题是您使用Field.get()错误。你在说:

fields[i].get(clazz)

...其中clazz是您尝试从中获取字段值的Object的类。这就是为什么你只能看到静态字段。你想做的是这样的:

fields[i].get(invokerObject)

要获取每实例属性的值,您需要传递要从中提取值的Object实例, not Object的类<的实例/ em>的

请注意,您还可以在致电private之前致电protected获取fields[i].setAccessible(true)fields[i].get()字段的值。

答案 1 :(得分:4)

方法Field#get(Object)使实际对象从Class中提取值,而不是fields[i].get(invokerObject) 。所以它应该是

invokerObject

因此StatusGUI应设置为jButton1ActionPerformed中的字段,以便将其传递给您的{{1}}方法。