-
Notifications
You must be signed in to change notification settings - Fork 188
/
ListSystemPackages.java
47 lines (41 loc) · 1.88 KB
/
ListSystemPackages.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*******************************************************************************
* Copyright (c) 2020 Red Hat Inc. and others.
* 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
*******************************************************************************/
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;
/*
* a copy of this class has to be compiled targeting Java 9 bytecode and jar'd, and included as resource in
* the artifact jar root.
* It's used to ask external JREs (defined in toolchains) for their system packages.
*/
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(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);
}
}