基本上,这是与this one in .NET相同的问题.我不想列出类中的字段,而是列出在类的给定方法中使用的字段.
例:
public class A {
int a;
int b;
public int bob(){
return a-b;
}
我想得到这样的字段:
Fields[] fields = FieldReader.(A.class.getMethod("bob"));
因此,字段[0] = A.a和字段[1] = A.b
最佳答案
以下是javassist的示例(您需要将其添加为依赖项,具体取决于您的依赖项管理器首选项).
原文链接:https://www.f2er.com/java/437283.html此代码列出了在public void doSomething()中访问的字段;方法.
package bcm;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.NotFoundException;
import javassist.bytecode.InstructionPrinter;
public class Person {
String name;
String surname;
int age;
boolean candrink = false;
public Person(String name,String surname,int age) {
super();
this.name = name;
this.surname = surname;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void doSomething() {
if (this.age > 18) {
candrink = true;
}
}
public static void main(String[] args) throws IOException,CannotCompileException {
ClassPool pool = ClassPool.getDefault();
try {
CtClass cc = pool.get("bcm.Person");
CtMethod m = cc.getDeclaredMethod("doSomething",null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
InstructionPrinter i = new InstructionPrinter(ps);
i.print(m);
String content = baos.toString();
for (String line : content.split("\\r?\\n")) {
if (line.contains("getfield")) {
System.out.println(line.replaceAll("getfield ",""));
}
}
} catch (NotFoundException e) {
e.printStackTrace();
}
}
}
HTH