/* * Copyright (c) 2010, 2015, 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 7013272 7127924 * @summary Automatically generate info about how compiler resource keys are used * @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 Example ArgTypeCompilerFactory MessageFile MessageInfo * @run main/othervm MessageInfo
*/ /* * See CR 7127924 for info on why othervm is used.
*/
/** * Utility to manipulate compiler.properties, and suggest info comments based * on information derived from running examples. * * Options: * -examples dir location of examples directory * -o file output file * -check just check message file * -ensureNewlines ensure newline after each entry * -fixIndent fix indentation of continuation lines * -sort sort messages * -verbose verbose output * -replace replace comments instead of merging comments * file javac compiler.properties file *
*/ publicclass MessageInfo { publicstaticvoid main(String... args) throws Exception {
jtreg = (System.getProperty("test.src") != null);
File tmpDir; if (jtreg) { // use standard jtreg scratch directory: the current directory
tmpDir = new File(System.getProperty("user.dir"));
} else {
tmpDir = new File(System.getProperty("java.io.tmpdir"),
MessageInfo.class.getName()
+ (new SimpleDateFormat("yyMMddHHmmss")).format(new Date()));
}
Example.setTempDir(tmpDir);
Example.Compiler.factory = new ArgTypeCompilerFactory();
MessageInfo mi = new MessageInfo();
try { if (mi.run(args)) return;
} finally { /* VERY IMPORTANT NOTE. In jtreg mode, tmpDir is set to the * jtreg scratch directory, which is the current directory. * In case someone is faking jtreg mode, make sure to only * clean tmpDir when it is reasonable to do so.
*/ if (tmpDir.isDirectory() &&
tmpDir.getName().startsWith(MessageInfo.class.getName())) { if (clean(tmpDir))
tmpDir.delete();
}
}
for (int i = 0; i < args.length; i++) {
String arg = args[i]; if (arg.equals("-examples") && (i + 1) < args.length)
examplesDir = new File(args[++i]); elseif(arg.equals("-notyet") && (i + 1) < args.length)
notYetFile = new File(args[++i]); elseif (arg.equals("-ensureNewlines"))
ensureNewlines = true; elseif (arg.equals("-fixIndent"))
fixIndent = true; elseif (arg.equals("-sort"))
sort = true; elseif (arg.equals("-verbose"))
verbose = true; elseif (arg.equals("-replace"))
replace = true; elseif (arg.equals("-check"))
check = true; elseif (arg.equals("-o") && (i + 1) < args.length)
outFile = new File(args[++i]); elseif (arg.startsWith("-")) {
error("unknown option: " + arg); returnfalse;
} elseif (i == args.length - 1) {
msgFile = new File(arg);
} else {
error("unknown arg: " + arg); returnfalse;
}
}
if (!check && outFile == null) {
usage(); returntrue;
}
if ((ensureNewlines || fixIndent || sort) && outFile == null) {
error("must set output file for these options"); returnfalse;
}
if (notYetFile == null) {
notYetFile = new File(examplesDir.getParentFile(), "examples.not-yet.txt");
}
if (msgFile == null) { for (File d = testSrc; d != null; d = d.getParentFile()) { if (new File(d, "TEST.ROOT").exists()) {
d = d.getParentFile();
File f = new File(d, "src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties"); if (f.exists()) {
msgFile = f; break;
}
}
} if (msgFile == null) { if (jtreg) {
System.err.println("Warning: no message file available, test skipped"); returntrue;
}
error("no message file available"); returnfalse;
}
}
for (Map.Entry<String, Set<String>> e: msgInfo.entrySet()) {
String k = e.getKey();
Set<String> suggestions = e.getValue();
MessageFile.Message m = mf.messages.get(k); if (m == null) {
error("Can't find message for " + k + " in message file"); continue;
}
MessageFile.Info info = m.getInfo();
Set<Integer> placeholders = m.getPlaceholders();
MessageFile.Info suggestedInfo = new MessageFile.Info(suggestions);
suggestedInfo.markUnused(placeholders);
if (!info.isEmpty()) { if (info.contains(suggestedInfo)) continue; if (!replace) { if (info.fields.size() != suggestedInfo.fields.size())
error("Cannot merge info for " + k); else
suggestedInfo.merge(info);
}
}
if (outFile == null) {
System.err.println("suggest for " + k);
System.err.println(suggestedInfo.toComment());
} else
m.setInfo(suggestedInfo);
}
Map<String, Set<String>> runExamples(File examplesDir, boolean verbose) {
Map<String, Set<String>> map = new TreeMap<String, Set<String>>(); for (Example e: getExamples(examplesDir)) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.run(pw, true, verbose);
pw.close();
String[] lines = sw.toString().split("\n"); for (String line: lines) { if (!line.startsWith("compiler.")) continue; int colon = line.indexOf(":"); if (colon == -1) continue;
String key = line.substring(0, colon);
StringBuilder sb = new StringBuilder();
sb.append("# "); int i = 0;
String[] descs = line.substring(colon + 1).split(", *"); for (String desc: descs) { if (i > 0) sb.append(", ");
sb.append(i++);
sb.append(": ");
sb.append(desc.trim());
}
Set<String> set = map.get(key); if (set == null)
map.put(key, set = new TreeSet<String>());
set.add(sb.toString());
}
}
return map;
}
/** * 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;
}
/** * Clean the contents of a directory.
*/ staticboolean clean(File dir) { boolean ok = true; for (File f: dir.listFiles()) { if (f.isDirectory())
ok &= clean(f);
ok &= f.delete();
} return ok;
}
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.14 Sekunden
(vorverarbeitet)
¤
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.