diff --git a/src/main/java/com/alibaba/fastjson/parser/JavaBeanInfo.java b/src/main/java/com/alibaba/fastjson/parser/JavaBeanInfo.java index 12e740c8bf..4c36d1db4c 100755 --- a/src/main/java/com/alibaba/fastjson/parser/JavaBeanInfo.java +++ b/src/main/java/com/alibaba/fastjson/parser/JavaBeanInfo.java @@ -478,10 +478,19 @@ public static JavaBeanInfo build(Class clazz, // { classfields = new ArrayList(declaredFields.length); for (Field f : declaredFields) { - if ((f.getModifiers() & Modifier.STATIC) != 0) { + int modifiers = f.getModifiers(); + if ((modifiers & Modifier.STATIC) != 0) { continue; } + if((modifiers & Modifier.FINAL) != 0) { + Class fieldType = f.getType(); + boolean supportReadOnly = Map.class.isAssignableFrom(fieldType) || Collection.class.isAssignableFrom(fieldType); + if (!supportReadOnly) { + continue; + } + } + if ((f.getModifiers() & Modifier.PUBLIC) != 0) { classfields.add(f); } @@ -489,11 +498,20 @@ public static JavaBeanInfo build(Class clazz, // for (Class c = clazz.getSuperclass(); c != null && c != Object.class; c = c.getSuperclass()) { for (Field f : c.getDeclaredFields()) { - if ((f.getModifiers() & Modifier.STATIC) != 0) { + int modifiers = f.getModifiers(); + if ((modifiers & Modifier.STATIC) != 0) { continue; } - if ((f.getModifiers() & Modifier.PUBLIC) != 0) { + if((modifiers & Modifier.FINAL) != 0) { + Class fieldType = f.getType(); + boolean supportReadOnly = Map.class.isAssignableFrom(fieldType) || Collection.class.isAssignableFrom(fieldType); + if (!supportReadOnly) { + continue; + } + } + + if ((modifiers & Modifier.PUBLIC) != 0) { classfields.add(f); } } diff --git a/src/test/java/com/alibaba/json/bvt/FinalTest.java b/src/test/java/com/alibaba/json/bvt/FinalTest.java new file mode 100644 index 0000000000..076be99210 --- /dev/null +++ b/src/test/java/com/alibaba/json/bvt/FinalTest.java @@ -0,0 +1,23 @@ +package com.alibaba.json.bvt; + +import org.junit.Assert; + +import com.alibaba.fastjson.JSON; + +import junit.framework.TestCase; + +public class FinalTest extends TestCase { + public void test_final() throws Exception { + VO vo = new VO(); + String text = JSON.toJSONString(vo); + Assert.assertEquals("{\"value\":1001}", text); + JSON.parseObject(text, VO.class); + JSON.parseObject("{\"id\":1001,\"value\":1001}", VO.class); + } + + + public static class VO { + public final static int id = 1001; + public final int value = 1001; + } +}