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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
/*
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8274211 8278538
* @summary Test man page that options are documented
* @modules jdk.javadoc/jdk.javadoc.internal.tool:+open
* @run main CheckManPageOptions
*/
import jdk.javadoc.doclet.Doclet;
import jdk.javadoc.doclet.StandardDoclet;
import jdk.javadoc.internal.tool.ToolOptions;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* Checks the set of options found by fuzzy-parsing the troff or Markdown versions
* of the javadoc man page against the set of options declared in the source code.
*/
public class CheckManPageOptions {
static class SourceDirNotFound extends Error { }
public static void main(String... args) throws Exception {
try {
new CheckManPageOptions().run(args);
} catch (SourceDirNotFound e) {
System.err.println("NOTE: Cannot find src directory; test skipped");
}
}
static final PrintStream out = System.err;
List<String> MISSING_IN_MAN_PAGE = List.of();
void run(String... args) throws Exception {
var file = args.length == 0 ? findDefaultFile() : Path.of(args[0]);
out.println("File: " + file);
out.println(Files.readAllLines(file).stream()
.filter(l -> l.contains("Automatically generated by Pandoc"))
.findFirst()
.map(l -> l.substring(l.indexOf("Auto")))
.orElse("version string not found"));
out.println();
var manPageOptions = getManPageOptions(file);
out.println("Man page options: " + manPageOptions);
out.println();
var toolOptions = getToolOptions();
out.println("ToolOptions: " + toolOptions);
out.println();
var docletOptions = getDocletOptions();
out.println("DocletOptions: " + docletOptions);
out.println();
var toolDocletOnly = new TreeSet<String>();
toolDocletOnly.addAll(toolOptions);
toolDocletOnly.addAll(docletOptions);
toolDocletOnly.removeAll(manPageOptions);
toolDocletOnly.removeAll(MISSING_IN_MAN_PAGE);
if (!toolDocletOnly.isEmpty()) {
error("The following options are defined by the tool or doclet, but not defined in the man page:\n"
+ toSimpleList(toolDocletOnly));
}
var manPageOnly = new TreeSet<String>();
manPageOnly.addAll(manPageOptions);
manPageOnly.removeAll(toolOptions);
manPageOnly.removeAll(docletOptions);
if (!manPageOnly.isEmpty()) {
error("The following options are defined in the man page, but not defined by the tool or doclet:\n"
+ toSimpleList(manPageOnly));
}
if (!MISSING_IN_MAN_PAGE.isEmpty()) {
var notMissing = new TreeSet<>(MISSING_IN_MAN_PAGE);
notMissing.retainAll(manPageOptions);
if (!notMissing.isEmpty()) {
error("The following options were declared as missing, but were found on the man page:\n"
+ toSimpleList(notMissing));
}
out.println("NOTE: the following options are currently excluded and need to be documented in the man page:");
out.println(toSimpleList(MISSING_IN_MAN_PAGE));
}
if (errors > 0) {
out.println(errors + " errors found");
throw new Exception(errors + " errors found");
}
}
int errors = 0;
void error(String message) {
("Error: " + message).lines().forEach(out::println);
errors++;
}
String toSimpleList(Collection<String> items) {
return items.stream().collect(Collectors.joining(", ", " ", ""));
}
Path findDefaultFile() {
return findRootDir().resolve("src/jdk.javadoc/share/man/javadoc.md");
}
Path findRootDir() {
Path dir = Path.of(System.getProperty("test.src", ".")).toAbsolutePath();
while (dir != null) {
if (Files.exists(dir.resolve("src"))) {
return dir;
} else {
Path openDir = dir.resolve("open");
if (Files.exists(openDir.resolve("src"))) {
return openDir;
}
}
dir = dir.getParent();
}
throw new SourceDirNotFound();
}
List<String> getToolOptions() throws Error {
try {
Class<ToolOptions> toolOptionsClass = ToolOptions.class;
Constructor<ToolOptions> constr = toolOptionsClass.getDeclaredConstructor();
constr.setAccessible(true);
Method getSupportedOptions = toolOptionsClass.getMethod("getSupportedOptions");
Class<?> toolOptionClass = List.of(toolOptionsClass.getDeclaredClasses()).stream()
.filter(c -> c.getSimpleName().equals("ToolOption"))
.findFirst()
.orElseThrow();
Field kindField = toolOptionClass.getDeclaredField("kind");
kindField.setAccessible(true);
Method getNames = toolOptionClass.getDeclaredMethod("getNames");
getNames.setAccessible(true);
ToolOptions t = constr.newInstance();
var list = new ArrayList<String>();
var options = (List<?>) getSupportedOptions.invoke(t);
for (var option : options) {
Object kind = kindField.get(option);
if (kind.toString().equals("HIDDEN")) {
continue;
}
@SuppressWarnings("unchecked")
var oNames = (List<String>) getNames.invoke(option);
oNames.stream()
.filter(o -> !o.equals("@"))
.forEach(list::add);
}
return list;
} catch (ReflectiveOperationException e) {
throw new Error(e);
}
}
List<String> getDocletOptions() {
StandardDoclet d = new StandardDoclet();
d.init(Locale.getDefault(), null);
return getDocletOptions(d);
}
List<String> getDocletOptions(Doclet d) {
return d.getSupportedOptions().stream()
.filter(o -> o.getKind() != Doclet.Option.Kind.OTHER)
.flatMap(o -> o.getNames().stream())
.map(n -> n.replaceAll(":$", ""))
.toList();
}
List<String> getManPageOptions(Path file) throws IOException {
String page = Files.readString(file);
String name = file.getFileName().toString();
String extn = name.substring(name.lastIndexOf('.'));
return switch (extn) {
case ".1" -> parseNRoff(page);
case ".md" -> parseMarkdown(page);
default -> throw new IllegalArgumentException(file.toString());
};
}
List<String> parseNRoff(String page) {
var list = new ArrayList<String>();
// In the troff man page, options are defined in one of two forms:
// 1. options delegated to javac appear in pairs of lines of the form
// .IP \[bu] 2
// \f[V]-....
// 2. options implemented by the tool or doclet appear in pairs of lines of the form
// .TP
// \f[V]-...
Pattern p1 = Pattern.compile("\\R" + Pattern.quote(".IP \\[bu] 2") + "\\R" + Pattern.quote("\\f[V]-") + ".*");
Pattern p2 = Pattern.compile("\\R" + Pattern.quote(".TP") + "\\R" + Pattern.quote("\\f[V]-") + ".*");
Pattern outer = Pattern.compile("(" + p1.pattern() + "|" + p2.pattern() + ")");
Matcher outerMatcher = outer.matcher(page);
// In the defining areas, option names are represented as follows:
// \f[V]OPTION\f[R] or \f[V]OPTION:
// where OPTION is the shortest string not containing whitespace or colon,
// and in which all '-' characters are escaped with a single backslash.
Pattern inner = Pattern.compile("\\s\\\\f\\[V](-[^ :]+?)(:|\\\\f\\[R])");
while (outerMatcher.find()) {
String lines = outerMatcher.group();
out.println("found:" + lines);
Matcher innerMatcher = inner.matcher(lines);
while (innerMatcher.find()) {
String option = innerMatcher.group(1);
out.println(" found option:" + option);
list.add(option);
}
out.println();
}
return list;
}
List<String> parseMarkdown(String page) {
var list = new ArrayList<String>();
// In the Markdown man page, options are defined in one of two forms:
// 1. options delegated to javac appear in lines of the form
// * <span id="...">`-...</span>
// 2. options implemented by the tool or doclet appear in lines of the form
// <span id="...">`-...</span>
Pattern p1 = Pattern.compile("\\R* <span id=\"[^\"]+\">`-.*</span>");
Pattern p2 = Pattern.compile("\\R<span id=\"[^\"]+\">`-.*</span>");
Pattern outer = Pattern.compile("(" + p1.pattern() + "|" + p2.pattern() + ")");
Matcher outerMatcher = outer.matcher(page);
// In the defining areas, option names are represented as follows:
// `OPTION` or `OPTION`
// where OPTION is the shortest string not containing whitespace or colon,
// and in which all '-' characters are escaped with a single backslash.
Pattern inner = Pattern.compile("[>\\s]`(-[^ :]+?)(:|`)");
while (outerMatcher.find()) {
String lines = outerMatcher.group();
out.println("found:" + lines);
Matcher innerMatcher = inner.matcher(lines);
while (innerMatcher.find()) {
String option = innerMatcher.group(1);
out.println(" found option:" + option);
list.add(option);
}
out.println();
}
return list;
}
}
|