Eclipse插件开发:如何获取当前所选项目的路径?

Spo*_*ike 6 java eclipse eclipse-api

我正在编写一个插件来解析项目中的一堆文件.但目前我一直在寻找Eclipse API寻找答案.

插件的工作方式如下:每当我打开源文件时,我都会让插件解析源代码的相应构建文件(这可以通过缓存解析结果进一步开发).获取文件很简单:

public void showSelection(IWorkbenchPart sourcePart) {
    // Gets the currently selected file from the editor
    IFile file = (IFile) workbenchPart.getSite().getPage().getActiveEditor()
        .getEditorInput().getAdapter(IFile.class);
    if (file != null) {
        String path = file.getProjectRelativePath();
        /** Snipped out: Rip out the source path part
         * and replace with build path
         * Then parse it. */
    }
}
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是我必须使用硬编码字符串作为源文件和构建文件的路径.任何人都知道如何从Eclipse中检索构建路径?(顺便说一下,我在CDT工作).还有一种简单的方法来确定源文件的源路径(例如,"src"目录下的一个文件)是什么?

Mar*_*iar 1

您应该查看ICProject,尤其是 getOutputEntries 和 getAllSourceRoots 操作。本教程也有一些简短的示例。我与 JDT 合作,所以这就是我能做的。希望能帮助到你 :)