From 42046295cef20c7c791e4a6257d84f0e06020a3f Mon Sep 17 00:00:00 2001 From: wenshao Date: Tue, 22 Oct 2024 18:57:32 +0800 Subject: [PATCH] Fix circular dependency, for issue #2994 --- .../com/alibaba/fastjson2/JSONFactory.java | 60 +++++++++++-------- .../reader/ObjectReaderProvider.java | 12 ++-- 2 files changed, 40 insertions(+), 32 deletions(-) diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONFactory.java b/core/src/main/java/com/alibaba/fastjson2/JSONFactory.java index 398f051da7..764cf9e68a 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONFactory.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONFactory.java @@ -25,6 +25,38 @@ import static com.alibaba.fastjson2.util.JDKUtils.VECTOR_BIT_LENGTH; public final class JSONFactory { + public static final class Conf { + static final Properties DEFAULT_PROPERTIES; + + static { + Properties properties = new Properties(); + + InputStream inputStream = AccessController.doPrivileged((PrivilegedAction) () -> { + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + + final String resourceFile = "fastjson2.properties"; + + if (cl != null) { + return cl.getResourceAsStream(resourceFile); + } else { + return ClassLoader.getSystemResourceAsStream(resourceFile); + } + }); + if (inputStream != null) { + try { + properties.load(inputStream); + } catch (java.io.IOException ignored) { + } finally { + IOUtils.close(inputStream); + } + } + DEFAULT_PROPERTIES = properties; + } + + public static String getProperty(String key) { + return DEFAULT_PROPERTIES.getProperty(key); + } + } static volatile Throwable initErrorLast; public static final String CREATOR; @@ -38,7 +70,7 @@ public final class JSONFactory { static boolean useGsonAnnotation; public static String getProperty(String key) { - return DEFAULT_PROPERTIES.getProperty(key); + return Conf.getProperty(key); } static long defaultReaderFeatures; @@ -136,29 +168,7 @@ public NameCacheEntry2(String name, long value0, long value1) { static final Double DOUBLE_ZERO = (double) 0; static { - Properties properties = new Properties(); - - InputStream inputStream = AccessController.doPrivileged((PrivilegedAction) () -> { - ClassLoader cl = Thread.currentThread().getContextClassLoader(); - - final String resourceFile = "fastjson2.properties"; - - if (cl != null) { - return cl.getResourceAsStream(resourceFile); - } else { - return ClassLoader.getSystemResourceAsStream(resourceFile); - } - }); - if (inputStream != null) { - try { - properties.load(inputStream); - } catch (java.io.IOException ignored) { - } finally { - IOUtils.close(inputStream); - } - } - DEFAULT_PROPERTIES = properties; - + Properties properties = Conf.DEFAULT_PROPERTIES; { String property = System.getProperty("fastjson2.creator"); if (property != null) { @@ -349,8 +359,6 @@ static final class CacheItem { volatile byte[] bytes; } - static final Properties DEFAULT_PROPERTIES; - static final ObjectWriterProvider defaultObjectWriterProvider = new ObjectWriterProvider(); static final ObjectReaderProvider defaultObjectReaderProvider = new ObjectReaderProvider(); diff --git a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderProvider.java b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderProvider.java index 29f44bb181..8523ad95a7 100644 --- a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderProvider.java +++ b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderProvider.java @@ -57,7 +57,7 @@ public ObjectReaderCachePair(long hashCode, ObjectReader reader) { { String property = System.getProperty(PROPERTY_DENY_PROPERTY); if (property == null) { - property = JSONFactory.getProperty(PROPERTY_DENY_PROPERTY); + property = JSONFactory.Conf.getProperty(PROPERTY_DENY_PROPERTY); } if (property != null && property.length() > 0) { DENYS = property.split(","); @@ -69,7 +69,7 @@ public ObjectReaderCachePair(long hashCode, ObjectReader reader) { { String property = System.getProperty(PROPERTY_AUTO_TYPE_ACCEPT); if (property == null) { - property = JSONFactory.getProperty(PROPERTY_AUTO_TYPE_ACCEPT); + property = JSONFactory.Conf.getProperty(PROPERTY_AUTO_TYPE_ACCEPT); } if (property != null && property.length() > 0) { AUTO_TYPE_ACCEPT_LIST = property.split(","); @@ -81,7 +81,7 @@ public ObjectReaderCachePair(long hashCode, ObjectReader reader) { { String property = System.getProperty(PROPERTY_AUTO_TYPE_BEFORE_HANDLER); if (property == null || property.isEmpty()) { - property = JSONFactory.getProperty(PROPERTY_AUTO_TYPE_BEFORE_HANDLER); + property = JSONFactory.Conf.getProperty(PROPERTY_AUTO_TYPE_BEFORE_HANDLER); } if (property != null) { @@ -104,7 +104,7 @@ public ObjectReaderCachePair(long hashCode, ObjectReader reader) { { String property = System.getProperty(PROPERTY_AUTO_TYPE_HANDLER); if (property == null || property.isEmpty()) { - property = JSONFactory.getProperty(PROPERTY_AUTO_TYPE_HANDLER); + property = JSONFactory.Conf.getProperty(PROPERTY_AUTO_TYPE_HANDLER); } if (property != null) { @@ -127,14 +127,14 @@ public ObjectReaderCachePair(long hashCode, ObjectReader reader) { { String property = System.getProperty("fastjson.parser.safeMode"); if (property == null || property.isEmpty()) { - property = JSONFactory.getProperty("fastjson.parser.safeMode"); + property = JSONFactory.Conf.getProperty("fastjson.parser.safeMode"); } if (property == null || property.isEmpty()) { property = System.getProperty("fastjson2.parser.safeMode"); } if (property == null || property.isEmpty()) { - property = JSONFactory.getProperty("fastjson2.parser.safeMode"); + property = JSONFactory.Conf.getProperty("fastjson2.parser.safeMode"); } if (property != null) {