Why is IJavaProject.findPackageFragmentRoots returning an empty array?
Every time I call IJavaProject.findPackageFragmentRoots(IClasspathEntry cpe)
and pass it an IClasspathEntry of kind CPE_PROJECT, it returns an empty list. I paused the debugger and ran the following lines in the Eclipse Display view to see what was going on:
IClasspathEntry cpe = javaProject.getRawClasspath()[8];
cpe.toString();
(java.lang.String) /Some Project[CPE_PROJECT][K_SOURCE][isExported:false][combine access rules:false]
// Show that the referenced project exists and has a root containing source
IClasspathEntry cpe = javaProject.getRawClasspath()[8];
IProject someProject = ResourcesPlugin.getWorkspace().getRoot().getProject(cpe.getPath().toString());
IJavaProject someJavaProject = (IJavaProject) someProject.getNature(JavaCore.NATURE_ID);
IPackageFragmentRoot pfr = someJavaProject.getPackageFragmentRoots()[0];
new Boolean(pfr.getKind() == IPackageFragmentRoot.K_SOURCE).toString();
(java.lang.String) true
IClasspathEntry cpe = javaProject.getRawClasspath()[8];
javaProject.findPackageFragmentRoots(cpe);
(org.eclipse.jdt.core.IPackageFragmentRoot[]) []
The classpath entry is part of the Java project's classpath and it's of kind CPE_PROJECT. In addition, the referenced Java project (titled "Some Project") has at least one root containing source. According to the javadoc for IJavaProject, it seems this should return at least one IPac开发者_StackOverflowkageFragmentRoot, but its doesn't.
Any idea what I'm misunderstanding here? How can I get the IPackageFragmentRoots from an IClasspathEntry of kind CPE_PROJECT without doing something hackish? (I mean, I could get the IJavaProject from the classpath entry and iterate through its raw classpath, ignoring non-exported entries, in search of IPackageFragmentRoots.)
This was a bug that was fixed in Eclipse 3.7 (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=324367).
As per the new API:
The result does not include package fragment roots in other projects referenced on this project's classpath.
See http://help.eclipse.org/indigo/index.jsp?topic=/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/core/IJavaProject.html
精彩评论