我们如何根据注释获取类的成员变量

时间:2014-08-21 09:25:44

标签: java user-interface annotations

我已经创建了一个注释“isAnnotatedVariable”,它将有一个字符串值,我在另一个gui类中使用了这个注释,用于标签和按钮变量,我的想法是根据注释值对这些按钮和标签进行分组,这些是声明的那些变量

@isAnnotatedVariable(varName = "CRD")
        public javax.swing.JButton crdbtn;
        @isAnnotatedVariable(varName="getFiles")
        public javax.swing.JButton getFilesbtn;
        @isAnnotatedVariable(varName="last")
        public javax.swing.JButton lastbtn;
        @isAnnotatedVariable(varName="CRD")
        public javax.swing.JLabel crdlbl;
        @isAnnotatedVariable(varName="getFiles")
        public javax.swing.JLabel getFileslbl;
        @isAnnotatedVariable(varName="last")
        public javax.swing.JLabel lastlbl;
        public javax.swing.JPanel jPanel1; 

现在我想分组并让他们对下面提到的操作进行一些操作

 private void jButton3MouseClicked(java.awt.event.MouseEvent evt)
    {                                      
        try
        {
            for(Field field : TestUi.class.getFields())
            {
                isAnnotatedVariable s = field.getAnnotation(isAnnotatedVariable.class);
                if(s.varName().equals("CRD"))
                {
                    if(field.toString().contains("lbl"))
                        {
                            System.out.println("Label "+field);
                            Class<?> o = field.getType();
                            System.out.println(o.getName());
                            o = (Class<?>) ClassLoader.getSystemResource(field.toString()).getClass();
                            System.out.println(o);
                            //o = (Class<?>) Class.forName(field.toString());
                            o.getAnnotation(isAnnotatedVariable.class).varName();
                            //Object d= o.newInstance();
                            Method m =o.getDeclaredMethod("setText", cArg);
                            m.invoke(o, "Hello");
                        }
                }
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    } 

但是我遇到了异常

java.lang.NullPointerException
    at annotationexmaple.TestUi.jButton3MouseClicked(TestUi.java:187)
    at TestUi.access$2(TestUi.java:157)
    at TestUi$3.mouseClicked(TestUi.java:84)
    at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)    

其中第187行是o =(Class)ClassLoader.getSystemResource(field.toString())。getClass();

0 个答案:

没有答案
相关问题