in fastmodel-core/src/main/java/com/aliyun/fastmodel/core/tree/util/PropertyUtil.java [114:194]
public static <T> T toObject(List<Property> properties, Class<T> clazz) throws PropertyConvertException {
Map<String, BaseLiteral> baseLiteralMap = Maps.newHashMap();
for (Property p : properties) {
baseLiteralMap.put(p.getName().toUpperCase(), p.getValueLiteral());
}
Field[] declaredFields = clazz.getDeclaredFields();
try {
T t = clazz.newInstance();
for (Field f : declaredFields) {
Class<?> type = f.getType();
String s = UnderLineUtils.humpToUnderLine(f.getName());
BaseLiteral baseLiteral = baseLiteralMap.get(s.toUpperCase());
if (baseLiteral == null) {
continue;
}
String prefix = "set";
if (type == String.class) {
if (!(baseLiteral instanceof StringLiteral)) {
continue;
}
StringLiteral stringLiteral = (StringLiteral)baseLiteral;
Method m = clazz.getDeclaredMethod(prefix + StringUtils.capitalize(f.getName()), String.class);
m.invoke(t, new Object[] {stringLiteral.getValue()});
} else if (type == Long.class || type == long.class || type == Integer.class || type == int.class) {
if (baseLiteral.getClass() != LongLiteral.class) {
continue;
}
Long literal = ((LongLiteral)baseLiteral).getValue();
if (type == Long.class || type == long.class) {
Method m = clazz.getDeclaredMethod(prefix + StringUtils.capitalize(f.getName()), Long.class);
m.invoke(t, literal);
} else if (type == Integer.class || type == int.class) {
Method m = clazz.getDeclaredMethod(prefix + StringUtils.capitalize(f.getName()),
Integer.class);
m.invoke(t, literal.intValue());
}
} else if (type == Boolean.class || type == boolean.class) {
if (baseLiteral.getClass() != BooleanLiteral.class) {
continue;
}
Method m = null;
if (type == boolean.class) {
m = clazz.getDeclaredMethod(prefix + StringUtils.capitalize(f.getName()), boolean.class);
} else if (type == Boolean.class) {
m = clazz.getDeclaredMethod(prefix + StringUtils.capitalize(f.getName()), Boolean.class);
}
boolean booleanLiteral = ((BooleanLiteral)baseLiteral).isValue();
m.invoke(t, booleanLiteral);
} else if (type.isEnum()) {
if (!(baseLiteral instanceof StringLiteral)) {
continue;
}
StringLiteral stringLiteral = (StringLiteral)baseLiteral;
Method m = clazz.getDeclaredMethod(prefix + StringUtils.capitalize(f.getName()), type);
Class<Enum> e = (Class<Enum>)type;
m.invoke(t, new Object[] {Enum.valueOf(e, stringLiteral.getValue())});
} else if (type == List.class) {
Method m = clazz.getDeclaredMethod(prefix + StringUtils.capitalize(f.getName()), List.class);
if (baseLiteral instanceof ListStringLiteral) {
ListStringLiteral listStringLiteral = (ListStringLiteral)baseLiteral;
List<StringLiteral> stringLiteralList = listStringLiteral.getStringLiteralList();
m.invoke(t, stringLiteralList.stream().map(x -> x.getValue()).collect(Collectors.toList()));
} else if (baseLiteral instanceof StringLiteral) {
StringLiteral stringLiteral = (StringLiteral)baseLiteral;
m.invoke(t, Arrays.asList(stringLiteral.getValue()));
}
} else if (type == URL.class) {
if (!(baseLiteral instanceof StringLiteral)) {
continue;
}
Method m = clazz.getDeclaredMethod(prefix + StringUtils.capitalize(f.getName()), URL.class);
StringLiteral stringLiteral = (StringLiteral)baseLiteral;
m.invoke(t, new URL(null, stringLiteral.getValue(), new EngineUrlStreamHandler()));
}
}
return t;
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException | MalformedURLException e) {
throw new PropertyConvertException("toObject error:" + clazz, e);
}
}