Eclipse JDT 获取类的名称
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试 org.eclipse.jdt.core.search.SearchEngine.searchAllTypeNames(...) 方法之一。
Try one of the org.eclipse.jdt.core.search.SearchEngine.searchAllTypeNames(...) methods.
来自 Java 文档
您的项目中不应有类文件,因为它们可能是源项目。因此寻找 ICompilationUnits。
From the Javadocs
There should be no class files in your projects because they are probably source projects. Hence look for ICompilationUnits.
您的代码似乎一切都是正确的。事情是:
1. 只有在插件开发项目中使用它才有效。也就是说,制作一个示例插件并从插件基类中调用此类来进行测试。
2. 它将在新打开的插件选项卡中列出项目的名称。因此,在新的 Eclipse 窗口中创建一些示例项目,并将其作为“Eclipse 应用程序”运行。
我有一个工作正常的更简单的代码,尝试一下:
import org.eclipse.core.resources.IProject;
导入 org.eclipse.core.resources.ResourcesPlugin;
公共类 GetProjectName {
}
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 {
}