java - 无法获得具有不同字段的初始化

我有一些问题。我不知道这是什么叫。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等?

回答:

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

以上是 java - 无法获得具有不同字段的初始化 的全部内容, 来源链接: utcz.com/qa/262587.html

回到顶部