langtools/src/share/classes/com/sun/tools/javac/util/Options.java
author briangoetz
Wed, 18 Dec 2013 16:05:18 -0500
changeset 22163 3651128c74eb
parent 11314 b612aaca08d0
permissions -rw-r--r--
8030244: Update langtools to use Diamond Reviewed-by: darcy
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. */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 11314
diff changeset
    45
    public static final Context.Key<Options> optionsKey = new Context.Key<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    private LinkedHashMap<String,String> values;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    /** Get the Options instance for this context. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    public static Options instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
        Options instance = context.get(optionsKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
            instance = new Options(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    protected Options(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
// DEBUGGING -- Use LinkedHashMap for reproducability
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 11314
diff changeset
    59
        values = new LinkedHashMap<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        context.put(optionsKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
    63
    /**
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
    64
     * Get the value for an undocumented option.
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
    65
     */
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    public String get(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
        return values.get(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
    70
    /**
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
    71
     * Get the value for an option.
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
    72
     */
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
    73
    public String get(Option option) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
    74
        return values.get(option.text);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
    77
    /**
8837
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    78
     * Get the boolean value for an option, patterned after Boolean.getBoolean,
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    79
     * essentially will return true, iff the value exists and is set to "true".
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    80
     */
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    81
    public boolean getBoolean(String name) {
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    82
        return getBoolean(name, false);
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    83
    }
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
     * Get the boolean with a default value if the option is not set.
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    87
     */
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    88
    public boolean getBoolean(String name, boolean defaultValue) {
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    89
        String value = get(name);
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    90
        return (value == null) ? defaultValue : Boolean.parseBoolean(value);
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    91
    }
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    92
141b22c7e7b2 7021927: javac: regression in performance
ksrini
parents: 7681
diff changeset
    93
    /**
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
    94
     * Check if the value for an undocumented option has been set.
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
    95
     */
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
    96
    public boolean isSet(String name) {
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
    97
        return (values.get(name) != null);
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
    98
    }
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
     * Check if the value for an option has been set.
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   102
     */
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
   103
    public boolean isSet(Option option) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
   104
        return (values.get(option.text) != null);
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   105
    }
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
     * 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
   109
     */
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
   110
    public boolean isSet(Option option, String value) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
   111
        return (values.get(option.text + value) != null);
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   112
    }
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
     * Check if the value for an undocumented option has not been set.
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   116
     */
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   117
    public boolean isUnset(String name) {
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   118
        return (values.get(name) == null);
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   119
    }
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
     * Check if the value for an option has not been set.
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   123
     */
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
   124
    public boolean isUnset(Option option) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
   125
        return (values.get(option.text) == null);
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   126
    }
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
     * 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
   130
     */
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
   131
    public boolean isUnset(Option option, String value) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
   132
        return (values.get(option.text + value) == null);
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   133
    }
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   134
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
    public void put(String name, String value) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
        values.put(name, value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
   139
    public void put(Option option, String value) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
   140
        values.put(option.text, value);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    public void putAll(Options options) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        values.putAll(options.values);
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
    public void remove(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        values.remove(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
    public Set<String> keySet() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        return values.keySet();
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    public int size() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
        return values.size();
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   159
    // light-weight notification mechanism
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   160
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   161
    private List<Runnable> listeners = List.nil();
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   162
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   163
    public void addListener(Runnable listener) {
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   164
        listeners = listeners.prepend(listener);
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   165
    }
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
    public void notifyListeners() {
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   168
        for (Runnable r: listeners)
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   169
            r.run();
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   170
    }
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   171
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
    /** Check for a lint suboption. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
    public boolean lint(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        // return true if either the specific option is enabled, or
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
        // they are all enabled without the specific one being
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        // disabled
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
        return
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   178
            isSet(XLINT_CUSTOM, s) ||
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   179
            (isSet(XLINT) || isSet(XLINT_CUSTOM, "all")) &&
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   180
                isUnset(XLINT_CUSTOM, "-" + s);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
}