使用反射API获取列表对象值

时间:2015-02-17 11:51:19

标签: java reflection collections illegalargumentexception

我有一个XML解析的pojo,如下面的

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "activity",
    "orderDetails"
})
@XmlRootElement(name = "GxML")
public class GxML {

    @XmlElement(name = "Activity", required = true)
    public String activity;
    @XmlElement(name = "OrderDetails", required = true)
    public GxML.OrderDetails orderDetails;

   //get set
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "initiatorRole",
        "productDetails"
    })
    public static class OrderDetails {

        @XmlElement(name = "InitiatorRole", required = true)
        public String initiatorRole;

        //get set
        @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "", propOrder = {
            "product"
        })
        public static class ProductDetails {

            @XmlElement(name = "Product", required = true)
            public List<GxML.OrderDetails.ProductDetails.Product> product;

            public List<GxML.OrderDetails.ProductDetails.Product> getProduct() {
                if (product == null) {
                    product = new ArrayList<GxML.OrderDetails.ProductDetails.Product>();
                }
                return this.product;
            }
            @XmlAccessorType(XmlAccessType.FIELD)
            @XmlType(name = "", propOrder = {
               "quantity",
                "commodity"
            })
            public static class Product {
            @XmlElement(name = "Quantity", required = true)
                public BigDecimal quantity;
                @XmlElement(name = "Commodity", required = true)
                public String commodity;
                //get set
            }}}}

我需要使用Java Reflection

获取所有字段名称及其对应的值
Class c1 = gXml.getClass();    
Field[] valueObjFields = c1.getDeclaredFields()[1].getType().getDeclaredFields();   
for (int i = 0; i < valueObjFields.length; i++)      {     
String fieldName = valueObjFields[i].getName();     
valueObjFields[i].setAccessible(true);    
Object newObj = valueObjFields[i].get(gXml.getOrderDetails());     
orderMap.put(valueObjFields[i].getAnnotation(XmlElement.class).name(), newObj);   
if("productDetails".equalsIgnoreCase(fieldName)){
Class c2 = gXml.getOrderDetails().getProductDetails().getProduct().get(0).getClass();  
Field[] valueObjFields1 = c2.getDeclaredFields();   
for (int j = 0; j < valueObjFields1.length; j++)      {     
String fieldName2 = valueObjFields1[j].getName();     
valueObjFields1[j].setAccessible(true);    
Object newObj2 = valueObjFields1[j].get(gXml.getOrderDetails().getProductDetails().getProduct());  
orderMap.put(valueObjFields1[j].getAnnotation(XmlElement.class).name(), newObj2);   
} } }  

对于订单详情,它工作正常,但在产品详细信息中获取数量和商品,它给出了非法参数例外 Object newObj2 = valueObjFields1 [j] .get(gXml.getOrderDetails()。getProductDetails()。getProduct());

如何使用反射获取列表值。

1 个答案:

答案 0 :(得分:0)

您从getProduct().get(0)类获得字段(看起来像列表的成员)

Class c2 = gXml.getOrderDetails().getProductDetails().getProduct().get(0).getClass();  
Field[] valueObjFields1 = c2.getDeclaredFields();

但是尝试从集合本身读取值(而不是get(0)元素)

valueObjFields1[j].get(gXml.getOrderDetails().getProductDetails().getProduct());