langtools/src/share/classes/com/sun/tools/javac/util/Options.java
author jjg
Tue, 13 Dec 2011 11:21:28 -0800
changeset 11314 b612aaca08d0
parent 11052 65b9fa7eaf55
child 22163 3651128c74eb
permissions -rw-r--r--
7120736: refactor javac option handling Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
9087
e9e44877cd18 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8837
diff changeset
     2
 * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
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: 10
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.util;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
    28
import java.util.*;
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
    29
import com.sun.tools.javac.main.Option;
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
    30
import static com.sun.tools.javac.main.Option.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
/** A table of all command-line options.
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
 *  If an option has an argument, the option name is mapped to the argument.
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
 *  If a set option has no argument, it is mapped to itself.
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    36
 *  <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
    37
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
public class Options {
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
    private static final long serialVersionUID = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
    /** The context key for the options. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
    public static final Context.Key<Options> optionsKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
        new Context.Key<Options>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    private LinkedHashMap<String,String> values;
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    /** Get the Options instance for this context. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    public static Options instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
        Options instance = context.get(optionsKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
            instance = new Options(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    protected Options(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
// DEBUGGING -- Use LinkedHashMap for reproducability
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        values = new LinkedHashMap<String,String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
        context.put(optionsKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
    64
    /**
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
    65
     * Get the value for an undocumented option.
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
    66
     */
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    public String get(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        return values.get(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
    71
    /**
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
    72
     * Get the value for an option.
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
    73
     */
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
    74
    public String get(Option option) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
    75
        return values.get(option.text);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
    78
    /**
8837
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    79
     * Get the boolean value for an option, patterned after Boolean.getBoolean,
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    80
     * essentially will return true, iff the value exists and is set to "true".
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    81
     */
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    82
    public boolean getBoolean(String name) {
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    83
        return getBoolean(name, false);
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    84
    }
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    85
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    86
    /**
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    87
     * Get the boolean with a default value if the option is not set.
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    88
     */
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    89
    public boolean getBoolean(String name, boolean defaultValue) {
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    90
        String value = get(name);
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    91
        return (value == null) ? defaultValue : Boolean.parseBoolean(value);
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    92
    }
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    93
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    94
    /**
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
    95
     * Check if the value for an undocumented option has been set.
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
    96
     */
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
    97
    public boolean isSet(String name) {
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
    98
        return (values.get(name) != null);
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
    99
    }
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   100
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   101
    /**
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   102
     * Check if the value for an option has been set.
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   103
     */
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
   104
    public boolean isSet(Option option) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
   105
        return (values.get(option.text) != null);
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   106
    }
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   107
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   108
    /**
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   109
     * Check if the value for a choice option has been set to a specific value.
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   110
     */
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
   111
    public boolean isSet(Option option, String value) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
   112
        return (values.get(option.text + value) != null);
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   113
    }
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   114
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   115
    /**
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   116
     * Check if the value for an undocumented option has not been set.
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   117
     */
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   118
    public boolean isUnset(String name) {
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   119
        return (values.get(name) == null);
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   120
    }
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   121
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   122
    /**
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   123
     * Check if the value for an option has not been set.
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   124
     */
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
   125
    public boolean isUnset(Option option) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
   126
        return (values.get(option.text) == null);
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   127
    }
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   128
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   129
    /**
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   130
     * Check if the value for a choice option has not been set to a specific value.
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   131
     */
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
   132
    public boolean isUnset(Option option, String value) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
   133
        return (values.get(option.text + value) == null);
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   134
    }
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   135
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    public void put(String name, String value) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
        values.put(name, value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
   140
    public void put(Option option, String value) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
   141
        values.put(option.text, value);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
    public void putAll(Options options) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        values.putAll(options.values);
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
    public void remove(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        values.remove(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
    public Set<String> keySet() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        return values.keySet();
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
    public int size() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        return values.size();
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   160
    // light-weight notification mechanism
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   161
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   162
    private List<Runnable> listeners = List.nil();
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   163
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   164
    public void addListener(Runnable listener) {
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   165
        listeners = listeners.prepend(listener);
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   166
    }
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   167
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   168
    public void notifyListeners() {
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   169
        for (Runnable r: listeners)
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   170
            r.run();
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   171
    }
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   172
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
    /** Check for a lint suboption. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
    public boolean lint(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
        // return true if either the specific option is enabled, or
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        // they are all enabled without the specific one being
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
        // disabled
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
        return
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   179
            isSet(XLINT_CUSTOM, s) ||
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   180
            (isSet(XLINT) || isSet(XLINT_CUSTOM, "all")) &&
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   181
                isUnset(XLINT_CUSTOM, "-" + s);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
}