[英]select node type in eclipse.jdt.core AST for external object
如果我們從 class 創建一個 object 然后在編譯時調用它的方法並將其添加到 AST 樹中,我們應該使用什么樣的節點?
例如: new PasswordFormate().format();
這個節點在 AST 中的類型是什么?
AST 中該節點的子節點類型是什么?
您可以通過實現 ASTVisitor 來了解 AST 的“外觀”。 ASTVisitors 有一個稱為preVisit
(和postVisit
)的方法,您可以使用它以您喜歡的任何形式打印 AST(例如,各個 ASTNode-Object 的類)。
Take a look at this: http://www.eclipse.org/articles/article.php?file=Article-JavaCodeManipulation_AST/index.html
我會 go 通過使用上述兩種方法實現 ASTVisitor 並將樹打印為字符串。 然后你可以看到使用了哪些 ASTNode 類。
要啟動您實現的 AST-visitor,請使用 visit 方法:
ASTParser astParser = ASTParser.newParser(AST.JLS8); // Or use whatever constant applies here
astParser.setKind(ASTParser.K_COMPILATION_UNIT);
astParser.setSource(unit);
astParser.setResolveBindings(true); // maybe false, depending on your use case?
CompilationUnit compilationUnit = (CompilationUnit) astParser.createAST(null);
ASTVisitor astPrintingVisitor = new ASTPrinter(); // you need to implement this
compilationUnit.visit(astPrintingVisitor);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.