Eclipse JDT 获取类的名称

发布于 2024-12-10 12:39:32 字数 1234 浏览 0 评论 0原文

我正在使用 Eclipse JDT 获取工作区中每个打开项目的所有类的名称,但直到现在我还无法做到这一点...

IWorkspace workspace = ResourcesPlugin.getWorkspace();
IProject[] iprojects = workspace.getRoot().getProjects();

for (IProject ip : iprojects)
{
    if (ip.isOpen() == true)
    {
        IJavaProject javaProject = JavaCore.create(ip);

    IPackageFragment[] packages;
    try
    {
        packages = javaProject.getPackageFragments();

        for (IPackageFragment mypackage : packages)
        {
            if (mypackage.getKind() == IPackageFragmentRoot.K_SOURCE)
            {
                System.out.println("Source Name " + mypackage.getElementName());
                System.out.println("Number of Classes: " + mypackage.getClassFiles().length);
            }
            else if (mypackage.getKind() == IPackageFragmentRoot.K_BINARY)
            {
                System.out.println("Binary Name " + mypackage.getElementName());
                System.out.println("Number of Classes: " + mypackage.getClassFiles().length);
            }
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
}

这样我只能获取每个包的类数:0在我的项目示例中!

怎么了?为什么我无法获取每个包的类,然后获取每个类的名称?

-- 干杯,泽·卡洛斯

I am using Eclipse JDT to obtain the name of all classes of every open projects in workspace, but until now I cannot do that...

IWorkspace workspace = ResourcesPlugin.getWorkspace();
IProject[] iprojects = workspace.getRoot().getProjects();

for (IProject ip : iprojects)
{
    if (ip.isOpen() == true)
    {
        IJavaProject javaProject = JavaCore.create(ip);

    IPackageFragment[] packages;
    try
    {
        packages = javaProject.getPackageFragments();

        for (IPackageFragment mypackage : packages)
        {
            if (mypackage.getKind() == IPackageFragmentRoot.K_SOURCE)
            {
                System.out.println("Source Name " + mypackage.getElementName());
                System.out.println("Number of Classes: " + mypackage.getClassFiles().length);
            }
            else if (mypackage.getKind() == IPackageFragmentRoot.K_BINARY)
            {
                System.out.println("Binary Name " + mypackage.getElementName());
                System.out.println("Number of Classes: " + mypackage.getClassFiles().length);
            }
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
}

With this I can only obtain Number of Classes: 0 for every package in my project example!

What is wrong? Why I cannot obtain the classes of every packages and next obtain the name of every class?

--
Cheers, Zé Carlos

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

小草泠泠 2024-12-17 12:39:32

尝试 org.eclipse.jdt.core.search.SearchEngine.searchAllTypeNames(...) 方法之一。

Try one of the org.eclipse.jdt.core.search.SearchEngine.searchAllTypeNames(...) methods.

意犹 2024-12-17 12:39:32

来自 Java 文档

org.eclipse.jdt.core.ICompilationUnit - 代表整个 Java
编译单元(具有类似 Java 扩展之一的源文件)。
编译单元元素需要打开才可以
导航或操纵。

org.eclipse.jdt.core.IClassFile - 表示整个二进制类型
(单个 .class 文件)。

您的项目中不应有类文件,因为它们可能是源项目。因此寻找 ICompilationUnits。

From the Javadocs

org.eclipse.jdt.core.ICompilationUnit - Represents an entire Java
compilation unit (source file with one of the Java-like extensions).
Compilation unit elements need to be opened before they can be
navigated or manipulated.

org.eclipse.jdt.core.IClassFile - Represents an entire binary type
(single .class file).

There should be no class files in your projects because they are probably source projects. Hence look for ICompilationUnits.

魂归处 2024-12-17 12:39:32

您的代码似乎一切都是正确的。事情是:
1. 只有在插件开发项目中使用它才有效。也就是说,制作一个示例插件并从插件基类中调用此类来进行测试。
2. 它将在新打开的插件选项卡中列出项目的名称。因此,在新的 Eclipse 窗口中创建一些示例项目,并将其作为“Eclipse 应用程序”运行。

我有一个工作正常的更简单的代码,尝试一下:

import org.eclipse.core.resources.IProject;
导入 org.eclipse.core.resources.ResourcesPlugin;

公共类 GetProjectName {

public static IProject[] getProjects()

{

    IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
    System.out.println(ResourcesPlugin.getWorkspace().getRoot().getName());

    System.out.println(" ****  "+ projects.length +"   ***");

    for (IProject project : projects) {
        System.out.println(project.getName());
    }



    return projects;

}

}

Everything seems to be correct with your code. Thing is:
1. It will work only if you use it in a plugin development project. That is, make a sample plugin and call this class form the base plugin class to test.
2. It will list the name of the projects in the newly opened plugin tab. So make some example projects in the new eclipse window that you get on running it as "eclipse application".

I have a simpler code that works fine, try it:

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;

public class GetProjectName {

public static IProject[] getProjects()

{

    IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
    System.out.println(ResourcesPlugin.getWorkspace().getRoot().getName());

    System.out.println(" ****  "+ projects.length +"   ***");

    for (IProject project : projects) {
        System.out.println(project.getName());
    }



    return projects;

}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文