aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/google/devtools/common')
-rw-r--r--src/main/java/com/google/devtools/common/options/BoolOrEnumConverter.java62
-rw-r--r--src/main/java/com/google/devtools/common/options/EnumConverter.java6
-rw-r--r--src/main/java/com/google/devtools/common/options/OptionsParserImpl.java12
3 files changed, 72 insertions, 8 deletions
diff --git a/src/main/java/com/google/devtools/common/options/BoolOrEnumConverter.java b/src/main/java/com/google/devtools/common/options/BoolOrEnumConverter.java
new file mode 100644
index 0000000000..1a11ee8a18
--- /dev/null
+++ b/src/main/java/com/google/devtools/common/options/BoolOrEnumConverter.java
@@ -0,0 +1,62 @@
+// Copyright 2014 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.common.options;
+
+import com.google.devtools.common.options.Converters.BooleanConverter;
+
+/**
+ * Converter that can also convert from booleans and enumerations.
+ *
+ * <p> This is able to additionally convert from the standard set of
+ * boolean string values. If there is an overlap in values, those from
+ * the underlying enumeration will be taken.
+ */
+public abstract class BoolOrEnumConverter<T extends Enum<T>> extends EnumConverter<T>{
+
+ private T falseValue;
+ private T trueValue;
+
+ /**
+ * You *must* implement a zero-argument constructor that delegates
+ * to this constructor, passing in the appropriate parameters. This
+ * comes from the base {@link EnumConverter} class.
+ *
+ * @param enumType The type of your enumeration; usually a class literal
+ * like MyEnum.class
+ * @param typeName The intuitive name of your enumeration, for example, the
+ * type name for CompilationMode might be "compilation mode".
+ * @param trueValue The enumeration value to associate with {@code true}.
+ * @param falseValue The enumeration value to associate with {@code false}.
+ */
+ protected BoolOrEnumConverter(Class<T> enumType, String typeName, T trueValue, T falseValue) {
+ super(enumType, typeName);
+ this.trueValue = trueValue;
+ this.falseValue = falseValue;
+ }
+
+ public T convert(String input) throws OptionsParsingException {
+ try {
+ return super.convert(input);
+ } catch (OptionsParsingException eEnum) {
+ try {
+ BooleanConverter booleanConverter = new BooleanConverter();
+ boolean value = booleanConverter.convert(input);
+ return value ? trueValue : falseValue;
+ } catch (OptionsParsingException eBoolean) {
+ throw eEnum;
+ }
+ }
+ }
+}
diff --git a/src/main/java/com/google/devtools/common/options/EnumConverter.java b/src/main/java/com/google/devtools/common/options/EnumConverter.java
index f65241a214..a1b72aed1a 100644
--- a/src/main/java/com/google/devtools/common/options/EnumConverter.java
+++ b/src/main/java/com/google/devtools/common/options/EnumConverter.java
@@ -19,10 +19,10 @@ import java.util.Arrays;
/**
* A converter superclass for converters that parse enums.
*
- * Just subclass this class, creating a zero aro argument constructor that
+ * <p>Just subclass this class, creating a zero aro argument constructor that
* calls {@link #EnumConverter(Class, String)}.
*
- * This class compares the input string to the string returned by the toString()
+ * <p>This class compares the input string to the string returned by the toString()
* method of each enum member in a case-insensitive way. Usually, this is the
* name of the symbol, but beware if you override toString()!
*/
@@ -51,7 +51,7 @@ public abstract class EnumConverter<T extends Enum<T>>
* Implements {@link #convert(String)}.
*/
@Override
- public final T convert(String input) throws OptionsParsingException {
+ public T convert(String input) throws OptionsParsingException {
for (T value : enumType.getEnumConstants()) {
if (value.toString().equalsIgnoreCase(input)) {
return value;
diff --git a/src/main/java/com/google/devtools/common/options/OptionsParserImpl.java b/src/main/java/com/google/devtools/common/options/OptionsParserImpl.java
index e339dcd7f2..4cd504b81e 100644
--- a/src/main/java/com/google/devtools/common/options/OptionsParserImpl.java
+++ b/src/main/java/com/google/devtools/common/options/OptionsParserImpl.java
@@ -421,8 +421,8 @@ class OptionsParserImpl {
}
}
- private void addListValue(Field field, String name, Object value,
- OptionPriority priority, String source, String implicitDependant, String expandedFrom) {
+ private void addListValue(Field field, Object value, OptionPriority priority, String source,
+ String implicitDependant, String expandedFrom) {
ParsedOptionEntry entry = parsedValues.get(field);
if (entry == null) {
entry = new ParsedOptionEntry(ArrayListMultimap.create(), priority, source,
@@ -609,8 +609,8 @@ class OptionsParserImpl {
// Note: The type of the list member is not known; Java introspection
// only makes it available in String form via the signature string
// for the field declaration.
- addListValue(field, originalName, convertedValue,
- priority, sourceFunction.apply(originalName), implicitDependant, expandedFrom);
+ addListValue(field, convertedValue, priority, sourceFunction.apply(originalName),
+ implicitDependant, expandedFrom);
}
}
@@ -683,7 +683,9 @@ class OptionsParserImpl {
}
static boolean isBooleanField(Field field) {
- return field.getType().equals(boolean.class) || field.getType().equals(TriState.class);
+ return field.getType().equals(boolean.class)
+ || field.getType().equals(TriState.class)
+ || findConverter(field) instanceof BoolOrEnumConverter;
}
static boolean isSpecialNullDefault(String defaultValueString, Field optionField) {