Skip to content

Commit 04f70c7

Browse files
7903840: apidiff: optionally show expansion of --jdk-build
Reviewed-by: iris
1 parent 18800d3 commit 04f70c7

File tree

6 files changed

+124
-26
lines changed

6 files changed

+124
-26
lines changed

src/share/classes/jdk/codetools/apidiff/JDKBuildOption.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020,2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -61,22 +61,44 @@ public class JDKBuildOption {
6161
}
6262

6363
void expand(Options options, Options.APIOptions apiOptions, Log log) {
64-
apiOptions.addFileManagerOpt("--system", getSystem().toString());
64+
65+
boolean verbose = options.isVerbose(Options.VerboseKind.OPTIONS);
66+
if (verbose) {
67+
log.err.println("Expanding --jdk-build for API " + apiOptions.name);
68+
log.err.println(" --jdk-build: " + buildDir);
69+
}
70+
71+
Path system = getSystem();
72+
if (verbose) {
73+
log.err.println(" --system " + system);
74+
}
75+
apiOptions.addFileManagerOpt("--system", system.toString());
6576

6677
// proactively get api dir if available, in case we want to subsequently
6778
// set compareAPIDescriptions by default
68-
apiOptions.apiDir = getAPIDirectory(options, log);
79+
Path apiDir = getAPIDirectory(options, log);
80+
if (verbose && apiDir != null) {
81+
log.err.println(" --api-directory " + apiDir);
82+
}
83+
apiOptions.apiDir = apiDir;
6984

7085
if (options.compareDocComments == Boolean.TRUE) {
7186
Set<String> modules = new LinkedHashSet<>();
7287
Path tmpDir = unpackSource(options, log, modules);
7388
for (String m : modules) {
74-
apiOptions.addFileManagerOpt("--patch-module",
75-
m + "=" + tmpDir.resolve(m));
89+
String patchModule = m + "=" + tmpDir.resolve(m);
90+
if (verbose) {
91+
log.err.println(" --patch-module " + patchModule);
92+
}
93+
apiOptions.addFileManagerOpt("--patch-module", patchModule);
7694
}
7795
// since we're also setting the --system option,
7896
// just set the --source option here
79-
apiOptions.source = getRelease(log);
97+
String release = getRelease(log);
98+
if (verbose) {
99+
log.err.println(" --source " + release);
100+
}
101+
apiOptions.source = release;
80102
}
81103
}
82104

