2

Eclipse 플러그인에서 CompilationUnit의 주석을 구문 분석하고 싶습니다. 내 다른 방문자 (예 : ForVisitor, VariableDeclarationVisitor 등)는 정상적으로 작동하지만 내 CommentVisitor는 아무 것도 반환하지 않습니다.ASTVisitor는 Eclipse 플러그인에서 아무런 답글도 반환하지 않습니다.

AST와 ASTVisitor 창조

void createAST(ICompilationUnit unit) throws JavaModelException { 
    CompilationUnit parse = parse(unit); 

    // return all comments 
    CommentVisitor visitor = new CommentVisitor(); 
    parse.accept(visitor); 
    System.out.println(parse.getCommentList().toString()); 
    for(LineComment lineComment : visitor.getLineComments()) { 
     lineComment.accept(visitor); // a try to make it work 
     System.out.println("Line Comment: " + lineComment.getLength()); 
    } 
    System.out.println("---------------------------------"); 
    for(BlockComment blockComment : visitor.getBlockComments()) { 
     System.out.println("Block Comment: " + blockComment.getLength()); 
    } 
} 

CompilationUnit parse(ICompilationUnit unit) { 
    ASTParser parser = ASTParser.newParser(AST.JLS4); 
    parser.setKind(ASTParser.K_COMPILATION_UNIT); 
    parser.setSource(unit); 
    parser.setResolveBindings(true); 
    return (CompilationUnit) parser.createAST(null); // parse 
} 

CommentVisitor.java (다른 모든 방문자와 같은 일반적으로 동일한 구문)

import java.util.ArrayList; 
import java.util.List; 

import org.eclipse.jdt.core.dom.ASTVisitor; 
import org.eclipse.jdt.core.dom.BlockComment; 
import org.eclipse.jdt.core.dom.LineComment; 

public class CommentVisitor extends ASTVisitor { 
    List<LineComment> lineComments = new ArrayList<LineComment>(); 
    List<BlockComment> blockComments = new ArrayList<BlockComment>(); 

    @Override 
    public boolean visit(LineComment node) { 
      lineComments.add(node); 
      return super.visit(node); 
    } 

    @Override 
    public boolean visit(BlockComment node) { 
      blockComments.add(node); 
      return super.visit(node); 
    } 

    public List<LineComment> getLineComments() { 
      return lineComments; 
    } 

    public List<BlockComment> getBlockComments() { 
      return blockComments; 
    } 
} 

것은 명확히하기를 (다른 모든 방문자를 작동) 내 문제는 다시 : 나는 위의 코드에서 어떤 반응도 얻지 못한다. 빈 문자열조차도 여기에있는 다른 몇 가지 질문의 주제였다.

답변

0

답변을 찾으려면 page을 참조하십시오.

public boolean visit(LineComment node) { 
     int start = node.getStartPosition(); 
     int end = start + node.getLength(); 
     // source is a string representing your source code 
     String comment = source.substring(start, end); 
     System.out.println(comment); 
     return true; 
    } 
관련 문제