Skip to content

Commit

Permalink
bug fix for ObjectReaderImplMapTyped, fix issue #557
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jul 14, 2022
1 parent 61ee908 commit 97a6b2c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.alibaba.fastjson2.util.ReferenceKey;
import com.alibaba.fastjson2.util.TypeUtils;

import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.HashMap;
Expand All @@ -25,6 +27,8 @@ class ObjectReaderImplMapTyped
final long features;
final Function builder;

final Constructor defaultConstructor;

ObjectReader valueObjectReader;
ObjectReader keyObjectReader;

Expand All @@ -40,6 +44,18 @@ public ObjectReaderImplMapTyped(Class mapType, Class instanceType, Type keyType,
this.valueClass = TypeUtils.getClass(valueType);
this.features = features;
this.builder = builder;

Constructor defaultConstructor = null;
Constructor[] constructors = this.instanceType.getDeclaredConstructors();
for (Constructor constructor : constructors) {
if (constructor.getParameterCount() == 0
&& !Modifier.isPublic(constructor.getModifiers())) {
constructor.setAccessible(true);
defaultConstructor = constructor;
break;
}
}
this.defaultConstructor = defaultConstructor;
}

@Override
Expand Down Expand Up @@ -92,9 +108,12 @@ public Object createInstance(Map input, long features) {
public Object createInstance(long features) {
if (instanceType != null && !instanceType.isInterface()) {
try {
if (defaultConstructor != null) {
return defaultConstructor.newInstance();
}
return instanceType.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new JSONException("create map error");
} catch (Exception e) {
throw new JSONException("create map error", e);
}
}
return new HashMap();
Expand Down
42 changes: 42 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/issues/Issue557.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.alibaba.fastjson2.issues;

import com.alibaba.fastjson2.JSONFactory;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.alibaba.fastjson2.reader.ObjectReader;
import com.alibaba.fastjson2.reader.ObjectReaderImplMap;
import org.junit.jupiter.api.Test;

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

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue557 {
static {
ObjectReader objectReader1 = ObjectReaderImplMap.of(new TypeReference<Map<String, Object>>() {
}.getType(), TestPublicMap.class, 0);
JSONFactory.getDefaultObjectReaderProvider().register(TestPublicMap.class, objectReader1);

ObjectReader objectReader2 = ObjectReaderImplMap.of(new TypeReference<Map<String, Object>>() {
}.getType(), TestPrivateMap.class, 0);
JSONFactory.getDefaultObjectReaderProvider().register(TestPrivateMap.class, objectReader2);
}

@Test
public void test() {
Map<String, Object> a = JSONObject.parseObject("{'a':'b'}").to(TestPublicMap.class);
assertEquals("b", a.get("a"));

Map<String, Object> b = JSONObject.parseObject("{'a':'b'}").to(TestPrivateMap.class);
assertEquals("b", b.get("a"));
}

public static class TestPublicMap
extends HashMap<String, Object> {
}

private static class TestPrivateMap
extends HashMap<String, Object> {
}
}

0 comments on commit 97a6b2c

Please sign in to comment.