Skip to content

Commit

Permalink
Fix circular dependency, for issue #2994
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Oct 22, 2024
1 parent e1880a5 commit 4204629
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
60 changes: 34 additions & 26 deletions core/src/main/java/com/alibaba/fastjson2/JSONFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<InputStream>) () -> {
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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<InputStream>) () -> {
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) {
Expand Down Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(",");
Expand All @@ -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(",");
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 4204629

Please # to comment.