|
1 | 1 | /*******************************************************************************
|
2 |
| - * Copyright (c) 2017 Microsoft Corporation and others. |
| 2 | + * Copyright (c) 2017-2020 Microsoft Corporation and others. |
3 | 3 | * All rights reserved. This program and the accompanying materials
|
4 | 4 | * are made available under the terms of the Eclipse Public License v1.0
|
5 | 5 | * which accompanies this distribution, and is available at
|
|
22 | 22 | import java.nio.file.Paths;
|
23 | 23 | import java.util.HashMap;
|
24 | 24 | import java.util.Map;
|
| 25 | +import java.util.jar.Attributes; |
| 26 | +import java.util.jar.Manifest; |
25 | 27 | import java.util.logging.Level;
|
26 | 28 | import java.util.logging.Logger;
|
27 | 29 |
|
| 30 | +import com.microsoft.java.debug.core.Configuration; |
| 31 | +import com.microsoft.java.debug.core.DebugException; |
| 32 | +import com.microsoft.java.debug.core.adapter.AdapterUtils; |
| 33 | +import com.microsoft.java.debug.core.adapter.Constants; |
| 34 | +import com.microsoft.java.debug.core.adapter.IDebugAdapterContext; |
| 35 | +import com.microsoft.java.debug.core.adapter.ISourceLookUpProvider; |
| 36 | + |
28 | 37 | import org.eclipse.core.resources.IResource;
|
| 38 | +import org.eclipse.core.runtime.CoreException; |
29 | 39 | import org.eclipse.debug.core.sourcelookup.ISourceContainer;
|
30 | 40 | import org.eclipse.jdt.core.IBuffer;
|
31 | 41 | import org.eclipse.jdt.core.IClassFile;
|
32 | 42 | import org.eclipse.jdt.core.IJavaElement;
|
| 43 | +import org.eclipse.jdt.core.IJavaProject; |
| 44 | +import org.eclipse.jdt.core.IPackageFragmentRoot; |
33 | 45 | import org.eclipse.jdt.core.ITypeRoot;
|
34 | 46 | import org.eclipse.jdt.core.JavaCore;
|
35 | 47 | import org.eclipse.jdt.core.JavaModelException;
|
36 | 48 | import org.eclipse.jdt.core.dom.AST;
|
37 | 49 | import org.eclipse.jdt.core.dom.ASTParser;
|
38 | 50 | import org.eclipse.jdt.core.dom.CompilationUnit;
|
| 51 | +import org.eclipse.jdt.internal.core.JarPackageFragmentRoot; |
39 | 52 | import org.eclipse.jdt.internal.debug.core.breakpoints.ValidBreakpointLocationLocator;
|
40 |
| - |
41 |
| -import com.microsoft.java.debug.core.Configuration; |
42 |
| -import com.microsoft.java.debug.core.DebugException; |
43 |
| -import com.microsoft.java.debug.core.adapter.AdapterUtils; |
44 |
| -import com.microsoft.java.debug.core.adapter.Constants; |
45 |
| -import com.microsoft.java.debug.core.adapter.IDebugAdapterContext; |
46 |
| -import com.microsoft.java.debug.core.adapter.ISourceLookUpProvider; |
| 53 | +import org.eclipse.jdt.launching.IVMInstall; |
| 54 | +import org.eclipse.jdt.launching.JavaRuntime; |
| 55 | +import org.eclipse.jdt.launching.LibraryLocation; |
47 | 56 |
|
48 | 57 | public class JdtSourceLookUpProvider implements ISourceLookUpProvider {
|
49 | 58 | private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME);
|
@@ -181,6 +190,25 @@ public String getSourceFileURI(String fullyQualifiedName, String sourcePath) {
|
181 | 190 | return null;
|
182 | 191 | }
|
183 | 192 |
|
| 193 | + @Override |
| 194 | + public String getJavaRuntimeVersion(String projectName) { |
| 195 | + IJavaProject project = JdtUtils.getJavaProject(projectName); |
| 196 | + if (project != null) { |
| 197 | + try { |
| 198 | + IVMInstall vmInstall = JavaRuntime.getVMInstall(project); |
| 199 | + if (vmInstall == null || vmInstall.getInstallLocation() == null) { |
| 200 | + return null; |
| 201 | + } |
| 202 | + |
| 203 | + return resolveSystemLibraryVersion(project, vmInstall); |
| 204 | + } catch (CoreException e) { |
| 205 | + logger.log(Level.SEVERE, "Failed to get Java runtime version for project '" + projectName + "': " + e.getMessage(), e); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + return null; |
| 210 | + } |
| 211 | + |
184 | 212 | /**
|
185 | 213 | * Get the project associated source containers.
|
186 | 214 | * @return the initialized source container list
|
@@ -280,4 +308,21 @@ private static String readFile(String filePath, Charset cs) {
|
280 | 308 | return builder.toString();
|
281 | 309 | }
|
282 | 310 |
|
| 311 | + private static String resolveSystemLibraryVersion(IJavaProject project, IVMInstall vmInstall) throws JavaModelException { |
| 312 | + LibraryLocation[] libraries = JavaRuntime.getLibraryLocations(vmInstall); |
| 313 | + if (libraries != null && libraries.length > 0) { |
| 314 | + IPackageFragmentRoot root = project.findPackageFragmentRoot(libraries[0].getSystemLibraryPath()); |
| 315 | + if (!(root instanceof JarPackageFragmentRoot)) { |
| 316 | + return null; |
| 317 | + } |
| 318 | + Manifest manifest = ((JarPackageFragmentRoot) root).getManifest(); |
| 319 | + if (manifest == null) { |
| 320 | + return null; |
| 321 | + } |
| 322 | + Attributes attributes = manifest.getMainAttributes(); |
| 323 | + return attributes.getValue("Implementation-Version"); |
| 324 | + } |
| 325 | + |
| 326 | + return null; |
| 327 | + } |
283 | 328 | }
|
0 commit comments