use of org.springframework.core.convert.ConversionFailedException in project centipede by paulhoule.
the class OptionParser method parse.
public HasOptions parse(Iterable<String> args) throws IllegalAccessException {
HasOptions options;
try {
options = (HasOptions) that.getConstructor().newInstance();
} catch (NoSuchMethodException ex) {
throw new IllegalArgumentException("Class " + that + " doesn't have a zero argument constructor", ex);
} catch (IllegalAccessException ex) {
throw new IllegalArgumentException("Class " + that + " has a non-public zero argument constructor", ex);
} catch (InstantiationException ex) {
throw new IllegalArgumentException("Class " + that + " cannot be abstract", ex);
} catch (InvocationTargetException ex) {
throw new IllegalArgumentException("Class " + that + " threw an exception during construction", ex);
}
Map<String, RWOption> lookup = getStringAnnotationMap(that);
for (RWOption o : lookup.values()) {
if (isSomeKindOfBoolean(o)) {
o.getField().setBoolean(options, false);
} else {
Object defaultValue = null;
if (!o.getDefaultValue().isEmpty()) {
try {
defaultValue = conversionService.convert(o.getDefaultValue(), TypeDescriptor.valueOf(String.class), new TypeDescriptor(o.getField()));
} catch (ConversionFailedException x) {
throw new UnparsableDefaultException(o.getName(), o.getDefaultValue(), o.getType(), x);
}
} else {
defaultValue = defaultValueFor(o.getType());
}
o.getField().set(options, defaultValue);
}
}
Set<Field> sawOption = new HashSet<>();
Iterator<String> p = args.iterator();
String peek = null;
while (p.hasNext()) {
peek = p.next();
if (!peek.startsWith("-"))
break;
String name = peek.substring(1);
if (!lookup.containsKey(name))
throw new InvalidOptionException("invalid option :" + name);
RWOption field = lookup.get(name);
sawOption.add(field.getField());
if (isSomeKindOfBoolean(field)) {
field.getField().setBoolean(options, true);
} else {
String value = p.next();
try {
if (field.isList()) {
String[] parts = value.split(",");
Class elementType = field.getElementType();
for (String part : parts) {
final Object innerValue = field.convertFrom(options, conversionService, part);
((List) field.getField().get(options)).add(innerValue);
}
} else {
final Object innerValue = field.convertFrom(options, conversionService, value);
field.getField().set(options, innerValue);
}
} catch (ConversionFailedException x) {
throw new UnparsableOptionException(name, value, field.getType(), x);
}
}
peek = null;
}
for (RWOption o : lookup.values()) {
if (o.isRequired() && !sawOption.contains(o.getField()))
throw new MissingOptionException("Required option -" + o.getName() + " is missing");
}
List<String> positional = new ArrayList<String>();
if (peek != null)
positional.add(peek);
while (p.hasNext()) positional.add(p.next());
Field positionalField = findPositionalParameter(that);
if (positionalField != null)
positionalField.set(options, positional);
return options;
}
use of org.springframework.core.convert.ConversionFailedException in project spring-framework by spring-projects.
the class AbstractPropertyAccessorTests method setPropertyIntermediateListIsNullWithBadConversionService.
@Test
public void setPropertyIntermediateListIsNullWithBadConversionService() {
Foo target = new Foo();
AbstractPropertyAccessor accessor = createAccessor(target);
accessor.setConversionService(new GenericConversionService() {
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
throw new ConversionFailedException(sourceType, targetType, source, null);
}
});
accessor.setAutoGrowNestedPaths(true);
accessor.setPropertyValue("listOfMaps[0]['luckyNumber']", "9");
assertEquals("9", target.listOfMaps.get(0).get("luckyNumber"));
}
use of org.springframework.core.convert.ConversionFailedException in project spring-framework by spring-projects.
the class ObjectToObjectConverter method convert.
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (source == null) {
return null;
}
Class<?> sourceClass = sourceType.getType();
Class<?> targetClass = targetType.getType();
Member member = getValidatedMember(targetClass, sourceClass);
try {
if (member instanceof Method) {
Method method = (Method) member;
ReflectionUtils.makeAccessible(method);
if (!Modifier.isStatic(method.getModifiers())) {
return method.invoke(source);
} else {
return method.invoke(null, source);
}
} else if (member instanceof Constructor) {
Constructor<?> ctor = (Constructor<?>) member;
ReflectionUtils.makeAccessible(ctor);
return ctor.newInstance(source);
}
} catch (InvocationTargetException ex) {
throw new ConversionFailedException(sourceType, targetType, source, ex.getTargetException());
} catch (Throwable ex) {
throw new ConversionFailedException(sourceType, targetType, source, ex);
}
// method or Integer(java.lang.Number) constructor exists on java.lang.Integer.
throw new IllegalStateException(String.format("No to%3$s() method exists on %1$s, " + "and no static valueOf/of/from(%1$s) method or %3$s(%1$s) constructor exists on %2$s.", sourceClass.getName(), targetClass.getName(), targetClass.getSimpleName()));
}
use of org.springframework.core.convert.ConversionFailedException in project spring-framework by spring-projects.
the class MapToMapConverterTests method scalarMap.
@Test
public void scalarMap() throws Exception {
Map<String, String> map = new HashMap<>();
map.put("1", "9");
map.put("2", "37");
TypeDescriptor sourceType = TypeDescriptor.forObject(map);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));
assertTrue(conversionService.canConvert(sourceType, targetType));
try {
conversionService.convert(map, sourceType, targetType);
} catch (ConversionFailedException ex) {
assertTrue(ex.getCause() instanceof ConverterNotFoundException);
}
conversionService.addConverterFactory(new StringToNumberConverterFactory());
assertTrue(conversionService.canConvert(sourceType, targetType));
@SuppressWarnings("unchecked") Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType);
assertFalse(map.equals(result));
assertEquals((Integer) 9, result.get(1));
assertEquals((Integer) 37, result.get(2));
}
use of org.springframework.core.convert.ConversionFailedException in project spring-framework by spring-projects.
the class MapToMapConverterTests method collectionMap.
@Test
public void collectionMap() throws Exception {
Map<String, List<String>> map = new HashMap<>();
map.put("1", Arrays.asList("9", "12"));
map.put("2", Arrays.asList("37", "23"));
TypeDescriptor sourceType = TypeDescriptor.forObject(map);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget"));
assertTrue(conversionService.canConvert(sourceType, targetType));
try {
conversionService.convert(map, sourceType, targetType);
} catch (ConversionFailedException ex) {
assertTrue(ex.getCause() instanceof ConverterNotFoundException);
}
conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
conversionService.addConverterFactory(new StringToNumberConverterFactory());
assertTrue(conversionService.canConvert(sourceType, targetType));
@SuppressWarnings("unchecked") Map<Integer, List<Integer>> result = (Map<Integer, List<Integer>>) conversionService.convert(map, sourceType, targetType);
assertFalse(map.equals(result));
assertEquals(Arrays.asList(9, 12), result.get(1));
assertEquals(Arrays.asList(37, 23), result.get(2));
}
Aggregations