Skip to content

Commit b0d1d77

Browse files
authored
Pass Java installation and info from Java LS per project (#1700)
* Pass Java installation and info from Java LS per project Signed-off-by: BoykoAlex <[email protected]> * VM -> Jre Signed-off-by: BoykoAlex <[email protected]> --------- Signed-off-by: BoykoAlex <[email protected]>
1 parent 475fe88 commit b0d1d77

File tree

19 files changed

+182
-120
lines changed

19 files changed

+182
-120
lines changed

headless-services/commons/commons-gradle/src/main/java/org/springframework/ide/vscode/commons/gradle/GradleProjectClasspath.java

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212

1313
import java.io.File;
1414
import java.net.MalformedURLException;
15-
import java.net.URL;
15+
import java.net.URI;
1616
import java.nio.file.Files;
1717
import java.nio.file.Path;
18+
import java.nio.file.Paths;
19+
import java.util.Optional;
1820
import java.util.stream.Stream;
1921

2022
import org.gradle.tooling.model.build.BuildEnvironment;
@@ -25,6 +27,7 @@
2527
import org.springframework.ide.vscode.commons.java.JavaUtils;
2628
import org.springframework.ide.vscode.commons.protocol.java.Classpath;
2729
import org.springframework.ide.vscode.commons.protocol.java.Classpath.CPE;
30+
import org.springframework.ide.vscode.commons.protocol.java.Jre;
2831

2932
import com.google.common.collect.ImmutableList;
3033
import com.google.common.collect.ImmutableList.Builder;
@@ -50,20 +53,8 @@ public GradleProjectClasspath(GradleCore gradle, File projectDir) throws GradleE
5053
this.buildEnvironment = gradle.getModel(projectDir, BuildEnvironment.class);
5154
}
5255

