src/share/classes/com/sun/tools/javap/JavapTask.java

Tue, 19 Feb 2013 17:19:01 -0800

author
ksrini
date
Tue, 19 Feb 2013 17:19:01 -0800
changeset 1592
9345394ac8fe
parent 1578
040f02711b73
child 1819
7fe655cad9b1
permissions
-rw-r--r--

8006948: Update javac for MethodParameters format change
Reviewed-by: ksrini, forax
Contributed-by: eric.mccorkle@oracle.com

jjg@46 1 /*
vromero@1561 2 * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
jjg@46 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@46 4 *
jjg@46 5 * This code is free software; you can redistribute it and/or modify it
jjg@46 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
jjg@46 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
jjg@46 10 *
jjg@46 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@46 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@46 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@46 14 * version 2 for more details (a copy is included in the LICENSE file that
jjg@46 15 * accompanied this code).
jjg@46 16 *
jjg@46 17 * You should have received a copy of the GNU General Public License version
jjg@46 18 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@90 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@46 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
jjg@46 24 */
jjg@46 25
jjg@46 26 package com.sun.tools.javap;
jjg@46 27
jjg@46 28 import java.io.EOFException;
jjg@46 29 import java.io.FileNotFoundException;
jjg@88 30 import java.io.FilterInputStream;
jjg@88 31 import java.io.InputStream;
jjg@46 32 import java.io.IOException;
jjg@46 33 import java.io.OutputStream;
jjg@46 34 import java.io.PrintWriter;
jjg@350 35 import java.io.Reader;
jjg@46 36 import java.io.StringWriter;
jjg@46 37 import java.io.Writer;
jjg@350 38 import java.net.URI;
jjg@88 39 import java.security.DigestInputStream;
jjg@88 40 import java.security.MessageDigest;
jjg@300 41 import java.security.NoSuchAlgorithmException;
jjg@46 42 import java.text.MessageFormat;
jjg@46 43 import java.util.ArrayList;
jjg@46 44 import java.util.Arrays;
jjg@283 45 import java.util.EnumSet;
jjg@46 46 import java.util.HashMap;
jjg@46 47 import java.util.Iterator;
jjg@46 48 import java.util.List;
jjg@46 49 import java.util.Locale;
jjg@46 50 import java.util.Map;
jjg@46 51 import java.util.MissingResourceException;
jjg@46 52 import java.util.ResourceBundle;
jjg@46 53
jjg@350 54 import javax.lang.model.element.Modifier;
jjg@350 55 import javax.lang.model.element.NestingKind;
jjg@46 56 import javax.tools.Diagnostic;
jjg@46 57 import javax.tools.DiagnosticListener;
jjg@46 58 import javax.tools.JavaFileManager;
jjg@46 59 import javax.tools.JavaFileObject;
jjg@46 60 import javax.tools.StandardJavaFileManager;
jjg@46 61 import javax.tools.StandardLocation;
jjg@46 62
jjg@46 63 import com.sun.tools.classfile.*;
jjg@350 64 import java.net.URISyntaxException;
jjg@350 65 import java.net.URL;
jjg@350 66 import java.net.URLConnection;
jjg@46 67
jjg@46 68 /**
jjg@46 69 * "Main" class for javap, normally accessed from the command line
jjg@46 70 * via Main, or from JSR199 via DisassemblerTool.
jjg@46 71 *
jjg@581 72 * <p><b>This is NOT part of any supported API.
jjg@581 73 * If you write code that depends on this, you do so at your own risk.
jjg@46 74 * This code and its internal interfaces are subject to change or
jjg@46 75 * deletion without notice.</b>
jjg@46 76 */
jjg@283 77 public class JavapTask implements DisassemblerTool.DisassemblerTask, Messages {
jjg@46 78 public class BadArgs extends Exception {
jjg@46 79 static final long serialVersionUID = 8765093759964640721L;
jjg@46 80 BadArgs(String key, Object... args) {
jjg@46 81 super(JavapTask.this.getMessage(key, args));
jjg@46 82 this.key = key;
jjg@46 83 this.args = args;
jjg@46 84 }
jjg@46 85
jjg@46 86 BadArgs showUsage(boolean b) {
jjg@46 87 showUsage = b;
jjg@46 88 return this;
jjg@46 89 }
jjg@46 90
jjg@46 91 final String key;
jjg@46 92 final Object[] args;
jjg@46 93 boolean showUsage;
jjg@46 94 }
jjg@46 95
jjg@46 96 static abstract class Option {
jjg@46 97 Option(boolean hasArg, String... aliases) {
jjg@46 98 this.hasArg = hasArg;
jjg@46 99 this.aliases = aliases;
jjg@46 100 }
jjg@46 101
jjg@46 102 boolean matches(String opt) {
jjg@46 103 for (String a: aliases) {
jjg@46 104 if (a.equals(opt))
jjg@46 105 return true;
jjg@46 106 }
jjg@46 107 return false;
jjg@46 108 }
jjg@46 109
jjg@46 110 boolean ignoreRest() {
jjg@46 111 return false;
jjg@46 112 }
jjg@46 113
jjg@46 114 abstract void process(JavapTask task, String opt, String arg) throws BadArgs;
jjg@46 115
jjg@46 116 final boolean hasArg;
jjg@46 117 final String[] aliases;
jjg@46 118 }
jjg@46 119
vromero@1442 120 static final Option[] recognizedOptions = {
jjg@46 121
jjg@46 122 new Option(false, "-help", "--help", "-?") {
jjg@46 123 void process(JavapTask task, String opt, String arg) {
jjg@46 124 task.options.help = true;
jjg@46 125 }
jjg@46 126 },
jjg@46 127
jjg@46 128 new Option(false, "-version") {
jjg@46 129 void process(JavapTask task, String opt, String arg) {
jjg@46 130 task.options.version = true;
jjg@46 131 }
jjg@46 132 },
jjg@46 133
jjg@46 134 new Option(false, "-fullversion") {
jjg@46 135 void process(JavapTask task, String opt, String arg) {
jjg@46 136 task.options.fullVersion = true;
jjg@46 137 }
jjg@46 138 },
jjg@46 139
jjg@46 140 new Option(false, "-v", "-verbose", "-all") {
jjg@46 141 void process(JavapTask task, String opt, String arg) {
jjg@46 142 task.options.verbose = true;
jjg@1578 143 task.options.showDescriptors = true;
jjg@46 144 task.options.showFlags = true;
jjg@46 145 task.options.showAllAttrs = true;
jjg@46 146 }
jjg@46 147 },
jjg@46 148
jjg@46 149 new Option(false, "-l") {
jjg@46 150 void process(JavapTask task, String opt, String arg) {
jjg@46 151 task.options.showLineAndLocalVariableTables = true;
jjg@46 152 }
jjg@46 153 },
jjg@46 154
jjg@46 155 new Option(false, "-public") {
jjg@46 156 void process(JavapTask task, String opt, String arg) {
jjg@68 157 task.options.accessOptions.add(opt);
jjg@46 158 task.options.showAccess = AccessFlags.ACC_PUBLIC;
jjg@46 159 }
jjg@46 160 },
jjg@46 161
jjg@46 162 new Option(false, "-protected") {
jjg@46 163 void process(JavapTask task, String opt, String arg) {
jjg@68 164 task.options.accessOptions.add(opt);
jjg@46 165 task.options.showAccess = AccessFlags.ACC_PROTECTED;
jjg@46 166 }
jjg@46 167 },
jjg@46 168
jjg@46 169 new Option(false, "-package") {
jjg@46 170 void process(JavapTask task, String opt, String arg) {
jjg@68 171 task.options.accessOptions.add(opt);
jjg@46 172 task.options.showAccess = 0;
jjg@46 173 }
jjg@46 174 },
jjg@46 175
jjg@46 176 new Option(false, "-p", "-private") {
jjg@46 177 void process(JavapTask task, String opt, String arg) {
jjg@68 178 if (!task.options.accessOptions.contains("-p") &&
jjg@68 179 !task.options.accessOptions.contains("-private")) {
jjg@68 180 task.options.accessOptions.add(opt);
jjg@68 181 }
jjg@46 182 task.options.showAccess = AccessFlags.ACC_PRIVATE;
jjg@46 183 }
jjg@46 184 },
jjg@46 185
jjg@46 186 new Option(false, "-c") {
jjg@46 187 void process(JavapTask task, String opt, String arg) {
jjg@46 188 task.options.showDisassembled = true;
jjg@46 189 }
jjg@46 190 },
jjg@46 191
jjg@46 192 new Option(false, "-s") {
jjg@46 193 void process(JavapTask task, String opt, String arg) {
jjg@1578 194 task.options.showDescriptors = true;
jjg@46 195 }
jjg@46 196 },
jjg@46 197
jjg@46 198 // new Option(false, "-all") {
jjg@46 199 // void process(JavapTask task, String opt, String arg) {
jjg@46 200 // task.options.showAllAttrs = true;
jjg@46 201 // }
jjg@46 202 // },
jjg@46 203
jjg@46 204 new Option(false, "-h") {
jjg@46 205 void process(JavapTask task, String opt, String arg) throws BadArgs {
jjg@46 206 throw task.new BadArgs("err.h.not.supported");
jjg@46 207 }
jjg@46 208 },
jjg@46 209
jjg@46 210 new Option(false, "-verify", "-verify-verbose") {
jjg@46 211 void process(JavapTask task, String opt, String arg) throws BadArgs {
jjg@46 212 throw task.new BadArgs("err.verify.not.supported");
jjg@46 213 }
jjg@46 214 },
jjg@46 215
jjg@88 216 new Option(false, "-sysinfo") {
jjg@88 217 void process(JavapTask task, String opt, String arg) {
jjg@88 218 task.options.sysInfo = true;
jjg@88 219 }
jjg@88 220 },
jjg@88 221
jjg@46 222 new Option(false, "-Xold") {
jjg@46 223 void process(JavapTask task, String opt, String arg) throws BadArgs {
jjg@291 224 task.log.println(task.getMessage("warn.Xold.not.supported"));
jjg@46 225 }
jjg@46 226 },
jjg@46 227
jjg@46 228 new Option(false, "-Xnew") {
jjg@46 229 void process(JavapTask task, String opt, String arg) throws BadArgs {
jjg@46 230 // ignore: this _is_ the new version
jjg@46 231 }
jjg@46 232 },
jjg@46 233
jjg@46 234 new Option(false, "-XDcompat") {
jjg@46 235 void process(JavapTask task, String opt, String arg) {
jjg@46 236 task.options.compat = true;
jjg@46 237 }
jjg@46 238 },
jjg@46 239
jjg@283 240 new Option(false, "-XDdetails") {
jjg@283 241 void process(JavapTask task, String opt, String arg) {
jjg@283 242 task.options.details = EnumSet.allOf(InstructionDetailWriter.Kind.class);
jjg@283 243 }
jjg@283 244
jjg@283 245 },
jjg@283 246
jjg@283 247 new Option(false, "-XDdetails:") {
jjg@283 248 @Override
jjg@283 249 boolean matches(String opt) {
jjg@283 250 int sep = opt.indexOf(":");
jjg@283 251 return sep != -1 && super.matches(opt.substring(0, sep + 1));
jjg@283 252 }
jjg@283 253
jjg@283 254 void process(JavapTask task, String opt, String arg) throws BadArgs {
jjg@283 255 int sep = opt.indexOf(":");
jjg@283 256 for (String v: opt.substring(sep + 1).split("[,: ]+")) {
jjg@283 257 if (!handleArg(task, v))
jjg@283 258 throw task.new BadArgs("err.invalid.arg.for.option", v);
jjg@283 259 }
jjg@283 260 }
jjg@283 261
jjg@283 262 boolean handleArg(JavapTask task, String arg) {
jjg@283 263 if (arg.length() == 0)
jjg@283 264 return true;
jjg@283 265
jjg@283 266 if (arg.equals("all")) {
jjg@283 267 task.options.details = EnumSet.allOf(InstructionDetailWriter.Kind.class);
jjg@283 268 return true;
jjg@283 269 }
jjg@283 270
jjg@283 271 boolean on = true;
jjg@283 272 if (arg.startsWith("-")) {
jjg@283 273 on = false;
jjg@283 274 arg = arg.substring(1);
jjg@283 275 }
jjg@283 276
jjg@283 277 for (InstructionDetailWriter.Kind k: InstructionDetailWriter.Kind.values()) {
jjg@283 278 if (arg.equalsIgnoreCase(k.option)) {
jjg@283 279 if (on)
jjg@283 280 task.options.details.add(k);
jjg@283 281 else
jjg@283 282 task.options.details.remove(k);
jjg@283 283 return true;
jjg@283 284 }
jjg@283 285 }
jjg@283 286 return false;
jjg@283 287 }
jjg@283 288 },
jjg@283 289
jjg@87 290 new Option(false, "-constants") {
jjg@87 291 void process(JavapTask task, String opt, String arg) {
jjg@87 292 task.options.showConstants = true;
jjg@87 293 }
jjg@346 294 },
jjg@346 295
jjg@346 296 new Option(false, "-XDinner") {
jjg@346 297 void process(JavapTask task, String opt, String arg) {
jjg@346 298 task.options.showInnerClasses = true;
jjg@346 299 }
jjg@348 300 },
jjg@348 301
jjg@348 302 new Option(false, "-XDindent:") {
jjg@348 303 @Override
jjg@348 304 boolean matches(String opt) {
jjg@348 305 int sep = opt.indexOf(":");
jjg@348 306 return sep != -1 && super.matches(opt.substring(0, sep + 1));
jjg@348 307 }
jjg@348 308
jjg@348 309 void process(JavapTask task, String opt, String arg) throws BadArgs {
jjg@348 310 int sep = opt.indexOf(":");
jjg@348 311 try {
jjg@348 312 task.options.indentWidth = Integer.valueOf(opt.substring(sep + 1));
jjg@348 313 } catch (NumberFormatException e) {
jjg@348 314 }
jjg@348 315 }
jjg@348 316 },
jjg@348 317
jjg@348 318 new Option(false, "-XDtab:") {
jjg@348 319 @Override
jjg@348 320 boolean matches(String opt) {
jjg@348 321 int sep = opt.indexOf(":");
jjg@348 322 return sep != -1 && super.matches(opt.substring(0, sep + 1));
jjg@348 323 }
jjg@348 324
jjg@348 325 void process(JavapTask task, String opt, String arg) throws BadArgs {
jjg@348 326 int sep = opt.indexOf(":");
jjg@348 327 try {
jjg@348 328 task.options.tabColumn = Integer.valueOf(opt.substring(sep + 1));
jjg@348 329 } catch (NumberFormatException e) {
jjg@348 330 }
jjg@348 331 }
jjg@46 332 }
jjg@46 333
jjg@46 334 };
jjg@46 335
jjg@300 336 public JavapTask() {
jjg@46 337 context = new Context();
jjg@283 338 context.put(Messages.class, this);
jjg@46 339 options = Options.instance(context);
jjg@300 340 attributeFactory = new Attribute.Factory();
jjg@46 341 }
jjg@46 342
jjg@300 343 public JavapTask(Writer out,
jjg@300 344 JavaFileManager fileManager,
jjg@300 345 DiagnosticListener<? super JavaFileObject> diagnosticListener) {
jjg@300 346 this();
jjg@300 347 this.log = getPrintWriterForWriter(out);
jjg@300 348 this.fileManager = fileManager;
jjg@300 349 this.diagnosticListener = diagnosticListener;
jjg@300 350 }
jjg@300 351
jjg@300 352 public JavapTask(Writer out,
jjg@46 353 JavaFileManager fileManager,
jjg@46 354 DiagnosticListener<? super JavaFileObject> diagnosticListener,
jjg@46 355 Iterable<String> options,
jjg@46 356 Iterable<String> classes) {
jjg@300 357 this(out, fileManager, diagnosticListener);
jjg@46 358
jjg@340 359 this.classes = new ArrayList<String>();
jjg@340 360 for (String classname: classes) {
jjg@340 361 classname.getClass(); // null-check
jjg@340 362 this.classes.add(classname);
jjg@340 363 }
jjg@340 364
jjg@46 365 try {
vromero@1561 366 if (options != null)
vromero@1561 367 handleOptions(options, false);
jjg@46 368 } catch (BadArgs e) {
jjg@46 369 throw new IllegalArgumentException(e.getMessage());
jjg@46 370 }
jjg@46 371 }
jjg@46 372
jjg@46 373 public void setLocale(Locale locale) {
jjg@46 374 if (locale == null)
jjg@46 375 locale = Locale.getDefault();
jjg@46 376 task_locale = locale;
jjg@46 377 }
jjg@46 378
jjg@1321 379 public void setLog(Writer log) {
jjg@1321 380 this.log = getPrintWriterForWriter(log);
jjg@46 381 }
jjg@46 382
jjg@46 383 public void setLog(OutputStream s) {
jjg@46 384 setLog(getPrintWriterForStream(s));
jjg@46 385 }
jjg@46 386
jjg@46 387 private static PrintWriter getPrintWriterForStream(OutputStream s) {
jjg@1321 388 return new PrintWriter(s == null ? System.err : s, true);
jjg@46 389 }
jjg@46 390
jjg@46 391 private static PrintWriter getPrintWriterForWriter(Writer w) {
jjg@46 392 if (w == null)
jjg@46 393 return getPrintWriterForStream(null);
jjg@46 394 else if (w instanceof PrintWriter)
jjg@46 395 return (PrintWriter) w;
jjg@46 396 else
jjg@46 397 return new PrintWriter(w, true);
jjg@46 398 }
jjg@46 399
jjg@46 400 public void setDiagnosticListener(DiagnosticListener<? super JavaFileObject> dl) {
jjg@46 401 diagnosticListener = dl;
jjg@46 402 }
jjg@46 403
jjg@46 404 public void setDiagnosticListener(OutputStream s) {
jjg@46 405 setDiagnosticListener(getDiagnosticListenerForStream(s));
jjg@46 406 }
jjg@46 407
jjg@46 408 private DiagnosticListener<JavaFileObject> getDiagnosticListenerForStream(OutputStream s) {
jjg@46 409 return getDiagnosticListenerForWriter(getPrintWriterForStream(s));
jjg@46 410 }
jjg@46 411
jjg@46 412 private DiagnosticListener<JavaFileObject> getDiagnosticListenerForWriter(Writer w) {
jjg@46 413 final PrintWriter pw = getPrintWriterForWriter(w);
jjg@46 414 return new DiagnosticListener<JavaFileObject> () {
jjg@46 415 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
jjg@340 416 switch (diagnostic.getKind()) {
jjg@340 417 case ERROR:
jjg@66 418 pw.print(getMessage("err.prefix"));
jjg@340 419 break;
jjg@340 420 case WARNING:
jjg@340 421 pw.print(getMessage("warn.prefix"));
jjg@340 422 break;
jjg@340 423 case NOTE:
jjg@340 424 pw.print(getMessage("note.prefix"));
jjg@340 425 break;
jjg@46 426 }
jjg@340 427 pw.print(" ");
jjg@46 428 pw.println(diagnostic.getMessage(null));
jjg@46 429 }
jjg@46 430 };
jjg@46 431 }
jjg@46 432
jjg@64 433 /** Result codes.
jjg@64 434 */
jjg@64 435 static final int
jjg@64 436 EXIT_OK = 0, // Compilation completed with no errors.
jjg@64 437 EXIT_ERROR = 1, // Completed but reported errors.
jjg@64 438 EXIT_CMDERR = 2, // Bad command-line arguments
jjg@64 439 EXIT_SYSERR = 3, // System error or resource exhaustion.
jjg@64 440 EXIT_ABNORMAL = 4; // Compiler terminated abnormally
jjg@64 441
jjg@46 442 int run(String[] args) {
jjg@46 443 try {
jjg@46 444 handleOptions(args);
jjg@64 445
jjg@64 446 // the following gives consistent behavior with javac
jjg@64 447 if (classes == null || classes.size() == 0) {
jjg@64 448 if (options.help || options.version || options.fullVersion)
jjg@64 449 return EXIT_OK;
jjg@64 450 else
jjg@64 451 return EXIT_CMDERR;
jjg@64 452 }
jjg@64 453
jjg@402 454 try {
jjg@402 455 boolean ok = run();
jjg@402 456 return ok ? EXIT_OK : EXIT_ERROR;
jjg@402 457 } finally {
jjg@402 458 if (defaultFileManager != null) {
jjg@402 459 try {
jjg@402 460 defaultFileManager.close();
jjg@402 461 defaultFileManager = null;
jjg@402 462 } catch (IOException e) {
jjg@402 463 throw new InternalError(e);
jjg@402 464 }
jjg@402 465 }
jjg@402 466 }
jjg@46 467 } catch (BadArgs e) {
jjg@340 468 reportError(e.key, e.args);
jjg@66 469 if (e.showUsage) {
jjg@66 470 log.println(getMessage("main.usage.summary", progname));
jjg@66 471 }
jjg@64 472 return EXIT_CMDERR;
jjg@46 473 } catch (InternalError e) {
jjg@46 474 Object[] e_args;
jjg@46 475 if (e.getCause() == null)
jjg@46 476 e_args = e.args;
jjg@46 477 else {
jjg@46 478 e_args = new Object[e.args.length + 1];
jjg@46 479 e_args[0] = e.getCause();
jjg@46 480 System.arraycopy(e.args, 0, e_args, 1, e.args.length);
jjg@46 481 }
jjg@340 482 reportError("err.internal.error", e_args);
jjg@64 483 return EXIT_ABNORMAL;
jjg@46 484 } finally {
jjg@46 485 log.flush();
jjg@46 486 }
jjg@46 487 }
jjg@46 488
jjg@46 489 public void handleOptions(String[] args) throws BadArgs {
jjg@46 490 handleOptions(Arrays.asList(args), true);
jjg@46 491 }
jjg@46 492
jjg@46 493 private void handleOptions(Iterable<String> args, boolean allowClasses) throws BadArgs {
jjg@46 494 if (log == null) {
jjg@46 495 log = getPrintWriterForStream(System.out);
jjg@46 496 if (diagnosticListener == null)
jjg@46 497 diagnosticListener = getDiagnosticListenerForStream(System.err);
jjg@46 498 } else {
jjg@46 499 if (diagnosticListener == null)
jjg@46 500 diagnosticListener = getDiagnosticListenerForWriter(log);
jjg@46 501 }
jjg@46 502
jjg@46 503
jjg@46 504 if (fileManager == null)
jjg@46 505 fileManager = getDefaultFileManager(diagnosticListener, log);
jjg@46 506
jjg@46 507 Iterator<String> iter = args.iterator();
jjg@64 508 boolean noArgs = !iter.hasNext();
jjg@46 509
jjg@46 510 while (iter.hasNext()) {
jjg@46 511 String arg = iter.next();
jjg@46 512 if (arg.startsWith("-"))
jjg@46 513 handleOption(arg, iter);
jjg@46 514 else if (allowClasses) {
jjg@46 515 if (classes == null)
jjg@46 516 classes = new ArrayList<String>();
jjg@46 517 classes.add(arg);
jjg@46 518 while (iter.hasNext())
jjg@46 519 classes.add(iter.next());
jjg@46 520 } else
jjg@46 521 throw new BadArgs("err.unknown.option", arg).showUsage(true);
jjg@46 522 }
jjg@46 523
jjg@68 524 if (!options.compat && options.accessOptions.size() > 1) {
jjg@68 525 StringBuilder sb = new StringBuilder();
jjg@68 526 for (String opt: options.accessOptions) {
jjg@68 527 if (sb.length() > 0)
jjg@68 528 sb.append(" ");
jjg@68 529 sb.append(opt);
jjg@68 530 }
jjg@68 531 throw new BadArgs("err.incompatible.options", sb);
jjg@68 532 }
jjg@68 533
jjg@46 534 if ((classes == null || classes.size() == 0) &&
jjg@64 535 !(noArgs || options.help || options.version || options.fullVersion)) {
jjg@46 536 throw new BadArgs("err.no.classes.specified");
jjg@46 537 }
jjg@64 538
jjg@64 539 if (noArgs || options.help)
jjg@64 540 showHelp();
jjg@64 541
jjg@64 542 if (options.version || options.fullVersion)
jjg@64 543 showVersion(options.fullVersion);
jjg@46 544 }
jjg@46 545
jjg@46 546 private void handleOption(String name, Iterator<String> rest) throws BadArgs {
jjg@46 547 for (Option o: recognizedOptions) {
jjg@46 548 if (o.matches(name)) {
jjg@46 549 if (o.hasArg) {
jjg@46 550 if (rest.hasNext())
jjg@46 551 o.process(this, name, rest.next());
jjg@46 552 else
jjg@46 553 throw new BadArgs("err.missing.arg", name).showUsage(true);
jjg@46 554 } else
jjg@46 555 o.process(this, name, null);
jjg@46 556
jjg@46 557 if (o.ignoreRest()) {
jjg@46 558 while (rest.hasNext())
jjg@46 559 rest.next();
jjg@46 560 }
jjg@46 561 return;
jjg@46 562 }
jjg@46 563 }
jjg@46 564
jjg@46 565 if (fileManager.handleOption(name, rest))
jjg@46 566 return;
jjg@46 567
jjg@46 568 throw new BadArgs("err.unknown.option", name).showUsage(true);
jjg@46 569 }
jjg@46 570
jjg@46 571 public Boolean call() {
jjg@46 572 return run();
jjg@46 573 }
jjg@46 574
jjg@46 575 public boolean run() {
jjg@46 576 if (classes == null || classes.size() == 0)
jjg@64 577 return false;
jjg@46 578
jjg@46 579 context.put(PrintWriter.class, log);
jjg@46 580 ClassWriter classWriter = ClassWriter.instance(context);
jjg@283 581 SourceWriter sourceWriter = SourceWriter.instance(context);
jjg@283 582 sourceWriter.setFileManager(fileManager);
jjg@46 583
jjg@346 584 attributeFactory.setCompat(options.compat);
jjg@346 585
jjg@46 586 boolean ok = true;
jjg@46 587
jjg@46 588 for (String className: classes) {
jjg@46 589 JavaFileObject fo;
jjg@46 590 try {
jjg@346 591 writeClass(classWriter, className);
jjg@46 592 } catch (ConstantPoolException e) {
jjg@340 593 reportError("err.bad.constant.pool", className, e.getLocalizedMessage());
jjg@46 594 ok = false;
jjg@46 595 } catch (EOFException e) {
jjg@340 596 reportError("err.end.of.file", className);
jjg@46 597 ok = false;
jjg@46 598 } catch (FileNotFoundException e) {
jjg@340 599 reportError("err.file.not.found", e.getLocalizedMessage());
jjg@46 600 ok = false;
jjg@46 601 } catch (IOException e) {
jjg@46 602 //e.printStackTrace();
jjg@46 603 Object msg = e.getLocalizedMessage();
jjg@46 604 if (msg == null)
jjg@46 605 msg = e;
jjg@340 606 reportError("err.ioerror", className, msg);
jjg@46 607 ok = false;
jjg@46 608 } catch (Throwable t) {
jjg@46 609 StringWriter sw = new StringWriter();
jjg@46 610 PrintWriter pw = new PrintWriter(sw);
jjg@46 611 t.printStackTrace(pw);
jjg@46 612 pw.close();
jjg@340 613 reportError("err.crash", t.toString(), sw.toString());
jjg@52 614 ok = false;
jjg@46 615 }
jjg@46 616 }
jjg@46 617
jjg@46 618 return ok;
jjg@46 619 }
jjg@46 620
jjg@346 621 protected boolean writeClass(ClassWriter classWriter, String className)
jjg@346 622 throws IOException, ConstantPoolException {
jjg@350 623 JavaFileObject fo = open(className);
jjg@350 624 if (fo == null) {
jjg@350 625 reportError("err.class.not.found", className);
jjg@350 626 return false;
jjg@346 627 }
jjg@346 628
jjg@346 629 ClassFileInfo cfInfo = read(fo);
jjg@346 630 if (!className.endsWith(".class")) {
jjg@346 631 String cfName = cfInfo.cf.getName();
jjg@346 632 if (!cfName.replaceAll("[/$]", ".").equals(className.replaceAll("[/$]", ".")))
jjg@346 633 reportWarning("warn.unexpected.class", className, cfName.replace('/', '.'));
jjg@346 634 }
jjg@346 635 write(cfInfo);
jjg@346 636
jjg@346 637 if (options.showInnerClasses) {
jjg@346 638 ClassFile cf = cfInfo.cf;
jjg@346 639 Attribute a = cf.getAttribute(Attribute.InnerClasses);
jjg@346 640 if (a instanceof InnerClasses_attribute) {
jjg@346 641 InnerClasses_attribute inners = (InnerClasses_attribute) a;
jjg@346 642 try {
jjg@346 643 boolean ok = true;
jjg@346 644 for (int i = 0; i < inners.classes.length; i++) {
jjg@346 645 int outerIndex = inners.classes[i].outer_class_info_index;
jjg@346 646 ConstantPool.CONSTANT_Class_info outerClassInfo = cf.constant_pool.getClassInfo(outerIndex);
jjg@346 647 String outerClassName = outerClassInfo.getName();
jjg@346 648 if (outerClassName.equals(cf.getName())) {
jjg@346 649 int innerIndex = inners.classes[i].inner_class_info_index;
jjg@346 650 ConstantPool.CONSTANT_Class_info innerClassInfo = cf.constant_pool.getClassInfo(innerIndex);
jjg@346 651 String innerClassName = innerClassInfo.getName();
jjg@346 652 classWriter.println("// inner class " + innerClassName.replaceAll("[/$]", "."));
jjg@346 653 classWriter.println();
jjg@346 654 ok = ok & writeClass(classWriter, innerClassName);
jjg@346 655 }
jjg@346 656 }
jjg@346 657 return ok;
jjg@346 658 } catch (ConstantPoolException e) {
jjg@346 659 reportError("err.bad.innerclasses.attribute", className);
jjg@346 660 return false;
jjg@346 661 }
jjg@346 662 } else if (a != null) {
jjg@346 663 reportError("err.bad.innerclasses.attribute", className);
jjg@346 664 return false;
jjg@346 665 }
jjg@346 666 }
jjg@346 667
jjg@346 668 return true;
jjg@346 669 }
jjg@346 670
jjg@350 671 protected JavaFileObject open(String className) throws IOException {
jjg@350 672 // for compatibility, first see if it is a class name
jjg@350 673 JavaFileObject fo = getClassFileObject(className);
jjg@350 674 if (fo != null)
jjg@350 675 return fo;
jjg@350 676
jjg@350 677 // see if it is an inner class, by replacing dots to $, starting from the right
jjg@350 678 String cn = className;
jjg@350 679 int lastDot;
jjg@350 680 while ((lastDot = cn.lastIndexOf(".")) != -1) {
jjg@350 681 cn = cn.substring(0, lastDot) + "$" + cn.substring(lastDot + 1);
jjg@350 682 fo = getClassFileObject(cn);
jjg@350 683 if (fo != null)
jjg@350 684 return fo;
jjg@350 685 }
jjg@350 686
jjg@350 687 if (!className.endsWith(".class"))
jjg@350 688 return null;
jjg@350 689
jjg@350 690 if (fileManager instanceof StandardJavaFileManager) {
jjg@350 691 StandardJavaFileManager sfm = (StandardJavaFileManager) fileManager;
jjg@350 692 fo = sfm.getJavaFileObjects(className).iterator().next();
jjg@350 693 if (fo != null && fo.getLastModified() != 0) {
jjg@350 694 return fo;
jjg@350 695 }
jjg@350 696 }
jjg@350 697
jjg@350 698 // see if it is a URL, and if so, wrap it in just enough of a JavaFileObject
jjg@350 699 // to suit javap's needs
jjg@350 700 if (className.matches("^[A-Za-z]+:.*")) {
jjg@350 701 try {
jjg@350 702 final URI uri = new URI(className);
jjg@350 703 final URL url = uri.toURL();
jjg@350 704 final URLConnection conn = url.openConnection();
jjg@350 705 return new JavaFileObject() {
jjg@350 706 public Kind getKind() {
jjg@350 707 return JavaFileObject.Kind.CLASS;
jjg@350 708 }
jjg@350 709
jjg@350 710 public boolean isNameCompatible(String simpleName, Kind kind) {
jjg@350 711 throw new UnsupportedOperationException();
jjg@350 712 }
jjg@350 713
jjg@350 714 public NestingKind getNestingKind() {
jjg@350 715 throw new UnsupportedOperationException();
jjg@350 716 }
jjg@350 717
jjg@350 718 public Modifier getAccessLevel() {
jjg@350 719 throw new UnsupportedOperationException();
jjg@350 720 }
jjg@350 721
jjg@350 722 public URI toUri() {
jjg@350 723 return uri;
jjg@350 724 }
jjg@350 725
jjg@350 726 public String getName() {
jjg@350 727 return url.toString();
jjg@350 728 }
jjg@350 729
jjg@350 730 public InputStream openInputStream() throws IOException {
jjg@350 731 return conn.getInputStream();
jjg@350 732 }
jjg@350 733
jjg@350 734 public OutputStream openOutputStream() throws IOException {
jjg@350 735 throw new UnsupportedOperationException();
jjg@350 736 }
jjg@350 737
jjg@350 738 public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
jjg@350 739 throw new UnsupportedOperationException();
jjg@350 740 }
jjg@350 741
jjg@350 742 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
jjg@350 743 throw new UnsupportedOperationException();
jjg@350 744 }
jjg@350 745
jjg@350 746 public Writer openWriter() throws IOException {
jjg@350 747 throw new UnsupportedOperationException();
jjg@350 748 }
jjg@350 749
jjg@350 750 public long getLastModified() {
jjg@350 751 return conn.getLastModified();
jjg@350 752 }
jjg@350 753
jjg@350 754 public boolean delete() {
jjg@350 755 throw new UnsupportedOperationException();
jjg@350 756 }
jjg@350 757
jjg@350 758 };
jjg@350 759 } catch (URISyntaxException ignore) {
jjg@350 760 } catch (IOException ignore) {
jjg@350 761 }
jjg@350 762 }
jjg@350 763
jjg@350 764 return null;
jjg@350 765 }
jjg@350 766
jjg@300 767 public static class ClassFileInfo {
jjg@300 768 ClassFileInfo(JavaFileObject fo, ClassFile cf, byte[] digest, int size) {
jjg@300 769 this.fo = fo;
jjg@300 770 this.cf = cf;
jjg@300 771 this.digest = digest;
jjg@300 772 this.size = size;
jjg@300 773 }
jjg@300 774 public final JavaFileObject fo;
jjg@300 775 public final ClassFile cf;
jjg@300 776 public final byte[] digest;
jjg@300 777 public final int size;
jjg@300 778 }
jjg@300 779
jjg@300 780 public ClassFileInfo read(JavaFileObject fo) throws IOException, ConstantPoolException {
jjg@300 781 InputStream in = fo.openInputStream();
jjg@300 782 try {
jjg@300 783 SizeInputStream sizeIn = null;
jjg@300 784 MessageDigest md = null;
jjg@300 785 if (options.sysInfo || options.verbose) {
jjg@300 786 try {
jjg@300 787 md = MessageDigest.getInstance("MD5");
jjg@300 788 } catch (NoSuchAlgorithmException ignore) {
jjg@300 789 }
jjg@300 790 in = new DigestInputStream(in, md);
jjg@300 791 in = sizeIn = new SizeInputStream(in);
jjg@300 792 }
jjg@300 793
jjg@300 794 ClassFile cf = ClassFile.read(in, attributeFactory);
jjg@300 795 byte[] digest = (md == null) ? null : md.digest();
jjg@300 796 int size = (sizeIn == null) ? -1 : sizeIn.size();
jjg@300 797 return new ClassFileInfo(fo, cf, digest, size);
jjg@300 798 } finally {
jjg@300 799 in.close();
jjg@300 800 }
jjg@300 801 }
jjg@300 802
jjg@300 803 public void write(ClassFileInfo info) {
jjg@300 804 ClassWriter classWriter = ClassWriter.instance(context);
jjg@300 805 if (options.sysInfo || options.verbose) {
jjg@300 806 classWriter.setFile(info.fo.toUri());
jjg@300 807 classWriter.setLastModified(info.fo.getLastModified());
jjg@300 808 classWriter.setDigest("MD5", info.digest);
jjg@300 809 classWriter.setFileSize(info.size);
jjg@300 810 }
jjg@300 811
jjg@300 812 classWriter.write(info.cf);
jjg@300 813 }
jjg@300 814
jjg@300 815 protected void setClassFile(ClassFile classFile) {
jjg@300 816 ClassWriter classWriter = ClassWriter.instance(context);
jjg@300 817 classWriter.setClassFile(classFile);
jjg@300 818 }
jjg@300 819
jjg@300 820 protected void setMethod(Method enclosingMethod) {
jjg@300 821 ClassWriter classWriter = ClassWriter.instance(context);
jjg@300 822 classWriter.setMethod(enclosingMethod);
jjg@300 823 }
jjg@300 824
jjg@300 825 protected void write(Attribute value) {
jjg@300 826 AttributeWriter attrWriter = AttributeWriter.instance(context);
jjg@300 827 ClassWriter classWriter = ClassWriter.instance(context);
jjg@300 828 ClassFile cf = classWriter.getClassFile();
jjg@300 829 attrWriter.write(cf, value, cf.constant_pool);
jjg@300 830 }
jjg@300 831
jjg@300 832 protected void write(Attributes attrs) {
jjg@300 833 AttributeWriter attrWriter = AttributeWriter.instance(context);
jjg@300 834 ClassWriter classWriter = ClassWriter.instance(context);
jjg@300 835 ClassFile cf = classWriter.getClassFile();
jjg@300 836 attrWriter.write(cf, attrs, cf.constant_pool);
jjg@300 837 }
jjg@300 838
jjg@300 839 protected void write(ConstantPool constant_pool) {
jjg@300 840 ConstantWriter constantWriter = ConstantWriter.instance(context);
jjg@300 841 constantWriter.writeConstantPool(constant_pool);
jjg@300 842 }
jjg@300 843
jjg@300 844 protected void write(ConstantPool constant_pool, int value) {
jjg@300 845 ConstantWriter constantWriter = ConstantWriter.instance(context);
jjg@300 846 constantWriter.write(value);
jjg@300 847 }
jjg@300 848
jjg@300 849 protected void write(ConstantPool.CPInfo value) {
jjg@300 850 ConstantWriter constantWriter = ConstantWriter.instance(context);
jjg@300 851 constantWriter.println(value);
jjg@300 852 }
jjg@300 853
jjg@300 854 protected void write(Field value) {
jjg@300 855 ClassWriter classWriter = ClassWriter.instance(context);
jjg@300 856 classWriter.writeField(value);
jjg@300 857 }
jjg@300 858
jjg@300 859 protected void write(Method value) {
jjg@300 860 ClassWriter classWriter = ClassWriter.instance(context);
jjg@300 861 classWriter.writeMethod(value);
jjg@300 862 }
jjg@300 863
jjg@46 864 private JavaFileManager getDefaultFileManager(final DiagnosticListener<? super JavaFileObject> dl, PrintWriter log) {
jjg@402 865 if (defaultFileManager == null)
jjg@402 866 defaultFileManager = JavapFileManager.create(dl, log);
jjg@402 867 return defaultFileManager;
jjg@46 868 }
jjg@46 869
jjg@46 870 private JavaFileObject getClassFileObject(String className) throws IOException {
jjg@46 871 JavaFileObject fo;
jjg@46 872 fo = fileManager.getJavaFileForInput(StandardLocation.PLATFORM_CLASS_PATH, className, JavaFileObject.Kind.CLASS);
jjg@46 873 if (fo == null)
jjg@46 874 fo = fileManager.getJavaFileForInput(StandardLocation.CLASS_PATH, className, JavaFileObject.Kind.CLASS);
jjg@46 875 return fo;
jjg@46 876 }
jjg@46 877
jjg@46 878 private void showHelp() {
jjg@46 879 log.println(getMessage("main.usage", progname));
jjg@46 880 for (Option o: recognizedOptions) {
jjg@46 881 String name = o.aliases[0].substring(1); // there must always be at least one name
jjg@46 882 if (name.startsWith("X") || name.equals("fullversion") || name.equals("h") || name.equals("verify"))
jjg@46 883 continue;
jjg@46 884 log.println(getMessage("main.opt." + name));
jjg@46 885 }
jjg@46 886 String[] fmOptions = { "-classpath", "-bootclasspath" };
jjg@46 887 for (String o: fmOptions) {
jjg@46 888 if (fileManager.isSupportedOption(o) == -1)
jjg@46 889 continue;
jjg@46 890 String name = o.substring(1);
jjg@46 891 log.println(getMessage("main.opt." + name));
jjg@46 892 }
jjg@46 893
jjg@46 894 }
jjg@46 895
jjg@46 896 private void showVersion(boolean full) {
jjg@46 897 log.println(version(full ? "full" : "release"));
jjg@46 898 }
jjg@46 899
jjg@46 900 private static final String versionRBName = "com.sun.tools.javap.resources.version";
jjg@46 901 private static ResourceBundle versionRB;
jjg@46 902
jjg@46 903 private String version(String key) {
jjg@46 904 // key=version: mm.nn.oo[-milestone]
jjg@46 905 // key=full: mm.mm.oo[-milestone]-build
jjg@46 906 if (versionRB == null) {
jjg@46 907 try {
jjg@46 908 versionRB = ResourceBundle.getBundle(versionRBName);
jjg@46 909 } catch (MissingResourceException e) {
jjg@46 910 return getMessage("version.resource.missing", System.getProperty("java.version"));
jjg@46 911 }
jjg@46 912 }
jjg@46 913 try {
jjg@46 914 return versionRB.getString(key);
jjg@46 915 }
jjg@46 916 catch (MissingResourceException e) {
jjg@46 917 return getMessage("version.unknown", System.getProperty("java.version"));
jjg@46 918 }
jjg@46 919 }
jjg@46 920
jjg@340 921 private void reportError(String key, Object... args) {
jjg@340 922 diagnosticListener.report(createDiagnostic(Diagnostic.Kind.ERROR, key, args));
jjg@340 923 }
jjg@340 924
jjg@340 925 private void reportNote(String key, Object... args) {
jjg@340 926 diagnosticListener.report(createDiagnostic(Diagnostic.Kind.NOTE, key, args));
jjg@340 927 }
jjg@340 928
jjg@340 929 private void reportWarning(String key, Object... args) {
jjg@340 930 diagnosticListener.report(createDiagnostic(Diagnostic.Kind.WARNING, key, args));
jjg@340 931 }
jjg@340 932
jjg@340 933 private Diagnostic<JavaFileObject> createDiagnostic(
jjg@340 934 final Diagnostic.Kind kind, final String key, final Object... args) {
jjg@46 935 return new Diagnostic<JavaFileObject>() {
jjg@46 936 public Kind getKind() {
jjg@340 937 return kind;
jjg@46 938 }
jjg@46 939
jjg@46 940 public JavaFileObject getSource() {
jjg@46 941 return null;
jjg@46 942 }
jjg@46 943
jjg@46 944 public long getPosition() {
jjg@46 945 return Diagnostic.NOPOS;
jjg@46 946 }
jjg@46 947
jjg@46 948 public long getStartPosition() {
jjg@46 949 return Diagnostic.NOPOS;
jjg@46 950 }
jjg@46 951
jjg@46 952 public long getEndPosition() {
jjg@46 953 return Diagnostic.NOPOS;
jjg@46 954 }
jjg@46 955
jjg@46 956 public long getLineNumber() {
jjg@46 957 return Diagnostic.NOPOS;
jjg@46 958 }
jjg@46 959
jjg@46 960 public long getColumnNumber() {
jjg@46 961 return Diagnostic.NOPOS;
jjg@46 962 }
jjg@46 963
jjg@46 964 public String getCode() {
jjg@46 965 return key;
jjg@46 966 }
jjg@46 967
jjg@46 968 public String getMessage(Locale locale) {
jjg@46 969 return JavapTask.this.getMessage(locale, key, args);
jjg@46 970 }
jjg@46 971
jjg@346 972 @Override
jjg@340 973 public String toString() {
jjg@340 974 return getClass().getName() + "[key=" + key + ",args=" + Arrays.asList(args) + "]";
jjg@340 975 }
jjg@340 976
jjg@46 977 };
jjg@46 978
jjg@46 979 }
jjg@46 980
jjg@283 981 public String getMessage(String key, Object... args) {
jjg@46 982 return getMessage(task_locale, key, args);
jjg@46 983 }
jjg@46 984
jjg@283 985 public String getMessage(Locale locale, String key, Object... args) {
jjg@46 986 if (bundles == null) {
jjg@46 987 // could make this a HashMap<Locale,SoftReference<ResourceBundle>>
jjg@46 988 // and for efficiency, keep a hard reference to the bundle for the task
jjg@46 989 // locale
jjg@46 990 bundles = new HashMap<Locale, ResourceBundle>();
jjg@46 991 }
jjg@46 992
jjg@46 993 if (locale == null)
jjg@46 994 locale = Locale.getDefault();
jjg@46 995
jjg@46 996 ResourceBundle b = bundles.get(locale);
jjg@46 997 if (b == null) {
jjg@46 998 try {
jjg@46 999 b = ResourceBundle.getBundle("com.sun.tools.javap.resources.javap", locale);
jjg@46 1000 bundles.put(locale, b);
jjg@46 1001 } catch (MissingResourceException e) {
jjg@46 1002 throw new InternalError("Cannot find javap resource bundle for locale " + locale);
jjg@46 1003 }
jjg@46 1004 }
jjg@46 1005
jjg@46 1006 try {
jjg@46 1007 return MessageFormat.format(b.getString(key), args);
jjg@46 1008 } catch (MissingResourceException e) {
jjg@46 1009 throw new InternalError(e, key);
jjg@46 1010 }
jjg@46 1011 }
jjg@46 1012
jjg@300 1013 protected Context context;
jjg@46 1014 JavaFileManager fileManager;
jjg@402 1015 JavaFileManager defaultFileManager;
jjg@46 1016 PrintWriter log;
jjg@46 1017 DiagnosticListener<? super JavaFileObject> diagnosticListener;
jjg@46 1018 List<String> classes;
jjg@46 1019 Options options;
jjg@46 1020 //ResourceBundle bundle;
jjg@46 1021 Locale task_locale;
jjg@46 1022 Map<Locale, ResourceBundle> bundles;
jjg@300 1023 protected Attribute.Factory attributeFactory;
jjg@46 1024
jjg@46 1025 private static final String progname = "javap";
jjg@88 1026
jjg@88 1027 private static class SizeInputStream extends FilterInputStream {
jjg@88 1028 SizeInputStream(InputStream in) {
jjg@88 1029 super(in);
jjg@88 1030 }
jjg@88 1031
jjg@88 1032 int size() {
jjg@88 1033 return size;
jjg@88 1034 }
jjg@88 1035
jjg@88 1036 @Override
jjg@88 1037 public int read(byte[] buf, int offset, int length) throws IOException {
jjg@88 1038 int n = super.read(buf, offset, length);
jjg@88 1039 if (n > 0)
jjg@88 1040 size += n;
jjg@88 1041 return n;
jjg@88 1042 }
jjg@88 1043
jjg@88 1044 @Override
jjg@88 1045 public int read() throws IOException {
jjg@88 1046 int b = super.read();
jjg@88 1047 size += 1;
jjg@88 1048 return b;
jjg@88 1049 }
jjg@88 1050
jjg@88 1051 private int size;
jjg@88 1052 }
jjg@46 1053 }

mercurial