Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » FINDING ALL PROJECTS AND ALL PACKAGES
FINDING ALL PROJECTS AND ALL PACKAGES [message #259921] Fri, 01 May 2009 14:40 Go to next message
Kap Pak is currently offline Kap PakFriend
Messages: 52
Registered: July 2009
Member
Hi,

I have the following questions.

1)
I want to have all the java projects in my workspace in an array form so
that i can process them one after the other. The following gives me only a
specific project:

IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject("talkProject");

but i want to have all the projects in an array and process them.


2)
Like the first question, i would like to have also all the packages in a
specific project in an array form and process them also one after the
other. i tried the following code snippet but didn't give me the array of
all the packages in the project.

IJavaProject javaProject = JavaCore.create(project);
IPackageFragment[] pk = javaProject.getPackageFragments();




Thank you all.


Regards,
Eddy.
Re: FINDING ALL PROJECTS AND ALL PACKAGES [message #259926 is a reply to message #259921] Fri, 01 May 2009 16:07 Go to previous messageGo to next message
Olivier Thomann is currently offline Olivier ThomannFriend
Messages: 518
Registered: July 2009
Senior Member
Eddy Freeman a écrit :
> IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
> IProject project = root.getProject("talkProject");
Try:
org.eclipse.core.resources.IWorkspaceRoot.getProjects()

Then you need to use JavaCore.create(IProject) in order to get java project.

> 2)
> Like the first question, i would like to have also all the packages in a
> specific project in an array form and process them also one after the
> other. i tried the following code snippet but didn't give me the array
> of all the packages in the project.
>
> IJavaProject javaProject = JavaCore.create(project);
> IPackageFragment[] pk = javaProject.getPackageFragments();

