langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java
author jjg
Thu, 20 Oct 2016 13:44:51 -0700
changeset 41637 7b24b4c32ee6
parent 40772 de87954b8f20
permissions -rw-r--r--
8145471: javac changes for enhanced deprecation Reviewed-by: smarks, tbell, mcimadamore, jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 25874
diff changeset
     2
 * Copyright (c) 2001, 2016, 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.*;
40772
de87954b8f20 8164073: Javac should unconditionally warn if deprecated javadoc tag is used without @Deprecated annotation
sadayapalam
parents: 40308
diff changeset
    29
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
    30
import com.sun.tools.javac.main.Option;
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
    31
import static com.sun.tools.javac.main.Option.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
/** A table of all command-line options.
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
 *  If an option has an argument, the option name is mapped to the argument.
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
 *  If a set option has no argument, it is mapped to itself.
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    37
 *  <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
    38
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
public class Options {
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
    private static final long serialVersionUID = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
    /** The context key for the options. */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 11314
diff changeset
    46
    public static final Context.Key<Options> optionsKey = new Context.Key<>();
10
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
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 11314
diff changeset
    60
        values = new LinkedHashMap<>();
10
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) {
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 25874
diff changeset
    75
        return values.get(option.primaryName);
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) {
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 25874
diff changeset
   105
        return (values.get(option.primaryName) != 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) {
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 25874
diff changeset
   112
        return (values.get(option.primaryName + 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
41637
7b24b4c32ee6 8145471: javac changes for enhanced deprecation
jjg
parents: 40772
diff changeset
   115
    /** Check if the value for a lint option has been explicitly set, either with -Xlint:opt
7b24b4c32ee6 8145471: javac changes for enhanced deprecation
jjg
parents: 40772
diff changeset
   116
     *  or if all lint options have enabled and this one not disabled with -Xlint:-opt.
7b24b4c32ee6 8145471: javac changes for enhanced deprecation
jjg
parents: 40772
diff changeset
   117
     */
7b24b4c32ee6 8145471: javac changes for enhanced deprecation
jjg
parents: 40772
diff changeset
   118
    public boolean isLintSet(String s) {
7b24b4c32ee6 8145471: javac changes for enhanced deprecation
jjg
parents: 40772
diff changeset
   119
        // return true if either the specific option is enabled, or
7b24b4c32ee6 8145471: javac changes for enhanced deprecation
jjg
parents: 40772
diff changeset
   120
        // they are all enabled without the specific one being
7b24b4c32ee6 8145471: javac changes for enhanced deprecation
jjg
parents: 40772
diff changeset
   121
        // disabled
7b24b4c32ee6 8145471: javac changes for enhanced deprecation
jjg
parents: 40772
diff changeset
   122
        return
7b24b4c32ee6 8145471: javac changes for enhanced deprecation
jjg
parents: 40772
diff changeset
   123
            isSet(XLINT_CUSTOM, s) ||
7b24b4c32ee6 8145471: javac changes for enhanced deprecation
jjg
parents: 40772
diff changeset
   124
            (isSet(XLINT) || isSet(XLINT_CUSTOM, "all")) && isUnset(XLINT_CUSTOM, "-" + s);
7b24b4c32ee6 8145471: javac changes for enhanced deprecation
jjg
parents: 40772
diff changeset
   125
    }
7b24b4c32ee6 8145471: javac changes for enhanced deprecation
jjg
parents: 40772
diff changeset
   126
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
     * Check if the value for an undocumented option has not been set.
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   129
     */
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   130
    public boolean isUnset(String name) {
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   131
        return (values.get(name) == null);
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   132
    }
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   133
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   134
    /**
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   135
     * Check if the value for an option has not been set.
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   136
     */
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
   137
    public boolean isUnset(Option option) {
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 25874
diff changeset
   138
        return (values.get(option.primaryName) == null);
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   139
    }
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   140
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   141
    /**
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   142
     * 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
   143
     */
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
   144
    public boolean isUnset(Option option, String value) {
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 25874
diff changeset
   145
        return (values.get(option.primaryName + value) == null);
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   146
    }
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 5847
diff changeset
   147
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
    public void put(String name, String value) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        values.put(name, value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11052
diff changeset
   152
    public void put(Option option, String value) {
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 25874
diff changeset
   153
        values.put(option.primaryName, value);
10
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 void putAll(Options options) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        values.putAll(options.values);
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
    public void remove(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        values.remove(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
    public Set<String> keySet() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        return values.keySet();
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
    public int size() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
        return values.size();
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   172
    // light-weight notification mechanism
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   173
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   174
    private List<Runnable> listeners = List.nil();
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   175
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   176
    public void addListener(Runnable listener) {
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   177
        listeners = listeners.prepend(listener);
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   178
    }
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   179
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   180
    public void notifyListeners() {
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   181
        for (Runnable r: listeners)
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   182
            r.run();
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9087
diff changeset
   183
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
}