从包中获取子包
在 Eclipse 中,如何获取包的子项?
考虑这个例子:
+ org.stack
org.stack.test
- StackTest.java
- Stack.java
当我们在 org.stack 中执行 IPackageFragment.getChildren() 时,Eclipse JDT 仅返回编译单元(Java 文件)!但我想要一个包的所有子项:所有 ICompilationUnit 和所有包。
在此示例中,当我在 org.stack 中应用 IPackageFragment.getChildren() 时,我需要 org.stack.test 和 ICompilationUnit Stack.java >...
我该怎么做?
In Eclipse, how can I get the package's children?
Consider this example:
+ org.stack
org.stack.test
- StackTest.java
- Stack.java
When we do IPackageFragment.getChildren() in org.stack, the Eclipse JDT only returns the compilation unit (Java Files)! But I want all children of a package: all ICompilationUnits and all Packages.
In this example when I apply IPackageFragment.getChildren() in org.stack, I want the org.stack.test and the ICompilationUnit Stack.java...
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
IPackageFragment 不是正确的起点。您必须向更高级别的包裹询问:
看看这篇关于 AST
IPackageFragment is not the correct starting point. You have to ask a higher level for the packages:
Have a look at this article about the AST
这是一些应该接近您需要的代码。 (由于 2011 年已经过去了,我怀疑它会对您有多大帮助,但也许对其他人会有帮助。)毫无疑问,它可以承受一些改进。
由于似乎不可能直接从
IPackageFragment
向下递归(如 Kai 提到的),基本思想是获取更高级别的IPackageFragmentRoot
并基于其子级进行过滤在原始片段的路径上。另一种不太优雅的解决方案是从原始片段的路径开始,然后使用 Java 的文件和目录功能沿目录层次结构向下延伸。然后,您可以使用
IJavaProject
的 findPackageFragment(IPath path)
连接到正确的IPackageFragments
。Here's some code that should be close to what you needed. (Since we're a bit past 2011, I doubt it will help you much, but maybe it will help somebody else.) Doubtless it can stand some improvement.
Since it doesn't seem possible to directly recurse downward from the
IPackageFragment
(as mentioned by Kai), the basic idea is to get the higher levelIPackageFragmentRoot
and filter it's children based on the original fragment's path.An alternative inelegant solution would be to start with the original fragment's path and then use Java's file and directory facilities to descend through the directory hierarchy. Then, you can use
IJavaProject
's findPackageFragment(IPath path)
to connect to the properIPackageFragments
.你需要以递归的方式来做。
这是一些伪代码
you need to do it in a recursive way.
here's some pseudo code