src/share/classes/jdk/codetools/apidiff/Main.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018,2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -170,10 +170,15 @@ private Result run(List<String> args, Log log) {
170170
Selector s = new Selector(options.includes, options.excludes);
171171
AccessKind ak = options.getAccessKind();
172172

173+
boolean verboseOptions = options.isVerbose(VerboseKind.OPTIONS);
174+
if (verboseOptions) {
175+
options.allAPIOptions.values().forEach(a -> a.showVerboseSummary(log));
176+
}
177+
173178
// TODO: when APIDiff moves to JDK 21, thia can trivially become SequencedSet,
174179
// which would be useful in varoius places, such as PageReporter.getResultGlyph
175180
Set<API> apis = options.allAPIOptions.values().stream()
176-
.map(a -> API.of(a, s, ak, log))
181+
.map(a -> API.of(a, s, ak, log, verboseOptions))
177182
.collect(Collectors.toCollection(LinkedHashSet::new));
178183

179184
List<Reporter> rList = new ArrayList<>();

src/share/classes/jdk/codetools/apidiff/Options.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -119,6 +119,33 @@ void addFileManagerOpt(String opt, String arg) {
119119
fileManagerOpts.computeIfAbsent(opt, _o -> new ArrayList<>()).add(arg);
120120
}
121121

122+
public void showVerboseSummary(Log log) {
123+
log.err.println("Summary of options for API " + name);
124+
if (label != null && !label.equals(name)) {
125+
log.err.println(" label: " + label);
126+
}
127+
if (jdkBuildDir != null) {
128+
log.err.println(" [--jdk-build " + jdkBuildDir + "]");
129+
}
130+
if (!fileManagerOpts.isEmpty()) {
131+
fileManagerOpts.forEach((opt, list) -> {
132+
log.err.println(" " + opt + " " + String.join(" ", list));
133+
});
134+
}
135+
if (release != null) {
136+
log.err.println(" --release " + release);
137+
}
138+
if (source != null) {
139+
log.err.println(" --source " + source);
140+
}
141+
if (enablePreview) {
142+
log.err.println(" --enable-preview");
143+
}
144+
if (apiDir != null) {
145+
log.err.println(" --api-directory " + apiDir);
146+
}
147+
}
148+
122149
public String toString() {
123150
return "APIOptions[name:" + name
124151
+ ",label:" + label
@@ -202,15 +229,16 @@ public enum VerboseKind {
202229
/** Generate messages about missing items. */
203230
MISSING,
204231
/** Generate messages about the time taken. */
205-
TIME
232+
TIME,
233+
/** Generate messages about handling the command-line options. */
234+
OPTIONS
206235
}
207236

208237
private Set<VerboseKind> verboseKinds = EnumSet.noneOf(VerboseKind.class);
209238

210239
// meta options
211240
boolean help;
212241
boolean version;
213-
private boolean verbose;
214242

215243
// hidden options
216244
private Map<String, String> hidden = new HashMap<>();
@@ -437,7 +465,7 @@ void process(String opt, String arg, Options options) throws BadOption {
437465
*/
438466
JDK_DOCS("--jdk-docs", "opt.arg.jdk-docs") {
439467
@Override
440-
void process(String opt, String arg, Options options) throws BadOption {
468+
void process(String opt, String arg, Options options) {
441469
options.jdkDocs = arg;
442470
}
443471
},

src/share/classes/jdk/codetools/apidiff/model/API.java

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -107,11 +107,12 @@ public abstract class API {
107107
* @param ak the access kind, to filter the set of elements to be compared according
108108
* their declared access
109109
* @param log a log, to which any problems will be reported
110+
* @param verboseOptions whether to be verbose about internal opton details
110111
*
111112
* @return the API
112113
*/
113-
public static API of(APIOptions opts, Selector s, AccessKind ak, Log log) {
114-
return new JavacAPI(opts, s, ak, log);
114+
public static API of(APIOptions opts, Selector s, AccessKind ak, Log log, boolean verboseOptions) {
115+
return new JavacAPI(opts, s, ak, log, verboseOptions);
115116
}
116117

117118
/**
@@ -145,20 +146,26 @@ public static API of(APIOptions opts, Selector s, AccessKind ak, Log log) {
145146
*/
146147
protected final StandardJavaFileManager fileManager;
147148

149+
/**
150+
* Whether to be verbose about internal option details.
151+
*/
152+
protected final boolean verboseOptions;
153+
148154
/**
149155
* Creates an instance of an API.
150156
*
151157
* @param opts the options for the API
152158
* @param s the selector for the elements to be compared
153159
* @param ak the access kind for the elements to be compared
154-
* @param log the log, to which any any problems will be reported
160+
* @param log the log, to which any problems will be reported
155161
*/
156-
protected API(APIOptions opts, Selector s, AccessKind ak, Log log) {
162+
protected API(APIOptions opts, Selector s, AccessKind ak, Log log, boolean verboseOptions) {
157163
this.name = opts.name;
158164
this.label = opts.label;
159165
this.selector = s;
160166
this.accessKind = ak;
161167
this.log = log;
168+
this.verboseOptions = verboseOptions;
162169

163170
fileManager = compiler.getStandardFileManager(null, null, null);
164171
}
@@ -339,6 +346,7 @@ public abstract List<JavaFileObject> listFiles(LocationKind kind, Element e, Str
339346

340347
static class JavacAPI extends API {
341348
private List<String> javacOpts;
349+
private List<String> fmOpts; // just for verbose reporting
342350
private int platformVersion;
343351
private Elements elements;
344352
private Types types;
@@ -352,7 +360,7 @@ static class JavacAPI extends API {
352360
/**
353361
* A tuple containing a location and the kinds of files that may be read from that location.
354362
*/
355-
private class LocationAndKinds {
363+
private static class LocationAndKinds {
356364
final Location locn;
357365
final Set<JavaFileObject.Kind> kinds;
358366
LocationAndKinds(Location locn, Set<JavaFileObject.Kind> kinds) {
@@ -381,13 +389,16 @@ protected boolean removeEldestEntry(Map.Entry<Path,APIDocs> eldest) {
381389
}
382390
};
383391

384-
JavacAPI(APIOptions opts, Selector s, AccessKind ak, Log log) {
385-
super(opts, s, ak, log);
392+
JavacAPI(APIOptions opts, Selector s, AccessKind ak, Log log, boolean verboseOptions) {
393+
super(opts, s, ak, log, verboseOptions);
386394

395+
fmOpts = new ArrayList<>();
387396
for (Map.Entry<String, List<String>> e : opts.fileManagerOpts.entrySet()) {
388397
String opt = e.getKey();
398+
fmOpts.add(opt);
389399
List<String> args = e.getValue();
390400
for (String arg : args) {
401+
fmOpts.add(arg);
391402
Iterator<String> argIter = arg == null
392403
? Collections.emptyIterator()
393404
: Collections.singleton(arg).iterator();
@@ -441,6 +452,9 @@ void initJavac(Set<String> selectedModules) {
441452
javacOpts.add(String.join(",", selectedModules));
442453
}
443454
javacOpts.add("-proc:only");
455+
if (verboseOptions) {
456+
showJavacOptions();
457+
}
444458
JavacTask javacTask = (JavacTask) compiler.getTask(log.err, fileManager, this::reportDiagnostic, javacOpts, null, null);
445459
elements = javacTask.getElements();
446460
elements.getModuleElement("java.base"); // forces module graph to be instantiated, etc
@@ -465,6 +479,32 @@ public SerializedFormDocs getSerializedFormDocs(TypeElement te) {
465479
};
466480
}
467481

482+
private void showJavacOptions() {
483+
log.err.println("Effective javac options for API " + name);
484+
boolean needNewline = false;
485+
// The following is a convenient fiction: to report all the javac options as "equivalent".
486+
// In reality, the file manager options have already been handled separately and are
487+
// now stashed in the file manager, without easy access (except via Locations).
488+
List<String> allOpts = new ArrayList<>();
489+
allOpts.addAll(fmOpts);
490+
allOpts.addAll(javacOpts);
491+
for (String opt : allOpts) {
492+
if (opt.startsWith("-")) {
493+
if (needNewline) {
494+
log.err.println();
495+
}
496+
log.err.print(" ");
497+
} else {
498+
log.err.print(" ");
499+
}
500+
log.err.print(opt);
501+
needNewline = true;
502+
}
503+
if (needNewline) {
504+
log.err.println();
505+
}
506+
}
507+
468508
@Override
469509
public Set<PackageElement> getPackageElements() {
470510
if (packages == null) {
@@ -566,7 +606,7 @@ public Set<PackageElement> getExportedPackageElements(ModuleElement m) {
566606
.filter(d -> d.getKind() == ModuleElement.DirectiveKind.EXPORTS)
567607
.map(d -> (ModuleElement.ExportsDirective) d)
568608
.filter(d -> d.getTargetModules() == null)
569-
.map(d -> d.getPackage())
609+
.map(ModuleElement.ExportsDirective::getPackage)
570610
.collect(Collectors.toSet());
571611

572612
return getPackageElements(m).stream()
@@ -1070,7 +1110,7 @@ public Void visitPrimitive(PrimitiveType t, StringBuilder sb) {
10701110

10711111
@Override
10721112
public Void visitNull(NullType t, StringBuilder sb) {
1073-
throw new IllegalArgumentException(t.getKind() + " " + t.toString());
1113+
throw new IllegalArgumentException(t.getKind() + " " + t);
10741114
}
10751115

10761116
@Override

src/share/doc/apidiff.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,11 @@ pairwise, with each of the older instances being compared against the newest ins
233233
<a id="option-verbose">`--verbose` _flag_[`,`_flag_]*</a>
234234
: Specifies the kinds of verbose output. _flag_ may be one of
235235
`all`, `none`, or one of the following, optionally preceded by `-`:
236-
`module`, `package`, `type`, `time`.
236+
`module`, `package`, `type`, `time`, `options`.
237237

238238
<a id="option-at">`@`*filename*</a>
239239
: Reads options from a file. To shorten or simplify the `apidiff` command, you can specify
240-
one or more files that contain arguments for the `apidiff` command. This lets you to create
240+
one or more files that contain arguments for the `apidiff` command. This lets you create
241241
`apidiff` commands of any length on any operating system.
242242

243243

@@ -420,6 +420,9 @@ you can combine the recommended options for the two modes, specifying both
420420
`--patch-module` options for the source files and `--api-directory` for the
421421
generated documentation.
422422

423+
If you want to see or understand how the options are used internally, you
424+
can use `--verbose options`, possibly implicitly as part of `--verbose all`.
425+
423426
### Comparing different releases of JDK
424427

425428
When comparing any generated API documentation, the comparison is sensitive to any

test/junit/apitest/TestAPI.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -49,7 +49,7 @@
4949
*/
5050
public class TestAPI extends API {
5151
TestAPI(String name) {
52-
super(new Options.APIOptions(name), null, null, null);
52+
super(new Options.APIOptions(name), null, null, null, false);
5353
}
5454

5555
@Override

0 commit comments

Comments
 (0)