2010-02-24 8 views
1

컴파일러 단위로 ASTParser 유형의 파서를 만들었습니다. 이 파서를 사용하여이 특정 컴파일 유닛에있는 함수의 모든 변수 선언을 나열하려고합니다. ASTVisitor를 사용해야합니까? 그렇다면 어떻게 또는 다른 방법이 있습니까?변수 선언 가져 오기

답변

2

당신은 this thread

당신이 org.eclipse.jdt.core 플러그인에서 모양과가 특별히 ASTParser 클래스해야 다음 시도 할 수 있습니다 도움이됩니다.
그냥 파서를 시작, 다음 코드는 충분하다 : 작업 공간

ASTParser parser = ASTParser.newParser(AST.JLS3); 
parser.setKind(ASTParser.K_COMPILATION_UNIT); // you tell parser, that source is whole java file. parser can also process single statements 
parser.setSource(source); 
CompilationUnit cu = (CompilationUnit) parser.createAST(null); // CompilationUnit here is of type org.eclipse.jdt.core.dom.CompilationUnit 
// source is either char array, like this: 

public class A { int i = 9; int j; }".toCharArray() 

//org.eclipse.jdt.core.ICompilationUnit type, which represents java source files 

합니다. 대서양 표준시가 구축 된 후

,이 같은 ASTVisitor를 확장하는 방문자로를 통과 할 수

cu.accept(new ASTVisitor() { 
    public boolean visit(SimpleName node) { 
    System.out.println(node); // print all simple names in compilation unit. in our example it would be A, i, j (class name, and then variables) 
    return true; 
    } 
}); 

자세한 내용 및 코드 예제를 this thread

ASTParser parser = ASTParser.newParser(AST.JLS3); 
parser.setSource(compilationUnit); 
parser.setSourceRange(method.getSourceRange().getOffset(), method.getSourceRange().getLength()); 
parser.setResolveBindings(true); 
CompilationUnit cu = (CompilationUnit)parser.createAST(null); 
cu.accept(new ASTMethodVisitor()); 
+0

덕분에 이잖아 내가 필요한 것 – Steven