Skip to content

Commit

Permalink
Fix package calculation do not work on Java 9 and return java version
Browse files Browse the repository at this point in the history
  • Loading branch information
laeubi authored and mickaelistria committed Jun 7, 2022
1 parent 9673011 commit 6f61ba0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*******************************************************************************/
package org.eclipse.tycho.core.ee;

import java.lang.Runtime.Version;
import java.lang.module.ModuleDescriptor.Exports;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
Expand All @@ -19,17 +21,27 @@
public class ListSystemPackages {

public static void main(String[] args) {
Version version = Runtime.version();
try {
System.out.println(version.feature());
} catch (NoSuchMethodError e) {
System.out.println("9");
}
getCurrentJREPackages().forEach(System.out::println);
}

public static Set<String> getCurrentJREPackages() {
return ModuleLayer.boot().modules().stream().map(Module::getDescriptor) //
.flatMap(desc -> desc.isAutomatic() ? //
desc.packages().stream() : //
desc.exports().stream()
.filter(Predicate.not(java.lang.module.ModuleDescriptor.Exports::isQualified))
desc.exports().stream().filter(not(java.lang.module.ModuleDescriptor.Exports::isQualified))
.map(java.lang.module.ModuleDescriptor.Exports::source) //
).collect(Collectors.toSet());
}

private static Predicate<? super Exports> not(Predicate<Exports> predicate) {
// java 9 compatible polyfill
return x -> !predicate.test(x);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -110,29 +110,56 @@ public class StandardExecutionEnvironment implements Comparable<StandardExecutio
this.logger = logger;
}

private Set<String> readFromToolchains(Toolchain toolchain) {
static JavaInfo readFromToolchains(Toolchain toolchain, Logger logger) {
if (toolchain == null) {
return Collections.emptySet();
return new JavaInfo(-1, Collections.emptySet());
}
String java = toolchain.findTool("java");
if (java == null) {
return Collections.emptySet();
return new JavaInfo(-1, Collections.emptySet());
}
Set<String> res = new HashSet<>();
int version = -1;
try {
ProcessBuilder builder = new ProcessBuilder(java, "-jar",
getSystemPackagesCompanionJar().getAbsolutePath());
try (BufferedReader reader = new BufferedReader(
new java.io.InputStreamReader(builder.start().getInputStream(), Charset.defaultCharset()))) {
String line = null;
String line = reader.readLine();
try {
if (line != null) {
//for old vms < java 9 we might get no response at all
version = Integer.parseInt(line);
}
} catch (NumberFormatException e) {
StringBuilder sb = new StringBuilder(line);
while ((line = reader.readLine()) != null) {
sb.append(System.lineSeparator());
sb.append(line);
}
logger.debug("[ReadPackagesFromToolchains] Can't read java version for " + java
+ ", full output was: " + sb);
return new JavaInfo(-1, List.of());
}
while ((line = reader.readLine()) != null) {
res.add(line);
}
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
logger.error("[ReadPackagesFromToolchains] start JVM process for " + java + " failed: " + e);
}
return new JavaInfo(version, res);
}

static final class JavaInfo {
final int version;
final Collection<String> packages;

private JavaInfo(int version, Collection<String> packages) {
this.version = version;
this.packages = Collections.unmodifiableCollection(packages);
}
return res;

}

static File getSystemPackagesCompanionJar() throws IOException {
Expand Down Expand Up @@ -226,7 +253,7 @@ public synchronized Collection<SystemPackageEntry> getSystemPackages() {
} else if (toolchain != null) {
logger.debug(
"No system.packages in profile definition file for " + profileName + "; checking toolchain.");
this.systemPackages = readFromToolchains(toolchain).stream()
this.systemPackages = readFromToolchains(toolchain, logger).packages.stream()
.map(packageName -> new SystemPackageEntry(packageName, null)).collect(Collectors.toList());
} else if (Integer.parseInt(compilerSourceLevel) == Runtime.version().feature()) {
logger.debug("Currently running JRE matches source level for " + getProfileName()
Expand Down

0 comments on commit 6f61ba0

Please sign in to comment.