Skip to main content



      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 13:28 Go to next message
Eclipse UserFriend

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
Re: Misleading behavior of root.getJavaProject() in non-java projects? [message #1259912 is a reply to message #1250909] Fri, 28 February 2014 18:14 Go to previous messageGo to next message
Eclipse UserFriend
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 02:45 Go to previous message
Eclipse UserFriend
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.
Previous Topic:Eclipse Java 8 Release in March
Next Topic:change text foreground color for specific words
Goto Forum:
  


Current Time: Sat Apr 26 21:29:11 EDT 2025

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

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

Back to the top