Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Getting all packages from a IJavaProject
Getting all packages from a IJavaProject [message #257037] Thu, 23 October 2008 16:55 Go to next message
Eclipse UserFriend
Originally posted by: eclipse-news.rizzoweb.com

What is the most straightforward way to get a collection or array of all
the packages from an IJavaProject? I need to present an
ElementListSelectionDialog whose elements are the packages, kind of like
what the New Java Class wizard does. I've looked at the code in
NewTypeWizardPage.choosePackage() but it depends on some other state in
the wizard and it's a bit hard to decipher.

Any help appreciated,
Eric
Re: Getting all packages from a IJavaProject [message #257041 is a reply to message #257037] Thu, 23 October 2008 17:48 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: eclipse-news.rizzoweb.com

I should clarify that I need only the packages that come from source
locations within the project, and I need to include empty packages, too.

On 10/23/2008 4:55 PM, Eric Rizzo wrote:
> What is the most straightforward way to get a collection or array of all
> the packages from an IJavaProject? I need to present an
> ElementListSelectionDialog whose elements are the packages, kind of like
> what the New Java Class wizard does. I've looked at the code in
> NewTypeWizardPage.choosePackage() but it depends on some other state in
> the wizard and it's a bit hard to decipher.
>
> Any help appreciated,
> Eric
Re: Getting all packages from a IJavaProject [message #257125 is a reply to message #257041] Thu, 30 October 2008 11:31 Go to previous messageGo to next message
Eclipse UserFriend
Hi Guy
I am not the definitive JDT expert !
The code above contains my solution,
hope it helps you

/**
* Get all the compilation unit corresponding to Java resources in
* the current Java project.
* That means classes and interfaces (.java)
* @return
*/
public Vector<ICompilationUnit> getUnitsOfInterest() {
IPackageFragmentRoot[] ipfr;
Vector<ICompilationUnit> icu = new Vector<ICompilationUnit>();
try {
// acces the package root for Java resources
ipfr = javaProject.getAllPackageFragmentRoots();
for (int i = 0; i < ipfr.length; i++) {
// select internal resources
if (!ipfr[i].isExternal()) {
// get the inner packages
IJavaElement [] ije = ipfr[i].getChildren();
for (int j = 0; j < ije.length; j++) {
// cast needed here
IPackageFragment pf = (IPackageFragment) ije[j];
// if there are Java resources inside
if (pf.containsJavaResources()) {
// search for compilation unit
ICompilationUnit [] tmp = pf.getCompilationUnits();
for (int k = 0; k < tmp.length; k++) {
ICompilationUnit cu = tmp[k];
icu.add(cu);
}
}
}
}
}
} catch (JavaModelException e) {
e.printStackTrace();
}
return icu;
}
Re: Getting all packages from a IJavaProject [message #257220 is a reply to message #257041] Wed, 05 November 2008 09:15 Go to previous message
Eclipse UserFriend
Originally posted by: eclipse-news.rizzoweb.com

On 10/23/2008 5:48 PM, Eric Rizzo wrote:
> I should clarify that I need only the packages that come from source
> locations within the project, and I need to include empty packages, too.
>
> On 10/23/2008 4:55 PM, Eric Rizzo wrote:
>> What is the most straightforward way to get a collection or array of all
>> the packages from an IJavaProject? I need to present an
>> ElementListSelectionDialog whose elements are the packages, kind of like
>> what the New Java Class wizard does. I've looked at the code in
>> NewTypeWizardPage.choosePackage() but it depends on some other state in
>> the wizard and it's a bit hard to decipher.

Well, I wrote some code to do this (see below) but I'm very surprised
there is not a more convenient API for this, which seems to me like a
pretty fundamental function of JDT. Really?



protected Object[] getPackages(IJavaProject project) {
Set<Object> packages = new HashSet<Object>();
Set<String> packageNames = new HashSet<String>();

if (project != null && project.exists()) {
for (IPackageFragmentRoot aSourceRoot : getSourceRoots(project)) {
try {
for (IJavaElement aPackage : aSourceRoot.getChildren()) {
String packageName = aPackage.getElementName();

if (checkDefaultPacakge(packageName) &&
checkDuplicatePackage(packageName, packageNames)) {
packageNames.add(packageName);
packages.add(aPackage);
}
}
} catch (JavaModelException ex) {
SkywayUIPlugin.getDefault().logError("Error getting packages from
source root " + aSourceRoot, ex); //$NON-NLS-1$
}
}
}

return packages.toArray();
}

protected boolean checkDefaultPacakge(String packageName) {
// Skip the default package unless showDefaultPacakge is true
return showDefaultPacakge || !"".equals(packageName); //$NON-NLS-1$
}

protected boolean checkDuplicatePackage(String packageName, Set<String>
packageNames) {
// Only allow duplicates if showDuplicateNames is true
return showDuplicateNames || !packageNames.contains(packageName);
}


protected IPackageFragmentRoot[] getSourceRoots(IJavaProject project) {
ArrayList<IPackageFragmentRoot> result = new
ArrayList<IPackageFragmentRoot>();
try {
IPackageFragmentRoot[] roots = project.getPackageFragmentRoots();
for (int i = 0; i < roots.length; i++) {
if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
result.add(roots[i]);
}
}
} catch (JavaModelException ex) {

}

return result.toArray(new IPackageFragmentRoot[result.size()]);
}
Previous Topic:Resolving conflicts
Next Topic:How not to export source folders
Goto Forum:
  


Current Time: Thu Jul 03 20:25:16 EDT 2025

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

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

Back to the top