53-
private EclipseProject getRootProject() {
54-
EclipseProject root = project;
55-
if (root == null) {
56-
return root;
57-
}
58-
while(root.getParent() != null) {
59-
root = root.getParent();
60-
}
61-
return root;
62-
}
63-
6456
@Override
6557
public ImmutableList<CPE> getClasspathEntries() throws Exception {
66-
EclipseProject root = getRootProject();
6758
if (project == null) {
6859
return ImmutableList.of();
6960
} else {
@@ -76,7 +67,7 @@ public ImmutableList<CPE> getClasspathEntries() throws Exception {
7667
}
7768
String urlStr = "https://docs.oracle.com/javase/" + javaVersion + "/docs/api/";
7869
try {
79-
cpe.setJavadocContainerUrl(new URL(urlStr));
70+
cpe.setJavadocContainerUrl(URI.create(urlStr).toURL());
8071
} catch (MalformedURLException e) {
8172
log.error("Invalid javadoc URL: " + urlStr, e);
8273
}
@@ -118,18 +109,17 @@ private static CPE createSourceCPE(EclipseProject project, EclipseSourceDirector
118109
return CPE.source(sourceFolder.getAbsoluteFile(), new File(project.getProjectDirectory(), of));
119110
}
120111

121-
private EclipseProject findPeer(EclipseProject root, String name) {
122-
return root.getChildren().stream().filter(p -> p.getName().equals(name)).findFirst().orElse(null);
123-
}
124-
125112
@Override
126113
public String getName() {
127114
return project == null ? null : project.getName();
128115
}
129116

130117
@Override
131-
public String getJavaVersion() {
132-
return JavaUtils.getJavaRuntimeMinorVersion(getJavaRuntimeVersion());
118+
public Jre getJre() {
119+
if (buildEnvironment == null) {
120+
throw new IllegalArgumentException("No Gradle build available");
121+
}
122+
return new Jre(getJavaRuntimeVersion(), buildEnvironment.getJava().getJavaHome().toPath().toString());
133123
}
134124

135125
public String getGradleVersion() throws GradleException {
@@ -152,14 +142,14 @@ public String getJavaRuntimeVersion() {
152142
return System.getProperty(JAVA_RUNTIME_VERSION);
153143
}
154144

155-
private String getJavaHome() {
145+
public Optional<Path> getJavaHome() {
156146
if (buildEnvironment == null) {
157-
return System.getProperty(JAVA_HOME);
147+
return Optional.of(Paths.get(System.getProperty(JAVA_HOME)));
158148
} else {
159-
return buildEnvironment.getJava().getJavaHome().toString();
149+
return Optional.of(buildEnvironment.getJava().getJavaHome().toPath());
160150
}
161151
}
162-
152+
163153
private Stream<Path> getJreLibs() {
164154
return JavaUtils.jreLibs(() -> JavaUtils.getJavaRuntimeMinorVersion(getJavaRuntimeVersion()), this::getJavaHome, () -> System.getProperty(JAVA_BOOT_CLASS_PATH));
165155
}

headless-services/commons/commons-java/src/main/java/org/springframework/ide/vscode/commons/java/ClasspathData.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.slf4j.Logger;
2020
import org.slf4j.LoggerFactory;
2121
import org.springframework.ide.vscode.commons.protocol.java.Classpath.CPE;
22+
import org.springframework.ide.vscode.commons.protocol.java.Jre;
2223

2324
import com.google.common.cache.Cache;
2425
import com.google.common.cache.CacheBuilder;
@@ -36,16 +37,16 @@ public class ClasspathData implements IClasspath {
3637

3738
private String name;
3839
private Set<CPE> classpathEntries;
39-
private String javaVersion;
40+
private Jre jre;
4041

4142
private Cache<String, Optional<CPE>> binaryLibLookupCache;
4243

4344
public ClasspathData() {}
4445

45-
public ClasspathData(String name, Collection<CPE> classpathEntries, String javaVersion) {
46+
public ClasspathData(String name, Collection<CPE> classpathEntries, Jre jre) {
4647
this.name = name;
4748
this.classpathEntries = ImmutableSet.copyOf(classpathEntries);
48-
this.javaVersion = javaVersion;
49+
this.jre = jre;
4950
this.binaryLibLookupCache = CacheBuilder.newBuilder().build();
5051
}
5152

@@ -60,7 +61,7 @@ public static ClasspathData from(IClasspath d) {
6061
return new ClasspathData(
6162
d.getName(),
6263
entries==null ? ImmutableSet.of() : entries,
63-
d.getJavaVersion()
64+
d.getJre()
6465
);
6566
}
6667

@@ -74,12 +75,12 @@ public void setName(String name) {
7475
}
7576

7677
@Override
77-
public String getJavaVersion() {
78-
return javaVersion;
78+
public Jre getJre() {
79+
return jre;
7980
}
8081

81-
public void setJavaVersion(String javaVersion) {
82-
this.javaVersion = javaVersion;
82+
public void setJre(Jre jre) {
83+
this.jre = jre;
8384
}
8485

8586
@Override

headless-services/commons/commons-java/src/main/java/org/springframework/ide/vscode/commons/java/DelegatingCachedClasspath.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.concurrent.atomic.AtomicReference;
1515

1616
import org.springframework.ide.vscode.commons.protocol.java.Classpath.CPE;
17+
import org.springframework.ide.vscode.commons.protocol.java.Jre;
1718
import org.springframework.ide.vscode.commons.util.Assert;
1819

1920
import com.google.common.base.Objects;
@@ -81,8 +82,8 @@ public ImmutableList<CPE> getClasspathEntries() throws Exception {
8182
}
8283

8384
@Override
84-
public String getJavaVersion() {
85-
return cachedData.get().getJavaVersion();
85+
public Jre getJre() {
86+
return cachedData.get().getJre();
8687
}
8788

8889
public boolean isCached() {

headless-services/commons/commons-java/src/main/java/org/springframework/ide/vscode/commons/java/IClasspath.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2016, 2023 Pivotal, Inc.
2+
* Copyright (c) 2016, 2025 Pivotal, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -18,6 +18,7 @@
1818
import org.slf4j.LoggerFactory;
1919
import org.springframework.ide.vscode.commons.protocol.java.Classpath;
2020
import org.springframework.ide.vscode.commons.protocol.java.Classpath.CPE;
21+
import org.springframework.ide.vscode.commons.protocol.java.Jre;
2122

2223
/**
2324
* Classpath for a Java artifact
@@ -59,9 +60,9 @@ default Optional<CPE> findBinaryLibrary(String prefix) {
5960
}
6061

6162
/**
62-
* Finds Java Version by parsing the classpath entries
63+
* VM info
6364
* @return returns java version
6465
*/
65-
String getJavaVersion();
66+
Jre getJre();
6667

6768
}

headless-services/commons/commons-java/src/main/java/org/springframework/ide/vscode/commons/java/JavaUtils.java

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import java.nio.file.Path;
1616
import java.nio.file.Paths;
1717
import java.util.Arrays;
18+
import java.util.List;
19+
import java.util.Optional;
1820
import java.util.stream.Stream;
1921

2022
import org.slf4j.Logger;
@@ -31,6 +33,45 @@
3133
public class JavaUtils {
3234

3335
private static Logger log = LoggerFactory.getLogger(JavaUtils.class);
36+
37+
private static final String JRE = "jre"; //$NON-NLS-1$
38+
39+
/**
40+
* The list of locations in which to look for the java executable in candidate
41+
* VM install locations, relative to the VM install location. From Java 9 onwards, there may not be a jre directory.
42+
*/
43+
private static final List<Path> CANDIDATE_JAVA_FILES = Stream.of("javaw", "javaw.exe", "java", "java.exe", "j9w", "j9w.exe", "j9", "j9.exe").map(Path::of).toList(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
44+
private static final Path[] CANDIDATE_JAVA_LOCATIONS = { Path.of(""), Path.of("bin"), Path.of(JRE, "bin") }; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
45+
private static final Path BIN = Path.of("bin"); //$NON-NLS-1$
46+
47+
/**
48+
* Starting in the specified VM install location, attempt to find the 'java' executable
49+
* file. If found, return the corresponding <code>File</code> object, otherwise return
50+
* <code>null</code>.
51+
* @param vmInstallLocation the {@link File} location to look in
52+
* @return the {@link File} for the Java executable or <code>null</code>
53+
*/
54+
public static File findJavaExecutable(File vmInstallLocation) {
55+
// Try each candidate in order. The first one found wins. Thus, the order
56+
// of fgCandidateJavaLocations and fgCandidateJavaFiles is significant.
57+
58+
Path filePath = vmInstallLocation.toPath();
59+
boolean isBin = filePath.endsWith(BIN);
60+
for (Path exeName : CANDIDATE_JAVA_FILES) {
61+
for (int j = 0; j < CANDIDATE_JAVA_LOCATIONS.length; j++) {
62+
if (!isBin && j == 0) {
63+
// search in "." only under bin for java executables for Java 9 and above
64+
continue;
65+
}
66+
Path javaFile = filePath.resolve(CANDIDATE_JAVA_LOCATIONS[j]).resolve(exeName);
67+
if (Files.isRegularFile(javaFile)) {
68+
return javaFile.toFile();
69+
}
70+
}
71+
}
72+
return null;
73+
}
74+
3475

3576
/**
3677
* Find JRE libs jars
@@ -40,14 +81,14 @@ public class JavaUtils {
4081
* @param bootClasspathSupplier
4182
* @return
4283
*/
43-
public static Stream<Path> jreLibs(Supplier<String> javaMinorVersionSupplier, Supplier<String> javaHomeSupplier, Supplier<String> bootClasspathSupplier) {
84+
public static Stream<Path> jreLibs(Supplier<String> javaMinorVersionSupplier, Supplier<Optional<Path>> javaHomeSupplier, Supplier<String> bootClasspathSupplier) {
4485
String versionString = javaMinorVersionSupplier.get();
4586
try {
4687
int version = versionString == null ? 8 : Integer.valueOf(versionString);
4788
if (version > 8) {
48-
String javaHome = javaHomeSupplier.get();
49-
if (javaHome != null) {
50-
Path rtPath= Paths.get(javaHome, "lib", "jrt-fs.jar");
89+
Optional<Path> javaHomeOpt = javaHomeSupplier.get();
90+
if (javaHomeOpt.isPresent()) {
91+
Path rtPath= javaHomeOpt.get().resolve("lib").resolve("jrt-fs.jar");
5192
if (Files.exists(rtPath)) {
5293
return Stream.of(rtPath);
5394
} else {
@@ -74,7 +115,7 @@ public static Stream<Path> jreLibs(Supplier<String> javaMinorVersionSupplier, Su
74115
*/
75116
public static String getJavaRuntimeMinorVersion(String fullVersion) {
76117
String[] tokenized = fullVersion.split("\\.");
77-
if (tokenized[0] == "1") {
118+
if ("1".equals(tokenized[0])) {
78119
if (tokenized.length > 1) {
79120
return tokenized[1];
80121
} else {

headless-services/commons/commons-java/src/test/java/org/springframework/ide/vscode/commons/jandex/JandexClasspathTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void createClass(String fqName) throws Exception {
6363
ClasspathData getClasspath() {
6464
return new ClasspathData(name, ImmutableList.of(
6565
CPE.source(new File(root, "src"), outputFolder)
66-
), "");
66+
), null);
6767
}
6868

6969
JandexClasspath getJandexClasspath() {

headless-services/commons/commons-lsp-extensions/src/main/java/org/springframework/ide/vscode/commons/protocol/java/Classpath.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,14 @@ public class Classpath {
2828

2929
public static final String ENTRY_KIND_SOURCE = "source";
3030
public static final String ENTRY_KIND_BINARY = "binary";
31-
public static final Classpath EMPTY = new Classpath(Collections.<CPE>emptyList(), "");
31+
public static final Classpath EMPTY = new Classpath(Collections.<CPE>emptyList(), null);
3232

3333
private List<CPE> entries;
34-
private String javaVersion;
34+
private Jre jre;
3535

36-
public Classpath(List<CPE> entries, String javaVersion) {
37-
super();
36+
public Classpath(List<CPE> entries, Jre jre) {
3837
this.entries = entries;
39-
this.javaVersion = javaVersion;
38+
this.jre = jre;
4039
}
4140

4241
public List<CPE> getEntries() {
@@ -47,17 +46,17 @@ public void setEntries(List<CPE> entries) {
4746
this.entries = entries;
4847
}
4948

50-
public String getJavaVersion() {
51-
return javaVersion;
49+
public Jre getJre() {
50+
return jre;
5251
}
5352

54-
public void setJavaVersion(String javaVersion) {
55-
this.javaVersion = javaVersion;
53+
public void setJre(Jre jre) {
54+
this.jre = jre;
5655
}
5756

5857
@Override
5958
public String toString() {
60-
return "Classpath [entries=" + entries + ", javaVersion=" + javaVersion + "]";
59+
return "Classpath [entries=" + entries + ", jre=" + jre + "]";
6160
}
6261

6362
public static class CPE {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Broadcom, Inc.
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+
* https://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Broadcom, Inc. - initial API and implementation
10+
*******************************************************************************/
11+
package org.springframework.ide.vscode.commons.protocol.java;
12+
13+
public record Jre(String version, String installationPath) {
14+
}

headless-services/commons/commons-maven/src/main/java/org/springframework/ide/vscode/commons/maven/MavenCore.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
import java.io.InputStreamReader;
1818
import java.nio.file.Files;
1919
import java.nio.file.Path;
20+
import java.nio.file.Paths;
2021
import java.util.ArrayList;
2122
import java.util.Arrays;
2223
import java.util.Collection;
2324
import java.util.Collections;
2425
import java.util.HashSet;
2526
import java.util.LinkedHashSet;
2627
import java.util.List;
28+
import java.util.Optional;
2729
import java.util.Set;
2830
import java.util.stream.Collectors;
2931
import java.util.stream.Stream;
@@ -272,10 +274,10 @@ public Artifact getTestSources(Artifact artifact, List<ArtifactRepository> repos
272274
public Stream<Path> getJreLibs() throws MavenException {
273275
return JavaUtils.jreLibs(this::getJavaRuntimeMinorVersion, () -> {
274276
try {
275-
return maven.createExecutionRequest().getSystemProperties().getProperty(JAVA_HOME);
277+
return Optional.of(Paths.get(getJavaHome()));
276278
} catch (MavenException e) {
277279
log.error("Cannot determine java home", e);
278-
return null;
280+
return Optional.empty();
279281
}
280282
},
281283
() -> {
@@ -292,7 +294,11 @@ public Stream<Path> getJreLibs() throws MavenException {
292294
public String getJavaRuntimeVersion() throws MavenException {
293295
return maven.createExecutionRequest().getSystemProperties().getProperty(JAVA_RUNTIME_VERSION);
294296
}
295-
297+
298+
public String getJavaHome() throws MavenException {
299+
return maven.createExecutionRequest().getSystemProperties().getProperty(JAVA_HOME);
300+
}
301+
296302
public String getJavaRuntimeMinorVersion() {
297303
try {
298304
return JavaUtils.getJavaRuntimeMinorVersion(getJavaRuntimeVersion());

0 commit comments

Comments
 (0)