Skip to content

Commit

Permalink
#83 additional built-in type converters: fixes for java.nio.file.Path
Browse files Browse the repository at this point in the history
  • Loading branch information
remkop committed Dec 19, 2017
1 parent 87e7f45 commit 4991e65
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -2001,7 +2001,7 @@ private class Interpreter {
BuiltIn.registerIfAvailable(converterRegistry, tracer, "java.time.ZoneId", "of", String.class);
BuiltIn.registerIfAvailable(converterRegistry, tracer, "java.time.ZoneOffset", "of", String.class);

BuiltIn.registerIfAvailable(converterRegistry, tracer, "java.nio.file.Paths", "get", String.class, String[].class);
BuiltIn.registerIfAvailable(converterRegistry, tracer, "java.nio.file.Path", "java.nio.file.Paths", "get", String.class, String[].class);

this.command = Assert.notNull(command, "command");
Class<?> cls = command.getClass();
Expand Down Expand Up @@ -2960,22 +2960,35 @@ static class DriverConverter implements ITypeConverter<Driver> {
static class TimestampConverter implements ITypeConverter<Timestamp> {
public Timestamp convert(String s) throws Exception { return Timestamp.valueOf(s); }
}
static void registerIfAvailable(Map<Class<?>, ITypeConverter<?>> registry, Tracer tracer, String fqcn, String methodName, Class<?>... paramTypes) {
static void registerIfAvailable(Map<Class<?>, ITypeConverter<?>> registry, Tracer tracer, String fqcn, String factoryMethodName, Class<?>... paramTypes) {
registerIfAvailable(registry, tracer, fqcn, fqcn, factoryMethodName, paramTypes);
}
static void registerIfAvailable(Map<Class<?>, ITypeConverter<?>> registry, Tracer tracer, String fqcn, String factoryClass, String factoryMethodName, Class<?>... paramTypes) {
try {
Class<?> cls = Class.forName(fqcn);
Method method = cls.getDeclaredMethod(methodName, paramTypes);
registry.put(cls, new ReflectionConverter(method));
Class<?> factory = Class.forName(factoryClass);
Method method = factory.getDeclaredMethod(factoryMethodName, paramTypes);
registry.put(cls, new ReflectionConverter(method, paramTypes));
} catch (Exception e) {
tracer.info("Could not register converter %s.%s: %s%n", fqcn, methodName, e.toString());
tracer.info("Could not register converter for %s: %s%n", fqcn, e.toString());
}
}
static class ReflectionConverter implements ITypeConverter<Object> {
private final Method method;
public ReflectionConverter(Method method) { this.method = Assert.notNull(method, "method"); }
private Class<?>[] paramTypes;

public ReflectionConverter(Method method, Class<?>... paramTypes) {
this.method = Assert.notNull(method, "method");
this.paramTypes = Assert.notNull(paramTypes, "paramTypes");
}

public Object convert(String s) {
try {
return method.invoke(null, s);
if (paramTypes.length > 1) {
return method.invoke(null, s, new String[0]);
} else {
return method.invoke(null, s);
}
} catch (Exception e) {
throw new TypeConversionException("Unable to convert " + s + " to " + method.getReturnType() + ": " + e.getMessage());
}
Expand Down

0 comments on commit 4991e65

Please sign in to comment.