src/main/java/org/deephacks/tools4j/cli/Command.java
private List<Object> adjustArgs(List<String> args, Method m) {
    final List<Object> result = new ArrayList<Object>();
    final Class<?>[] types = m.getParameterTypes();
    if (m.isVarArgs()) {
      types[types.length - 1] = types[types.length - 1].getComponentType();
      if (!String.class.isAssignableFrom(types[types.length - 1])) {
        throw new CliException("Only String varargs is supported.");
      }
      types[types.length - 1] = String.class;
    }

    List<Object> varargs = new ArrayList<>();
    for (int i = 0; i < args.size(); i++) {
      try {
        if (m.isVarArgs() && i >= types.length - 1) {
          varargs.add(c.convert(args.get(i), types[types.length - 1]));
        } else {
          result.add(c.convert(args.get(i), types[i]));
        }
      } catch (ConversionException e) {
        throw CliException.WRONG_ARG_TYPE(getArguments().get(i).getName(),
                types[i].getName(), args.get(i));
      }
    }
    if (m.isVarArgs()) {
      result.add(varargs.toArray(new String[0]));
    }
    return result;
  }
private List<Object> adjustArgs(List<String> args, Method m) {
    final List<Object> result = new ArrayList<Object>();
    final Class<?>[] types = m.getParameterTypes();
    if (m.isVarArgs()) {
      types[types.length - 1] = types[types.length - 1].getComponentType();
      if (!String.class.isAssignableFrom(types[types.length - 1])) {
        throw new CliException("Only String varargs is supported.");
      }
      types[types.length - 1] = String.class;
    }

    List<Object> varargs = new ArrayList<>();
    for (int i = 0; i < args.size(); i++) {
      try {
        if (m.isVarArgs() && i >= types.length - 1) {
          varargs.add(c.convert(args.get(i), types[types.length - 1]));
        } else {
          result.add(c.convert(args.get(i), types[i]));
        }
      } catch (ConversionException e) {
        throw CliException.WRONG_ARG_TYPE(getArguments().get(i).getName(),
                types[i].getName(), args.get(i));
      }
    }
    if (m.isVarArgs()) {
      result.add(varargs.toArray(new String[0]));
    }
    return result;
  }
private List < Object > adjustArgs ( List < String > args , Method m ) { final List < Object > result = new ArrayList < Object > ( ) ; final Class < ? > [ ] types = m . getParameterTypes ( ) ; if ( m . isVarArgs ( ) ) { types [ types . length - 1 ] = types [ types . length - 1 ] . getComponentType ( ) ; if ( ! String . class . isAssignableFrom ( types [ types . length - 1 ] ) ) { throw new CliException ( "Only String varargs is supported." ) ; } types [ types . length - 1 ] = String . class ; } List < Object > varargs = new ArrayList <> ( ) ; for ( int i = 0 ; i < args . size ( ) ; i ++ ) { try { if ( m . isVarArgs ( ) && i >= types . length - 1 ) { varargs . add ( c . convert ( args . get ( i ) , types [ types . length - 1 ] ) ) ; } else { result . add ( c . convert ( args . get ( i ) , types [ i ] ) ) ; } } catch ( ConversionException e ) { throw CliException . WRONG_ARG_TYPE ( getArguments ( ) . get ( i ) . getName ( ) , types [ i ] . getName ( ) , args . get ( i ) ) ; } } if ( m . isVarArgs ( ) ) { result . add ( varargs . toArray ( new String [ 0 ] ) ) ; } return result ; }
Add or remove parameters to fit the method declaration and also convert them
to appropriate data type.
Add or remove parameters to fit the method declaration and also convert them to appropriate data type .