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