private List resolveMainClassUnderPaths()

in com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/ResolveMainClassHandler.java [100:162]


    private List<ResolutionItem> resolveMainClassUnderPaths(List<IPath> parentPaths) {
        // Limit to search main method from source code only.
        IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(ProjectUtils.getJavaProjects(),
            IJavaSearchScope.REFERENCED_PROJECTS | IJavaSearchScope.SOURCES);
        SearchPattern pattern = SearchPattern.createPattern("main(String[]) void", IJavaSearchConstants.METHOD,
                IJavaSearchConstants.DECLARATIONS, SearchPattern.R_CASE_SENSITIVE | SearchPattern.R_EXACT_MATCH);
        final List<ResolutionItem> res = new ArrayList<>();
        SearchRequestor requestor = new SearchRequestor() {
            @Override
            public void acceptSearchMatch(SearchMatch match) {
                Object element = match.getElement();
                if (element instanceof IMethod) {
                    IMethod method = (IMethod) element;
                    try {
                        if (method.isMainMethod()) {
                            IResource resource = method.getResource();
                            if (resource != null) {
                                IProject project = resource.getProject();
                                if (project != null) {
                                    String mainClass = method.getDeclaringType().getFullyQualifiedName();
                                    IJavaProject javaProject = JdtUtils.getJavaProject(project);
                                    if (javaProject != null) {
                                        String moduleName = JdtUtils.getModuleName(javaProject);
                                        if (moduleName != null) {
                                            mainClass = moduleName + "/" + mainClass;
                                        }
                                    }
                                    String projectName = ProjectsManager.DEFAULT_PROJECT_NAME.equals(project.getName()) ? null : project.getName();
                                    if (parentPaths.isEmpty()
                                        || ResourceUtils.isContainedIn(project.getLocation(), parentPaths)
                                        || isContainedInInvisibleProject(project, parentPaths)) {
                                        String filePath = null;

                                        if (match.getResource() instanceof IFile) {
                                            try {
                                                filePath = match.getResource().getLocation().toOSString();
                                            } catch (Exception ex) {
                                                // ignore
                                            }
                                        }
                                        res.add(new ResolutionItem(mainClass, projectName, filePath));
                                    }
                                }
                            }
                        }
                    } catch (JavaModelException e) {
                        // ignore
                    }
                }
            }
        };
        SearchEngine searchEngine = new SearchEngine();
        try {
            searchEngine.search(pattern, new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
                    searchScope, requestor, null /* progress monitor */);
        } catch (Exception e) {
            logger.log(Level.SEVERE, String.format("Searching the main class failure: %s", e.toString()), e);
        }

        List<ResolutionItem> resolutions = res.stream().distinct().collect(Collectors.toList());
        Collections.sort(resolutions);
        return resolutions;
    }