javaeclipse-pluginswtjface

Eclipse API Read all submodules/child modules


I am trying to read all submodules of project. Below is the code which I wrote it is working as expected but I am not sure if this is an efficient way of reading all submodules of project. Please advice.

public static List<IResource> getMembers(IProject project){
List<IResource> childFolders = new ArrayList<IResource>();
try {
    IResource[] children = project.members();
    for (int i = 0; i < children.length; i++) {
// not sure about this if condition part
        if (children[i].FOLDER == IResource.FOLDER && !children[i].toString().startsWith("L/")
                && !children[i].toString().contains(".")) {
            childFolders.add(children[i]);
            System.out.println(children[i]);
        }
    }
} catch (CoreException e) {
    LOGGER.debug(e.getMessage());
}
return childFolders;
}
  

Solution

  • children[i].FOLDER == IResource.FOLDER doesn't do anything - FOLDER is a constant so it is always true.

    You should never have to use toString to work out member types.

    If you want a list of folders in a project you would use something like:

    final List<IFolder> folders = new ArrayList<>();
    
    for (final IResource resource : project.members()) {
      if (resource instanceof IFolder && !resource.getName().startsWith("."))
        folders.add((IFolder)resource);
    }
    

    or even:

    List<IFolder> folders = Arrays.stream(project.members())
        .filter(resource -> !resource.getName().startsWith("."))
        .filter(IFolder.class::isInstance)
        .map(IFolder.class::cast)
        .toList();