2015-06-27 25 views
0

我有一些問題。我不知道這是什麼叫。java - 無法獲得具有不同字段的初始化

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像File fJOptionPane等?

+0

你將打印在類聲明的所有字段的代碼。類中的代碼訪問的類與聲明的字段無關。 –

+0

那麼,我該怎麼做才能讓他們? – newbie

+1

你想達到什麼目的? –

回答

1

以下是使用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)