barfuin
barfuin

Reputation: 17474

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!

Upvotes: 5

Views: 763

Answers (2)

Andrew Eisenberg
Andrew Eisenberg

Reputation: 28757

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.

Upvotes: 3

Francis Upton IV
Francis Upton IV

Reputation: 19443

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.

Upvotes: 2

Related Questions