gpt4 book ai didi

java - 如何从工作集中获取项目(IProject)?

转载 作者:行者123 更新时间:2023-12-02 04:41:31 25 4
gpt4 key购买 nike

在下一个代码中,我将从默认工作区的项目中获取所需的信息,但我需要针对工作集的项目更改它。

    IWorkspace workspace = ResourcesPlugin.getWorkspace();

// Get the root of the workspace
IWorkspaceRoot root = workspace.getRoot();

// Get all projects in the workspace
IProject[] projects = root.getProjects();

// Loop over all projects
for (IProject project : projects) {
try {
if (project.getName().equals("RemoteSystemsTempFiles"))
{
System.out.println("Ignored project");
//setSystemName(project.getName());
}
else
{
// Load the data model by extracting APIs declaration (Imports)
printProjectInfo(project);
}
} catch (CoreException e) {
e.printStackTrace();
}
}

所以,我找到了一个answer但当循环后返回时,我收到许多信息,例如:

- ProjectName (not open) 

- ProjectName
src
<default> (...)
src (...)
src.tryone (...)
C:\Program Files\Java\jre1.8.0_31\lib\resources.jar (not open)

返回该信息的代码是:

     IWorkingSetManager manager = PlatformUI.getWorkbench().getWorkingSetManager();

IWorkingSet [] allWorkingSets = manager.getAllWorkingSets();

for (IWorkingSet aWorkingset : allWorkingSets) {

IAdaptable[] elems = aWorkingset.getElements();
System.out.println("Working set " + aWorkingset.getName() + " has "+ elems.length + " projects.");
for (IAdaptable elem : elems) {
System.out.println("Working set " + aWorkingset.getName()+ " contains " + elem.toString());
}
}

简单来说,我想知道如何以与工作区项目相同或类似的方式从工作集中获取 IProject?

最佳答案

在每个 IAdaptable 元素上调用

IResource resource = (IResource)elem.getAdapter(IResource.class);

它将返回该元素代表的任何资源(某些工作集包含其他内容,因此结果可能为空)。

然后您可以调用

IProject project = resource.getProject();

关于java - 如何从工作集中获取项目(IProject)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30131614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com