private Fixture selectFixture(String[] fixtureAndParameters) throws Throwable { List<String> values = toList(fixtureAndParameters); String name = CollectionUtil.shift(values); return systemUnderDevelopment.getFixture(name, values.toArray(new String[values.size()])); } }
@Override public Object convert(String value) throws IllegalArgumentException, SecurityException, ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { List<String> parameters = toList(ClassUtils.escapeValues(value.split(";"))); Class< ? > klass = ClassUtils.loadClass(shift(parameters)); if (expectedType != null && ! expectedType.isAssignableFrom(klass)) { throw new IllegalArgumentException("Class " + expectedType.getName() + " is not assignable from" + klass .getName()); } if (parameters.size() == 0) { return klass.newInstance(); } String[] args = parameters.toArray(new String[parameters.size()]); Constructor< ? > constructor = klass.getConstructor(args.getClass()); return constructor.newInstance(new Object[] { args }); } }
public static <C> C createInstanceFromClassNameWithArguments(ClassLoader classLoader, String classWithArguments, Class<C> expectedType) throws UndeclaredThrowableException { try { List<String> parameters = toList(escapeValues(classWithArguments.split(";"))); Class< ? > klass = ClassUtils.loadClass(classLoader, shift(parameters)); if ( ! expectedType.isAssignableFrom(klass)) { throw new IllegalArgumentException("Class " + expectedType.getName() + " is not assignable from " + klass .getName()); } if (parameters.size() == 0) { return expectedType.cast(klass.newInstance()); } String[] args = parameters.toArray(new String[parameters.size()]); Constructor< ? > constructor = klass.getConstructor(args.getClass()); return expectedType.cast(constructor.newInstance(new Object[] { args })); } catch (ClassNotFoundException e) { throw new UndeclaredThrowableException(e); } catch (InstantiationException e) { throw new UndeclaredThrowableException(e); } catch (IllegalAccessException e) { throw new UndeclaredThrowableException(e); } catch (NoSuchMethodException e) { throw new UndeclaredThrowableException(e); } catch (InvocationTargetException e) { throw new UndeclaredThrowableException(e); } }
@Override public Example addChild() { if (hasChild()) { return child.addSibling(); } if (childTags.isEmpty()) { throw new IllegalStateException("No child tag"); } List<String> moreTags = new ArrayList<String>(childTags); String childTag = firstPattern(CollectionUtil.shift(moreTags)); child = createSpecification(childTag, moreTags); return child; }
@Override public void parse(CommandLine cli, String[] args) throws InvalidOptionException, WrongOptionUsageException { List<String> tokens = CollectionUtil.toList(args); while ( ! tokens.isEmpty()) { String token = CollectionUtil.shift(tokens); if (Pattern.compile("(--.+)").matcher(token).lookingAt()) { cli.getOptionWithLongForm(token).consume(tokens); } else if (Pattern.compile("(-.)").matcher(token).lookingAt()) { cli.getOptionWithShortForm(token).consume(tokens); } else { cli.addArgument(token); } } } }
public void consume(List<String> args) throws WrongOptionUsageException { if (wantsArg() && ( args.isEmpty() )) { throw new WrongOptionUsageException(this); } value = wantsArg() ? convert(CollectionUtil.shift(args)) : true; }