[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[news.eclipse.tools.jdt] Re: How to get the correct package declaration for a given IFolder

Truck Turner wrote:
Hi,

I have an IFolder instance and I need to find out the corresponding package declaration.

Given circumstances:
- the IFolder instance IS part of an IJavaProject
- the IFolder instance IS on the classpath
- the IFolder instance does NOT yet contain any other Java files (it only contains Java properties files)


Example:
- the getFullPath() method of the IFolder instance returns:
"/src/com/mycompany/test/nls"

- then I need a method to resolve the package declaration to:
"com.mycompany.test.nls"


Is there a simple/obvious way do that?

Thanks,
T^2
Rich'd suggestion is good and it ensures that the package exists.
However if you already know that the folder exists, this should do
the trick (and this should be faster):

IFolder folder = ...
IJavaElement element = JavaCore.create(folder);
if (element == null)
  return null;
if (element.getElementType == IJavaElement.PACKAGE_FRAGEMENT_ROOT)
  return ""; // default package
return element.getElementName();

-- Jerome