in src/main/java/com/ql/util/express/ExpressUtil.java [688:724]
public static Object castObject(Object value, Class<?> type, boolean isForce) {
if (value == null) {
return null;
}
if (value.getClass() == type || type.isAssignableFrom(value.getClass())) {
return value;
}
if (value instanceof Number
&& (type.isPrimitive() || Number.class.isAssignableFrom(type))) {
return OperatorOfNumber.transfer((Number)value, type, isForce);
} else if (type.isArray() && value.getClass().isArray()) {
//需要对元素做兼容性,如果value的元素全部为null并且和声明的不一致,转化为所声明的类型
Class<?> valueType = value.getClass().getComponentType();
Class<?> declareType = type.getComponentType();
if (declareType != valueType) {
Object[] values = (Object[])value;
boolean allBlank = true;
for (Object o : values) {
if (o != null) {
allBlank = false;
break;
}
}
if (allBlank) {
return Array.newInstance(declareType, values.length);
}
}
return value;
} else if (value.getClass() == QLambda.class && isFunctionInterface(type)) {
// 动态代理 QLambda 为指定接口类
return Proxy.newProxyInstance(type.getClassLoader(), new Class<?>[] {type},
new QLambdaInvocationHandler((QLambda)value));
} else {
return value;
}
}