java - get project Linked resources path from eclipse-plugin run -
i writing eclipse-plugin accesses opened projects in eclipse. in these projects need access source files, unfortunately can in different location project working directory.
consider creating new project existing sources:
the new project in folder c:/users/username/runtime-eclipseapplication/jabref
whereas source files reside in c:/users/username/downloads/git/jabref/
this creates following entry in .project-file:
... <linkedresources> ... <link> <name>java</name> <type>2</type> <location>c:/users/username/downloads/git/jabref/src/main/java</location> </link> </linkedresources> ...
and .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 current codes gives me filepath this: net/sf/jabref/imports/msbibimporter.java
(which findbugs buginstance via bug.getprimarysourcelineannotation().getsourcepath()
).
my goal find corresponding git directory file using if(new repositorybuilder().findgitdir(new file(filepath)).getgitdir() != null)
all approaches gave me paths within project direcotry "c:/users/username/runtime-eclipseapplication/jabref/jabref/java" not exist physically.
i have access project via iproject project
.
i got work following 3 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 method https://stackoverflow.com/a/7727264/455578
iresource getresource(iproject project, string folderpath, string filename) { ijavaproject javaproject = javacore.create(project); try { (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 source path return null; }
Comments
Post a Comment