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

changeset 46
7708bd6d800d
child 52
3cb4fb6e0720
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javap/JavapTask.java	Tue Jun 03 13:26:47 2008 -0700
     1.3 @@ -0,0 +1,624 @@
     1.4 +/*
     1.5 + * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Sun designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Sun in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-15301 USA.
    1.23 + *
    1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.26 + * have any questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javap;
    1.30 +
    1.31 +import java.io.EOFException;
    1.32 +import java.io.FileNotFoundException;
    1.33 +import java.io.IOException;
    1.34 +import java.io.OutputStream;
    1.35 +import java.io.PrintWriter;
    1.36 +import java.io.StringWriter;
    1.37 +import java.io.Writer;
    1.38 +import java.text.MessageFormat;
    1.39 +import java.util.ArrayList;
    1.40 +import java.util.Arrays;
    1.41 +import java.util.HashMap;
    1.42 +import java.util.Iterator;
    1.43 +import java.util.List;
    1.44 +import java.util.Locale;
    1.45 +import java.util.Map;
    1.46 +import java.util.MissingResourceException;
    1.47 +import java.util.ResourceBundle;
    1.48 +
    1.49 +import javax.tools.Diagnostic;
    1.50 +import javax.tools.DiagnosticListener;
    1.51 +import javax.tools.JavaFileManager;
    1.52 +import javax.tools.JavaFileObject;
    1.53 +import javax.tools.StandardJavaFileManager;
    1.54 +import javax.tools.StandardLocation;
    1.55 +
    1.56 +import com.sun.tools.classfile.*;
    1.57 +
    1.58 +/**
    1.59 + *  "Main" class for javap, normally accessed from the command line
    1.60 + *  via Main, or from JSR199 via DisassemblerTool.
    1.61 + *
    1.62 + *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    1.63 + *  you write code that depends on this, you do so at your own risk.
    1.64 + *  This code and its internal interfaces are subject to change or
    1.65 + *  deletion without notice.</b>
    1.66 + */
    1.67 +public class JavapTask implements DisassemblerTool.DisassemblerTask {
    1.68 +    public class BadArgs extends Exception {
    1.69 +        static final long serialVersionUID = 8765093759964640721L;
    1.70 +        BadArgs(String key, Object... args) {
    1.71 +            super(JavapTask.this.getMessage(key, args));
    1.72 +            this.key = key;
    1.73 +            this.args = args;
    1.74 +        }
    1.75 +
    1.76 +        BadArgs showUsage(boolean b) {
    1.77 +            showUsage = b;
    1.78 +            return this;
    1.79 +        }
    1.80 +
    1.81 +        final String key;
    1.82 +        final Object[] args;
    1.83 +        boolean showUsage;
    1.84 +    }
    1.85 +
    1.86 +    static abstract class Option {
    1.87 +        Option(boolean hasArg, String... aliases) {
    1.88 +            this.hasArg = hasArg;
    1.89 +            this.aliases = aliases;
    1.90 +        }
    1.91 +
    1.92 +        boolean matches(String opt) {
    1.93 +            for (String a: aliases) {
    1.94 +                if (a.equals(opt))
    1.95 +                    return true;
    1.96 +            }
    1.97 +            return false;
    1.98 +        }
    1.99 +
   1.100 +        boolean ignoreRest() {
   1.101 +            return false;
   1.102 +        }
   1.103 +
   1.104 +        abstract void process(JavapTask task, String opt, String arg) throws BadArgs;
   1.105 +
   1.106 +        final boolean hasArg;
   1.107 +        final String[] aliases;
   1.108 +    }
   1.109 +
   1.110 +    static Option[] recognizedOptions = {
   1.111 +
   1.112 +        new Option(false, "-help", "--help", "-?") {
   1.113 +            void process(JavapTask task, String opt, String arg) {
   1.114 +                task.options.help = true;
   1.115 +            }
   1.116 +        },
   1.117 +
   1.118 +        new Option(false, "-version") {
   1.119 +            void process(JavapTask task, String opt, String arg) {
   1.120 +                task.options.version = true;
   1.121 +            }
   1.122 +        },
   1.123 +
   1.124 +        new Option(false, "-fullversion") {
   1.125 +            void process(JavapTask task, String opt, String arg) {
   1.126 +                task.options.fullVersion = true;
   1.127 +            }
   1.128 +        },
   1.129 +
   1.130 +        new Option(false, "-v", "-verbose", "-all") {
   1.131 +            void process(JavapTask task, String opt, String arg) {
   1.132 +                task.options.verbose = true;
   1.133 +                task.options.showFlags = true;
   1.134 +                task.options.showAllAttrs = true;
   1.135 +            }
   1.136 +        },
   1.137 +
   1.138 +        new Option(false, "-l") {
   1.139 +            void process(JavapTask task, String opt, String arg) {
   1.140 +                task.options.showLineAndLocalVariableTables = true;
   1.141 +            }
   1.142 +        },
   1.143 +
   1.144 +        new Option(false, "-public") {
   1.145 +            void process(JavapTask task, String opt, String arg) {
   1.146 +                task.options.showAccess = AccessFlags.ACC_PUBLIC;
   1.147 +            }
   1.148 +        },
   1.149 +
   1.150 +        new Option(false, "-protected") {
   1.151 +            void process(JavapTask task, String opt, String arg) {
   1.152 +                task.options.showAccess = AccessFlags.ACC_PROTECTED;
   1.153 +            }
   1.154 +        },
   1.155 +
   1.156 +        new Option(false, "-package") {
   1.157 +            void process(JavapTask task, String opt, String arg) {
   1.158 +                task.options.showAccess = 0;
   1.159 +            }
   1.160 +        },
   1.161 +
   1.162 +        new Option(false, "-p", "-private") {
   1.163 +            void process(JavapTask task, String opt, String arg) {
   1.164 +                task.options.showAccess = AccessFlags.ACC_PRIVATE;
   1.165 +            }
   1.166 +        },
   1.167 +
   1.168 +        new Option(false, "-c") {
   1.169 +            void process(JavapTask task, String opt, String arg) {
   1.170 +                task.options.showDisassembled = true;
   1.171 +            }
   1.172 +        },
   1.173 +
   1.174 +        new Option(false, "-s") {
   1.175 +            void process(JavapTask task, String opt, String arg) {
   1.176 +                task.options.showInternalSignatures = true;
   1.177 +            }
   1.178 +        },
   1.179 +
   1.180 +//        new Option(false, "-all") {
   1.181 +//            void process(JavapTask task, String opt, String arg) {
   1.182 +//                task.options.showAllAttrs = true;
   1.183 +//            }
   1.184 +//        },
   1.185 +
   1.186 +        new Option(false, "-h") {
   1.187 +            void process(JavapTask task, String opt, String arg) throws BadArgs {
   1.188 +                throw task.new BadArgs("err.h.not.supported");
   1.189 +            }
   1.190 +        },
   1.191 +
   1.192 +        new Option(false, "-verify", "-verify-verbose") {
   1.193 +            void process(JavapTask task, String opt, String arg) throws BadArgs {
   1.194 +                throw task.new BadArgs("err.verify.not.supported");
   1.195 +            }
   1.196 +        },
   1.197 +
   1.198 +        new Option(false, "-Xold") {
   1.199 +            void process(JavapTask task, String opt, String arg) throws BadArgs {
   1.200 +                // -Xold is only supported as first arg when invoked from
   1.201 +                // command line; this is handled in Main,main
   1.202 +                throw task.new BadArgs("err.Xold.not.supported.here");
   1.203 +            }
   1.204 +        },
   1.205 +
   1.206 +        new Option(false, "-Xnew") {
   1.207 +            void process(JavapTask task, String opt, String arg) throws BadArgs {
   1.208 +                // ignore: this _is_ the new version
   1.209 +            }
   1.210 +        },
   1.211 +
   1.212 +        new Option(false, "-XDcompat") {
   1.213 +            void process(JavapTask task, String opt, String arg) {
   1.214 +                task.options.compat = true;
   1.215 +            }
   1.216 +        },
   1.217 +
   1.218 +        new Option(false, "-XDjsr277") {
   1.219 +            void process(JavapTask task, String opt, String arg) {
   1.220 +                task.options.jsr277 = true;
   1.221 +            }
   1.222 +        },
   1.223 +
   1.224 +        new Option(false, "-XDignore.symbol.file") {
   1.225 +            void process(JavapTask task, String opt, String arg) {
   1.226 +                task.options.ignoreSymbolFile = true;
   1.227 +            }
   1.228 +        }
   1.229 +
   1.230 +    };
   1.231 +
   1.232 +    JavapTask() {
   1.233 +        context = new Context();
   1.234 +        options = Options.instance(context);
   1.235 +    }
   1.236 +
   1.237 +    JavapTask(Writer out,
   1.238 +            JavaFileManager fileManager,
   1.239 +            DiagnosticListener<? super JavaFileObject> diagnosticListener,
   1.240 +            Iterable<String> options,
   1.241 +            Iterable<String> classes) {
   1.242 +        this();
   1.243 +        this.log = getPrintWriterForWriter(out);
   1.244 +        this.fileManager = fileManager;
   1.245 +        this.diagnosticListener = diagnosticListener;
   1.246 +
   1.247 +        try {
   1.248 +            handleOptions(options, false);
   1.249 +        } catch (BadArgs e) {
   1.250 +            throw new IllegalArgumentException(e.getMessage());
   1.251 +        }
   1.252 +
   1.253 +        this.classes = new ArrayList<String>();
   1.254 +        for (String classname: classes) {
   1.255 +            classname.getClass(); // null-check
   1.256 +            this.classes.add(classname);
   1.257 +        }
   1.258 +    }
   1.259 +
   1.260 +    public void setLocale(Locale locale) {
   1.261 +        if (locale == null)
   1.262 +            locale = Locale.getDefault();
   1.263 +        task_locale = locale;
   1.264 +    }
   1.265 +
   1.266 +    public void setLog(PrintWriter log) {
   1.267 +        this.log = log;
   1.268 +    }
   1.269 +
   1.270 +    public void setLog(OutputStream s) {
   1.271 +        setLog(getPrintWriterForStream(s));
   1.272 +    }
   1.273 +
   1.274 +    private static PrintWriter getPrintWriterForStream(OutputStream s) {
   1.275 +        return new PrintWriter(s, true);
   1.276 +    }
   1.277 +
   1.278 +    private static PrintWriter getPrintWriterForWriter(Writer w) {
   1.279 +        if (w == null)
   1.280 +            return getPrintWriterForStream(null);
   1.281 +        else if (w instanceof PrintWriter)
   1.282 +            return (PrintWriter) w;
   1.283 +        else
   1.284 +            return new PrintWriter(w, true);
   1.285 +    }
   1.286 +
   1.287 +    public void setDiagnosticListener(DiagnosticListener<? super JavaFileObject> dl) {
   1.288 +        diagnosticListener = dl;
   1.289 +    }
   1.290 +
   1.291 +    public void setDiagnosticListener(OutputStream s) {
   1.292 +        setDiagnosticListener(getDiagnosticListenerForStream(s));
   1.293 +    }
   1.294 +
   1.295 +    private DiagnosticListener<JavaFileObject> getDiagnosticListenerForStream(OutputStream s) {
   1.296 +        return getDiagnosticListenerForWriter(getPrintWriterForStream(s));
   1.297 +    }
   1.298 +
   1.299 +    private DiagnosticListener<JavaFileObject> getDiagnosticListenerForWriter(Writer w) {
   1.300 +        final PrintWriter pw = getPrintWriterForWriter(w);
   1.301 +        return new DiagnosticListener<JavaFileObject> () {
   1.302 +            public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.303 +                if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
   1.304 +                    pw.print(getMessage("err.prefix"));
   1.305 +                    pw.print(" ");
   1.306 +                }
   1.307 +                pw.println(diagnostic.getMessage(null));
   1.308 +            }
   1.309 +        };
   1.310 +    }
   1.311 +
   1.312 +    int run(String[] args) {
   1.313 +        try {
   1.314 +            handleOptions(args);
   1.315 +            boolean ok = run();
   1.316 +            return ok ? 0 : 1;
   1.317 +        } catch (BadArgs e) {
   1.318 +            diagnosticListener.report(createDiagnostic(e.key, e.args));
   1.319 +            return 1;
   1.320 +        } catch (InternalError e) {
   1.321 +            Object[] e_args;
   1.322 +            if (e.getCause() == null)
   1.323 +                e_args = e.args;
   1.324 +            else {
   1.325 +                e_args = new Object[e.args.length + 1];
   1.326 +                e_args[0] = e.getCause();
   1.327 +                System.arraycopy(e.args, 0, e_args, 1, e.args.length);
   1.328 +            }
   1.329 +            diagnosticListener.report(createDiagnostic("err.internal.error", e_args));
   1.330 +            return 1;
   1.331 +        } finally {
   1.332 +            log.flush();
   1.333 +        }
   1.334 +    }
   1.335 +
   1.336 +    public void handleOptions(String[] args) throws BadArgs {
   1.337 +        handleOptions(Arrays.asList(args), true);
   1.338 +    }
   1.339 +
   1.340 +    private void handleOptions(Iterable<String> args, boolean allowClasses) throws BadArgs {
   1.341 +        if (log == null) {
   1.342 +            log = getPrintWriterForStream(System.out);
   1.343 +            if (diagnosticListener == null)
   1.344 +              diagnosticListener = getDiagnosticListenerForStream(System.err);
   1.345 +        } else {
   1.346 +            if (diagnosticListener == null)
   1.347 +              diagnosticListener = getDiagnosticListenerForWriter(log);
   1.348 +        }
   1.349 +
   1.350 +
   1.351 +        if (fileManager == null)
   1.352 +            fileManager = getDefaultFileManager(diagnosticListener, log);
   1.353 +
   1.354 +        Iterator<String> iter = args.iterator();
   1.355 +        if (!iter.hasNext())
   1.356 +            options.help = true;
   1.357 +
   1.358 +        while (iter.hasNext()) {
   1.359 +            String arg = iter.next();
   1.360 +            if (arg.startsWith("-"))
   1.361 +                handleOption(arg, iter);
   1.362 +            else if (allowClasses) {
   1.363 +                if (classes == null)
   1.364 +                    classes = new ArrayList<String>();
   1.365 +                classes.add(arg);
   1.366 +                while (iter.hasNext())
   1.367 +                    classes.add(iter.next());
   1.368 +            } else
   1.369 +                throw new BadArgs("err.unknown.option", arg).showUsage(true);
   1.370 +        }
   1.371 +
   1.372 +        if (options.ignoreSymbolFile && fileManager instanceof JavapFileManager)
   1.373 +            ((JavapFileManager) fileManager).setIgnoreSymbolFile(true);
   1.374 +
   1.375 +        if ((classes == null || classes.size() == 0) &&
   1.376 +                !(options.help || options.version || options.fullVersion)) {
   1.377 +            throw new BadArgs("err.no.classes.specified");
   1.378 +        }
   1.379 +    }
   1.380 +
   1.381 +    private void handleOption(String name, Iterator<String> rest) throws BadArgs {
   1.382 +        for (Option o: recognizedOptions) {
   1.383 +            if (o.matches(name)) {
   1.384 +                if (o.hasArg) {
   1.385 +                    if (rest.hasNext())
   1.386 +                        o.process(this, name, rest.next());
   1.387 +                    else
   1.388 +                        throw new BadArgs("err.missing.arg", name).showUsage(true);
   1.389 +                } else
   1.390 +                    o.process(this, name, null);
   1.391 +
   1.392 +                if (o.ignoreRest()) {
   1.393 +                    while (rest.hasNext())
   1.394 +                        rest.next();
   1.395 +                }
   1.396 +                return;
   1.397 +            }
   1.398 +        }
   1.399 +
   1.400 +        if (fileManager.handleOption(name, rest))
   1.401 +            return;
   1.402 +
   1.403 +        throw new BadArgs("err.unknown.option", name).showUsage(true);
   1.404 +    }
   1.405 +
   1.406 +    public Boolean call() {
   1.407 +        return run();
   1.408 +    }
   1.409 +
   1.410 +    public boolean run() {
   1.411 +        if (options.help)
   1.412 +            showHelp();
   1.413 +
   1.414 +        if (options.version || options.fullVersion)
   1.415 +            showVersion(options.fullVersion);
   1.416 +
   1.417 +        if (classes == null || classes.size() == 0)
   1.418 +            return true;
   1.419 +
   1.420 +        context.put(PrintWriter.class, log);
   1.421 +        ClassWriter classWriter = ClassWriter.instance(context);
   1.422 +
   1.423 +        boolean ok = true;
   1.424 +
   1.425 +        for (String className: classes) {
   1.426 +            JavaFileObject fo;
   1.427 +            try {
   1.428 +                if (className.endsWith(".class")) {
   1.429 +                    if (fileManager instanceof StandardJavaFileManager) {
   1.430 +                        StandardJavaFileManager sfm = (StandardJavaFileManager) fileManager;
   1.431 +                        fo = sfm.getJavaFileObjects(className).iterator().next();
   1.432 +                    } else {
   1.433 +                       diagnosticListener.report(createDiagnostic("err.not.standard.file.manager", className));
   1.434 +                       ok = false;
   1.435 +                       continue;
   1.436 +                    }
   1.437 +                } else {
   1.438 +                    fo = getClassFileObject(className);
   1.439 +                    if (fo == null) {
   1.440 +                        // see if it is an inner class, by replacing dots to $, starting from the right
   1.441 +                        String cn = className;
   1.442 +                        int lastDot;
   1.443 +                        while (fo == null && (lastDot = cn.lastIndexOf(".")) != -1) {
   1.444 +                            cn = cn.substring(0, lastDot) + "$" + cn.substring(lastDot + 1);
   1.445 +                            fo = getClassFileObject(cn);
   1.446 +                        }
   1.447 +                    }
   1.448 +                    if (fo == null) {
   1.449 +                       diagnosticListener.report(createDiagnostic("err.class.not.found", className));
   1.450 +                       ok = false;
   1.451 +                       continue;
   1.452 +                    }
   1.453 +                }
   1.454 +                Attribute.Factory attributeFactory = new Attribute.Factory();
   1.455 +                attributeFactory.setCompat(options.compat);
   1.456 +                attributeFactory.setJSR277(options.jsr277);
   1.457 +                ClassFile cf = ClassFile.read(fo.openInputStream(), attributeFactory);
   1.458 +                classWriter.write(cf);
   1.459 +            } catch (ConstantPoolException e) {
   1.460 +                diagnosticListener.report(createDiagnostic("err.bad.constant.pool", className, e.getLocalizedMessage()));
   1.461 +                ok = false;
   1.462 +            } catch (EOFException e) {
   1.463 +                diagnosticListener.report(createDiagnostic("err.end.of.file", className));
   1.464 +                ok = false;
   1.465 +            } catch (FileNotFoundException e) {
   1.466 +                diagnosticListener.report(createDiagnostic("err.file.not.found", e.getLocalizedMessage()));
   1.467 +                ok = false;
   1.468 +            } catch (IOException e) {
   1.469 +                //e.printStackTrace();
   1.470 +                Object msg = e.getLocalizedMessage();
   1.471 +                if (msg == null)
   1.472 +                    msg = e;
   1.473 +                diagnosticListener.report(createDiagnostic("err.ioerror", className, msg));
   1.474 +                ok = false;
   1.475 +            } catch (Throwable t) {
   1.476 +                StringWriter sw = new StringWriter();
   1.477 +                PrintWriter pw = new PrintWriter(sw);
   1.478 +                t.printStackTrace(pw);
   1.479 +                pw.close();
   1.480 +                diagnosticListener.report(createDiagnostic("err.crash", t.toString(), sw.toString()));
   1.481 +            }
   1.482 +        }
   1.483 +
   1.484 +        return ok;
   1.485 +    }
   1.486 +
   1.487 +    private JavaFileManager getDefaultFileManager(final DiagnosticListener<? super JavaFileObject> dl, PrintWriter log) {
   1.488 +        return JavapFileManager.create(dl, log, options);
   1.489 +    }
   1.490 +
   1.491 +    private JavaFileObject getClassFileObject(String className) throws IOException {
   1.492 +        JavaFileObject fo;
   1.493 +        fo = fileManager.getJavaFileForInput(StandardLocation.PLATFORM_CLASS_PATH, className, JavaFileObject.Kind.CLASS);
   1.494 +        if (fo == null)
   1.495 +            fo = fileManager.getJavaFileForInput(StandardLocation.CLASS_PATH, className, JavaFileObject.Kind.CLASS);
   1.496 +        return fo;
   1.497 +    }
   1.498 +
   1.499 +    private void showHelp() {
   1.500 +        log.println(getMessage("main.usage", progname));
   1.501 +        for (Option o: recognizedOptions) {
   1.502 +            String name = o.aliases[0].substring(1); // there must always be at least one name
   1.503 +            if (name.startsWith("X") || name.equals("fullversion") || name.equals("h") || name.equals("verify"))
   1.504 +                continue;
   1.505 +            log.println(getMessage("main.opt." + name));
   1.506 +        }
   1.507 +        String[] fmOptions = { "-classpath", "-bootclasspath" };
   1.508 +        for (String o: fmOptions) {
   1.509 +            if (fileManager.isSupportedOption(o) == -1)
   1.510 +                continue;
   1.511 +            String name = o.substring(1);
   1.512 +            log.println(getMessage("main.opt." + name));
   1.513 +        }
   1.514 +
   1.515 +    }
   1.516 +
   1.517 +    private void showVersion(boolean full) {
   1.518 +        log.println(version(full ? "full" : "release"));
   1.519 +    }
   1.520 +
   1.521 +    private static final String versionRBName = "com.sun.tools.javap.resources.version";
   1.522 +    private static ResourceBundle versionRB;
   1.523 +
   1.524 +    private String version(String key) {
   1.525 +        // key=version:  mm.nn.oo[-milestone]
   1.526 +        // key=full:     mm.mm.oo[-milestone]-build
   1.527 +        if (versionRB == null) {
   1.528 +            try {
   1.529 +                versionRB = ResourceBundle.getBundle(versionRBName);
   1.530 +            } catch (MissingResourceException e) {
   1.531 +                return getMessage("version.resource.missing", System.getProperty("java.version"));
   1.532 +            }
   1.533 +        }
   1.534 +        try {
   1.535 +            return versionRB.getString(key);
   1.536 +        }
   1.537 +        catch (MissingResourceException e) {
   1.538 +            return getMessage("version.unknown", System.getProperty("java.version"));
   1.539 +        }
   1.540 +    }
   1.541 +
   1.542 +    private Diagnostic<JavaFileObject> createDiagnostic(final String key, final Object... args) {
   1.543 +        return new Diagnostic<JavaFileObject>() {
   1.544 +            public Kind getKind() {
   1.545 +                return Diagnostic.Kind.ERROR;
   1.546 +            }
   1.547 +
   1.548 +            public JavaFileObject getSource() {
   1.549 +                return null;
   1.550 +            }
   1.551 +
   1.552 +            public long getPosition() {
   1.553 +                return Diagnostic.NOPOS;
   1.554 +            }
   1.555 +
   1.556 +            public long getStartPosition() {
   1.557 +                return Diagnostic.NOPOS;
   1.558 +            }
   1.559 +
   1.560 +            public long getEndPosition() {
   1.561 +                return Diagnostic.NOPOS;
   1.562 +            }
   1.563 +
   1.564 +            public long getLineNumber() {
   1.565 +                return Diagnostic.NOPOS;
   1.566 +            }
   1.567 +
   1.568 +            public long getColumnNumber() {
   1.569 +                return Diagnostic.NOPOS;
   1.570 +            }
   1.571 +
   1.572 +            public String getCode() {
   1.573 +                return key;
   1.574 +            }
   1.575 +
   1.576 +            public String getMessage(Locale locale) {
   1.577 +                return JavapTask.this.getMessage(locale, key, args);
   1.578 +            }
   1.579 +
   1.580 +        };
   1.581 +
   1.582 +    }
   1.583 +
   1.584 +    private String getMessage(String key, Object... args) {
   1.585 +        return getMessage(task_locale, key, args);
   1.586 +    }
   1.587 +
   1.588 +    private String getMessage(Locale locale, String key, Object... args) {
   1.589 +        if (bundles == null) {
   1.590 +            // could make this a HashMap<Locale,SoftReference<ResourceBundle>>
   1.591 +            // and for efficiency, keep a hard reference to the bundle for the task
   1.592 +            // locale
   1.593 +            bundles = new HashMap<Locale, ResourceBundle>();
   1.594 +        }
   1.595 +
   1.596 +        if (locale == null)
   1.597 +            locale = Locale.getDefault();
   1.598 +
   1.599 +        ResourceBundle b = bundles.get(locale);
   1.600 +        if (b == null) {
   1.601 +            try {
   1.602 +                b = ResourceBundle.getBundle("com.sun.tools.javap.resources.javap", locale);
   1.603 +                bundles.put(locale, b);
   1.604 +            } catch (MissingResourceException e) {
   1.605 +                throw new InternalError("Cannot find javap resource bundle for locale " + locale);
   1.606 +            }
   1.607 +        }
   1.608 +
   1.609 +        try {
   1.610 +            return MessageFormat.format(b.getString(key), args);
   1.611 +        } catch (MissingResourceException e) {
   1.612 +            throw new InternalError(e, key);
   1.613 +        }
   1.614 +    }
   1.615 +
   1.616 +    Context context;
   1.617 +    JavaFileManager fileManager;
   1.618 +    PrintWriter log;
   1.619 +    DiagnosticListener<? super JavaFileObject> diagnosticListener;
   1.620 +    List<String> classes;
   1.621 +    Options options;
   1.622 +    //ResourceBundle bundle;
   1.623 +    Locale task_locale;
   1.624 +    Map<Locale, ResourceBundle> bundles;
   1.625 +
   1.626 +    private static final String progname = "javap";
   1.627 +}

mercurial