Add @Options annotation

Support using @Option annotations from other objects during command
line parsing so that options can be shared by different classes
using composition instead of inheritance.

Change-Id: I949ae8387847057301e22bd2b2be77d1dc3c25aa
This commit is contained in:
Zac Livingston
2017-07-11 17:02:48 -06:00
parent 3bb66d66b0
commit 2511dd9622
3 changed files with 70 additions and 2 deletions

View File

@@ -138,7 +138,7 @@ public class DynamicOptions {
public void parseDynamicBeans(CmdLineParser clp) {
for (Entry<String, DynamicBean> e : beansByPlugin.entrySet()) {
clp.parseWithPrefix(e.getKey(), e.getValue());
clp.parseWithPrefix("--" + e.getKey(), e.getValue());
}
}

View File

@@ -48,9 +48,11 @@ import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.IllegalAnnotationError;
@@ -381,7 +383,7 @@ public class CmdLineParser {
}
private static String getPrefixedName(String prefix, String name) {
return "--" + prefix + name;
return prefix + name;
}
}
@@ -393,11 +395,19 @@ public class CmdLineParser {
MyParser(Object bean) {
super(bean);
parseAdditionalOptions("", bean, new HashSet<>());
ensureOptionsInitialized();
}
// NOTE: Argument annotations on bean are ignored.
public void parseWithPrefix(String prefix, Object bean) {
parseWithPrefix(prefix, bean, new HashSet<>());
}
private void parseWithPrefix(String prefix, Object bean, Set<Object> parsedBeans) {
if (!parsedBeans.add(bean)) {
return;
}
// recursively process all the methods/fields.
for (Class<?> c = bean.getClass(); c != null; c = c.getSuperclass()) {
for (Method m : c.getDeclaredMethods()) {
@@ -411,6 +421,31 @@ public class CmdLineParser {
if (o != null) {
addOption(Setters.create(f, bean), new PrefixedOption(prefix, o));
}
if (f.isAnnotationPresent(Options.class)) {
try {
parseWithPrefix(
prefix + f.getAnnotation(Options.class).prefix(), f.get(bean), parsedBeans);
} catch (IllegalAccessException e) {
throw new IllegalAnnotationError(e);
}
}
}
}
}
private void parseAdditionalOptions(String prefix, Object bean, Set<Object> parsedBeans) {
for (Class c = bean.getClass(); c != null; c = c.getSuperclass()) {
for (Field f : c.getDeclaredFields()) {
if (f.isAnnotationPresent(Options.class)) {
Object additionalBean = null;
try {
additionalBean = f.get(bean);
} catch (IllegalAccessException e) {
throw new IllegalAnnotationError(e);
}
parseWithPrefix(
prefix + f.getAnnotation(Options.class).prefix(), additionalBean, parsedBeans);
}
}
}
}

View File

@@ -0,0 +1,33 @@
// Copyright (C) 2017 The Android Open Source Project
//
// 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.gerrit.util.cli;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
/**
* Marks a field that refers to a class with @Option annotations
*
* <p>Any @Option annotations found on the referred class will be handled as if they were found on
* the referring class.
*/
@Retention(RUNTIME)
@Target({FIELD})
public @interface Options {
String prefix() default "";
}