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