从包中获取子包

发布于 2024-12-18 11:54:29 字数 444 浏览 2 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巨坚强 2024-12-25 11:54:29

IPackageFragment 不是正确的起点。您必须向更高级别的包裹询问:

IPackageFragment:单个包。它包含 ICompilationUnits 或 IClassFiles,具体取决于 IPackageFragmentRoot 是源类型还是二进制类型。 请注意,IPackageFragment 不是按父子组织的。例如net.sf.a不是net.sf.ab的父级,它们是同一个IPackageFragmentRoot的两个独立的子级。

看看这篇关于 AST

IPackageFragment is not the correct starting point. You have to ask a higher level for the packages:

IPackageFragment: A single package. It contains ICompilationUnits or IClassFiles, depending on whether the IPackageFragmentRoot is of type source or of type binary. Note that IPackageFragment are not organized as parent-children. E.g. net.sf.a is not the parent of net.sf.a.b. They are two independent children of the same IPackageFragmentRoot.

Have a look at this article about the AST

幽蝶幻影 2024-12-25 11:54:29

这是一些应该接近您需要的代码。 (由于 2011 年已经过去了,我怀疑它会对您有多大帮助,但也许对其他人会有帮助。)毫无疑问,它可以承受一些改进。

由于似乎不可能直接从 IPackageFragment 向下递归(如 Kai 提到的),基本思想是获取更高级别的 IPackageFragmentRoot 并基于其子级进行过滤在原始片段的路径上。

PackageFragment originFragment; // = org.stack's fragment
try {
    String fragmentPath = originFragment.getPath().toString();
    IJavaElement parent = originFragment.getParent();
    ArrayList<IJavaElement> allChildren =
            new ArrayList<IJavaElement>();

    if (parent instanceof IPackageFragmentRoot) {
        IPackageFragmentRoot root = (IPackageFragmentRoot)parent;
        IJavaElement[] rootChildren = root.getChildren();

        // originsFragments includes the origin and all package
        // fragments beneath it
        List<IJavaElement> originsFragments =
           Arrays.asList(rootChildren).stream()
            .filter(c -> c.getPath().toString().startsWith(fragmentPath))
            .collect(Collectors.toList());
        allChildren.addAll(originsFragments);

        // Gather the children of the package fragments
        for (IJavaElement o : originsFragments) {
            if (o instanceof IPackageFragment ) {
                IPackageFragment oFragment = (IPackageFragment)o;
                IJavaElement[] fChildren = oFragment.getChildren();
                allChildren.addAll(Arrays.asList(fChildren));
            }
        }
    }
} catch (JavaModelException e) {
    e.printStackTrace();
}

另一种不太优雅的解决方案是从原始片段的路径开始,然后使用 Java 的文件和目录功能沿目录层次结构向下延伸。然后,您可以使用 IJavaProjectfindPackageFragment(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 level IPackageFragmentRoot and filter it's children based on the original fragment's path.

PackageFragment originFragment; // = org.stack's fragment
try {
    String fragmentPath = originFragment.getPath().toString();
    IJavaElement parent = originFragment.getParent();
    ArrayList<IJavaElement> allChildren =
            new ArrayList<IJavaElement>();

    if (parent instanceof IPackageFragmentRoot) {
        IPackageFragmentRoot root = (IPackageFragmentRoot)parent;
        IJavaElement[] rootChildren = root.getChildren();

        // originsFragments includes the origin and all package
        // fragments beneath it
        List<IJavaElement> originsFragments =
           Arrays.asList(rootChildren).stream()
            .filter(c -> c.getPath().toString().startsWith(fragmentPath))
            .collect(Collectors.toList());
        allChildren.addAll(originsFragments);

        // Gather the children of the package fragments
        for (IJavaElement o : originsFragments) {
            if (o instanceof IPackageFragment ) {
                IPackageFragment oFragment = (IPackageFragment)o;
                IJavaElement[] fChildren = oFragment.getChildren();
                allChildren.addAll(Arrays.asList(fChildren));
            }
        }
    }
} catch (JavaModelException e) {
    e.printStackTrace();
}

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 proper IPackageFragments.

晚风撩人 2024-12-25 11:54:29

你需要以递归的方式来做。

这是一些伪代码

findAllClasses(package, classesCollection) {
    for(Class c: package.getClasses)
        classesCollection.add(c.getResourcePath)
    if(package.hasChildPackages)
        for(Package p: packages)
            findAllClasses(p, classesCollection)
}

you need to do it in a recursive way.

here's some pseudo code

findAllClasses(package, classesCollection) {
    for(Class c: package.getClasses)
        classesCollection.add(c.getResourcePath)
    if(package.hasChildPackages)
        for(Package p: packages)
            findAllClasses(p, classesCollection)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文