为什么 IJavaProject.findPackageFragmentRoots 返回空数组?
每次我调用 IJavaProject.findPackageFragmentRoots(IClasspathEntry cpe) 并向其传递 CPE_PROJECT 类型的 IClasspathEntry 时,它都会返回一个空列表。我暂停了调试器,并在 Eclipse Display 视图中运行以下几行来查看发生了什么:
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[]) []
类路径条目是 Java 项目类路径的一部分,并且属于 CPE_PROJECT 类型。此外,引用的 Java 项目(标题为“Some Project”)至少有一个包含源代码的根。根据 IJavaProject 的 javadoc,看起来这应该至少返回一个 IPackageFragmentRoot,但事实并非如此。
知道我在这里误解了什么吗?如何从 CPE_PROJECT 类型的 IClasspathEntry 获取 IPackageFragmentRoots 而不做一些黑客行为? (我的意思是,我可以从类路径条目获取 IJavaProject 并迭代其原始类路径,忽略未导出的条目,以搜索 IPackageFragmentRoots。)
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 IPackageFragmentRoot, 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.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 Eclipse 3.7 中修复的错误(请参阅 https://bugs.eclipse .org/bugs/show_bug.cgi?id=324367)。
根据新的 API:
请参阅
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:
See http://help.eclipse.org/indigo/index.jsp?topic=/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/core/IJavaProject.html