Options.java revision 3573:c4a18ee691c4
162053Smarkm/* 2128059Smarkm * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved. 362053Smarkm * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 462053Smarkm * 562053Smarkm * This code is free software; you can redistribute it and/or modify it 662053Smarkm * under the terms of the GNU General Public License version 2 only, as 762053Smarkm * published by the Free Software Foundation. Oracle designates this 862053Smarkm * particular file as subject to the "Classpath" exception as provided 962053Smarkm * by Oracle in the LICENSE file that accompanied this code. 1062053Smarkm * 1162053Smarkm * This code is distributed in the hope that it will be useful, but WITHOUT 1262053Smarkm * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1362053Smarkm * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1462053Smarkm * version 2 for more details (a copy is included in the LICENSE file that 1562053Smarkm * accompanied this code). 1662053Smarkm * 1762053Smarkm * You should have received a copy of the GNU General Public License version 1862053Smarkm * 2 along with this work; if not, write to the Free Software Foundation, 1962053Smarkm * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2062053Smarkm * 2162053Smarkm * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2262053Smarkm * or visit www.oracle.com if you need additional information or have any 2362053Smarkm * questions. 2462053Smarkm */ 2562053Smarkm 2662053Smarkmpackage com.sun.tools.javac.util; 2762053Smarkm 28119418Sobrienimport java.util.*; 29119418Sobrienimport com.sun.tools.javac.main.Option; 30119418Sobrienimport static com.sun.tools.javac.main.Option.*; 3162053Smarkm 3262053Smarkm/** A table of all command-line options. 3376166Smarkm * If an option has an argument, the option name is mapped to the argument. 3462053Smarkm * If a set option has no argument, it is mapped to itself. 3576166Smarkm * 3674771Smarkm * <p><b>This is NOT part of any supported API. 3762053Smarkm * If you write code that depends on this, you do so at your own risk. 3874072Smarkm * This code and its internal interfaces are subject to change or 3976166Smarkm * deletion without notice.</b> 4062053Smarkm */ 41129876Sphkpublic class Options { 4276166Smarkm private static final long serialVersionUID = 0; 4367112Smarkm 4483366Sjulian /** The context key for the options. */ 4570834Swollman public static final Context.Key<Options> optionsKey = new Context.Key<>(); 4676166Smarkm 4776166Smarkm private LinkedHashMap<String,String> values; 4867112Smarkm 4974072Smarkm /** Get the Options instance for this context. */ 5062053Smarkm public static Options instance(Context context) { 5174072Smarkm Options instance = context.get(optionsKey); 5262053Smarkm if (instance == null) 5374072Smarkm instance = new Options(context); 5462053Smarkm return instance; 55122871Smarkm } 5662053Smarkm 57128059Smarkm protected Options(Context context) { 58128059Smarkm// DEBUGGING -- Use LinkedHashMap for reproducability 59128059Smarkm values = new LinkedHashMap<>(); 60128059Smarkm context.put(optionsKey, this); 61128059Smarkm } 62122871Smarkm 6362053Smarkm /** 64128059Smarkm * Get the value for an undocumented option. 65128321Smarkm */ 66128059Smarkm public String get(String name) { 67128059Smarkm return values.get(name); 68128059Smarkm } 69128059Smarkm 70128059Smarkm /** 71128059Smarkm * Get the value for an option. 7262053Smarkm */ 7362053Smarkm public String get(Option option) { 74128059Smarkm return values.get(option.primaryName); 7574072Smarkm } 7662053Smarkm 77130585Sphk /** 7862053Smarkm * Get the boolean value for an option, patterned after Boolean.getBoolean, 79128059Smarkm * essentially will return true, iff the value exists and is set to "true". 80128059Smarkm */ 81128059Smarkm public boolean getBoolean(String name) { 8274072Smarkm return getBoolean(name, false); 8374072Smarkm } 8462053Smarkm 8591600Smarkm /** 8662053Smarkm * Get the boolean with a default value if the option is not set. 87130585Sphk */ 88128059Smarkm public boolean getBoolean(String name, boolean defaultValue) { 8969172Smarkm String value = get(name); 90128059Smarkm return (value == null) ? defaultValue : Boolean.parseBoolean(value); 91128059Smarkm } 92128059Smarkm 93128059Smarkm /** 9483976Srwatson * Check if the value for an undocumented option has been set. 95128321Smarkm */ 96128059Smarkm public boolean isSet(String name) { 9769172Smarkm return (values.get(name) != null); 9869172Smarkm } 9991600Smarkm 10069172Smarkm /** 101130585Sphk * Check if the value for an option has been set. 10262053Smarkm */ 103128059Smarkm public boolean isSet(Option option) { 104128367Smarkm return (values.get(option.primaryName) != null); 10562053Smarkm } 106128059Smarkm 107128059Smarkm /** 10867286Speter * Check if the value for a choice option has been set to a specific value. 109128059Smarkm */ 110128321Smarkm public boolean isSet(Option option, String value) { 111132346Smarkm return (values.get(option.primaryName + value) != null); 112128321Smarkm } 113128059Smarkm 114128321Smarkm /** 11567112Smarkm * Check if the value for an undocumented option has not been set. 116128059Smarkm */ 117128059Smarkm public boolean isUnset(String name) { 118128059Smarkm return (values.get(name) == null); 119128367Smarkm } 120128367Smarkm 121128367Smarkm /** 122128059Smarkm * Check if the value for an option has not been set. 123128059Smarkm */ 124128059Smarkm public boolean isUnset(Option option) { 125128059Smarkm return (values.get(option.primaryName) == null); 126128059Smarkm } 127128367Smarkm 128128367Smarkm /** 129128367Smarkm * Check if the value for a choice option has not been set to a specific value. 13067286Speter */ 131128151Smarkm public boolean isUnset(Option option, String value) { 132128059Smarkm return (values.get(option.primaryName + value) == null); 13362053Smarkm } 13462053Smarkm 13591600Smarkm public void put(String name, String value) { 13662053Smarkm values.put(name, value); 137130585Sphk } 13862053Smarkm 139128059Smarkm public void put(Option option, String value) { 140128367Smarkm values.put(option.primaryName, value); 14162053Smarkm } 142128367Smarkm 143128367Smarkm public void putAll(Options options) { 14462053Smarkm values.putAll(options.values); 145128059Smarkm } 14662765Smarkm 14762053Smarkm public void remove(String name) { 14862053Smarkm values.remove(name); 149128059Smarkm } 15062053Smarkm 151128321Smarkm public Set<String> keySet() { 152128367Smarkm return values.keySet(); 153128367Smarkm } 154128059Smarkm 15562053Smarkm public int size() { 15662053Smarkm return values.size(); 15791600Smarkm } 15862053Smarkm 159130585Sphk // light-weight notification mechanism 16091600Smarkm 16165686Smarkm private List<Runnable> listeners = List.nil(); 162128059Smarkm 163128059Smarkm public void addListener(Runnable listener) { 16474771Smarkm listeners = listeners.prepend(listener); 165128059Smarkm } 16674771Smarkm 16774771Smarkm public void notifyListeners() { 168128059Smarkm for (Runnable r: listeners) 16974771Smarkm r.run(); 170128059Smarkm } 17174771Smarkm 172128059Smarkm /** Check for a lint suboption. */ 17365686Smarkm public boolean lint(String s) { 17465686Smarkm // return true if either the specific option is enabled, or 17591600Smarkm // they are all enabled without the specific one being 17665686Smarkm // disabled 177130585Sphk return 17867112Smarkm isSet(XLINT_CUSTOM, s) || 179128059Smarkm (isSet(XLINT) || isSet(XLINT_CUSTOM, "all")) && 18067112Smarkm isUnset(XLINT_CUSTOM, "-" + s); 18167112Smarkm } 18274072Smarkm} 18367112Smarkm