Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine absolute file path of a Java source file in Eclipse plugin?

In my custom Eclipse plugin, I have the fully qualified class name of a Java class as a String, and want to know its actual file path. In fact, I want to know the name of the source folder it resides in.

The class could be from any of the Java projects in the Workspace. The source folder names are arbitrary.

I use Eclipse 3.6.

Thanks!

like image 525
barfuin Avatar asked Jun 01 '12 15:06

barfuin


2 Answers

You will have to use the search engine API. See org.eclipse.jdt.core.search.SearchEngine.

You can see that there are various static functions you can call, each with their own options. You will need to create an appropriate org.eclipse.jdt.core.search.SearchPattern and then pass it to the search engine along with a scope (the workspace) and a requestor (something that gathers all of the results).

Typically, you will get a bunch of stuff back, like ITypes, which are the public API for accessing types in the Java model. You can call IType.getResource().getLocation() to get the filesystem location of any type. The getResource method may return null, so you need to check for that.

like image 126
Andrew Eisenberg Avatar answered Sep 29 '22 08:09

Andrew Eisenberg


You will need to use the JDT API stuff to get to the IResource of the Java class. From there you can use the Resource API to get the containing folders and whatever else you need.

like image 31
Francis Upton IV Avatar answered Sep 29 '22 07:09

Francis Upton IV