void addAll(String[] unmatched) { if (setter != null) { try { setter.set(unmatched); } catch (Exception ex) { throw new PicocliException(String.format("Could not invoke setter (%s) with unmatched argument array '%s': %s", setter, Arrays.toString(unmatched), ex), ex); } } if (getter != null) { try { Collection<String> collection = getter.get(); Assert.notNull(collection, "getter returned null Collection"); collection.addAll(Arrays.asList(unmatched)); } catch (Exception ex) { throw new PicocliException(String.format("Could not add unmatched argument array '%s' to collection returned by getter (%s): %s", Arrays.toString(unmatched), getter, ex), ex); } } } }
@Test public void testUnmatchedArgsBinding_forStringArrayConsumer_exceptionsRethrownAsPicocliException() { class ThrowingSetter implements ISetter { public <T> T set(T value) { throw new RuntimeException("test"); } } try { UnmatchedArgsBinding.forStringArrayConsumer(new ThrowingSetter()).addAll(new String[0]); fail("Expected exception"); } catch (PicocliException ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Could not invoke setter ")); assertTrue(ex.getMessage(), ex.getMessage().contains(") with unmatched argument array '[]': java.lang.RuntimeException: test")); } } }
@Test public void testMethodMustBeAccessible() throws Exception { Method setX = ModelMethodBindingBean.class.getDeclaredMethod("setX", int.class); MethodBinding binding = new MethodBinding(new ModelMethodBindingBean(), setX, CommandSpec.create()); try { binding.set(1); fail("Expected exception"); } catch (PicocliException ok) { assertThat("not accessible", ok.getCause() instanceof IllegalAccessException); } }
void addAll(String[] unmatched) { if (setter != null) { try { setter.set(unmatched); } catch (Exception ex) { throw new PicocliException(String.format("Could not invoke setter (%s) with unmatched argument array '%s': %s", setter, Arrays.toString(unmatched), ex), ex); } } if (getter != null) { try { Collection<String> collection = getter.get(); Assert.notNull(collection, "getter returned null Collection"); collection.addAll(Arrays.asList(unmatched)); } catch (Exception ex) { throw new PicocliException(String.format("Could not add unmatched argument array '%s' to collection returned by getter (%s): %s", Arrays.toString(unmatched), getter, ex), ex); } } } }
private boolean canConsumeOneMapArgument(ArgSpec argSpec, Range arity, int consumed, String raw, Class<?>[] classes, ITypeConverter<?> keyConverter, ITypeConverter<?> valueConverter, String argDescription) { String[] values = argSpec.splitValue(raw, commandSpec.parser(), arity, consumed); try { for (String value : values) { String[] keyValue = splitKeyValue(argSpec, value); tryConvert(argSpec, -1, keyConverter, keyValue[0], classes[0]); tryConvert(argSpec, -1, valueConverter, keyValue[1], classes[1]); } return true; } catch (PicocliException ex) { tracer.debug("$s cannot be assigned to %s: type conversion fails: %s.%n", raw, argDescription, ex.getMessage()); return false; } }
private boolean canConsumeOneArgument(ArgSpec argSpec, Range arity, int consumed, String arg, Class<?> type, String argDescription) { ITypeConverter<?> converter = getTypeConverter(type, argSpec, 0); try { String[] values = argSpec.splitValue(trim(arg), commandSpec.parser(), arity, consumed); // if (!argSpec.acceptsValues(values.length, commandSpec.parser())) { // tracer.debug("$s would split into %s values but %s cannot accept that many values.%n", arg, values.length, argDescription); // return false; // } for (String value : values) { tryConvert(argSpec, -1, converter, value, type); } return true; } catch (PicocliException ex) { tracer.debug("$s cannot be assigned to %s: type conversion fails: %s.%n", arg, argDescription, ex.getMessage()); return false; } }
@Test public void testArgSpecSetter2WrapNonPicocliException() { final Exception expected = new Exception("boom"); ISetter setter = new ISetter() { public <T> T set(T value) throws Exception { throw expected; } }; PositionalParamSpec positional = PositionalParamSpec.builder().setter(setter).build(); try { positional.setValue("abc"); } catch (CommandLine.PicocliException ex) { assertSame(expected, ex.getCause()); } }
@Test public void testArgSpecSetterRethrowsPicocliException() { final CommandLine.PicocliException expected = new CommandLine.PicocliException("boom"); ISetter setter = new ISetter() { public <T> T set(T value) throws Exception { throw expected; } }; PositionalParamSpec positional = PositionalParamSpec.builder().setter(setter).build(); try { positional.setValue("abc"); } catch (CommandLine.PicocliException ex) { assertSame(expected, ex); } }
@Test public void testUnmatchedArgsBinding_forStringArrayConsumer_exceptionsRethrownAsPicocliException() { class ThrowingSetter implements ISetter { public <T> T set(T value) { throw new RuntimeException("test"); } } try { UnmatchedArgsBinding.forStringArrayConsumer(new ThrowingSetter()).addAll(new String[0]); fail("Expected exception"); } catch (PicocliException ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Could not invoke setter ")); assertTrue(ex.getMessage(), ex.getMessage().contains(") with unmatched argument array '[]': java.lang.RuntimeException: test")); } } }
@Test public void testArgSpecGetterWrapNonPicocliException() { final Exception expected = new Exception("boom"); IGetter getter = new IGetter() { public <T> T get() throws Exception { throw expected; } }; PositionalParamSpec positional = PositionalParamSpec.builder().getter(getter).build(); try { positional.getValue(); } catch (CommandLine.PicocliException ex) { assertSame(expected, ex.getCause()); } }
@Test public void testUnmatchedArgsBinding_forStringCollectionSupplier_exceptionsRethrownAsPicocliException() { class ThrowingGetter implements IGetter { public <T> T get() { throw new RuntimeException("test"); } } try { UnmatchedArgsBinding.forStringCollectionSupplier(new ThrowingGetter()).addAll(new String[0]); fail("Expected exception"); } catch (PicocliException ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Could not add unmatched argument array '[]' to collection returned by getter (")); assertTrue(ex.getMessage(), ex.getMessage().endsWith("): java.lang.RuntimeException: test")); } }
@Test public void testArgSpecGetterRethrowsPicocliException() { final CommandLine.PicocliException expected = new CommandLine.PicocliException("boom"); IGetter getter = new IGetter() { public <T> T get() { throw expected; } }; PositionalParamSpec positional = PositionalParamSpec.builder().getter(getter).build(); try { positional.getValue(); } catch (CommandLine.PicocliException ex) { assertSame(expected, ex); } }
@Test public void testArgSpecSetterWrapNonPicocliException() { final Exception expected = new Exception("boom"); ISetter setter = new ISetter() { public <T> T set(T value) throws Exception { throw expected; } }; PositionalParamSpec positional = PositionalParamSpec.builder().setter(setter).build(); try { positional.setValue("abc"); } catch (CommandLine.PicocliException ex) { assertSame(expected, ex.getCause()); } }
@Test public void testMethodMustBeAccessible() throws Exception { Method setX = ModelMethodBindingBean.class.getDeclaredMethod("setX", int.class); MethodBinding binding = new MethodBinding(new ModelMethodBindingBean(), setX, CommandSpec.create()); try { binding.set(1); fail("Expected exception"); } catch (PicocliException ok) { assertThat("not accessible", ok.getCause() instanceof IllegalAccessException); } }
@Test public void testFieldBindingDoesNotSetAccessible() throws Exception { Field f = ModelMethodBindingBean.class.getDeclaredField("x"); FieldBinding binding = new FieldBinding(new ModelMethodBindingBean(), f); try { binding.get(); fail("Expected exception"); } catch (PicocliException ok) { assertThat("not accessible", ok.getCause() instanceof IllegalAccessException); } }
public <T> T set(T value) throws PicocliException { try { @SuppressWarnings("unchecked") T result = (T) field.get(scope); field.set(scope, value); return result; } catch (Exception ex) { throw new PicocliException("Could not set value for field " + field + " to " + value, ex); } } }
/** Sets the value of this argument to the specified value and returns the previous value. Delegates to the current {@link #setter()}. */ public <T> T setValue(T newValue) throws PicocliException { try { return setter.set(newValue); } catch (PicocliException ex) { throw ex; } catch (Exception ex) { throw new PicocliException("Could not set value (" + newValue + ") for " + this + ": " + ex, ex); } } /** Sets the value of this argument to the specified value and returns the previous value. Delegates to the current {@link #setter()}.
/** Returns the current value of this argument. Delegates to the current {@link #getter()}. */ public <T> T getValue() throws PicocliException { try { return getter.get(); } catch (PicocliException ex) { throw ex; } catch (Exception ex) { throw new PicocliException("Could not get value for " + this + ": " + ex, ex); } } /** Sets the value of this argument to the specified value and returns the previous value. Delegates to the current {@link #setter()}. */
public <T> T get() throws PicocliException { try { @SuppressWarnings("unchecked") T result = (T) field.get(scope); return result; } catch (Exception ex) { throw new PicocliException("Could not get value for field " + field, ex); } } public <T> T set(T value) throws PicocliException {
private boolean canConsumeOneArgument(ArgSpec argSpec, Range arity, int consumed, String arg, Class<?> type, String argDescription) { ITypeConverter<?> converter = getTypeConverter(type, argSpec, 0); try { String[] values = argSpec.splitValue(trim(arg), commandSpec.parser(), arity, consumed); // if (!argSpec.acceptsValues(values.length, commandSpec.parser())) { // tracer.debug("$s would split into %s values but %s cannot accept that many values.%n", arg, values.length, argDescription); // return false; // } for (String value : values) { tryConvert(argSpec, -1, converter, value, type); } return true; } catch (PicocliException ex) { tracer.debug("$s cannot be assigned to %s: type conversion fails: %s.%n", arg, argDescription, ex.getMessage()); return false; } }