diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/converter/WxPayOrderNotifyResultConverter.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/converter/WxPayOrderNotifyResultConverter.java index 0498520a..2d703480 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/converter/WxPayOrderNotifyResultConverter.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/converter/WxPayOrderNotifyResultConverter.java @@ -3,6 +3,7 @@ package com.github.binarywang.wxpay.converter; import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyCoupon; import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult; import com.google.common.base.Function; +import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.converters.MarshallingContext; @@ -21,6 +22,9 @@ import java.util.Arrays; import java.util.List; import java.util.Map; +/** + * @author aimilin + */ public class WxPayOrderNotifyResultConverter extends AbstractReflectionConverter { public WxPayOrderNotifyResultConverter(Mapper mapper, ReflectionProvider reflectionProvider) { @@ -72,26 +76,26 @@ public class WxPayOrderNotifyResultConverter extends AbstractReflectionConverter fields.addAll(Arrays.asList(obj.getClass().getSuperclass().getDeclaredFields())); Map fieldMap = getFieldMap(fields); - List coupons = new ArrayList<>(10); + Map coupons = Maps.newTreeMap(); while (reader.hasMoreChildren()) { reader.moveDown(); if (fieldMap.containsKey(reader.getNodeName())) { Field field = fieldMap.get(reader.getNodeName()); - setFieldValue(context, obj, field); + this.setFieldValue(context, obj, field); } else if (StringUtils.startsWith(reader.getNodeName(), "coupon_id_")) { String id = (String) context.convertAnother(obj, String.class); - getIndex(coupons, reader.getNodeName()).setCouponId(id); + this.getElement(coupons, reader.getNodeName()).setCouponId(id); } else if (StringUtils.startsWith(reader.getNodeName(), "coupon_type_")) { String type = (String) context.convertAnother(obj, String.class); - getIndex(coupons, reader.getNodeName()).setCouponType(type); + this.getElement(coupons, reader.getNodeName()).setCouponType(type); } else if (StringUtils.startsWith(reader.getNodeName(), "coupon_fee_")) { Integer fee = (Integer) context.convertAnother(obj, Integer.class); - getIndex(coupons, reader.getNodeName()).setCouponFee(fee); + this.getElement(coupons, reader.getNodeName()).setCouponFee(fee); } reader.moveUp(); } - obj.setCouponList(coupons); + obj.setCouponList(Lists.newArrayList(coupons.values())); return obj; } @@ -102,12 +106,12 @@ public class WxPayOrderNotifyResultConverter extends AbstractReflectionConverter PropertyDescriptor pd = new PropertyDescriptor(field.getName(), obj.getClass()); pd.getWriteMethod().invoke(obj, val); } - } catch (Exception e) { + } catch (Exception ignored) { } } private Map getFieldMap(List fields) { - Map fieldMap = Maps.uniqueIndex(fields, new Function() { + return Maps.uniqueIndex(fields, new Function() { @Override public String apply(Field field) { if (field.isAnnotationPresent(XStreamAlias.class)) { @@ -116,14 +120,14 @@ public class WxPayOrderNotifyResultConverter extends AbstractReflectionConverter return field.getName(); } }); - return fieldMap; } - private WxPayOrderNotifyCoupon getIndex(List coupons, String nodeName) { - Integer index = Integer.valueOf(StringUtils.substring(nodeName, nodeName.lastIndexOf("_") + 1)); - if (index >= coupons.size() || coupons.get(index) == null) { - coupons.add(index, new WxPayOrderNotifyCoupon()); + private WxPayOrderNotifyCoupon getElement(Map coupons, String nodeName) { + Integer index = Integer.valueOf(StringUtils.substringAfterLast(nodeName, "_")); + if (coupons.get(index) == null) { + coupons.put(index, new WxPayOrderNotifyCoupon()); } + return coupons.get(index); } } diff --git a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/bean/notify/WxPayOrderNotifyResultTest.java b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/bean/notify/WxPayOrderNotifyResultTest.java index 384de4c9..40446a29 100644 --- a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/bean/notify/WxPayOrderNotifyResultTest.java +++ b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/bean/notify/WxPayOrderNotifyResultTest.java @@ -12,7 +12,7 @@ import org.testng.annotations.*; */ public class WxPayOrderNotifyResultTest { @Test - public void testFromXML() throws Exception { + public void testFromXML() { String xmlString = "\n" + " \n" + " \n" + @@ -32,12 +32,12 @@ public class WxPayOrderNotifyResultTest { " \n" + " \n" + " 2\n" + - " \n" + - " 10000\n" + - " 100\n" + " \n" + " 10001\n" + " 200\n" + + " \n" + + " 10000\n" + + " 100\n" + ""; WxPayOrderNotifyResult result = WxPayOrderNotifyResult.fromXML(xmlString);