10
10
11
11
package org .junit .api .tools ;
12
12
13
+ import static java .nio .charset .StandardCharsets .UTF_8 ;
13
14
import static java .util .stream .Collectors .toCollection ;
14
15
15
16
import java .io .BufferedOutputStream ;
17
+ import java .io .File ;
16
18
import java .io .IOException ;
17
19
import java .io .OutputStream ;
18
20
import java .io .PrintWriter ;
19
21
import java .io .UncheckedIOException ;
22
+ import java .lang .module .ModuleFinder ;
20
23
import java .nio .file .Files ;
21
24
import java .nio .file .Path ;
22
25
import java .util .ArrayList ;
25
28
import java .util .EnumSet ;
26
29
import java .util .List ;
27
30
import java .util .Map ;
31
+ import java .util .Set ;
28
32
import java .util .SortedSet ;
29
33
import java .util .TreeSet ;
30
34
import java .util .stream .Stream ;
@@ -52,30 +56,30 @@ public static void main(String... args) {
52
56
// CAUTION: The output produced by this method is used to
53
57
// generate a table in the User Guide.
54
58
55
- var reportGenerator = new ApiReportGenerator ();
59
+ try ( var scanResult = scanClasspath ()) {
56
60
57
- // scan all types below "org.junit" package
58
- var apiReport = reportGenerator . generateReport ( "org.junit" );
59
-
60
- // ApiReportWriter reportWriter = new MarkdownApiReportWriter (apiReport);
61
- ApiReportWriter reportWriter = new AsciidocApiReportWriter (apiReport );
62
- // ApiReportWriter reportWriter = new HtmlApiReportWriter(apiReport);
63
-
64
- // reportWriter.printReportHeader(new PrintWriter(System.out, true));
65
-
66
- // Print report for all Usage enum constants
67
- // reportWriter.printDeclarationInfo(new PrintWriter(System.out, true), EnumSet.allOf(Status.class));
68
-
69
- // Print report only for specific Status constants, defaults to only EXPERIMENTAL
70
- parseArgs ( args ). forEach (( status , opener ) -> {
71
- try ( var stream = opener . openStream ()) {
72
- var writer = new PrintWriter ( stream == null ? System . out : stream , true );
73
- reportWriter . printDeclarationInfo ( writer , EnumSet . of ( status ));
74
- }
75
- catch ( IOException e ) {
76
- throw new UncheckedIOException ( "Failed to write report" , e );
77
- }
78
- });
61
+ var apiReport = generateReport ( scanResult );
62
+
63
+ // ApiReportWriter reportWriter = new MarkdownApiReportWriter(apiReport);
64
+ ApiReportWriter reportWriter = new AsciidocApiReportWriter (apiReport );
65
+ // ApiReportWriter reportWriter = new HtmlApiReportWriter (apiReport);
66
+
67
+ // reportWriter.printReportHeader(new PrintWriter(System.out, true));
68
+
69
+ // Print report for all Usage enum constants
70
+ // reportWriter.printDeclarationInfo(new PrintWriter(System.out, true), EnumSet.allOf(Status.class));
71
+
72
+ // Print report only for specific Status constants, defaults to only EXPERIMENTAL
73
+ parseArgs ( args ). forEach (( status , opener ) -> {
74
+ try ( var stream = opener . openStream ()) {
75
+ var writer = new PrintWriter ( stream == null ? System . out : stream , true , UTF_8 );
76
+ reportWriter . printDeclarationInfo ( writer , EnumSet . of ( status ) );
77
+ }
78
+ catch ( IOException e ) {
79
+ throw new UncheckedIOException ( "Failed to write report" , e );
80
+ }
81
+ });
82
+ }
79
83
}
80
84
81
85
// -------------------------------------------------------------------------
@@ -102,45 +106,48 @@ private interface StreamOpener {
102
106
OutputStream openStream () throws IOException ;
103
107
}
104
108
105
- ApiReport generateReport (String ... packages ) {
109
+ private static ApiReport generateReport (ScanResult scanResult ) {
106
110
Map <Status , List <Declaration >> declarations = new EnumMap <>(Status .class );
107
111
for (var status : Status .values ()) {
108
112
declarations .put (status , new ArrayList <>());
109
113
}
110
114
111
- try (var scanResult = scanClasspath (packages )) {
112
-
113
- var types = collectTypes (scanResult );
114
- types .stream () //
115
- .map (Declaration .Type ::new ) //
116
- .forEach (type -> declarations .get (type .status ()).add (type ));
115
+ var types = collectTypes (scanResult );
116
+ types .stream () //
117
+ .map (Declaration .Type ::new ) //
118
+ .forEach (type -> declarations .get (type .status ()).add (type ));
117
119
118
- collectMethods (scanResult ) //
119
- .map (Declaration .Method ::new ) //
120
- .filter (method -> !declarations .get (method .status ()) //
121
- .contains (new Declaration .Type (method .classInfo ()))) //
122
- .forEach (method -> {
123
- types .add (method .classInfo ());
124
- declarations .get (method .status ()).add (method );
125
- });
120
+ collectMethods (scanResult ) //
121
+ .map (Declaration .Method ::new ) //
122
+ .filter (method -> !declarations .get (method .status ()) //
123
+ .contains (new Declaration .Type (method .classInfo ()))) //
124
+ .forEach (method -> {
125
+ types .add (method .classInfo ());
126
+ declarations .get (method .status ()).add (method );
127
+ });
126
128
127
- declarations .values ().forEach (list -> list .sort (null ));
129
+ declarations .values ().forEach (list -> list .sort (null ));
128
130
129
- return new ApiReport (types , declarations );
130
- }
131
+ return new ApiReport (types , declarations );
131
132
}
132
133
133
- private static ScanResult scanClasspath (String [] packages ) {
134
+ private static ScanResult scanClasspath () {
135
+ // scan all types below "org.junit" package
134
136
var classGraph = new ClassGraph () //
135
- .acceptPackages (packages ) //
136
- .rejectPackages ("*.shadow.*" ) //
137
+ .acceptPackages ("org.junit" ) //
138
+ .rejectPackages ("*.shadow.*" , "org.opentest4j.*" ) //
137
139
.disableNestedJarScanning () //
138
140
.enableClassInfo () //
139
141
.enableMethodInfo () //
140
142
.enableAnnotationInfo (); //
141
143
var apiClasspath = System .getProperty ("api.classpath" );
142
144
if (apiClasspath != null ) {
143
- classGraph = classGraph .overrideClasspath (apiClasspath );
145
+ var paths = Arrays .stream (apiClasspath .split (File .pathSeparator )).map (Path ::of ).toArray (Path []::new );
146
+ var bootLayer = ModuleLayer .boot ();
147
+ var configuration = bootLayer .configuration ().resolveAndBind (ModuleFinder .of (), ModuleFinder .of (paths ),
148
+ Set .of ());
149
+ var layer = bootLayer .defineModulesWithOneLoader (configuration , ClassLoader .getPlatformClassLoader ());
150
+ classGraph = classGraph .overrideModuleLayers (layer );
144
151
}
145
152
return classGraph .scan ();
146
153
}
0 commit comments