|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2017 Microsoft Corporation and others. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Microsoft Corporation - initial API and implementation |
| 10 | + *******************************************************************************/ |
| 11 | + |
| 12 | +package com.microsoft.java.debug.plugin.internal; |
| 13 | + |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
| 16 | +import java.util.logging.Logger; |
| 17 | +import java.util.stream.Collectors; |
| 18 | + |
| 19 | +import org.eclipse.core.resources.IProject; |
| 20 | +import org.eclipse.core.resources.IResource; |
| 21 | +import org.eclipse.core.runtime.CoreException; |
| 22 | +import org.eclipse.jdt.core.IMethod; |
| 23 | +import org.eclipse.jdt.core.JavaModelException; |
| 24 | +import org.eclipse.jdt.core.search.IJavaSearchConstants; |
| 25 | +import org.eclipse.jdt.core.search.IJavaSearchScope; |
| 26 | +import org.eclipse.jdt.core.search.SearchEngine; |
| 27 | +import org.eclipse.jdt.core.search.SearchMatch; |
| 28 | +import org.eclipse.jdt.core.search.SearchParticipant; |
| 29 | +import org.eclipse.jdt.core.search.SearchPattern; |
| 30 | +import org.eclipse.jdt.core.search.SearchRequestor; |
| 31 | +import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager; |
| 32 | + |
| 33 | +import com.microsoft.java.debug.core.Configuration; |
| 34 | + |
| 35 | +public class ResolveMainClassHandler { |
| 36 | + private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME); |
| 37 | + |
| 38 | + /** |
| 39 | + * resolve main class and project name. |
| 40 | + * @return an array of main class and project name |
| 41 | + * @throws CoreException when there are errors when resolving main class. |
| 42 | + */ |
| 43 | + public Object resolveMainClass() throws CoreException { |
| 44 | + return resolveMainClassCore(); |
| 45 | + } |
| 46 | + |
| 47 | + private List<ResolutionItem> resolveMainClassCore() throws CoreException { |
| 48 | + IJavaSearchScope searchScope = SearchEngine.createWorkspaceScope(); |
| 49 | + SearchPattern pattern = SearchPattern.createPattern("main(String[]) void", IJavaSearchConstants.METHOD, |
| 50 | + IJavaSearchConstants.DECLARATIONS, SearchPattern.R_CASE_SENSITIVE | SearchPattern.R_EXACT_MATCH); |
| 51 | + ArrayList<ResolutionItem> res = new ArrayList<>(); |
| 52 | + SearchRequestor requestor = new SearchRequestor() { |
| 53 | + @Override |
| 54 | + public void acceptSearchMatch(SearchMatch match) { |
| 55 | + Object element = match.getElement(); |
| 56 | + if (element instanceof IMethod) { |
| 57 | + IMethod method = (IMethod) element; |
| 58 | + try { |
| 59 | + if (method.isMainMethod()) { |
| 60 | + IResource resource = method.getResource(); |
| 61 | + if (resource != null) { |
| 62 | + IProject project = resource.getProject(); |
| 63 | + if (project != null) { |
| 64 | + String mainClass = method.getDeclaringType().getFullyQualifiedName(); |
| 65 | + String projectName = ProjectsManager.DEFAULT_PROJECT_NAME.equals(project.getName()) ? null : project.getName(); |
| 66 | + res.add(new ResolutionItem(mainClass, projectName)); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } catch (JavaModelException e) { |
| 71 | + // ignore |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + }; |
| 76 | + SearchEngine searchEngine = new SearchEngine(); |
| 77 | + searchEngine.search(pattern, new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()}, |
| 78 | + searchScope, requestor, null /* progress monitor */); |
| 79 | + return res.stream().distinct().collect(Collectors.toList()); |
| 80 | + } |
| 81 | + |
| 82 | + private class ResolutionItem { |
| 83 | + private String mainClass; |
| 84 | + private String projectName; |
| 85 | + |
| 86 | + public ResolutionItem(String mainClass, String projectName) { |
| 87 | + this.mainClass = mainClass; |
| 88 | + this.projectName = projectName; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public boolean equals(Object o) { |
| 93 | + if (this == o) { |
| 94 | + return true; |
| 95 | + } |
| 96 | + if (o instanceof ResolutionItem) { |
| 97 | + ResolutionItem item = (ResolutionItem) o; |
| 98 | + if (mainClass != null ? !mainClass.equals(item.mainClass) : item.mainClass != null) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + if (projectName != null ? !projectName.equals(item.projectName) : item.projectName != null) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + return true; |
| 105 | + } |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public int hashCode() { |
| 111 | + return (mainClass == null ? 0 : mainClass.hashCode()) * 13 + (projectName == null ? 0 : projectName.hashCode()); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments