Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » JavaModelException when trying to get compilation units from JavaProject!
JavaModelException when trying to get compilation units from JavaProject! [message #250605] Fri, 11 January 2008 14:14 Go to next message
Eclipse UserFriend
Originally posted by: cassio.ufpe.gmail.com

Hello everyone,

I am writing a plug-in which need to manipulate all Java source files
located in the specified project.

When the method IJavaProject.getPackageFragments() is invoked it throws an
JavaModelException: Java Model Status [TestProject does not exist]. Take a
look at the code:

IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject("TestProject");
IJavaProject javaProject = JavaCore.create(project);
IPackageFragmentRoot[] fragmentRoots =
javaProject.getPackageFragmentRoots();

Is there any way to get all compilation units just located in this
project?

Thanks!
Cassio
Re: JavaModelException when trying to get compilation units from JavaProject! [message #250609 is a reply to message #250605] Fri, 11 January 2008 14:45 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: wharley.bea.com

"Cassio Melo" <cassio.ufpe@gmail.com> wrote in message
news:84342829a9e8d9e377f8daf284824482$1@www.eclipse.org...
> Hello everyone,
>
> I am writing a plug-in which need to manipulate all Java source files
> located in the specified project.
>
> When the method IJavaProject.getPackageFragments() is invoked it throws an
> JavaModelException: Java Model Status [TestProject does not exist]. Take a
> look at the code:
>
> IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
> IProject project = root.getProject("TestProject");
> IJavaProject javaProject = JavaCore.create(project);
> IPackageFragmentRoot[] fragmentRoots =
> javaProject.getPackageFragmentRoots();
>
> Is there any way to get all compilation units just located in this
> project?


First, does the project actually exist? JavaCore.create(), despite its
misleading name, does not create a project; it only creates an IJavaProject
handle around an IProject, which may or may not actually exist. To actually
create a project that doesn't exist, you'd need to call IProject.create()
and IProject.open(). You'd then need to take some additional steps to turn
it into a viable Java project, such as adding the right project natures and
builders, setting the classpath properly, and so forth.

If the project does already exist in the workspace, then we'll need to look
further to understand why you are getting the JavaModelException. This is
probably the most important thing to solve, because no JDT methods are going
to work right until you get that fixed.

That said, to get all compilation units located in the project, I think you
would want to iterate all package fragment roots looking for ones where
getKind() returns IPackageFragmentRoot.K_SOURCE. I don't *think* that will
return package fragment roots from source folders in upstream projects -
anyone else reading this want to comment?

For that matter, what is it you're trying to do? Processing all compilation
units seems like an unusual thing to do; more often, you'd want to process
only those that have changed, or those that don't meet some dependency
criterion, or those that contain some sort of element, etc. There may be
some existing Eclipse or JDT functionality to do what you're trying to do.
Let us know.
Re: JavaModelException when trying to get compilation units from JavaProject! [message #250616 is a reply to message #250609] Fri, 11 January 2008 17:48 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: cassio.ufpe.gmail.com

Thanks Walter, comments inline

Walter Harley wrote:

> "Cassio Melo" <cassio.ufpe@gmail.com> wrote in message
> news:84342829a9e8d9e377f8daf284824482$1@www.eclipse.org...
>> Hello everyone,
>>
>> I am writing a plug-in which need to manipulate all Java source files
>> located in the specified project.
>>
>> When the method IJavaProject.getPackageFragments() is invoked it throws an
>> JavaModelException: Java Model Status [TestProject does not exist]. Take a
>> look at the code:
>>
>> IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
>> IProject project = root.getProject("TestProject");
>> IJavaProject javaProject = JavaCore.create(project);
>> IPackageFragmentRoot[] fragmentRoots =
>> javaProject.getPackageFragmentRoots();
>>
>> Is there any way to get all compilation units just located in this
>> project?


> First, does the project actually exist? JavaCore.create(), despite its
> misleading name, does not create a project; it only creates an IJavaProject
> handle around an IProject, which may or may not actually exist. To actually
> create a project that doesn't exist, you'd need to call IProject.create()
> and IProject.open(). You'd then need to take some additional steps to turn
> it into a viable Java project, such as adding the right project natures and
> builders, setting the classpath properly, and so forth.

Yes the project exists, it is just a helloworld..

> If the project does already exist in the workspace, then we'll need to look
> further to understand why you are getting the JavaModelException. This is
> probably the most important thing to solve, because no JDT methods are going
> to work right until you get that fixed.

> That said, to get all compilation units located in the project, I think you
> would want to iterate all package fragment roots looking for ones where
> getKind() returns IPackageFragmentRoot.K_SOURCE. I don't *think* that will
> return package fragment roots from source folders in upstream projects -
> anyone else reading this want to comment?

> For that matter, what is it you're trying to do? Processing all compilation
> units seems like an unusual thing to do; more often, you'd want to process
> only those that have changed, or those that don't meet some dependency
> criterion, or those that contain some sort of element, etc. There may be
> some existing Eclipse or JDT functionality to do what you're trying to do.
> Let us know.

I'm trying to capture classes relationships (imports, method calls, etc)
in order to create a graph of dependencies among classes. By doing so, I
will be able to apply some clustering algorithms in those graphs to
identify possible reusable modules - which is my dissertation to get the
graduate degree! ;)

Any suggestion?

Thanks!
Cassio
Re: JavaModelException when trying to get compilation units from JavaProject! [message #250620 is a reply to message #250616] Fri, 11 January 2008 18:14 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: wharley.bea.com

"Cassio Melo" <cassio.ufpe@gmail.com> wrote in message
news:1d6b7a084ee77b03659abc77394d2584$1@www.eclipse.org...
>>> When the method IJavaProject.getPackageFragments() is invoked it throws
>>> an JavaModelException: Java Model Status [TestProject does not exist].
>>> Take a look at the code:
>>>
>>> IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
>>> IProject project = root.getProject("TestProject");
>>> IJavaProject javaProject = JavaCore.create(project);
>>> IPackageFragmentRoot[] fragmentRoots =
>>> javaProject.getPackageFragmentRoots();
>>>
>> First, does the project actually exist?
>
> Yes the project exists, it is just a helloworld..

So, if the project exists but javaProject.getPackageFragmentRoots() is
throwing the exception you cite, then something is not right. Do any of the
IProject methods work on it? For instance, if you call
project.getDescription(), do you get back what you'd expect?



>> For that matter, what is it you're trying to do? Processing all
>> compilation units seems like an unusual thing to do; more often, you'd
>> want to process only those that have changed, or those that don't meet
>> some dependency criterion, or those that contain some sort of element,
>> etc. There may be some existing Eclipse or JDT functionality to do what
>> you're trying to do. Let us know.
>
> I'm trying to capture classes relationships (imports, method calls, etc)
> in order to create a graph of dependencies among classes. By doing so, I
> will be able to apply some clustering algorithms in those graphs to
> identify possible reusable modules - which is my dissertation to get the
> graduate degree! ;)

You might also take a look at Doxygen, if you haven't already
(http://www.doxygen.org).

The Java compiler itself needs to track dependencies, in order to
efficiently do incremental compilation. Perhaps some of the Java Core
developers on this newsgroup can point you to where in the jdt.core code you
could look to see how that is handled. That wouldn't be exposed in the
public Java Model API, but for your purposes it may not be important to use
only public API.
Re: JavaModelException when trying to get compilation units from JavaProject! [message #250651 is a reply to message #250620] Sun, 13 January 2008 15:45 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: cassio.ufpe.gmail.com

Walter Harley wrote:

> "Cassio Melo" <cassio.ufpe@gmail.com> wrote in message
> news:1d6b7a084ee77b03659abc77394d2584$1@www.eclipse.org...
>>>> When the method IJavaProject.getPackageFragments() is invoked it throws
>>>> an JavaModelException: Java Model Status [TestProject does not exist].
>>>> Take a look at the code:
>>>>
>>>> IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
>>>> IProject project = root.getProject("TestProject");
>>>> IJavaProject javaProject = JavaCore.create(project);
>>>> IPackageFragmentRoot[] fragmentRoots =
>>>> javaProject.getPackageFragmentRoots();
>>>>
>>> First, does the project actually exist?
>>
>> Yes the project exists, it is just a helloworld..

> So, if the project exists but javaProject.getPackageFragmentRoots() is
> throwing the exception you cite, then something is not right. Do any of the
> IProject methods work on it? For instance, if you call
> project.getDescription(), do you get back what you'd expect?

Hmmm.. The project exists in the workspace, but the getProjects() is
returning an empty array (but not null), so when I try to get its
description i throws an Exception. I guess it is not getting any project
in the workspace because it is running another instance of eclipse
instead..( http://dev.eclipse.org/newslists/news.eclipse.platform/msg01 186.html)..
But I dont know how do I configure it to run with all the projects in the
"normal" workspace.. do you have any idea?

>>> For that matter, what is it you're trying to do? Processing all
>>> compilation units seems like an unusual thing to do; more often, you'd
>>> want to process only those that have changed, or those that don't meet
>>> some dependency criterion, or those that contain some sort of element,
>>> etc. There may be some existing Eclipse or JDT functionality to do what
>>> you're trying to do. Let us know.
>>
>> I'm trying to capture classes relationships (imports, method calls, etc)
>> in order to create a graph of dependencies among classes. By doing so, I
>> will be able to apply some clustering algorithms in those graphs to
>> identify possible reusable modules - which is my dissertation to get the
>> graduate degree! ;)

> You might also take a look at Doxygen, if you haven't already
> (http://www.doxygen.org).

> The Java compiler itself needs to track dependencies, in order to
> efficiently do incremental compilation. Perhaps some of the Java Core
> developers on this newsgroup can point you to where in the jdt.core code you
> could look to se
te how that is handled. That wouldn't be exposed in the
> public Java Model API, but for your purposes it may not be important to use
> only public API.
That would be great! Does anyone here have any other suggestion in this
sense? Thanks again!
Re: JavaModelException when trying to get compilation units from JavaProject! [message #250654 is a reply to message #250651] Mon, 14 January 2008 02:10 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: wharley.bea.com

"Cassio Melo" <cassio.ufpe@gmail.com> wrote in message
news:3ce7fb4d54605954b7a2786dd161de28$1@www.eclipse.org...
> Hmmm.. The project exists in the workspace, but the getProjects() is
> returning an empty array (but not null), so when I try to get its
> description i throws an Exception. I guess it is not getting any project
> in the workspace because it is running another instance of eclipse
> instead..( http://dev.eclipse.org/newslists/news.eclipse.platform/msg01 186.html)..
> But I dont know how do I configure it to run with all the projects in the
> "normal" workspace.. do you have any idea?


Generally, to work on an Eclipse application, you need a "host" workspace
that contains the plug-ins you are developing, and a "target" workspace that
will contain the sample projects that you are testing with. The Launch
Configuration dialog (e.g., Run -> Run As... or Debug -> Debug As...) allows
you to configure the location of the target workspace, so you can point it
at an existing workspace. You cannot, however, point it at the same
workspace that the host is running.

As a concrete example: I work on the JDT annotation processing plug-ins,
that is, org.eclipse.jdt.apt.*. So, I have a host workspace containing
those plug-in projects. Then, to test, I have a target workspace containing
some Java projects with code that has annotations. I start by launching my
host workspace, and doing whatever editing I need to do on my plug-in
projects. Notice that in that host instance of Eclipse, my plug-ins are not
*running*, they're just projects: that is, the functionality of my plug-ins
is not available in the host application. If I created a menu item, for
example, it would not show up in the host instance.

Then, I have a launch configuration defined that points to the location of
the target workspace. So, when I say Run -> Run As... -> Eclipse
Application, it launches the target workspace. In the target instance of
Eclipse, my plug-ins are running, just like any other Eclipse plug-in. So,
the menu item that I created in my host workspace would show up on the menu
of the target Eclipse instance. However, the target workspace does not
include my plug-in projects; in fact it does not contain any plug-in
projects at all.

So you see, I am drawing a distinction between the Eclipse *instance*, which
is a collection of plug-ins that are running, and the *workspace*, which
contains projects. The host instance runs the host workspace, which
contains the plug-in project code; the host instance launches the target
instance; the target instance runs the target workspace, in whatever
location it is configured for; the target instance is executing the plug-ins
in the host workspace.
Re: JavaModelException when trying to get compilation units from JavaProject! [message #250674 is a reply to message #250654] Mon, 14 January 2008 15:13 Go to previous message
Eclipse UserFriend
Originally posted by: cassio.ufpe.gmail.com

Walter Harley wrote:

> "Cassio Melo" <cassio.ufpe@gmail.com> wrote in message
> news:3ce7fb4d54605954b7a2786dd161de28$1@www.eclipse.org...
>> Hmmm.. The project exists in the workspace, but the getProjects() is
>> returning an empty array (but not null), so when I try to get its
>> description i throws an Exception. I guess it is not getting any project
>> in the workspace because it is running another instance of eclipse
>>
instead..( http://dev.eclipse.org/newslists/news.eclipse.platform/msg01 186.html)..
>> But I dont know how do I configure it to run with all the projects in the
>> "normal" workspace.. do you have any idea?


> Generally, to work on an Eclipse application, you need a "host" workspace
> that contains the plug-ins you are developing, and a "target" workspace that
> will contain the sample projects that you are testing with. The Launch
> Configuration dialog (e.g., Run -> Run As... or Debug -> Debug As...) allows
> you to configure the location of the target workspace, so you can point it
> at an existing workspace. You cannot, however, point it at the same
> workspace that the host is running.

> As a concrete example: I work on the JDT annotation processing plug-ins,
> that is, org.eclipse.jdt.apt.*. So, I have a host workspace containing
> those plug-in projects. Then, to test, I have a target workspace containing
> some Java projects with code that has annotations. I start by launching my
> host workspace, and doing whatever editing I need to do on my plug-in
> projects. Notice that in that host instance of Eclipse, my plug-ins are not
> *running*, they're just projects: that is, the functionality of my plug-ins
> is not available in the host application. If I created a menu item, for
> example, it would not show up in the host instance.

> Then, I have a launch configuration defined that points to the location of
> the target workspace. So, when I say Run -> Run As... -> Eclipse
> Application, it launches the target workspace. In the target instance of
> Eclipse, my plug-ins are running, just like any other Eclipse plug-in. So,
> the menu item that I created in my host workspace would show up on the menu
> of the target Eclipse instance. However, the target workspace does not
> include my plug-in projects; in fact it does not contain any plug-in
> projects at all.

> So you see, I am drawing a distinction between the Eclipse *instance*, which
> is a collection of plug-ins that are running, and the *workspace*, which
> contains projects. The host instance runs the host workspace, which
> contains the plug-in project code; the host instance launches the target
> instance; the target instance runs the target workspace, in whatever
> location it is configured for; the target instance is executing the plug-ins
> in the host workspace.

Walter, thanks for your nicely explanation, it's working on another
workspace. You gave me an immense help in those questions.

Best,
Cassio
Previous Topic:Where are junit test cases for jdt quick fixes ?
Next Topic:Question about eclipse build process
Goto Forum:
  


Current Time: Tue May 13 23:09:55 EDT 2025

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

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

Back to the top