Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.stream.Stream;

import org.gradle.tooling.model.build.BuildEnvironment;
Expand All @@ -25,6 +27,7 @@
import org.springframework.ide.vscode.commons.java.JavaUtils;
import org.springframework.ide.vscode.commons.protocol.java.Classpath;
import org.springframework.ide.vscode.commons.protocol.java.Classpath.CPE;
import org.springframework.ide.vscode.commons.protocol.java.Jre;

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

private EclipseProject getRootProject() {
EclipseProject root = project;
if (root == null) {
return root;
}
while(root.getParent() != null) {
root = root.getParent();
}
return root;
}

@Override
public ImmutableList<CPE> getClasspathEntries() throws Exception {
EclipseProject root = getRootProject();
if (project == null) {
return ImmutableList.of();
} else {
Expand All @@ -76,7 +67,7 @@ public ImmutableList<CPE> getClasspathEntries() throws Exception {
}
String urlStr = "https://docs.oracle.com/javase/" + javaVersion + "/docs/api/";
try {
cpe.setJavadocContainerUrl(new URL(urlStr));
cpe.setJavadocContainerUrl(URI.create(urlStr).toURL());
} catch (MalformedURLException e) {
log.error("Invalid javadoc URL: " + urlStr, e);
}
Expand Down Expand Up @@ -118,18 +109,17 @@ private static CPE createSourceCPE(EclipseProject project, EclipseSourceDirector
return CPE.source(sourceFolder.getAbsoluteFile(), new File(project.getProjectDirectory(), of));
}

private EclipseProject findPeer(EclipseProject root, String name) {
return root.getChildren().stream().filter(p -> p.getName().equals(name)).findFirst().orElse(null);
}

@Override
public String getName() {
return project == null ? null : project.getName();
}

@Override
public String getJavaVersion() {
return JavaUtils.getJavaRuntimeMinorVersion(getJavaRuntimeVersion());
public Jre getJre() {
if (buildEnvironment == null) {
throw new IllegalArgumentException("No Gradle build available");
}
return new Jre(getJavaRuntimeVersion(), buildEnvironment.getJava().getJavaHome().toPath().toString());
}

public String getGradleVersion() throws GradleException {
Expand All @@ -152,14 +142,14 @@ public String getJavaRuntimeVersion() {
return System.getProperty(JAVA_RUNTIME_VERSION);
}

private String getJavaHome() {
public Optional<Path> getJavaHome() {
if (buildEnvironment == null) {
return System.getProperty(JAVA_HOME);
return Optional.of(Paths.get(System.getProperty(JAVA_HOME)));
} else {
return buildEnvironment.getJava().getJavaHome().toString();
return Optional.of(buildEnvironment.getJava().getJavaHome().toPath());
}
}

private Stream<Path> getJreLibs() {
return JavaUtils.jreLibs(() -> JavaUtils.getJavaRuntimeMinorVersion(getJavaRuntimeVersion()), this::getJavaHome, () -> System.getProperty(JAVA_BOOT_CLASS_PATH));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ide.vscode.commons.protocol.java.Classpath.CPE;
import org.springframework.ide.vscode.commons.protocol.java.Jre;

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

private String name;
private Set<CPE> classpathEntries;
private String javaVersion;
private Jre jre;

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

public ClasspathData() {}

public ClasspathData(String name, Collection<CPE> classpathEntries, String javaVersion) {
public ClasspathData(String name, Collection<CPE> classpathEntries, Jre jre) {
this.name = name;
this.classpathEntries = ImmutableSet.copyOf(classpathEntries);
this.javaVersion = javaVersion;
this.jre = jre;
this.binaryLibLookupCache = CacheBuilder.newBuilder().build();
}

Expand All @@ -60,7 +61,7 @@ public static ClasspathData from(IClasspath d) {
return new ClasspathData(
d.getName(),
entries==null ? ImmutableSet.of() : entries,
d.getJavaVersion()
d.getJre()
);
}

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

@Override
public String getJavaVersion() {
return javaVersion;
public Jre getJre() {
return jre;
}

public void setJavaVersion(String javaVersion) {
this.javaVersion = javaVersion;
public void setJre(Jre jre) {
this.jre = jre;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.concurrent.atomic.AtomicReference;

import org.springframework.ide.vscode.commons.protocol.java.Classpath.CPE;
import org.springframework.ide.vscode.commons.protocol.java.Jre;
import org.springframework.ide.vscode.commons.util.Assert;

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

@Override
public String getJavaVersion() {
return cachedData.get().getJavaVersion();
public Jre getJre() {
return cachedData.get().getJre();
}

public boolean isCached() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2016, 2023 Pivotal, Inc.
* Copyright (c) 2016, 2025 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -18,6 +18,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.ide.vscode.commons.protocol.java.Classpath;
import org.springframework.ide.vscode.commons.protocol.java.Classpath.CPE;
import org.springframework.ide.vscode.commons.protocol.java.Jre;

/**
* Classpath for a Java artifact
Expand Down Expand Up @@ -59,9 +60,9 @@ default Optional<CPE> findBinaryLibrary(String prefix) {
}

/**
* Finds Java Version by parsing the classpath entries
* VM info
* @return returns java version
*/
String getJavaVersion();
Jre getJre();

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import org.slf4j.Logger;
Expand All @@ -31,6 +33,45 @@
public class JavaUtils {

private static Logger log = LoggerFactory.getLogger(JavaUtils.class);

private static final String JRE = "jre"; //$NON-NLS-1$

/**
* The list of locations in which to look for the java executable in candidate
* VM install locations, relative to the VM install location. From Java 9 onwards, there may not be a jre directory.
*/
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$
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$
private static final Path BIN = Path.of("bin"); //$NON-NLS-1$

/**
* Starting in the specified VM install location, attempt to find the 'java' executable
* file. If found, return the corresponding <code>File</code> object, otherwise return
* <code>null</code>.
* @param vmInstallLocation the {@link File} location to look in
* @return the {@link File} for the Java executable or <code>null</code>
*/
public static File findJavaExecutable(File vmInstallLocation) {
// Try each candidate in order. The first one found wins. Thus, the order
// of fgCandidateJavaLocations and fgCandidateJavaFiles is significant.

Path filePath = vmInstallLocation.toPath();
boolean isBin = filePath.endsWith(BIN);
for (Path exeName : CANDIDATE_JAVA_FILES) {
for (int j = 0; j < CANDIDATE_JAVA_LOCATIONS.length; j++) {
if (!isBin && j == 0) {
// search in "." only under bin for java executables for Java 9 and above
continue;
}
Path javaFile = filePath.resolve(CANDIDATE_JAVA_LOCATIONS[j]).resolve(exeName);
if (Files.isRegularFile(javaFile)) {
return javaFile.toFile();
}
}
}
return null;
}


/**
* Find JRE libs jars
Expand All @@ -40,14 +81,14 @@ public class JavaUtils {
* @param bootClasspathSupplier
* @return
*/
public static Stream<Path> jreLibs(Supplier<String> javaMinorVersionSupplier, Supplier<String> javaHomeSupplier, Supplier<String> bootClasspathSupplier) {
public static Stream<Path> jreLibs(Supplier<String> javaMinorVersionSupplier, Supplier<Optional<Path>> javaHomeSupplier, Supplier<String> bootClasspathSupplier) {
String versionString = javaMinorVersionSupplier.get();
try {
int version = versionString == null ? 8 : Integer.valueOf(versionString);
if (version > 8) {
String javaHome = javaHomeSupplier.get();
if (javaHome != null) {
Path rtPath= Paths.get(javaHome, "lib", "jrt-fs.jar");
Optional<Path> javaHomeOpt = javaHomeSupplier.get();
if (javaHomeOpt.isPresent()) {
Path rtPath= javaHomeOpt.get().resolve("lib").resolve("jrt-fs.jar");
if (Files.exists(rtPath)) {
return Stream.of(rtPath);
} else {
Expand All @@ -74,7 +115,7 @@ public static Stream<Path> jreLibs(Supplier<String> javaMinorVersionSupplier, Su
*/
public static String getJavaRuntimeMinorVersion(String fullVersion) {
String[] tokenized = fullVersion.split("\\.");
if (tokenized[0] == "1") {
if ("1".equals(tokenized[0])) {
if (tokenized.length > 1) {
return tokenized[1];
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void createClass(String fqName) throws Exception {
ClasspathData getClasspath() {
return new ClasspathData(name, ImmutableList.of(
CPE.source(new File(root, "src"), outputFolder)
), "");
), null);
}

JandexClasspath getJandexClasspath() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ public class Classpath {

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

private List<CPE> entries;
private String javaVersion;
private Jre jre;

public Classpath(List<CPE> entries, String javaVersion) {
super();
public Classpath(List<CPE> entries, Jre jre) {
this.entries = entries;
this.javaVersion = javaVersion;
this.jre = jre;
}

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

public String getJavaVersion() {
return javaVersion;
public Jre getJre() {
return jre;
}

public void setJavaVersion(String javaVersion) {
this.javaVersion = javaVersion;
public void setJre(Jre jre) {
this.jre = jre;
}

@Override
public String toString() {
return "Classpath [entries=" + entries + ", javaVersion=" + javaVersion + "]";
return "Classpath [entries=" + entries + ", jre=" + jre + "]";
}

public static class CPE {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*******************************************************************************
* Copyright (c) 2025 Broadcom, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Broadcom, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.commons.protocol.java;

public record Jre(String version, String installationPath) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -272,10 +274,10 @@ public Artifact getTestSources(Artifact artifact, List<ArtifactRepository> repos
public Stream<Path> getJreLibs() throws MavenException {
return JavaUtils.jreLibs(this::getJavaRuntimeMinorVersion, () -> {
try {
return maven.createExecutionRequest().getSystemProperties().getProperty(JAVA_HOME);
return Optional.of(Paths.get(getJavaHome()));
} catch (MavenException e) {
log.error("Cannot determine java home", e);
return null;
return Optional.empty();
}
},
() -> {
Expand All @@ -292,7 +294,11 @@ public Stream<Path> getJreLibs() throws MavenException {
public String getJavaRuntimeVersion() throws MavenException {
return maven.createExecutionRequest().getSystemProperties().getProperty(JAVA_RUNTIME_VERSION);
}


public String getJavaHome() throws MavenException {
return maven.createExecutionRequest().getSystemProperties().getProperty(JAVA_HOME);
}

public String getJavaRuntimeMinorVersion() {
try {
return JavaUtils.getJavaRuntimeMinorVersion(getJavaRuntimeVersion());
Expand Down
Loading