Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Misleading behavior of root.getJavaProject() in non-java projects?(committer)
Misleading behavior of root.getJavaProject() in non-java projects? [message #1250909] Wed, 19 February 2014 18:28 Go to next message
Marcel Bruch is currently offline Marcel Bruch
Messages: 230
Registered: July 2009
Senior Member

I've a "simple project" (non-java) and in there a Test.java source file. When that file is opened with the Java Editor, my selection listeners obtain a ITypeRoot element whose root.getJavaProject() method returns a handle which does not exist (i.e., handle.exists() returns false).

The Javadoc of IJavaElement.exists() states "Returns the Java project this element is contained in, or null if this element is not contained in any Java project (for instance, the IJavaModel is not contained in any Java project). This is a handle-only method."

I wonder whether this method should return null rather than returning a newly created, non-existing handle in this case? Is this behavior "works as intended" and if so, when does this method return null then?


Thanks
Marcel


tw: @MarcelBruch
tw: @Recommenders
Re: Misleading behavior of root.getJavaProject() in non-java projects? [message #1259912 is a reply to message #1250909] Fri, 28 February 2014 23:14 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan Herrmann
Messages: 1023
Registered: July 2009
Senior Member
Hi Marcel,

I don't know any other examples than what is stated in the javadoc:
"(for instance, the IJavaModel is not contained in any Java project)."

Apparently, for any other IJavaElement to exist, it must be created as a descendent of an IJavaProject, just the project may not exist(), as you experience.

Is it much trouble to ask exists() on your side?

cheers,
Stephan
Re: Misleading behavior of root.getJavaProject() in non-java projects? [message #1260226 is a reply to message #1259912] Sat, 01 March 2014 07:45 Go to previous message
Marcel Bruch is currently offline Marcel Bruch
Messages: 230
Registered: July 2009
Senior Member
No, it's not. I just have to figure out whether the file in the editor/selection is actually in a classpath because otherwise I get exceptions. The current workaround is to check whether the element's package fragment root exists:

IJavaElement ancestor = root.getAncestor(IJavaProject.PACKAGE_FRAGMENT_ROOT);
if (!ancestor.exists()) {
return true;
}
...



I was just wondering whether the API documentation should be adjusted or there is a better approach than that. If the latter is not the case I'm fine.


tw: @MarcelBruch
tw: @Recommenders
Previous Topic:Eclipse Java 8 Release in March
Next Topic:change text foreground color for specific words
Goto Forum:
  


Current Time: Thu Oct 23 08:01:20 GMT 2014

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

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