簡體   English   中英

Eclipse插件-查找源零件位置

[英]Eclipse plug-in - Finding source parts location

我正在編寫一個Eclipse插件,該插件應在Java編輯器中修改源代碼。 我如何確定源代碼部分的位置,例如

  • 類聲明
  • 進口
  • 類字段
  • 方法

所以。

您需要了解JDT在Eclipse中的工作方式。

您可以在插件中編寫如下內容:

IProject project = ResourcesPlugin.getWorkspace().getRoot()
    .getProject(PROJECT_NAME);
IJavaProject javaProject = JavaCore.create(project);
IType type = project.findType(TYPE_NAME);
ICompilationUnit icu = type.getCompilationUnit();

閱讀“ 操作Java代碼”以了解可以使用ICompilationUnit做什么。

如果需要更多選項,可以使用以下示例生成ICompilationUnit的AST:

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

請注意,將resolveBindings設置為true非常昂貴,因此僅在需要時才執行。 CompilationUnit是AST的根,您可以使用ASTVisitor進行訪問。 再次查看上文檔,了解如何使用AST。

在線閱讀文檔,檢查所涉及類型的API,並嘗試查找某些示例插件的源代碼。

您要修改抽象語法樹 (AST)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM