Пример #1
0
  /**
   * Get the field type of a Field instance.
   *
   * @param field the field instance we want the type for.
   * @return the type of the {@code field} in question.
   */
  private static FieldType fieldTypeOf(Field field, Flag flag) {
    if (field.getType().isAssignableFrom(Long.TYPE)
        || field.getType().isAssignableFrom(Long.class)) {
      return FieldType.LONG;
    }

    if (field.getType().isAssignableFrom(Boolean.TYPE)
        || field.getType().isAssignableFrom(Boolean.class)) {
      return FieldType.BOOLEAN;
    }

    if (field.getType().isAssignableFrom(String.class)) {
      return FieldType.STRING;
    }

    if (field.getType().isAssignableFrom(Integer.TYPE)
        || field.getType().isAssignableFrom(Integer.class)) {
      return FieldType.INTEGER;
    }

    if (flag.options() != NoOption.class && field.getType().isAssignableFrom(flag.options())) {
      return FieldType.ENUM;
    }

    return FieldType.UNKNOWN;
  }
Пример #2
0
  private Flags loadOpts(Object o, boolean instanced) {
    final Field[] declaredFields;
    Class<?> c = null;
    if (instanced) {
      declaredFields = o.getClass().getDeclaredFields();
    } else {
      c = ((Class<?>) o);
      declaredFields = c.getDeclaredFields();
    }

    for (Field field : declaredFields) {
      Flag flag = field.getAnnotation(Flag.class);
      // Check if we found a flag annotation for this field.
      if (null == flag) {
        continue;
      }

      // Flag fields must be static if you are initializing the flags through a Class instance.
      if (!instanced && !Modifier.isStatic(field.getModifiers())) {
        throw new IllegalArgumentException(
            "Field "
                + field.toGenericString()
                + " is not static. Flag fields "
                + "must be static when initializing through a Class instance.");
      }

      String name = flag.name();
      String description = flag.description();

      // Determine the type of field
      FieldType type = fieldTypeOf(field, flag);

      switch (type) {
        case INTEGER:
          OptionSpec<Integer> intOption;
          if (flag.required()) {
            intOption =
                optionParser.accepts(name, description).withRequiredArg().ofType(Integer.class);
          } else {
            intOption =
                optionParser.accepts(name, description).withOptionalArg().ofType(Integer.class);
          }
          if (instanced) {
            addInstancedOption(type, flag, field, intOption, o);
          } else {
            addOption(type, flag, field, intOption, c);
          }
          break;

        case STRING:
          OptionSpec<String> stringOption;
          if (flag.required()) {
            stringOption =
                optionParser.accepts(name, description).withRequiredArg().ofType(String.class);
          } else {
            stringOption =
                optionParser.accepts(name, description).withOptionalArg().ofType(String.class);
          }
          if (instanced) {
            addInstancedOption(type, flag, field, stringOption, o);
          } else {
            addOption(type, flag, field, stringOption, c);
          }
          break;

        case BOOLEAN:
          OptionSpec<Boolean> booleanOption;
          if (flag.required()) {
            booleanOption =
                optionParser.accepts(name, description).withOptionalArg().ofType(Boolean.class);
          } else {
            booleanOption =
                optionParser.accepts(name, description).withOptionalArg().ofType(Boolean.class);
          }
          if (instanced) {
            addInstancedOption(type, flag, field, booleanOption, o);
          } else {
            addOption(type, flag, field, booleanOption, c);
          }
          break;

        case LONG:
          OptionSpec<Long> longOption;
          if (flag.required()) {
            longOption =
                optionParser.accepts(name, description).withRequiredArg().ofType(Long.class);
          } else {
            longOption =
                optionParser.accepts(name, description).withOptionalArg().ofType(Long.class);
          }
          if (instanced) {
            addInstancedOption(type, flag, field, longOption, o);
          } else {
            addOption(type, flag, field, longOption, c);
          }
          break;

        case ENUM:
          Class<? extends Enum<?>> enumClass = flag.options();
          Object[] enumConstants = enumClass.getEnumConstants();
          if (enumConstants == null) {
            throw new IllegalArgumentException(
                "Field " + field.toGenericString() + " is not an enum type.");
          }
          for (Object object : enumConstants) {
            addEnumOption(enumClass, object.toString());
          }
          OptionSpec<?> enumOption;
          if (flag.required()) {
            enumOption =
                optionParser.accepts(name, description).withRequiredArg().ofType(enumClass);
          } else {
            enumOption =
                optionParser.accepts(name, description).withOptionalArg().ofType(enumClass);
          }
          if (instanced) {
            addInstancedOption(type, flag, field, enumOption, o);
          } else {
            addOption(type, flag, field, enumOption, c);
          }
          break;

        case UNKNOWN:
        default:
          throw new IllegalArgumentException(
              "Field " + field.toGenericString() + " is not of a supported type.");
      }
    }
    return this;
  }