use of com.alipay.api.AlipayResponse in project synergic-developing by zeemood.
the class Converters method convert.
/**
* 使用指定 的读取器去转换字符串为对象。
*
* @param <T> 领域泛型
* @param clazz 领域类型
* @param reader 读取器
* @return 领域对象
* @throws ApiException
*/
public static <T> T convert(Class<T> clazz, Reader reader) throws AlipayApiException {
T rsp = null;
try {
rsp = clazz.newInstance();
BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
boolean isResponseClazz = AlipayResponse.class.isAssignableFrom(clazz);
for (PropertyDescriptor pd : pds) {
Method writeMethod = pd.getWriteMethod();
if (writeMethod == null) {
// ignore read-only fields
continue;
}
String itemName = pd.getName();
String listName = null;
// 之前errorCode的属性要剔除掉
if (isResponseClazz && excludeFields.contains(itemName)) {
continue;
}
List<AlipayFieldMethod> alipayFieldMethods = new ArrayList<AlipayFieldMethod>();
if (baseFields.contains(itemName) && isResponseClazz) {
Field field = AlipayResponse.class.getDeclaredField(itemName);
AlipayFieldMethod alipayFieldMethod = new AlipayFieldMethod();
alipayFieldMethod.setField(field);
// writeMethod属于父类,则直接使用
if (writeMethod.getDeclaringClass().getName().contains("AlipayResponse")) {
alipayFieldMethod.setMethod(writeMethod);
} else {
// 否则从父类再取一次
writeMethod = tryGetSetMethod(AlipayResponse.class, field, writeMethod.getName());
if (writeMethod == null) {
continue;
}
alipayFieldMethod.setMethod(writeMethod);
}
alipayFieldMethods.add(alipayFieldMethod);
// 如果被子类覆盖的,则从尝试从子类中获取
if (overideFields.contains(itemName)) {
field = tryGetFieldWithoutExp(clazz, itemName);
// 属性存在则需要重新从子类获取访问方法
if (field != null) {
writeMethod = tryGetSetMethod(clazz, field, writeMethod.getName());
if (writeMethod == null) {
continue;
}
alipayFieldMethod = new AlipayFieldMethod();
alipayFieldMethod.setField(field);
alipayFieldMethod.setMethod(writeMethod);
alipayFieldMethods.add(alipayFieldMethod);
}
}
} else {
Field field = clazz.getDeclaredField(itemName);
AlipayFieldMethod alipayFieldMethod = new AlipayFieldMethod();
alipayFieldMethod.setField(field);
alipayFieldMethod.setMethod(writeMethod);
alipayFieldMethods.add(alipayFieldMethod);
}
// 迭代设置属性
for (AlipayFieldMethod alipayFieldMethod : alipayFieldMethods) {
Field field = alipayFieldMethod.getField();
Method method = alipayFieldMethod.getMethod();
ApiField jsonField = field.getAnnotation(ApiField.class);
if (jsonField != null) {
itemName = jsonField.value();
}
ApiListField jsonListField = field.getAnnotation(ApiListField.class);
if (jsonListField != null) {
listName = jsonListField.value();
}
if (!reader.hasReturnField(itemName)) {
if (listName == null || !reader.hasReturnField(listName)) {
// ignore non-return field
continue;
}
}
Class<?> typeClass = field.getType();
// 目前
if (String.class.isAssignableFrom(typeClass)) {
Object value = reader.getPrimitiveObject(itemName);
if (value instanceof String) {
method.invoke(rsp, value.toString());
} else {
if (isCheckJsonType && value != null) {
throw new AlipayApiException(itemName + " is not a String");
}
if (value != null) {
method.invoke(rsp, value.toString());
} else {
method.invoke(rsp, "");
}
}
} else if (Long.class.isAssignableFrom(typeClass)) {
Object value = reader.getPrimitiveObject(itemName);
if (value instanceof Long) {
method.invoke(rsp, (Long) value);
} else {
if (isCheckJsonType && value != null) {
throw new AlipayApiException(itemName + " is not a Number(Long)");
}
if (StringUtils.isNumeric(value)) {
method.invoke(rsp, Long.valueOf(value.toString()));
}
}
} else if (Integer.class.isAssignableFrom(typeClass)) {
Object value = reader.getPrimitiveObject(itemName);
if (value instanceof Integer) {
method.invoke(rsp, (Integer) value);
} else {
if (isCheckJsonType && value != null) {
throw new AlipayApiException(itemName + " is not a Number(Integer)");
}
if (StringUtils.isNumeric(value)) {
method.invoke(rsp, Integer.valueOf(value.toString()));
}
}
} else if (Boolean.class.isAssignableFrom(typeClass)) {
Object value = reader.getPrimitiveObject(itemName);
if (value instanceof Boolean) {
method.invoke(rsp, (Boolean) value);
} else {
if (isCheckJsonType && value != null) {
throw new AlipayApiException(itemName + " is not a Boolean");
}
if (value != null) {
method.invoke(rsp, Boolean.valueOf(value.toString()));
}
}
} else if (Double.class.isAssignableFrom(typeClass)) {
Object value = reader.getPrimitiveObject(itemName);
if (value instanceof Double) {
method.invoke(rsp, (Double) value);
} else {
if (isCheckJsonType && value != null) {
throw new AlipayApiException(itemName + " is not a Double");
}
}
} else if (Number.class.isAssignableFrom(typeClass)) {
Object value = reader.getPrimitiveObject(itemName);
if (value instanceof Number) {
method.invoke(rsp, (Number) value);
} else {
if (isCheckJsonType && value != null) {
throw new AlipayApiException(itemName + " is not a Number");
}
}
} else if (Date.class.isAssignableFrom(typeClass)) {
DateFormat format = new SimpleDateFormat(AlipayConstants.DATE_TIME_FORMAT);
format.setTimeZone(TimeZone.getTimeZone(AlipayConstants.DATE_TIMEZONE));
Object value = reader.getPrimitiveObject(itemName);
if (value instanceof String) {
method.invoke(rsp, format.parse(value.toString()));
}
} else if (List.class.isAssignableFrom(typeClass)) {
Type fieldType = field.getGenericType();
if (fieldType instanceof ParameterizedType) {
ParameterizedType paramType = (ParameterizedType) fieldType;
Type[] genericTypes = paramType.getActualTypeArguments();
if (genericTypes != null && genericTypes.length > 0) {
if (genericTypes[0] instanceof Class<?>) {
Class<?> subType = (Class<?>) genericTypes[0];
List<?> listObjs = reader.getListObjects(listName, itemName, subType);
if (listObjs != null) {
method.invoke(rsp, listObjs);
}
}
}
}
} else {
Object obj = reader.getObject(itemName, typeClass);
if (obj != null) {
method.invoke(rsp, obj);
}
}
}
}
} catch (Exception e) {
throw new AlipayApiException(e);
}
return rsp;
}
Aggregations