diff --git a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderCreatorASM.java b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderCreatorASM.java index e3f56883ec..bb8d72af42 100644 --- a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderCreatorASM.java +++ b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderCreatorASM.java @@ -400,7 +400,7 @@ public ObjectReader createObjectReader(Class objectClass, Type objectT "(J)Ljava/lang/Object;" ); - if (fieldBased && defaultConstructor == null) { + if (fieldBased && (defaultConstructor == null || !Modifier.isPublic(defaultConstructor.getModifiers()) || !Modifier.isPublic(objectClass.getModifiers()))) { mw.visitFieldInsn(Opcodes.GETSTATIC, TYPE_UNSAFE_UTILS, "UNSAFE", "Lsun/misc/Unsafe;"); mw.visitVarInsn(Opcodes.ALOAD, 0); mw.visitFieldInsn(Opcodes.GETFIELD, TYPE_OBJECT_READER_ADAPTER, "objectClass", "Ljava/lang/Class;"); diff --git a/core/src/test/java/com/alibaba/fastjson2/issues/Issue751.java b/core/src/test/java/com/alibaba/fastjson2/issues/Issue751.java new file mode 100644 index 0000000000..178060ff02 --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/issues/Issue751.java @@ -0,0 +1,34 @@ +package com.alibaba.fastjson2.issues; + +import com.alibaba.fastjson2.JSONB; +import com.alibaba.fastjson2.JSONReader; +import org.junit.jupiter.api.Test; + +import java.io.Serializable; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class Issue751 { + @Test + public void test() { + byte[] bytes = JSONB.toBytes(new Data()); + Object o = JSONB.parseObject(bytes, Data.class, JSONReader.Feature.FieldBased); + assertEquals(Data.class, o.getClass()); + } + + static class Data + implements Serializable { + private String mData = ""; + + public Data() { + } + + public String getData() { + return mData; + } + + public void setData(String data) { + mData = data; + } + } +}