Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 2691 #2730

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3743,7 +3743,15 @@ private int genReadFieldValueList(
mw.visitInsn(Opcodes.DUP);
mw.visitTypeInsn(Opcodes.CHECKCAST, TYPE_FIELD_CLASS);
mw.visitVarInsn(Opcodes.ASTORE, LIST);
mw.visitJumpInsn(IFNONNULL, listInitEnd_);
Label listNull_ = new Label();
mw.visitJumpInsn(Opcodes.IFNULL, listNull_);

mw.visitVarInsn(Opcodes.ALOAD, LIST);
mw.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
mw.visitFieldInsn(Opcodes.GETSTATIC, "java/util/Collections", "EMPTY_LIST", "Ljava/util/List;");
mw.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
mw.visitJumpInsn(Opcodes.IF_ACMPNE, listInitEnd_);
mw.visitLabel(listNull_);
}

mw.visitTypeInsn(Opcodes.NEW, LIST_TYPE);
Expand Down
107 changes: 107 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/issues_2600/Issue2691.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.alibaba.fastjson2.issues_2600;

import com.alibaba.fastjson2.JSONB;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.junit.jupiter.api.Test;

import java.util.*;

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

public class Issue2691 {
@Test
void testUnmodifiableList() {
UnmodifiableList list = new UnmodifiableList();
ArrayList<String> strings = new ArrayList<>();
strings.add("1");
list.setList(strings);
writeReade(list);
}

@Test
void testUnmodifiableMap() {
UnmodifiableMap map = new UnmodifiableMap();
Map<String, String> stringMap = new HashMap<>();
stringMap.put("1", "1");
map.setMap(stringMap);
writeReade(map);
}

@Test
void testUnmodifiableSet() {
UnmodifiableSet set = new UnmodifiableSet();
Set<String> stringSet = new HashSet<>();
stringSet.add("1");
set.setSet(stringSet);
writeReade(set);
}

void writeReade(Object value) {
byte[] bytes = JSONB.toBytes(
value,
JSONWriter.Feature.WriteClassName,
JSONWriter.Feature.FieldBased,
JSONWriter.Feature.NotWriteHashMapArrayListClassName
);

Object result = JSONB.parseObject(
bytes,
value.getClass(),
JSONReader.Feature.FieldBased);
assertNotNull(result);
assertEquals(result, value);
}

@Test
public void test() {
Page<String> page = new Page<>();
List<String> data = new ArrayList<>();
data.add(null);
data.add("abc");
data.add(null);
page.setData(data);
byte[] bytes = JSONB.toBytes(page);
Page<String> page1 = JSONB.parseObject(bytes, Page.class);
assertNotNull(page1);
assertEquals(3, page1.data.size());
assertEquals("abc", page1.data.get(1));
}

@Getter
@Setter
@EqualsAndHashCode
@Accessors(chain = true)
private static class UnmodifiableList {
private List<String> list = Collections.emptyList();
}

@Getter
@Setter
@EqualsAndHashCode
@Accessors(chain = true)
private static class UnmodifiableSet {
private Set<String> set = Collections.emptySet();
}

@Getter
@Setter
@EqualsAndHashCode
@Accessors(chain = true)
private static class UnmodifiableMap {
private Map<String, String> map = Collections.emptyMap();
}

public static class Page<T> {
public List<T> data = Collections.emptyList();

public void setData(List<T> items) {
this.data = items;
}
}
}
Loading