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