test/tools/javac/diags/ArgTypeCompilerFactory.java

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 3938
93012e2a5d1d
parent 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset eb6ee6a5f2fe

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 import java.io.*;
aoqi@0 25 import java.util.*;
aoqi@0 26 import java.util.List;
aoqi@0 27 import javax.tools.*;
aoqi@0 28
aoqi@0 29 import com.sun.tools.javac.api.*;
aoqi@0 30 import com.sun.tools.javac.api.DiagnosticFormatter.Configuration.DiagnosticPart;
aoqi@0 31 import com.sun.tools.javac.api.Formattable.LocalizedString;
aoqi@0 32 import com.sun.tools.javac.code.Flags.Flag;
aoqi@0 33 import com.sun.tools.javac.code.Kinds.KindName;
aoqi@0 34 import com.sun.tools.javac.code.*;
aoqi@0 35 import com.sun.tools.javac.file.*;
aoqi@0 36 import com.sun.tools.javac.main.Main;
aoqi@0 37 import com.sun.tools.javac.main.JavaCompiler;
aoqi@0 38 import com.sun.tools.javac.parser.Tokens.TokenKind;
aoqi@0 39 import com.sun.tools.javac.util.*;
aoqi@0 40 import com.sun.tools.javac.util.AbstractDiagnosticFormatter.SimpleConfiguration;
aoqi@0 41 import javax.lang.model.SourceVersion;
aoqi@0 42
aoqi@0 43 /**
aoqi@0 44 * Compiler factory for instances of Example.Compiler that use custom
aoqi@0 45 * DiagnosticFormatter and Messages objects to track the types of args
aoqi@0 46 * when when localizing diagnostics.
aoqi@0 47 * The compiler objects only support "output" mode, not "check" mode.
aoqi@0 48 */
aoqi@0 49 class ArgTypeCompilerFactory implements Example.Compiler.Factory {
aoqi@0 50 // Same code as Example.Compiler.DefaultFactory, but the names resolve differently
aoqi@0 51 public Example.Compiler getCompiler(List<String> opts, boolean verbose) {
aoqi@0 52 String first;
aoqi@0 53 String[] rest;
aoqi@0 54 if (opts == null || opts.isEmpty()) {
aoqi@0 55 first = null;
aoqi@0 56 rest = new String[0];
aoqi@0 57 } else {
aoqi@0 58 first = opts.get(0);
aoqi@0 59 rest = opts.subList(1, opts.size()).toArray(new String[opts.size() - 1]);
aoqi@0 60 }
aoqi@0 61 if (first == null || first.equals("jsr199"))
aoqi@0 62 return new Jsr199Compiler(verbose, rest);
aoqi@0 63 else if (first.equals("simple"))
aoqi@0 64 return new SimpleCompiler(verbose);
aoqi@0 65 else if (first.equals("backdoor"))
aoqi@0 66 return new BackdoorCompiler(verbose);
aoqi@0 67 else
aoqi@0 68 throw new IllegalArgumentException(first);
aoqi@0 69 }
aoqi@0 70
aoqi@0 71 /**
aoqi@0 72 * Compile using the JSR 199 API. The diagnostics generated are
aoqi@0 73 * scanned for resource keys. Not all diagnostic keys are generated
aoqi@0 74 * via the JSR 199 API -- for example, rich diagnostics are not directly
aoqi@0 75 * accessible, and some diagnostics generated by the file manager may
aoqi@0 76 * not be generated (for example, the JSR 199 file manager does not see
aoqi@0 77 * -Xlint:path).
aoqi@0 78 */
aoqi@0 79 static class Jsr199Compiler extends Example.Compiler {
aoqi@0 80 List<String> fmOpts;
aoqi@0 81
aoqi@0 82 Jsr199Compiler(boolean verbose, String... args) {
aoqi@0 83 super(verbose);
aoqi@0 84 for (int i = 0; i < args.length; i++) {
aoqi@0 85 String arg = args[i];
aoqi@0 86 if (arg.equals("-filemanager") && (i + 1 < args.length)) {
aoqi@0 87 fmOpts = Arrays.asList(args[++i].split(","));
aoqi@0 88 } else
aoqi@0 89 throw new IllegalArgumentException(arg);
aoqi@0 90 }
aoqi@0 91 }
aoqi@0 92
aoqi@0 93 @Override
aoqi@0 94 boolean run(PrintWriter out, Set<String> keys, boolean raw, List<String> opts, List<File> files) {
aoqi@0 95 assert out != null && keys == null;
aoqi@0 96
aoqi@0 97 if (verbose)
aoqi@0 98 System.err.println("run_jsr199: " + opts + " " + files);
aoqi@0 99
aoqi@0 100 JavacTool tool = JavacTool.create();
aoqi@0 101
aoqi@0 102 StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
aoqi@0 103 if (fmOpts != null)
aoqi@0 104 fm = new FileManager(fm, fmOpts);
aoqi@0 105
aoqi@0 106 Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);
aoqi@0 107
aoqi@0 108 Context c = new Context();
aoqi@0 109 ArgTypeMessages.preRegister(c);
aoqi@0 110 ArgTypeJavaCompiler.preRegister(c);
aoqi@0 111 JavacTaskImpl t = (JavacTaskImpl) tool.getTask(out, fm, null, opts, null, fos, c);
aoqi@0 112 return t.call();
aoqi@0 113 }
aoqi@0 114 }
aoqi@0 115
aoqi@0 116 /**
aoqi@0 117 * Run the test using the standard simple entry point.
aoqi@0 118 */
aoqi@0 119 static class SimpleCompiler extends Example.Compiler {
aoqi@0 120 SimpleCompiler(boolean verbose) {
aoqi@0 121 super(verbose);
aoqi@0 122 }
aoqi@0 123
aoqi@0 124 @Override
aoqi@0 125 boolean run(PrintWriter out, Set<String> keys, boolean raw, List<String> opts, List<File> files) {
aoqi@0 126 assert out != null && keys == null;
aoqi@0 127
aoqi@0 128 if (verbose)
aoqi@0 129 System.err.println("run_simple: " + opts + " " + files);
aoqi@0 130
aoqi@0 131 List<String> args = new ArrayList<String>();
aoqi@0 132
aoqi@0 133 args.addAll(opts);
aoqi@0 134 for (File f: files)
aoqi@0 135 args.add(f.getPath());
aoqi@0 136
aoqi@0 137 Main main = new Main("javac", out);
aoqi@0 138 Context c = new Context() {
aoqi@0 139 @Override public void clear() {
aoqi@0 140 ((JavacFileManager) get(JavaFileManager.class)).close();
aoqi@0 141 super.clear();
aoqi@0 142 }
aoqi@0 143 };
aoqi@0 144 JavacFileManager.preRegister(c); // can't create it until Log has been set up
aoqi@0 145 ArgTypeJavaCompiler.preRegister(c);
aoqi@0 146 ArgTypeMessages.preRegister(c);
aoqi@0 147 Main.Result result = main.compile(args.toArray(new String[args.size()]), c);
aoqi@0 148
aoqi@0 149 return result.isOK();
aoqi@0 150 }
aoqi@0 151 }
aoqi@0 152
aoqi@0 153 static class BackdoorCompiler extends Example.Compiler {
aoqi@0 154 BackdoorCompiler(boolean verbose) {
aoqi@0 155 super(verbose);
aoqi@0 156 }
aoqi@0 157
aoqi@0 158 @Override
aoqi@0 159 boolean run(PrintWriter out, Set<String> keys, boolean raw, List<String> opts, List<File> files) {
aoqi@0 160 assert out != null && keys == null;
aoqi@0 161
aoqi@0 162 if (verbose)
aoqi@0 163 System.err.println("run_simple: " + opts + " " + files);
aoqi@0 164
aoqi@0 165 List<String> args = new ArrayList<String>(opts);
aoqi@0 166 for (File f: files)
aoqi@0 167 args.add(f.getPath());
aoqi@0 168
aoqi@0 169 Context c = new Context();
aoqi@0 170 JavacFileManager.preRegister(c); // can't create it until Log has been set up
aoqi@0 171 ArgTypeJavaCompiler.preRegister(c);
aoqi@0 172 ArgTypeMessages.preRegister(c);
aoqi@0 173 Main m = new Main("javac", out);
aoqi@0 174 Main.Result result = m.compile(args.toArray(new String[args.size()]), c);
aoqi@0 175
aoqi@0 176 return result.isOK();
aoqi@0 177 }
aoqi@0 178
aoqi@0 179 }
aoqi@0 180
aoqi@0 181
aoqi@0 182 // <editor-fold defaultstate="collapsed" desc="Custom Javac components">
aoqi@0 183
aoqi@0 184 /**
aoqi@0 185 * Diagnostic formatter which reports formats a diag as a series of lines
aoqi@0 186 * containing a key, and a possibly empty set of descriptive strings for the
aoqi@0 187 * arg types.
aoqi@0 188 */
aoqi@0 189 static class ArgTypeDiagnosticFormatter extends AbstractDiagnosticFormatter {
aoqi@0 190
aoqi@0 191 ArgTypeDiagnosticFormatter(Options options) {
aoqi@0 192 super(null, new SimpleConfiguration(options,
aoqi@0 193 EnumSet.of(DiagnosticPart.SUMMARY,
aoqi@0 194 DiagnosticPart.DETAILS,
aoqi@0 195 DiagnosticPart.SUBDIAGNOSTICS)));
aoqi@0 196 }
aoqi@0 197
aoqi@0 198 @Override
aoqi@0 199 protected String formatDiagnostic(JCDiagnostic d, Locale locale) {
aoqi@0 200 return formatMessage(d, locale);
aoqi@0 201 }
aoqi@0 202
aoqi@0 203 @Override
aoqi@0 204 public String formatMessage(JCDiagnostic d, Locale l) {
aoqi@0 205 StringBuilder buf = new StringBuilder();
aoqi@0 206 formatMessage(d, buf);
aoqi@0 207 return buf.toString();
aoqi@0 208 }
aoqi@0 209
aoqi@0 210 private void formatMessage(JCDiagnostic d, StringBuilder buf) {
aoqi@0 211 String key = d.getCode();
aoqi@0 212 Object[] args = d.getArgs();
aoqi@0 213 // report the primary arg types, without recursing into diag fragments
aoqi@0 214 buf.append(getKeyArgsString(key, args));
aoqi@0 215 // report details for any diagnostic fragments
aoqi@0 216 for (Object arg: args) {
aoqi@0 217 if (arg instanceof JCDiagnostic) {
aoqi@0 218 buf.append("\n");
aoqi@0 219 formatMessage((JCDiagnostic) arg, buf);
aoqi@0 220 }
aoqi@0 221 }
aoqi@0 222 // report details for any subdiagnostics
aoqi@0 223 for (String s: formatSubdiagnostics(d, null)) {
aoqi@0 224 buf.append("\n");
aoqi@0 225 buf.append(s);
aoqi@0 226 }
aoqi@0 227 }
aoqi@0 228
aoqi@0 229 @Override
aoqi@0 230 public boolean isRaw() {
aoqi@0 231 return true;
aoqi@0 232 }
aoqi@0 233 }
aoqi@0 234
aoqi@0 235 /**
aoqi@0 236 * Trivial subtype of JavaCompiler to get access to the protected compilerKey field.
aoqi@0 237 * The factory is used to ensure that the log is initialized with an instance of
aoqi@0 238 * ArgTypeDiagnosticFormatter before we create the required JavaCompiler.
aoqi@0 239 */
aoqi@0 240 static class ArgTypeJavaCompiler extends JavaCompiler {
aoqi@0 241 static void preRegister(Context context) {
aoqi@0 242 context.put(compilerKey, new Context.Factory<JavaCompiler>() {
aoqi@0 243 public JavaCompiler make(Context c) {
aoqi@0 244 Log log = Log.instance(c);
aoqi@0 245 Options options = Options.instance(c);
aoqi@0 246 log.setDiagnosticFormatter(new ArgTypeDiagnosticFormatter(options));
aoqi@0 247 return new JavaCompiler(c);
aoqi@0 248 }
aoqi@0 249 });
aoqi@0 250 }
aoqi@0 251
aoqi@0 252 // not used
aoqi@0 253 private ArgTypeJavaCompiler() {
aoqi@0 254 super(null);
aoqi@0 255 }
aoqi@0 256 }
aoqi@0 257
aoqi@0 258 /**
aoqi@0 259 * Diagnostic formatter which "localizes" a message as a line
aoqi@0 260 * containing a key, and a possibly empty set of descriptive strings for the
aoqi@0 261 * arg types.
aoqi@0 262 */
aoqi@0 263 static class ArgTypeMessages extends JavacMessages {
aoqi@0 264 static void preRegister(Context context) {
aoqi@0 265 context.put(JavacMessages.messagesKey, new Context.Factory<JavacMessages>() {
aoqi@0 266 public JavacMessages make(Context c) {
aoqi@0 267 return new ArgTypeMessages(c) {
aoqi@0 268 @Override
aoqi@0 269 public String getLocalizedString(Locale l, String key, Object... args) {
aoqi@0 270 return getKeyArgsString(key, args);
aoqi@0 271 }
aoqi@0 272 };
aoqi@0 273 }
aoqi@0 274 });
aoqi@0 275 }
aoqi@0 276
aoqi@0 277 ArgTypeMessages(Context context) {
aoqi@0 278 super(context);
aoqi@0 279 }
aoqi@0 280 }
aoqi@0 281
aoqi@0 282 /**
aoqi@0 283 * Utility method to generate a string for key and args
aoqi@0 284 */
aoqi@0 285 static String getKeyArgsString(String key, Object... args) {
aoqi@0 286 StringBuilder buf = new StringBuilder();
aoqi@0 287 buf.append(key);
aoqi@0 288 String sep = ": ";
aoqi@0 289 for (Object o : args) {
aoqi@0 290 buf.append(sep);
aoqi@0 291 buf.append(getArgTypeOrStringValue(o));
aoqi@0 292 sep = ", ";
aoqi@0 293 }
aoqi@0 294 return buf.toString();
aoqi@0 295 }
aoqi@0 296
aoqi@0 297 static boolean showStringValues = false;
aoqi@0 298
aoqi@0 299 static String getArgTypeOrStringValue(Object o) {
aoqi@0 300 if (showStringValues && o instanceof String)
aoqi@0 301 return "\"" + o + "\"";
aoqi@0 302 return getArgType(o);
aoqi@0 303 }
aoqi@0 304
aoqi@0 305 static String getArgType(Object o) {
aoqi@0 306 if (o == null)
aoqi@0 307 return "null";
aoqi@0 308 if (o instanceof Name)
aoqi@0 309 return "name";
aoqi@0 310 if (o instanceof Boolean)
aoqi@0 311 return "boolean";
aoqi@0 312 if (o instanceof Integer)
aoqi@0 313 return "number";
aoqi@0 314 if (o instanceof String)
aoqi@0 315 return "string";
aoqi@0 316 if (o instanceof Flag)
aoqi@0 317 return "modifier";
aoqi@0 318 if (o instanceof KindName)
aoqi@0 319 return "symbol kind";
aoqi@0 320 if (o instanceof TokenKind)
aoqi@0 321 return "token";
aoqi@0 322 if (o instanceof Symbol)
aoqi@0 323 return "symbol";
aoqi@0 324 if (o instanceof Type)
aoqi@0 325 return "type";
aoqi@0 326 if (o instanceof List) {
aoqi@0 327 List<?> l = (List<?>) o;
aoqi@0 328 if (l.isEmpty())
aoqi@0 329 return "list";
aoqi@0 330 else
aoqi@0 331 return "list of " + getArgType(l.get(0));
aoqi@0 332 }
aoqi@0 333 if (o instanceof ListBuffer)
aoqi@0 334 return getArgType(((ListBuffer) o).toList());
aoqi@0 335 if (o instanceof Set) {
aoqi@0 336 Set<?> s = (Set<?>) o;
aoqi@0 337 if (s.isEmpty())
aoqi@0 338 return "set";
aoqi@0 339 else
aoqi@0 340 return "set of " + getArgType(s.iterator().next());
aoqi@0 341 }
aoqi@0 342 if (o instanceof SourceVersion)
aoqi@0 343 return "source version";
aoqi@0 344 if (o instanceof FileObject || o instanceof File)
aoqi@0 345 return "file name";
aoqi@0 346 if (o instanceof JCDiagnostic)
aoqi@0 347 return "message segment";
aoqi@0 348 if (o instanceof LocalizedString)
aoqi@0 349 return "message segment"; // only instance is "no arguments"
aoqi@0 350 String s = o.getClass().getSimpleName();
aoqi@0 351 return (s.isEmpty() ? o.getClass().getName() : s);
aoqi@0 352 }
aoqi@0 353
aoqi@0 354 // </editor-fold>
aoqi@0 355
aoqi@0 356 }

mercurial