@SuppressWarnings("unchecked") private void parseAdditionalOptions(ClassParser classParser, Object bean, Set<Class<?>> visited) { // The top-level bean was already parsed by the superclass constructor, // so an empty visited set means we're parsing the top-level bean. if (!visited.isEmpty()) { classParser.parse(bean, this); // 'Parse' the class of the bean looking for annotations. } Class<?> beanClass = bean.getClass(); if (visited.contains(beanClass)) { throw new IllegalAnnotationError(beanClass.getCanonicalName() + " used more than once."); } else { visited.add(beanClass); } for (Field f : beanClass.getDeclaredFields()) { if (f.isAnnotationPresent(AdditionalOptions.class)) { try { // TODO(user): nicer to do this lazily in parseArgument() Object fieldValue = f.getType().newInstance(); Setters.create(f, bean).addValue(fieldValue); parseAdditionalOptions(classParser, fieldValue, visited); } catch (Exception e) { throw Throwables.propagate(e); } } } }
@SuppressWarnings("unchecked") private void parseAdditionalOptions(ClassParser classParser, Object bean, Set<Class<?>> visited) { // The top-level bean was already parsed by the superclass constructor, // so an empty visited set means we're parsing the top-level bean. if (!visited.isEmpty()) { classParser.parse(bean, this); // 'Parse' the class of the bean looking for annotations. } Class<?> beanClass = bean.getClass(); if (visited.contains(beanClass)) { throw new IllegalAnnotationError(beanClass.getCanonicalName() + " used more than once."); } else { visited.add(beanClass); } for (Field f : beanClass.getDeclaredFields()) { if (f.isAnnotationPresent(AdditionalOptions.class)) { try { // TODO(vlada): nicer to do this lazily in parseArgument() Object fieldValue = f.getType().newInstance(); Setters.create(f, bean).addValue(fieldValue); parseAdditionalOptions(classParser, fieldValue, visited); } catch (CmdLineException | IllegalAccessException | InstantiationException e) { throw new RuntimeException(e); } } } }
/** * Creates a new command line owner that parses arguments/options and set them into * the given object. * * @param bean * instance of a class annotated by {@link Option}, {@link Argument} * and {@link AdditionalOptions}. * This object will receive values. If this is null, the processing will * be skipped, which is useful if you'd like to feed metadata from other sources. * * @throws IllegalAnnotationError * if the option bean class is using args4j annotations incorrectly. * @see CmdLineParser#CmdLineParser(Object) */ public CmdLineParserAdditionalOptions(Object bean) { super(bean); if (bean != null) { Set<Class<?>> visited = Sets.newHashSet(); parseAdditionalOptions(new ClassParser(), bean, visited); } }
/** * Creates a new command line owner that parses arguments/options and set them into the given * object. * * @param bean instance of a class annotated by {@link Option}, {@link Argument} and {@link * AdditionalOptions}. This object will receive values. If this is null, the processing will * be skipped, which is useful if you'd like to feed metadata from other sources. * @throws IllegalAnnotationError if the option bean class is using args4j annotations * incorrectly. * @see CmdLineParser#CmdLineParser(Object) */ public AdditionalOptionsCmdLineParser(Object bean) { super(bean); Set<Class<?>> visited = new HashSet<>(); parseAdditionalOptions(new ClassParser(), bean, visited); }