Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
bug fixed for final field. fixed issue #698
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jun 27, 2016
1 parent 31a396f commit 0a9eff8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/main/java/com/alibaba/fastjson/parser/JavaBeanInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -478,22 +478,40 @@ public static JavaBeanInfo build(Class<?> clazz, //
{
classfields = new ArrayList<Field>(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);
}
}

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);
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/alibaba/json/bvt/FinalTest.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 0a9eff8

Please sign in to comment.