简体   繁体   中英

get project Linked resources path from eclipse-plugin run

I am writing an eclipse-plugin which accesses currently opened projects in eclipse. In these projects I need to access the source files, which unfortunately can also be in a different location than the project working directory.

Consider creating a new project from existing sources:

The new project is in the folder C:/Users/username/runtime-EclipseApplication/JabRef whereas the source files reside in C:/Users/username/Downloads/git/jabref/

This creates the following entry in the .project-File:

...
<linkedResources>
    ...
    <link>
        <name>java</name>
        <type>2</type>
        <location>C:/Users/username/Downloads/git/jabref/src/main/java</location>
    </link>
</linkedResources>
...

and a .classpath-File:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
  <classpathentry kind="src" output="classes" path="java"/>
  <classpathentry kind="src" output="classes" path="gen"/>
  <classpathentry kind="var" path="JRE_LIB" rootpath="JRE_SRCROOT" sourcepath="JRE_SRC"/>
 ...

Now my current codes gives me a filePath like this: net/sf/jabref/imports/MsBibImporter.java (Which I get from a FindBugs BugInstance via bug.getPrimarySourceLineAnnotation().getSourcePath() ).

My goal is to find the corresponding git directory for a file by using if(new RepositoryBuilder().findGitDir(new File(filePath)).getGitDir() != null)

All my approaches gave me paths within the project direcotry like "C:/Users/username/runtime-EclipseApplication/JabRef/JabRef/java" which do not exist physically.

I have access to the project via IProject project .

I got it to work with the following three lines

IPath path = project.getFile(bug.getPrimarySourceLineAnnotation().getSourcePath()).getProjectRelativePath();
IResource res = getResource(project, path.removeLastSegments(1).toString(), path.lastSegment());
File file = res.getLocation().toFile();

and a method from https://stackoverflow.com/a/7727264/455578

IResource getResource(IProject project, String folderPath, String fileName) {

    IJavaProject javaProject = JavaCore.create(project);
    try {
        for (IPackageFragmentRoot root : javaProject.getAllPackageFragmentRoots()) {
            IPackageFragment folderFragment = root.getPackageFragment(folderPath);
            IResource folder = folderFragment.getResource();
            if (folder == null || ! folder.exists() || !(folder instanceof IContainer)) {
                continue;
            }

            IResource resource = ((IContainer) folder).findMember(fileName);
            if (resource.exists()) {
                return resource;
            }
        }
    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // file not found in any source path
    return null;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM