/* * Copyright (c) 2010, 2016, 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 6968063 7127924 * @summary provide examples of code that generate diagnostics * @modules jdk.compiler/com.sun.tools.javac.api * jdk.compiler/com.sun.tools.javac.code * jdk.compiler/com.sun.tools.javac.file * jdk.compiler/com.sun.tools.javac.main * jdk.compiler/com.sun.tools.javac.parser * jdk.compiler/com.sun.tools.javac.util * @build ArgTypeCompilerFactory Example HTMLWriter RunExamples DocCommentProcessor * @run main/othervm RunExamples
*/ /* * See CR 7127924 for info on why othervm is used.
*/
/** * Utility to run selected or all examples, writing results to * stdout, a plain text file or an HTML file. This program can be * run standalone, or as a jtreg test. * * Options: * -examples dir directory of examples. Defaults to ${test.src}/examples * -raw run examples with -XDrawDiagnostics * -showFiles include text of source files in the output * -verbose verbose output * -o file write output to file: format will be HTML if * file has .html extension; otherwise it will be plain text. * default is to stdout * -title string specify a title, only applies to HTML output
*/ publicclass RunExamples { publicstaticvoid main(String... args) throws Exception {
jtreg = (System.getProperty("test.src") != null);
Path tmpDir; boolean deleteOnExit; if (jtreg) { // use standard jtreg scratch directory: the current directory
tmpDir = Paths.get(System.getProperty("user.dir"));
deleteOnExit = false;
} else {
tmpDir = Files.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")),
RunExamples.class.getName());
deleteOnExit = true;
}
Example.setTempDir(tmpDir.toFile());
RunExamples r = new RunExamples();
try { if (r.run(args)) return;
} finally { if (deleteOnExit) {
clean(tmpDir);
}
}
boolean run(String... args) {
Set<String> selectedKeys = new TreeSet<String>();
Set<Example> selectedExamples = new TreeSet<Example>();
File testSrc = new File(System.getProperty("test.src", "."));
File examplesDir = new File(testSrc, "examples");
File outFile = null; boolean raw = false; boolean showFiles = false; boolean verbose = false; boolean argTypes = false;
String title = null;
for (int i = 0; i < args.length; i++) {
String arg = args[i]; if (arg.equals("-k") && (i + 1) < args.length)
selectedKeys.add(args[++i]); elseif (arg.equals("-examples") && (i + 1) < args.length)
examplesDir = new File(args[++i]); elseif (arg.equals("-raw"))
raw = true; elseif (arg.equals("-showFiles"))
showFiles = true; elseif (arg.equals("-verbose"))
verbose = true; elseif (arg.equals("-o") && (i + 1) < args.length)
outFile = new File(args[++i]); elseif (arg.equals("-title") && (i + 1) < args.length)
title = args[++i]; elseif (arg.equals("-argtypes"))
argTypes = true; elseif (arg.startsWith("-")) {
error("unknown option: " + arg); returnfalse;
} else { while (i < args.length) {
File f = new File(examplesDir, args[i]);
selectedExamples.add(new Example(f));
i++;
}
}
}
// special mode to show message keys and the types of the args that // are used. if (argTypes)
Example.Compiler.factory = new ArgTypeCompilerFactory();
if (selectedKeys.size() > 0) {
Set<Example> examples = getExamples(examplesDir);
nextKey: for (String k: selectedKeys) { for (Example e: examples) { if (e.getDeclaredKeys().contains(k)) continue nextKey;
}
error("Key " + k + ": no examples found");
}
} else { if (selectedExamples.isEmpty())
selectedExamples = getExamples(examplesDir);
}
try {
Runner r; if (outFile == null) {
PrintWriter out = new PrintWriter(System.out);
r = new TextRunner(out, showFiles, raw, verbose);
} elseif (outFile.getName().endsWith(".html"))
r = new HTMLRunner(outFile, showFiles, raw, verbose, title); else
r = new TextRunner(outFile, showFiles, raw, verbose);
r.run(selectedExamples);
r.close();
} catch (IOException e) {
error("Error writing output: " + e);
}
return (errors == 0);
}
/** * Get the complete set of examples to be checked.
*/
Set<Example> getExamples(File examplesDir) {
Set<Example> results = new TreeSet<Example>(); for (File f: examplesDir.listFiles()) { if (isValidExample(f))
results.add(new Example(f));
} return results;
}
@Override void showFile(Example e, File f) {
out.println("--- " + f);
String text; try {
text = read(f);
} catch (IOException ex) {
text = "Error reading " + f + "; " + ex;
}
Matcher m = copyrightHeaderPat.matcher(text); if (m.matches()) {
out.println("(Copyright)");
writeLines(m.group(2));
} else {
writeLines(text);
}
out.println();
}
@Override void run(Example e) { // only show Output: header if also showing files if (showFiles)
out.println("--- Output:");
e.run(out, raw, verbose);
out.println();
}
void writeLines(String text) { for (String line: text.split("\n"))
out.println(line);
}
void writeIndex(Collection<Example> examples) throws IOException {
Map<String, Set<Example>> index = new TreeMap<String, Set<Example>>();
Set<String> initials = new HashSet<String>(); for (Example e: examples) { for (String k: e.getDeclaredKeys()) {
Set<Example> s = index.get(k); if (s == null)
index.put(k, s = new TreeSet<Example>());
s.add(e);
}
initials.add(e.getName().substring(0, 1).toUpperCase());
}
if (INDEX_HEADER != null) {
html.startTag(INDEX_HEADER);
html.write("Index");
html.endTag(INDEX_HEADER);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.