Skip to content

Commit

Permalink
serialize ignore Lock & ReentrantLock, for issue #2437
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Apr 12, 2024
1 parent 1bb3a3f commit 444b461
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
11 changes: 10 additions & 1 deletion core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;

import static com.alibaba.fastjson2.util.JDKUtils.ANDROID_SDK_INT;
Expand All @@ -42,6 +44,7 @@ public abstract class BeanUtils {

public static final String SUPER = "$super$";

// com.alibaba.fastjson2.util.BeanUtilsTest.buildIgnores
static final long[] IGNORE_CLASS_HASH_CODES = {
-9214723784238596577L,
-9030616758866828325L,
Expand All @@ -61,6 +64,7 @@ public abstract class BeanUtils {
3742915795806478647L,
3977020351318456359L,
4882459834864833642L,
6033839080488254886L,
7981148566008458638L,
8344106065386396833L
};
Expand Down Expand Up @@ -285,13 +289,18 @@ public static void declaredFields(Class objectClass, Consumer<Field> fieldConsum

for (Field field : fields) {
int modifiers = field.getModifiers();
Class<?> fieldClass = field.getType();
if ((modifiers & Modifier.STATIC) != 0) {
continue;
}

if (fieldClass == Lock.class
|| fieldClass == ReentrantLock.class) {
continue;
}

if (protobufMessageV3) {
String fieldName = field.getName();
Class<?> fieldClass = field.getType();
if ("cardsmap_".equals(fieldName)
&& "com.google.protobuf.MapField".equals(fieldClass.getName())) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.alibaba.fastjson2.issues_2400;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import org.junit.jupiter.api.Test;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

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

public class Issue2437 {
@Test
public void test() {
Bean bean = new Bean();
String json = JSON.toJSONString(bean, JSONWriter.Feature.FieldBased);
assertEquals("{}", json);
Bean bean1 = JSON.parseObject(json, Bean.class);
assertNotNull(bean1.lock);

assertEquals("{}", JSON.toJSONString(new ReentrantLock()));
}

public static class Bean {
public Lock lock = new ReentrantLock();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ public void buildIgnores() {
"org.apache.commons.collections.functors.ChainedTransformer",
"org.mockito.internal.creation.bytebuddy.MockMethodInterceptor",
"sun.nio.ch.FileChannelImpl",
"java.io.InputStream"
"java.io.InputStream",
"java.util.concurrent.locks.ReentrantLock"
};

long[] hashCodes = new long[names.length];
Expand Down

0 comments on commit 444b461

Please sign in to comment.