gpt4 book ai didi

java - 获取Eclipse项目中某个内容类型的所有资源的API

转载 作者:搜寻专家 更新时间:2023-11-01 02:34:27 25 4
gpt4 key购买 nike

是否有任何API 可以获取Eclipse 项目中特定内容类型的所有文件?

一种选择是访问所有资源并收集某种内容类型的文件。

我正在查看将 IProject 和内容类型 ID 作为参数并返回 IPath 或 IFile 或 IResource 对象的 API。例如获取项目中的所有 Java 文件。

提前致谢。

最佳答案

这就是我用来在当前项目中查找所有 c 文件的方法:

    public static ArrayList<IResource> getAllCFilesInProject(){
ArrayList<IResource> allCFiles = new ArrayList<IResource>();
IWorkspaceRoot myWorkspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
IProject project = FileParaviserUtils.getCurrentProject();

IPath path = project.getLocation();

recursiveFindCFiles(allCFiles,path,myWorkspaceRoot);
return allCFiles;
}

private static void recursiveFindCFiles(ArrayList<IResource> allCFiles,IPath path, IWorkspaceRoot myWorkspaceRoot){
IContainer container = myWorkspaceRoot.getContainerForLocation(path);

try {
IResource[] iResources;
iResources = container.members();
for (IResource iR : iResources){
// for c files
if ("c".equalsIgnoreCase(iR.getFileExtension()))
allCFiles.add(iR);
if (iR.getType() == IResource.FOLDER){
IPath tempPath = iR.getLocation();
recursiveFindCFiles(allCFiles,tempPath,myWorkspaceRoot);
}
}
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static IProject getCurrentProject(){
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window != null)
{
IStructuredSelection selection = (IStructuredSelection) window.getSelectionService().getSelection();
Object firstElement = selection.getFirstElement();
if (firstElement instanceof IAdaptable)
{
IProject project = (IProject)((IAdaptable)firstElement).getAdapter(IProject.class);
return project;
}
}
return null;
}

关于java - 获取Eclipse项目中某个内容类型的所有资源的API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2205254/

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