是否有任何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/