1 /* 2 * Copyright (c) 2001, 2016, 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 import com.sun.tools.javac.main.Option; 30 import static com.sun.tools.javac.main.Option.*; 31 32 /** A table of all command-line options. 33 * If an option has an argument, the option name is mapped to the argument. 34 * If a set option has no argument, it is mapped to itself. 35 * 36 * <p><b>This is NOT part of any supported API. 37 * If you write code that depends on this, you do so at your own risk. 38 * This code and its internal interfaces are subject to change or 39 * deletion without notice.</b> 40 */ 41 public class Options { 42 private static final long serialVersionUID = 0; 43 44 /** The context key for the options. */ 45 public static final Context.Key<Options> optionsKey = new Context.Key<>(); 46 47 private LinkedHashMap<String,String> values; 48 49 /** Get the Options instance for this context. */ 50 public static Options instance(Context context) { 51 Options instance = context.get(optionsKey); 52 if (instance == null) 53 instance = new Options(context); 54 return instance; 55 } 56 57 protected Options(Context context) { 58 // DEBUGGING -- Use LinkedHashMap for reproducability 59 values = new LinkedHashMap<>(); 60 context.put(optionsKey, this); 61 } 62 63 /** 64 * Get the value for an undocumented option. 65 */ 66 public String get(String name) { 67 return values.get(name); 68 } 69 70 /** 71 * Get the value for an option. 72 */ 73 public String get(Option option) { 74 return values.get(option.primaryName); 75 } 76 77 /** 78 * Get the boolean value for an option, patterned after Boolean.getBoolean, 79 * essentially will return true, iff the value exists and is set to "true". 80 */ 81 public boolean getBoolean(String name) { 82 return getBoolean(name, false); 83 } 84 85 /** 86 * Get the boolean with a default value if the option is not set. 87 */ 88 public boolean getBoolean(String name, boolean defaultValue) { 89 String value = get(name); 90 return (value == null) ? defaultValue : Boolean.parseBoolean(value); 91 } 92 93 /** 94 * Check if the value for an undocumented option has been set. 95 */ 96 public boolean isSet(String name) { 97 return (values.get(name) != null); 98 } 99 100 /** 101 * Check if the value for an option has been set. 102 */ 103 public boolean isSet(Option option) { 104 return (values.get(option.primaryName) != null); 105 } 106 107 /** 108 * Check if the value for a choice option has been set to a specific value. 109 */ 110 public boolean isSet(Option option, String value) { 111 return (values.get(option.primaryName + value) != null); 112 } 113 114 /** 115 * Check if the value for an undocumented option has not been set. 116 */ 117 public boolean isUnset(String name) { 118 return (values.get(name) == null); 119 } 120 121 /** 122 * Check if the value for an option has not been set. 123 */ 124 public boolean isUnset(Option option) { 125 return (values.get(option.primaryName) == null); 126 } 127 128 /** 129 * Check if the value for a choice option has not been set to a specific value. 130 */ 131 public boolean isUnset(Option option, String value) { 132 return (values.get(option.primaryName + value) == null); 133 } 134 135 public void put(String name, String value) { 136 values.put(name, value); 137 } 138 139 public void put(Option option, String value) { 140 values.put(option.primaryName, value); 141 } 142 143 public void putAll(Options options) { 144 values.putAll(options.values); 145 } 146 147 public void remove(String name) { 148 values.remove(name); 149 } 150 151 public Set<String> keySet() { 152 return values.keySet(); 153 } 154 155 public int size() { 156 return values.size(); 157 } 158 159 // light-weight notification mechanism 160 161 private List<Runnable> listeners = List.nil(); 162 163 public void addListener(Runnable listener) { 164 listeners = listeners.prepend(listener); 165 } 166 167 public void notifyListeners() { 168 for (Runnable r: listeners) 169 r.run(); 170 } 171 172 /** Check for a lint suboption. */ 173 public boolean lint(String s) { 174 // return true if either the specific option is enabled, or 175 // they are all enabled without the specific one being 176 // disabled 177 return 178 isSet(XLINT_CUSTOM, s) || 179 (isSet(XLINT) || isSet(XLINT_CUSTOM, "all")) && 180 isUnset(XLINT_CUSTOM, "-" + s); 181 } 182 }