Permalink
Browse files

support getOnly Collection Property

  • Loading branch information...
1 parent 3443a16 commit 65f16b622b472d5ea63eedf30ba8c5ff48785187 @wenshao wenshao committed Jul 31, 2012
@@ -370,6 +370,11 @@ public ObjectDeserializer createJavaBeanDeserializer(Class<?> clazz, Type type)
if (asmEnable) {
DeserializeBeanInfo beanInfo = DeserializeBeanInfo.computeSetters(clazz, type);
for (FieldInfo fieldInfo : beanInfo.getFieldList()) {
+ if (fieldInfo.isGetOnly()) {
+ asmEnable = false;
+ break;
+ }
+
Class<?> fieldClass = fieldInfo.getFieldClass();
if (!Modifier.isPublic(fieldClass.getModifiers())) {
asmEnable = false;
@@ -2,6 +2,7 @@
import java.lang.reflect.Method;
import java.lang.reflect.Type;
+import java.util.Collection;
import java.util.Map;
import com.alibaba.fastjson.JSONException;
@@ -31,7 +32,8 @@ public Type getFieldType() {
return fieldInfo.getFieldType();
}
- public abstract void parseField(DefaultJSONParser parser, Object object, Type objectType, Map<String, Object> fieldValues);
+ public abstract void parseField(DefaultJSONParser parser, Object object, Type objectType,
+ Map<String, Object> fieldValues);
public abstract int getFastMatchToken();
@@ -51,11 +53,17 @@ public void setValue(Object object, String value) {
setValue(object, (Object) value);
}
+ @SuppressWarnings({ "rawtypes", "unchecked" })
public void setValue(Object object, Object value) {
Method method = fieldInfo.getMethod();
if (method != null) {
try {
- method.invoke(object, value);
+ if (fieldInfo.isGetOnly()) {
+ Collection collection = (Collection) method.invoke(object);
+ collection.addAll((Collection) value);
+ } else {
+ method.invoke(object, value);
+ }
} catch (Exception e) {
throw new JSONException("set property error, " + fieldInfo.getName(), e);
}
@@ -7,6 +7,7 @@
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.List;
import com.alibaba.fastjson.JSONException;
@@ -65,6 +66,16 @@ public Type getType() {
return fieldList;
}
+ public FieldInfo getField(String propertyName) {
+ for (FieldInfo item : this.fieldList) {
+ if (item.getName().equals(propertyName)) {
+ return item;
+ }
+ }
+
+ return null;
+ }
+
public boolean add(FieldInfo field) {
for (FieldInfo item : this.fieldList) {
if (item.getName().equals(field.getName())) {
@@ -231,6 +242,37 @@ public static DeserializeBeanInfo computeSetters(Class<?> clazz, Type type) {
beanInfo.add(new FieldInfo(field.getName(), null, field));
}
+
+ for (Method method : clazz.getMethods()) {
+ String methodName = method.getName();
+ if (methodName.length() < 4) {
+ continue;
+ }
+
+ if (Modifier.isStatic(method.getModifiers())) {
+ continue;
+ }
+
+ if (methodName.startsWith("get") && Character.isUpperCase(methodName.charAt(3))) {
+ if (method.getParameterTypes().length != 0) {
+ continue;
+ }
+
+ if (!Collection.class.isAssignableFrom(method.getReturnType())) {
+ continue;
+ }
+
+ String propertyName = Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4);
+
+ FieldInfo fieldInfo = beanInfo.getField(propertyName);
+ if (fieldInfo != null) {
+ continue;
+ }
+
+ beanInfo.add(new FieldInfo(propertyName, method, null, clazz, type));
+ method.setAccessible(true);
+ }
+ }
return beanInfo;
}
@@ -17,6 +17,7 @@
private final Class<?> fieldClass;
private final Type fieldType;
private final Class<?> declaringClass;
+ private boolean getOnly = false;
public FieldInfo(String name, Class<?> declaringClass, Class<?> fieldClass, Type fieldType, Method method,
Field field){
@@ -62,6 +63,7 @@ public FieldInfo(String name, Method method, Field field, Class<?> clazz, Type t
} else {
fieldClass = method.getReturnType();
fieldType = method.getGenericReturnType();
+ getOnly = true;
}
this.declaringClass = method.getDeclaringClass();
} else {
@@ -181,4 +183,9 @@ public void setAccessible(boolean flag) throws SecurityException {
field.setAccessible(flag);
}
+
+ public boolean isGetOnly() {
+ return getOnly;
+ }
+
}
@@ -0,0 +1,27 @@
+package com.alibaba.json.bvt.parser.deser;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.alibaba.fastjson.JSON;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+public class GetOnlyCollectionTest extends TestCase {
+
+ public void test_getOnly() throws Exception {
+ VO vo = JSON.parseObject("{\"items\":[\"a\",\"b\"]}", VO.class);
+ Assert.assertEquals(2, vo.getItems().size());
+ Assert.assertEquals("a", vo.getItems().get(0));
+ Assert.assertEquals("b", vo.getItems().get(1));
+ }
+
+ public static class VO {
+ private final List<String> items = new ArrayList<String>();
+
+ public List<String> getItems() {
+ return items;
+ }
+ }
+}

0 comments on commit 65f16b6

Please sign in to comment.