Пример #1
0
  private static void applyParameters(JSAPResult args, Object o, Class<?> clazz)
      throws ReflectiveOperationException {
    for (Field field : clazz.getFields()) {
      if (Modifier.isPublic(field.getModifiers())) {
        String fieldName = field.getName().toLowerCase();
        if (args.contains(fieldName)) {
          Object parameterValue = args.getObject(fieldName);
          ReflectionUtils.setFieldFromObject(o, field, parameterValue);

          LOGGER.info("Setting " + fieldName + " to '" + parameterValue + "'.");
        } else {
          LOGGER.warn("The field " + fieldName + " has not been set by command line arguments.");
        }
      }
    }
  }
Пример #2
0
  @SuppressWarnings("unchecked")
  public static <T> JsapResultsWithObject<T> constructObject(
      Class<T> clazz, String[] rawArguments, String mainHelp, Parameter[] otherParameters)
      throws JSAPException, IOException, IllegalArgumentException, ReflectiveOperationException {
    Constructor<T> constructor = null;
    for (Constructor<?> constr : clazz.getConstructors()) {
      boolean annotationPresent = constr.isAnnotationPresent(CommandLine.class);
      if (annotationPresent) {
        constructor = (Constructor<T>) constr;
        break;
      }
    }
    if (constructor == null)
      throw new IllegalAccessError(
          "Class " + clazz + " must have a constructor annotated with @CommandLine");

    String[] argNames = constructor.getAnnotation(CommandLine.class).argNames();
    if (argNames.length != constructor.getParameterCount())
      throw new IllegalAnnotationException(
          "Annotation @CommandLine argNames are out of sync with the constructor arguments.");

    boolean[] isSerializedFile = new boolean[argNames.length];

    SimpleJSAP jsap = new SimpleJSAP(clazz.getName(), mainHelp, otherParameters);
    int i = 0;
    for (java.lang.reflect.Parameter x : constructor.getParameters()) {
      Parameter option;
      if (x.getType().equals(boolean.class)) {
        isSerializedFile[i] = false;
        option =
            new Switch(
                argNames[i],
                JSAP.NO_SHORTFLAG,
                argNames[i],
                "Set the value of " + argNames[i] + " for " + clazz.getSimpleName() + " as true.");
      } else {
        StringParser parser;
        String help;
        try {
          parser = getParserFor(x.getType());
          isSerializedFile[i] = false;
          help = "The " + x.getType().getSimpleName() + " value of " + argNames[i];
        } catch (NoJSAPParserForThisTypeException e) {
          parser = JSAP.STRING_PARSER;
          isSerializedFile[i] = true;
          help =
              "A serialized " + x.getType().getSimpleName() + " file to initialize " + argNames[i];
        }
        option = new UnflaggedOption(argNames[i], parser, JSAP.REQUIRED, help);
      }

      jsap.registerParameter(option);

      i++;
    }

    JSAPResult args = jsap.parse(rawArguments);
    if (jsap.messagePrinted()) System.exit(1);

    LOGGER.info("Initializing...");
    Object[] arguments = new Object[argNames.length];
    i = 0;
    for (String argName : argNames) {
      if (isSerializedFile[i]) arguments[i] = SerializationUtils.read(args.getString(argName));
      else arguments[i] = args.getObject(argName);
      i++;
    }
    T object = constructor.newInstance(arguments);
    LOGGER.info("Ready.");

    return new JsapResultsWithObject<T>(args, object);
  }
Пример #3
0
  public static void main(final String[] arg)
      throws IOException, JSAPException, NoSuchMethodException {

    final SimpleJSAP jsap =
        new SimpleJSAP(
            BloomFilter.class.getName(),
            "Creates a Bloom filter reading from standard input a newline-separated list of terms.",
            new Parameter[] {
              new FlaggedOption(
                  "bufferSize",
                  IntSizeStringParser.getParser(),
                  "64Ki",
                  JSAP.NOT_REQUIRED,
                  'b',
                  "buffer-size",
                  "The size of the I/O buffer used to read terms."),
              new FlaggedOption(
                  "encoding",
                  ForNameStringParser.getParser(Charset.class),
                  "UTF-8",
                  JSAP.NOT_REQUIRED,
                  'e',
                  "encoding",
                  "The term file encoding."),
              new UnflaggedOption(
                  "bloomFilter",
                  JSAP.STRING_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.REQUIRED,
                  JSAP.NOT_GREEDY,
                  "The filename for the serialised front-coded list."),
              new UnflaggedOption(
                  "size",
                  JSAP.INTSIZE_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.REQUIRED,
                  JSAP.NOT_GREEDY,
                  "The size of the filter (i.e., the expected number of elements in the filter; usually, the number of terms)."),
              new UnflaggedOption(
                  "precision",
                  JSAP.INTEGER_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.REQUIRED,
                  JSAP.NOT_GREEDY,
                  "The precision of the filter.")
            });

    JSAPResult jsapResult = jsap.parse(arg);
    if (jsap.messagePrinted()) return;

    final int bufferSize = jsapResult.getInt("bufferSize");
    final String filterName = jsapResult.getString("bloomFilter");
    final Charset encoding = (Charset) jsapResult.getObject("encoding");

    BloomFilter filter = new BloomFilter(jsapResult.getInt("size"), jsapResult.getInt("precision"));
    final ProgressLogger pl = new ProgressLogger();
    pl.itemsName = "terms";
    pl.start("Reading terms...");
    MutableString s = new MutableString();
    FastBufferedReader reader =
        new FastBufferedReader(new InputStreamReader(System.in, encoding), bufferSize);
    while (reader.readLine(s) != null) {
      filter.add(s);
      pl.lightUpdate();
    }
    pl.done();

    BinIO.storeObject(filter, filterName);
  }