diff --git a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterProvider.java b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterProvider.java index d991fad09d..6e8b783ca7 100644 --- a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterProvider.java +++ b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterProvider.java @@ -397,6 +397,11 @@ private ObjectWriter getObjectWriterInternal(Type objectType, Class objectClass, if (objectWriter != null) { return objectWriter; } + } else { + objectWriter = cache.get(objectType); + if (objectWriter != null) { + return objectWriter; + } } } diff --git a/core/src/test/java/com/alibaba/fastjson2/write/ObjectWriterProviderTest.java b/core/src/test/java/com/alibaba/fastjson2/write/ObjectWriterProviderTest.java index c1507626ca..82eb3a6acd 100644 --- a/core/src/test/java/com/alibaba/fastjson2/write/ObjectWriterProviderTest.java +++ b/core/src/test/java/com/alibaba/fastjson2/write/ObjectWriterProviderTest.java @@ -6,8 +6,13 @@ import com.alibaba.fastjson2.writer.ObjectWriter; import com.alibaba.fastjson2.writer.ObjectWriterProvider; import org.junit.jupiter.api.Test; +import org.springframework.cglib.proxy.Enhancer; +import org.springframework.cglib.proxy.MethodInterceptor; +import org.springframework.cglib.proxy.MethodProxy; +import java.lang.reflect.Method; import java.lang.reflect.Type; +import java.util.concurrent.TimeUnit; import static org.junit.jupiter.api.Assertions.*; @@ -131,9 +136,28 @@ public void testWriter2() { assertSame(writer1, JSON.registerIfAbsent(Bean.class, writer, false)); } + @Test + public void testWriterProxy() throws InterruptedException { + Enhancer enhancer = new Enhancer(); + enhancer.setSuperclass(Bean.class); + enhancer.setCallback(new BeanMethodInterceptor()); + Object proxy1 = enhancer.create(); + JSON.toJSONString(proxy1); + JSON.toJSONString(proxy1); + TimeUnit.MINUTES.sleep(60); + } + public static class Bean { } + public static class BeanMethodInterceptor implements MethodInterceptor { + + @Override + public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { + return methodProxy.invokeSuper(o, objects); + } + } + public static class BeanWriter implements ObjectWriter { @Override