This should work. What packages are missing?
--
Olivier
Re: FINDING ALL PROJECTS AND ALL PACKAGES [message #259930 is a reply to message #259926] Fri, 01 May 2009 16:40 Go to previous messageGo to next message
Kap Pak is currently offline Kap PakFriend
Messages: 52
Registered: July 2009
Member
Hi ,

I have the following code snipet but i get an "ArrayOutOfIndex
Exception" ..
I want to get the first class in the first package but i get
exception

IPackageFragment[] pk = null;
try {
pk = javaProject.getPackageFragments();
} catch (JavaModelException e1) {
e1.printStackTrace();
}

ICompilationUnit[] ic = null;
try {
ic = pk[0].getCompilationUnits();
} catch (JavaModelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// This line gives me "ArrayOutOfIndexException"
ICompilationUnit lwCompilationUnit = ic[0].getPrimary();

return lwCompilationUnit;


Can you help me to find the solution.

Thanks



Eddy.
Re: FINDING ALL PROJECTS AND ALL PACKAGES [message #259948 is a reply to message #259930] Sat, 02 May 2009 03:34 Go to previous messageGo to next message
Walter Harley is currently offline Walter HarleyFriend
Messages: 847
Registered: July 2009
Senior Member
"Eddy Freeman" <win1for@yahoo.com> wrote in message
news:4a2bb1c2c714f39505c78ba45084ca18$1@www.eclipse.org...
> Hi ,
>
> I have the following code snipet but i get an "ArrayOutOfIndex
> Exception" ..
> I want to get the first class in the first package but i get
> exception
> IPackageFragment[] pk = null;
> try {
> pk = javaProject.getPackageFragments();
> } catch (JavaModelException e1) {
> e1.printStackTrace();
> }
>
> ICompilationUnit[] ic = null;
> try {
> ic = pk[0].getCompilationUnits();
> } catch (JavaModelException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
>
> // This line gives me "ArrayOutOfIndexException" ICompilationUnit
> lwCompilationUnit = ic[0].getPrimary();
>
> return lwCompilationUnit;
>
>
> Can you help me to find the solution.


Have you carefully read the javadoc for IJavaProject, IPackageFragment, and
ICompilationUnit?

A package fragment does not have to contain any compilation units; it could
be empty, or it could contain only class files. What is the actual package
fragment that you are getting - i.e., what does pk[0].getElementName()
return? What compilation units do you expect it to contain?
Re: FINDING ALL PROJECTS AND ALL PACKAGES [message #259952 is a reply to message #259948] Sat, 02 May 2009 12:12 Go to previous messageGo to next message
Kap Pak is currently offline Kap PakFriend
Messages: 52
Registered: July 2009
Member
Hello,


I did the following:

System.out.println("pacakge: " + pk.length + " name: " +
pk[0].getElementName());


and get this results:

"pacakge: 678 name: "


There is only one package in the project's "src" folder and only four
classes in that package.

Should i access the "src" folder before i can access the packages inside
them? Is that why i get this strange results. I read the
IPackageFramentsRoots but i don't understand it well. Should i use it
before i use the IPackageFrament?

Thanks




Eddy.
Re: FINDING ALL PROJECTS AND ALL PACKAGES [message #259959 is a reply to message #259952] Sat, 02 May 2009 18:30 Go to previous messageGo to next message
Walter Harley is currently offline Walter HarleyFriend
Messages: 847
Registered: July 2009
Senior Member
"Eddy Freeman" <win1for@yahoo.com> wrote in message
news:5ebdc79ffc187e4e4d477a794b174f74$1@www.eclipse.org...
> Hello,
>
>
> I did the following:
>
> System.out.println("pacakge: " + pk.length + " name: " +
> pk[0].getElementName());
>
> and get this results:
>
> "pacakge: 678 name: "
>
>
> There is only one package in the project's "src" folder and only four
> classes in that package.


Package fragments include all packages in the classpath. Not just source
files, but everything in every jar on the classpath, including rt.jar (the
Java runtimes). So for instance you will see java.lang,
java.util.collection, etc... That is what I meant when, in an earlier post,
I told you that the Java Model includes all the classes on the classpath,
not just the source files. And that is what I meant when I told you that
not every package contains compilation units.

The default package (that is, the package that contains classes that don't
have a 'package' statement) has an empty name. This is stated in the
javadoc for IPackageFragment.getElementName(), I believe. Sounds like
that's the first one on the list. I imagine that it always shows up on the
list even if there are no classes in it.

The lesson here is that, when your code doesn't work as expected, you might
want to take a look with the debugger (or with System.out.println) and see
what's going on. As a programmer, your tools should be:

1. Carefully read the javadoc for all the classes you are considering
using, and for the other classes in the same packages.
2. Study other code that uses those classes. Google Code is useful here.
You can also import the source code of Eclipse plug-ins into your workspace
(in Plug-in Development perspective, bring up the Plug-ins view and
right-click on a plug-in, then Import As... Source Project, that's one way
to do it), and then just look for usages of the classes.
3. Use the debugger to step through your code to see what data you are
getting. THIS IS CRUCIAL. DEBUG DATA, NOT CODE. If you don't know what
data you are working with (i.e. in this case, what getPackageFragments() is
returning) you cannot possibly write correct code. Guessing does not work.
4. Write test cases and use assertions to validate each of your
assumptions, step by step.
5. When all else fails, search Google and/or post to a newsgroup to try to
get help.

This is pretty broad advice but I think most of the professional developers
on this group would agree that it reflects how they work. In other words,
experience proves that these steps (in this order) are necessary in order to
work efficiently.


> Should i access the "src" folder before i can access the packages inside
> them? Is that why i get this strange results. I read the
> IPackageFramentsRoots but i don't understand it well. Should i use it
> before i use the IPackageFrament?

You might want to, yes. That way you could restrict yourself to only
looking at package fragments that are in source folders.
IPackageFragmentRoot.getKind() will tell you whether the root is a K_SOURCE
or K_BINARY, that is, whether it refers to a source folder or to a binary
container such as a jar file.

If you know the names of the source folders already, then you could use the
IProject.getPackageFragmentRoot(IResource) form of the method, to get the
package fragment root corresponding to each source folder. That might run a
little quicker than getting all the roots and checking each one to see if
it's a source root.
Re: FINDING ALL PROJECTS AND ALL PACKAGES [message #260111 is a reply to message #259959] Mon, 11 May 2009 22:42 Go to previous message
Lars Vogel is currently offline Lars VogelFriend
Messages: 1098
Registered: July 2009
Senior Member

Hi Walter

your explanations in this thread were very interesting, thank you.

I created a little example to demonstrate the usage of JDT for finding
projects, package and source files, perhaps this helps someone:

http://www.vogella.de/articles/EclipseJDT/article.html

Best regards, Lars

Walter Harley wrote:
> "Eddy Freeman" <win1for@yahoo.com> wrote in message
> news:5ebdc79ffc187e4e4d477a794b174f74$1@www.eclipse.org...
>> Hello,
>>
>>
>> I did the following:
>>
>> System.out.println("pacakge: " + pk.length + " name: " +
>> pk[0].getElementName());
>>
>> and get this results:
>>
>> "pacakge: 678 name: "
>>
>>
>> There is only one package in the project's "src" folder and only four
>> classes in that package.
>
>
> Package fragments include all packages in the classpath. Not just source
> files, but everything in every jar on the classpath, including rt.jar (the
> Java runtimes). So for instance you will see java.lang,
> java.util.collection, etc... That is what I meant when, in an earlier post,
> I told you that the Java Model includes all the classes on the classpath,
> not just the source files. And that is what I meant when I told you that
> not every package contains compilation units.
>
> The default package (that is, the package that contains classes that don't
> have a 'package' statement) has an empty name. This is stated in the
> javadoc for IPackageFragment.getElementName(), I believe. Sounds like
> that's the first one on the list. I imagine that it always shows up on the
> list even if there are no classes in it.
>
> The lesson here is that, when your code doesn't work as expected, you might
> want to take a look with the debugger (or with System.out.println) and see
> what's going on. As a programmer, your tools should be:
>
> 1. Carefully read the javadoc for all the classes you are considering
> using, and for the other classes in the same packages.
> 2. Study other code that uses those classes. Google Code is useful here.
> You can also import the source code of Eclipse plug-ins into your workspace
> (in Plug-in Development perspective, bring up the Plug-ins view and
> right-click on a plug-in, then Import As... Source Project, that's one way
> to do it), and then just look for usages of the classes.
> 3. Use the debugger to step through your code to see what data you are
> getting. THIS IS CRUCIAL. DEBUG DATA, NOT CODE. If you don't know what
> data you are working with (i.e. in this case, what getPackageFragments() is
> returning) you cannot possibly write correct code. Guessing does not work.
> 4. Write test cases and use assertions to validate each of your
> assumptions, step by step.
> 5. When all else fails, search Google and/or post to a newsgroup to try to
> get help.
>
> This is pretty broad advice but I think most of the professional developers
> on this group would agree that it reflects how they work. In other words,
> experience proves that these steps (in this order) are necessary in order to
> work efficiently.
>
>
>> Should i access the "src" folder before i can access the packages inside
>> them? Is that why i get this strange results. I read the
>> IPackageFramentsRoots but i don't understand it well. Should i use it
>> before i use the IPackageFrament?
>
> You might want to, yes. That way you could restrict yourself to only
> looking at package fragments that are in source folders.
> IPackageFragmentRoot.getKind() will tell you whether the root is a K_SOURCE
> or K_BINARY, that is, whether it refers to a source folder or to a binary
> container such as a jar file.
>
> If you know the names of the source folders already, then you could use the
> IProject.getPackageFragmentRoot(IResource) form of the method, to get the
> package fragment root corresponding to each source folder. That might run a
> little quicker than getting all the roots and checking each one to see if
> it's a source root.
>
>
>
>
Previous Topic:Batch Compiler: Method inlining
Next Topic:FIELD/CONTENT ASSIST and SYNTAX HIGHLIGHTING
Goto Forum:
  


Current Time: Sat Oct 19 09:04:45 GMT 2024

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

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

Back to the top