Eclipse plugin 如何在JDT中获取某个方法的所有可见变量
我想开发一个Eclipse插件,它可以获取特定方法的所有可见变量。 例如:Eclipse plugin 如何在JDT中获取某个方法的所有可见变量,eclipse-plugin,eclipse-jdt,Eclipse Plugin,Eclipse Jdt,我想开发一个Eclipse插件,它可以获取特定方法的所有可见变量。 例如: public class testVariable { String test1; Object test2; void method_test1(){ int test3,test4; } void method_test2(){ int test5,test6; //get
public class testVariable {
String test1;
Object test2;
void method_test1(){
int test3,test4;
}
void method_test2(){
int test5,test6;
//get variable here!!!!
}
}
我只想得到可见变量是:
test1,test2,test5,test6
在方法method\u test2
中。我能做什么?实际上,JDT可以在插件之外使用,也就是说,它可以在独立的Java应用程序中使用
以下代码可以返回所需的变量:
public static void parse(char[] str) {
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(str);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
cu.accept(new ASTVisitor() {
public boolean visit(VariableDeclarationFragment var) {
System.out.println("variable: " + var.getName());
return false;
}
public boolean visit(MethodDeclaration md) {
if (md.getName().toString().equals("method_test2")) {
md.accept(new ASTVisitor() {
public boolean visit(VariableDeclarationFragment fd) {
System.out.println("in method: " + fd);
return false;
}
});
}
return false;
}
});
}
输出为:
variable: test1
variable: test2
in method: test5
in method: test6
查看更多的例子 请详细说明你的问题。上面代码中没有
方法\u test2
“可见”是什么意思?你是说private
,protected
,public
访问修饰符..不!这就是范围!在test5
中,test6
具有method\u test2
的范围,test3
,test4
具有method\u test1
的范围,因此,test3
,test4
是不可见的!你想知道哪些变量可以从特定的代码中访问吗?如果是,那么:您只需执行Ctrl+Space
即可获得该功能,而且:您为什么需要它?对不起!我的问题不清楚。我被编辑了@benzonico:我想编写EclipseJDT的插件