in shell/console/src/main/java/org/apache/felix/gogo/commands/converter/DefaultConverter.java [138:204]
public Object convertFromString(String value, Class toType, Object loader) throws Exception {
toType = unwrap(toType);
if (ReifiedType.class == toType) {
try {
return GenericType.parse(value, loader);
} catch (ClassNotFoundException e) {
throw new Exception("Unable to convert", e);
}
} else if (Class.class == toType) {
try {
return GenericType.parse(value, loader).getRawClass();
} catch (ClassNotFoundException e) {
throw new Exception("Unable to convert", e);
}
} else if (Locale.class == toType) {
String[] tokens = value.split("_");
if (tokens.length == 1) {
return new Locale(tokens[0]);
} else if (tokens.length == 2) {
return new Locale(tokens[0], tokens[1]);
} else if (tokens.length == 3) {
return new Locale(tokens[0], tokens[1], tokens[2]);
} else {
throw new Exception("Invalid locale string:" + value);
}
} else if (Pattern.class == toType) {
return Pattern.compile(value);
} else if (Properties.class == toType) {
Properties props = new Properties();
ByteArrayInputStream in = new ByteArrayInputStream(value.getBytes(StandardCharsets.UTF_8));
props.load(in);
return props;
} else if (Boolean.class == toType) {
if ("yes".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value) || "on".equalsIgnoreCase(value)) {
return Boolean.TRUE;
} else if ("no".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value) || "off".equalsIgnoreCase(value)) {
return Boolean.FALSE;
} else {
throw new RuntimeException("Invalid boolean value: " + value);
}
} else if (Integer.class == toType) {
return Integer.valueOf(value);
} else if (Short.class == toType) {
return Short.valueOf(value);
} else if (Long.class == toType) {
return Long.valueOf(value);
} else if (Float.class == toType) {
return Float.valueOf(value);
} else if (Double.class == toType) {
return Double.valueOf(value);
} else if (Character.class == toType) {
if (value.length() == 6 && value.startsWith("\\u")) {
int code = Integer.parseInt(value.substring(2), 16);
return (char) code;
} else if (value.length() == 1) {
return value.charAt(0);
} else {
throw new Exception("Invalid value for character type: " + value);
}
} else if (Byte.class == toType) {
return Byte.valueOf(value);
} else if (Enum.class.isAssignableFrom(toType)) {
return Enum.valueOf((Class<Enum>) toType, value);
} else {
return createObject(value, toType);
}
}