Skip to content

Commit

Permalink
fix : none-public nested class fieldBased not work, for issue #751
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Sep 11, 2022
1 parent 0d0621b commit 409d01e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ public <T> ObjectReader<T> createObjectReader(Class<T> 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;");
Expand Down
34 changes: 34 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/issues/Issue751.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit 409d01e

Please sign in to comment.