|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2017 Red Hat Inc. 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 | +package com.microsoft.java.debug.plugin.internal; |
| 12 | + |
| 13 | +import static org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin.logException; |
| 14 | + |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | +import java.util.concurrent.CompletableFuture; |
| 18 | + |
| 19 | +import org.eclipse.core.resources.IProject; |
| 20 | +import org.eclipse.core.resources.IWorkspaceRoot; |
| 21 | +import org.eclipse.core.resources.ResourcesPlugin; |
| 22 | +import org.eclipse.core.runtime.CoreException; |
| 23 | +import org.eclipse.core.runtime.IStatus; |
| 24 | +import org.eclipse.core.runtime.Status; |
| 25 | +import org.eclipse.jdt.core.IJavaElement; |
| 26 | +import org.eclipse.jdt.core.IJavaProject; |
| 27 | +import org.eclipse.jdt.core.JavaCore; |
| 28 | +import org.eclipse.jdt.core.search.IJavaSearchConstants; |
| 29 | +import org.eclipse.jdt.core.search.IJavaSearchScope; |
| 30 | +import org.eclipse.jdt.core.search.SearchEngine; |
| 31 | +import org.eclipse.jdt.core.search.SearchMatch; |
| 32 | +import org.eclipse.jdt.core.search.SearchParticipant; |
| 33 | +import org.eclipse.jdt.core.search.SearchPattern; |
| 34 | +import org.eclipse.jdt.core.search.SearchRequestor; |
| 35 | +import org.eclipse.jdt.launching.JavaRuntime; |
| 36 | +import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; |
| 37 | + |
| 38 | +public class ResolveClasspathsHandler { |
| 39 | + |
| 40 | + public String[] resolveClasspaths(List<Object> arguments) { |
| 41 | + try { |
| 42 | + return computeClassPath((String)arguments.get(0), (String)arguments.get(1)); |
| 43 | + } catch (CoreException e) { |
| 44 | + logException("Failed to resolve classpath.", e); |
| 45 | + } |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Get java project from name. |
| 51 | + * |
| 52 | + * @param projectName |
| 53 | + * project name |
| 54 | + * @return java project |
| 55 | + * @throws CoreException |
| 56 | + * CoreException |
| 57 | + */ |
| 58 | + private static IJavaProject getJavaProjectFromName(String projectName) throws CoreException { |
| 59 | + IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); |
| 60 | + IProject project = root.getProject(projectName); |
| 61 | + if (!project.exists()) { |
| 62 | + throw new CoreException(new Status(IStatus.ERROR, JavaLanguageServerPlugin.PLUGIN_ID, |
| 63 | + String.format("Cannot find the project with name '%s'.", projectName))); |
| 64 | + } |
| 65 | + if (!project.isNatureEnabled("org.eclipse.jdt.core.javanature")) { |
| 66 | + throw new CoreException(new Status(IStatus.ERROR, JavaLanguageServerPlugin.PLUGIN_ID, |
| 67 | + String.format("The project '%s' does not have java nature enabled.", projectName))); |
| 68 | + } |
| 69 | + IJavaProject javaProject = JavaCore.create(project); |
| 70 | + return javaProject; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Get java project from type. |
| 75 | + * |
| 76 | + * @param typeFullyQualifiedName |
| 77 | + * fully qualified name of type |
| 78 | + * @return java project |
| 79 | + * @throws CoreException |
| 80 | + * CoreException |
| 81 | + */ |
| 82 | + private static List<IJavaProject> getJavaProjectFromType(String typeFullyQualifiedName) throws CoreException { |
| 83 | + SearchPattern pattern = SearchPattern.createPattern(typeFullyQualifiedName, IJavaSearchConstants.TYPE, |
| 84 | + IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH); |
| 85 | + IJavaSearchScope scope = SearchEngine.createWorkspaceScope(); |
| 86 | + ArrayList<IJavaProject> projects = new ArrayList<>(); |
| 87 | + SearchRequestor requestor = new SearchRequestor() { |
| 88 | + @Override |
| 89 | + public void acceptSearchMatch(SearchMatch match) { |
| 90 | + Object element = match.getElement(); |
| 91 | + if (element instanceof IJavaElement) { |
| 92 | + projects.add(((IJavaElement) element).getJavaProject()); |
| 93 | + } |
| 94 | + } |
| 95 | + }; |
| 96 | + SearchEngine searchEngine = new SearchEngine(); |
| 97 | + searchEngine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, |
| 98 | + requestor, null /* progress monitor */); |
| 99 | + |
| 100 | + return projects; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Accord to the project name and the main class, compute runtime classpath. |
| 105 | + * |
| 106 | + * @param projectName |
| 107 | + * project name |
| 108 | + * @param mainClass |
| 109 | + * full qualified class name |
| 110 | + * @return class path |
| 111 | + * @throws CoreException |
| 112 | + * CoreException |
| 113 | + */ |
| 114 | + private static String[] computeClassPath(String projectName, String mainClass) throws CoreException { |
| 115 | + IJavaProject project = null; |
| 116 | + // if type exists in multiple projects, debug configuration need provide |
| 117 | + // project name. |
| 118 | + if (projectName != null) { |
| 119 | + project = getJavaProjectFromName(projectName); |
| 120 | + } else { |
| 121 | + List<IJavaProject> projects = getJavaProjectFromType(mainClass); |
| 122 | + if (projects.size() == 0) { |
| 123 | + throw new CoreException(new Status(IStatus.ERROR, JavaLanguageServerPlugin.PLUGIN_ID, |
| 124 | + String.format("Main class '%s' doesn't exist in the workspace.", mainClass))); |
| 125 | + } |
| 126 | + if (projects.size() > 1) { |
| 127 | + throw new CoreException(new Status(IStatus.ERROR, JavaLanguageServerPlugin.PLUGIN_ID, |
| 128 | + String.format( |
| 129 | + "Main class '%s' isn't unique in the workspace, please pass in specified projectname.", |
| 130 | + mainClass))); |
| 131 | + } |
| 132 | + project = projects.get(0); |
| 133 | + } |
| 134 | + return computeClassPath(project); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Compute runtime classpath of a java project. |
| 139 | + * |
| 140 | + * @param javaProject |
| 141 | + * java project |
| 142 | + * @return class path |
| 143 | + * @throws CoreException |
| 144 | + * CoreException |
| 145 | + */ |
| 146 | + private static String[] computeClassPath(IJavaProject javaProject) throws CoreException { |
| 147 | + if (javaProject == null) { |
| 148 | + throw new IllegalArgumentException("javaProject is null"); |
| 149 | + } |
| 150 | + return JavaRuntime.computeDefaultRuntimeClassPath(javaProject); |
| 151 | + } |
| 152 | +} |
0 commit comments