OSDN Git Service

Support array parameterized types.
authorNathanSweet <nathan.sweet@gmail.com>
Tue, 1 Oct 2013 22:04:29 +0000 (00:04 +0200)
committerNathanSweet <nathan.sweet@gmail.com>
Tue, 1 Oct 2013 22:04:29 +0000 (00:04 +0200)
gdx/src/com/badlogic/gdx/utils/Json.java
gdx/src/com/badlogic/gdx/utils/reflect/Field.java

index d82a919..452a7db 100644 (file)
@@ -857,7 +857,7 @@ public class Json {
                                return (T)newArray;\r
                        }\r
                        if (ClassReflection.isAssignableFrom(List.class, type)) {\r
-                               List newArray = type == null ? new ArrayList() : (List)newInstance(type);\r
+                               List newArray = (type == null || type.isInterface()) ? new ArrayList() : (List)newInstance(type);\r
                                for (JsonValue child = jsonData.child(); child != null; child = child.next())\r
                                        newArray.add(readValue(elementType, null, child));\r
                                return (T)newArray;\r
@@ -865,7 +865,7 @@ public class Json {
                        if (type.isArray()) {\r
                                Class componentType = type.getComponentType();\r
                                if (elementType == null) elementType = componentType;\r
-                               Object newArray = ArrayReflection.newInstance(componentType, jsonData.size());\r
+                               Object newArray = ArrayReflection.newInstance(componentType, jsonData.size);\r
                                int i = 0;\r
                                for (JsonValue child = jsonData.child(); child != null; child = child.next())\r
                                        ArrayReflection.set(newArray, i++, readValue(elementType, null, child));\r
@@ -976,7 +976,9 @@ public class Json {
 \r
                public FieldMetadata (Field field) {\r
                        this.field = field;\r
-                       this.elementType = field.getElementType();\r
+                       int index = (ClassReflection.isAssignableFrom(ObjectMap.class, field.getType()) || ClassReflection.isAssignableFrom(\r
+                               HashMap.class, field.getType())) ? 1 : 0;\r
+                       this.elementType = field.getElementType(index);\r
                }\r
        }\r
 \r
index 91753fe..769dc6c 100644 (file)
@@ -1,6 +1,7 @@
 
 package com.badlogic.gdx.utils.reflect;
 
+import java.lang.reflect.GenericArrayType;
 import java.lang.reflect.Modifier;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
@@ -83,16 +84,22 @@ public final class Field {
                return field.isSynthetic();
        }
 
-       /** If the type of the field is parameterized, returns the Class object representing the parameter type, null otherwise. */
-       public Class getElementType () {
+       /** If the type of the field is parameterized, returns the Class object representing the parameter type at the specified index,
+        * null otherwise. */
+       public Class getElementType (int index) {
                Type genericType = field.getGenericType();
                if (genericType instanceof ParameterizedType) {
                        Type[] actualTypes = ((ParameterizedType)genericType).getActualTypeArguments();
-                       if (actualTypes.length == 1) {
-                               Type actualType = actualTypes[0];
+                       if (actualTypes.length - 1 >= index) {
+                               Type actualType = actualTypes[index];
                                if (actualType instanceof Class)
                                        return (Class)actualType;
-                               else if (actualType instanceof ParameterizedType) return (Class)((ParameterizedType)actualType).getRawType();
+                               else if (actualType instanceof ParameterizedType)
+                                       return (Class)((ParameterizedType)actualType).getRawType();
+                               else if (actualType instanceof GenericArrayType) {
+                                       Type componentType = ((GenericArrayType)actualType).getGenericComponentType();
+                                       if (componentType instanceof Class) return ArrayReflection.newInstance((Class)componentType, 0).getClass();
+                               }
                        }
                }
                return null;