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

Commit

Permalink
Merge pull request #5 from alibaba/master
Browse files Browse the repository at this point in the history
merge
  • Loading branch information
VictorZeng committed Apr 25, 2016
2 parents f78259d + 0b72ea1 commit 7bb847b
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.2.RELEASE</version>
<version>4.2.5.RELEASE</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,38 @@ public void setValue(Object object, Object value) {
return;
} else {
final Field field = fieldInfo.field;
if (field != null) {
field.set(object, value);


if (fieldInfo.getOnly) {
if (fieldInfo.fieldClass == AtomicInteger.class) {
AtomicInteger atomic = (AtomicInteger) field.get(object);
if (atomic != null) {
atomic.set(((AtomicInteger) value).get());
}
} else if (fieldInfo.fieldClass == AtomicLong.class) {
AtomicLong atomic = (AtomicLong) field.get(object);
if (atomic != null) {
atomic.set(((AtomicLong) value).get());
}
} else if (fieldInfo.fieldClass == AtomicBoolean.class) {
AtomicBoolean atomic = (AtomicBoolean) field.get(object);
if (atomic != null) {
atomic.set(((AtomicBoolean) value).get());
}
} else if (Map.class.isAssignableFrom(fieldInfo.fieldClass)) {
Map map = (Map) field.get(object);
if (map != null) {
map.putAll((Map) value);
}
} else {
Collection collection = (Collection) field.get(object);
if (collection != null) {
collection.addAll((Collection) value);
}
}
} else {
if (field != null) {
field.set(object, value);
}
}
}
} catch (Exception e) {
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/com/alibaba/fastjson/util/ASMUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,23 @@ public static String type(Class<?> parameterType) {


public static String getPrimitiveLetter(Class<?> type) {
if (Integer.TYPE.equals(type)) {
if (Integer.TYPE == type) {
return "I";
} else if (Void.TYPE.equals(type)) {
} else if (Void.TYPE == type) {
return "V";
} else if (Boolean.TYPE.equals(type)) {
} else if (Boolean.TYPE == type) {
return "Z";
} else if (Character.TYPE.equals(type)) {
} else if (Character.TYPE == type) {
return "C";
} else if (Byte.TYPE.equals(type)) {
} else if (Byte.TYPE == type) {
return "B";
} else if (Short.TYPE.equals(type)) {
} else if (Short.TYPE == type) {
return "S";
} else if (Float.TYPE.equals(type)) {
} else if (Float.TYPE == type) {
return "F";
} else if (Long.TYPE.equals(type)) {
} else if (Long.TYPE == type) {
return "J";
} else if (Double.TYPE.equals(type)) {
} else if (Double.TYPE == type) {
return "D";
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/alibaba/fastjson/util/FieldInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public FieldInfo(String name, //
fieldClass = field.getType();
fieldType = field.getGenericType();
this.declaringClass = field.getDeclaringClass();
getOnly = Modifier.isFinal(field.getModifiers());
}
this.getOnly = getOnly;

Expand Down
50 changes: 50 additions & 0 deletions src/test/java/com/alibaba/json/bvt/JSONArrayTest3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.alibaba.json.bvt;

import java.util.Arrays;

import org.junit.Assert;

import com.alibaba.fastjson.JSONArray;

import junit.framework.TestCase;

public class JSONArrayTest3 extends TestCase {
public void test_0() throws Exception {
JSONArray array = new JSONArray();
array.set(1, "1001");
Assert.assertEquals(2, array.size());
Assert.assertNull(array.get(0));
Assert.assertEquals("1001", array.get(1));

array.clear();
Assert.assertEquals(0, array.size());

array.set(-1, "1001");
Assert.assertEquals(1, array.size());
Assert.assertEquals("1001", array.get(0));

array.fluentAdd("1002").fluentClear();
Assert.assertEquals(0, array.size());

array.fluentAdd("1002").fluentRemove("1002");
Assert.assertEquals(0, array.size());

array.fluentAdd("1002").fluentRemove(0);
Assert.assertEquals(0, array.size());

array.fluentSet(1, "1001");
Assert.assertEquals(2, array.size());
Assert.assertNull(array.get(0));
Assert.assertEquals("1001", array.get(1));

array.fluentRemoveAll(Arrays.asList(null, "1001"));
Assert.assertEquals(0, array.size());

array.fluentAddAll(Arrays.asList("1001", "1002", "1003"));
Assert.assertEquals(3, array.size());

array.retainAll(Arrays.asList("1002", "1004"));
Assert.assertEquals(1, array.size());
Assert.assertEquals("1002", array.get(0));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.alibaba.json.bvt.parser;

import java.util.List;

import org.junit.Assert;

import com.alibaba.fastjson.JSON;

import junit.framework.TestCase;

public class ReadOnlyCollectionTest_final_field extends TestCase {

public void test_readOnlyNullList() throws Exception {
String text = "{\"list\":[1,2,3]}";
Entity entity = JSON.parseObject(text, Entity.class);
Assert.assertNull(entity.list);
}

public static class Entity {

public final List<Object> list = null;

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.alibaba.json.bvt.parser;

import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;

import com.alibaba.fastjson.JSON;

import junit.framework.TestCase;

public class ReadOnlyCollectionTest_final_field_null extends TestCase {

public void test_readOnlyNullList() throws Exception {
String text = "{\"list\":[1,2,3]}";
Entity entity = JSON.parseObject(text, Entity.class);
Assert.assertNotNull(entity);
Assert.assertEquals(3, entity.list.size());
}

public static class Entity {

public final List<Object> list = new ArrayList<Object>();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.alibaba.json.bvt.parser;

import java.util.Map;

import org.junit.Assert;

import com.alibaba.fastjson.JSON;

import junit.framework.TestCase;

public class ReadOnlyMapTest2_final_field extends TestCase {

public void test_readOnlyNullList() throws Exception {
String text = "{\"values\":{\"a\":{}}}";
Entity entity = JSON.parseObject(text, Entity.class);
Assert.assertNotNull(entity);
Assert.assertNull(entity.values);
}

public static class Entity {

public final Map<String, A> values = null;


}

public static class A {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.alibaba.json.bvt.parser;

import java.util.HashMap;
import java.util.Map;

import junit.framework.TestCase;

import org.junit.Assert;

import com.alibaba.fastjson.JSON;

public class ReadOnlyMapTest_final_field extends TestCase {

public void test_readOnlyNullList() throws Exception {
String text = "{\"values\":{\"a\":{}}}";
Entity entity = JSON.parseObject(text, Entity.class);
Assert.assertNotNull(entity);
Assert.assertNotNull(entity.values.get("a"));
Assert.assertTrue(entity.values.get("a") instanceof A);
}

public static class Entity {

public final Map<String, A> values = new HashMap<String, A>();

}

public static class A {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public void test_put() throws Exception {

JSONPath path = new JSONPath("$.values");
path.arrayAdd(root, 123);
path.arrayAdd(root, (Object[])null);
path.arrayAdd(root, new Object[0]);

Assert.assertEquals(1, list.size());
Assert.assertEquals(123, ((Integer) list.get(0)).intValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public void test_root() throws Exception {

Assert.assertTrue(JSONPath.containsValue(list, "/0", "kiki"));
Assert.assertFalse(JSONPath.containsValue(list, "/0", "kiki_"));

Assert.assertTrue(JSONPath.containsValue(list, "/", "kiki"));
Assert.assertFalse(JSONPath.containsValue(list, "/", "kiki_"));


Assert.assertTrue(JSONPath.contains(list, "/"));
Assert.assertTrue(JSONPath.contains(list, "/0"));
Assert.assertTrue(JSONPath.contains(list, "/1"));
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/alibaba/json/bvt/path/JSONPath_list_size_1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.alibaba.json.bvt.path;

import org.junit.Assert;

import com.alibaba.fastjson.JSONPath;

import junit.framework.TestCase;

public class JSONPath_list_size_1 extends TestCase {
public void test_obj_array() throws Exception {
Object[] array = new Object[] {1, 2, 3};
JSONPath path = new JSONPath("$.size()");
Integer result = (Integer) path.eval(array);
Assert.assertEquals(array.length, result.intValue());
}

public void test_int_array() throws Exception {
int[] array = new int[] {1, 2, 3};
JSONPath path = new JSONPath("$.size()");
Integer result = (Integer) path.eval(array);
Assert.assertEquals(array.length, result.intValue());
}
}
31 changes: 31 additions & 0 deletions src/test/java/com/alibaba/json/bvt/path/JSONPath_list_size_2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.alibaba.json.bvt.path;

import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;

import com.alibaba.fastjson.JSONPath;

import junit.framework.TestCase;

public class JSONPath_list_size_2 extends TestCase {
public void test_map() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("1001", 1001);
map.put("1002", 1002);
JSONPath path = new JSONPath("$.size()");
Integer result = (Integer) path.eval(map);
Assert.assertEquals(map.size(), result.intValue());
}

public void test_map_null() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("1001", 1001);
map.put("1002", 1002);
map.put("1003", null);
JSONPath path = new JSONPath("$.size()");
Integer result = (Integer) path.eval(map);
Assert.assertEquals(2, result.intValue());
}
}
32 changes: 32 additions & 0 deletions src/test/java/com/alibaba/json/bvt/path/JSONPath_list_size_3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.alibaba.json.bvt.path;

import org.junit.Assert;

import com.alibaba.fastjson.JSONPath;

import junit.framework.TestCase;

public class JSONPath_list_size_3 extends TestCase {
public void test_java_bean() throws Exception {
Model model = new Model();
model.id = 1001;
model.name = "wenshao";
JSONPath path = new JSONPath("$.size()");
Integer result = (Integer) path.eval(model);
Assert.assertEquals(2, result.intValue());
}

public void test_java_bean_field_null() throws Exception {
Model model = new Model();
model.id = 1001;
model.name = null;
JSONPath path = new JSONPath("$.size()");
Integer result = (Integer) path.eval(model);
Assert.assertEquals(1, result.intValue());
}

public static class Model {
public int id;
public String name;
}
}

0 comments on commit 7bb847b

Please sign in to comment.