| 211 | } |
| 212 | |
| 213 | private static <T extends Enum<T>> T valueOf( |
| 214 | ProcessingEnvironment processingEnv, String key, T defaultValue, Set<T> validValues) { |
| 215 | Map<String, String> options = processingEnv.getOptions(); |
| 216 | if (options.containsKey(key)) { |
| 217 | try { |
| 218 | T type = |
| 219 | Enum.valueOf(defaultValue.getDeclaringClass(), Ascii.toUpperCase(options.get(key))); |
| 220 | if (!validValues.contains(type)) { |
| 221 | throw new IllegalArgumentException(); // let handler below print out good msg. |
| 222 | } |
| 223 | return type; |
| 224 | } catch (IllegalArgumentException e) { |
| 225 | processingEnv |
| 226 | .getMessager() |
| 227 | .printMessage( |
| 228 | Diagnostic.Kind.ERROR, |
| 229 | "Processor option -A" |
| 230 | + key |
| 231 | + " may only have the values " |
| 232 | + validValues |
| 233 | + " (case insensitive), found: " |
| 234 | + options.get(key)); |
| 235 | } |
| 236 | } |
| 237 | return defaultValue; |
| 238 | } |
| 239 | } |