Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » How to get the correct package declaration for a given IFolder
How to get the correct package declaration for a given IFolder [message #227274] Wed, 05 April 2006 15:59 Go to next message
Eclipse UserFriend
Originally posted by: eclipse.lokahl.de

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
Re: How to get the correct package declaration for a given IFolder [message #227297 is a reply to message #227274] Wed, 05 April 2006 16:36 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: richkulp.us.NO_SPAM.ibm.com

From the IJavaProject for the project do:

IPackageFragment je = javaProject.findPackageFragment(folder.getFullPath());

And to get the IJavaProject:

IJavaProject javaProject = JavaCore.create(iproject);

--
Thanks,
Rich Kulp
Re: How to get the correct package declaration for a given IFolder [message #227316 is a reply to message #227297] Wed, 05 April 2006 20:07 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: eclipse.lokahl.de

Hi Rich,

thank you very much! That's just what I was looking for ;-)

I wrote myself a utility-method that now looks like the following and
works just fine:

public static String getJavaPackageNameFromFolder(IFolder aFolder){
IJavaProject aJavaProject = JavaCore.create(aFolder.getProject());
IPackageFragment myFragment = null;
try {
myFragment =
aJavaProject.findPackageFragment(aFolder.getFullPath());
} catch (JavaModelException e) {
// @TODO handle correctly
}
return myFragment.getElementName();
}


The findPackageFragment(..) call seems to be quite costly, though.

Thanks again,
T^2
Re: How to get the correct package declaration for a given IFolder [message #227335 is a reply to message #227274] Thu, 06 April 2006 08:24 Go to previous message
Jerome Lanneluc is currently offline Jerome LannelucFriend
Messages: 572
Registered: July 2009
Senior Member
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
Previous Topic:Cannot get APTDemo.jar (or any annotation processor to work) in eclipse
Next Topic:Plugin and Registry
Goto Forum:
  


Current Time: Tue Apr 23 13:06:26 GMT 2024

Powered by FUDForum. Page generated in 0.03100 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top