无法获取具有不同初始化的字段



我遇到了一些问题。我不知道这样的召唤是什么。

class test{
    JButton button=new JButton("button");
    JFileChooser fc=new JFileChooser() {
        @Override
        public void approveSelection(){
        File f = getSelectedFile();
           if(f.exists() && getDialogType() == SAVE_DIALOG){
                int result = JOptionPane.showConfirmDialog(this,"The file exists, overwrite?","Existing file",JOptionPane.YES_NO_CANCEL_OPTION);
                switch(result){
                    case JOptionPane.YES_OPTION:
                        super.approveSelection();
                        return;
                    case JOptionPane.NO_OPTION:
                        cancelSelection();
                        return;
                    case JOptionPane.CLOSED_OPTION:
                        return;
                    case JOptionPane.CANCEL_OPTION:
             //         cancelSelection();
                        return;
                }
            }
            super.approveSelection();
        }        
    };
    void test()
    {
    }
}

所以,我必须使用反射来获得字段:

Class cls=Class.forName("test");
Field[]field=cls.getDeclaredFields();
for(Field f : field) {
    System.out.println (f.getType().getSimpleName()+ " : "+ f.getType());
}

输出如下:

JButton : class javax.swing.JButton
JFileChooser : class javax.swing.JFileChooser

那么,如何在JFileChooser的initialize中获取所有Object类,如File fJOptionPane等?

下面是一个使用BCEL的示例代码。匿名内部类具有独立于外部类的名称。在本示例中,其名称为stackoverflow.SampleBcel$1

package stackoverflow;
import java.io.*;
import org.apache.bcel.*;
public class SampleBcel {
    Runnable runnable = new Runnable() {
        File f = new File("temp.txt");
        @Override
        public void run() {
            try (BufferedReader r = new BufferedReader(new FileReader(f))) {
                String line;
                while ((line = r.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
    public static void main(String[] args) throws ClassNotFoundException {
        JavaClass runnableClass = Repository.lookupClass("stackoverflow.SampleBcel$1");
        System.out.println(runnableClass); 
        Method[] runnableMethods = runnableClass.getMethods();
        for (Method method : runnableMethods) {
            System.out.println("** method : " + method);
            LocalVariableTable local = method.getLocalVariableTable();
            System.out.println(local);
        }
    }
}

结果:

class stackoverflow.SampleBcel$1 extends java.lang.Object
implements      java.lang.Runnable
filename        stackoverflow.SampleBcel$1
compiled from       SampleBcel.java
compiler version    52.0
access flags        32
constant pool       87 entries
ACC_SUPER flag      true
Attribute(s):
    SourceFile(SampleBcel.java)
    (Unknown attribute EnclosingMethod: 00 54 00 00)
    InnerClass:stackoverflow.SampleBcel$1("<not a member>", "<anonymous>")
2 fields:
    java.io.File f
    final synthetic stackoverflow.SampleBcel this$0
2 methods:
    void <init>(stackoverflow.SampleBcel)
    public void run()
** method : void <init>(stackoverflow.SampleBcel)
LocalVariable(start_pc = 0, length = 23, index = 0:stackoverflow.SampleBcel$1 this)
** method : public void run()
LocalVariable(start_pc = 0, length = 94, index = 0:stackoverflow.SampleBcel$1 this)
LocalVariable(start_pc = 23, length = 41, index = 3:java.io.BufferedReader r)
LocalVariable(start_pc = 26, length = 8, index = 4:String line)
LocalVariable(start_pc = 41, length = 3, index = 4:String line)
LocalVariable(start_pc = 89, length = 4, index = 1:java.io.IOException e)

相关内容

  • 没有找到相关文章