API for getting all content type resources in an Eclipse project

Is there any API for getting all files of a certain type of content in an Eclipse project?

One option is to visit all resources and collect content type files.

I am considering an API that takes an IProject and a content type identifier as parameters and returns IPath or IFile or IResource objects. For example, get all the Java files in the project.

Thanks in advance.

+4
source share
3 answers

No no. Your idea will usually be fulfilled.

+2
source

Here is what I used to search for all c files in the current project:

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; } 
+3
source

eclipse3.1 release notes at the time indicated a heuristic change to match content types. <sh> This was due to error 90218 , part of error 82986 (improvements for matching in 3.1), which refers to error 86862 ("I need an API to search for related user objects")

This API did not do this, but is reusable.

 public Object[] findRelatedObjects(IContentType type, String fileName, IRelatedRegistry registry) { List allRelated = new ArrayList(); // first add any objects directly related to the content type Object[] related = registry.getRelatedObjects(type); for (int i = 0; i < related.length; i++) { allRelated.add(related[i]); } // backward compatibility requested - add any objects related to the file name if (fileName != null) { related = registry.getRelatedObjects(fileName); for (int i = 0; i < related.length; i++) { if (!allRelated.contains(related[i])) { // we don't want to return duplicates allRelated.add(related[i]); } } } // now add any indirectly related objects, walking up the content type hierarchy while ((type = type.getBaseType()) != null) { related = registry.getRelatedObjects(type); for (int i = 0; i < related.length; i++) { if (!allRelated.contains(related[i])) { // we don't want to return duplicates allRelated.add(related[i]); } } } return allRelated.toArray(); } 
0
source

Source: https://habr.com/ru/post/1300371/