From 3c33ab7fc26908835fe1e9223fbc6bdd16426d1d Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 31 Mar 2023 22:09:59 +0800 Subject: [PATCH] fix code --- .../main/java/cn/hutool/core/map/MapUtil.java | 32 ++++++------- .../java/cn/hutool/core/util/JdkUtil.java | 9 ++-- .../java/cn/hutool/core/util/SystemUtil.java | 46 ++++++++++++------- 3 files changed, 48 insertions(+), 39 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/map/MapUtil.java b/hutool-core/src/main/java/cn/hutool/core/map/MapUtil.java index 31eb9f871..30ad2c764 100755 --- a/hutool-core/src/main/java/cn/hutool/core/map/MapUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/map/MapUtil.java @@ -12,6 +12,7 @@ package cn.hutool.core.map; +import cn.hutool.core.array.ArrayUtil; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.ListUtil; import cn.hutool.core.collection.iter.ArrayIter; @@ -19,23 +20,11 @@ import cn.hutool.core.collection.iter.IterUtil; import cn.hutool.core.exceptions.UtilException; import cn.hutool.core.reflect.ConstructorUtil; import cn.hutool.core.text.StrUtil; -import cn.hutool.core.array.ArrayUtil; +import cn.hutool.core.util.JdkUtil; import cn.hutool.core.util.ObjUtil; -import java.util.AbstractMap; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.IdentityHashMap; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.Map.Entry; -import java.util.NavigableMap; -import java.util.SortedMap; -import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; import java.util.function.BiFunction; import java.util.function.Function; @@ -1282,6 +1271,7 @@ public class MapUtil extends MapGetUtil { * 方法来自Dubbo,解决使用ConcurrentHashMap.computeIfAbsent导致的死循环问题。(issues#2349)
* A temporary workaround for Java 8 specific performance issue JDK-8161372 .
* This class should be removed once we drop Java 8 support. + * 参考:https://github.com/apache/dubbo/blob/3.2/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ConcurrentHashMapUtils.java * * @param 键类型 * @param 值类型 @@ -1292,11 +1282,15 @@ public class MapUtil extends MapGetUtil { * @see https://bugs.openjdk.java.net/browse/JDK-8161372 */ public static V computeIfAbsent(final Map map, final K key, final Function mappingFunction) { - V value = map.get(key); - if (null == value) { - map.putIfAbsent(key, mappingFunction.apply(key)); - value = map.get(key); + if (JdkUtil.IS_JDK8) { + V value = map.get(key); + if (null == value) { + //map.putIfAbsent(key, mappingFunction.apply(key)); + value = map.computeIfAbsent(key, mappingFunction); + } + return value; + } else { + return map.computeIfAbsent(key, mappingFunction); } - return value; } } diff --git a/hutool-core/src/main/java/cn/hutool/core/util/JdkUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/JdkUtil.java index 0312f1ec6..ff58b0197 100755 --- a/hutool-core/src/main/java/cn/hutool/core/util/JdkUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/JdkUtil.java @@ -57,18 +57,19 @@ public class JdkUtil { * @return JVM名称 */ private static String _getJvmName() { - return System.getProperty("java.vm.name"); + return SystemUtil.getQuietly("java.vm.name"); } /** - * 根据{@code java.specification.version}属性值,获取版本号 + * 根据{@code java.specification.version}属性值,获取版本号
+ * 默认8 * * @return 版本号 */ private static int _getJvmVersion() { - int jvmVersion = -1; + int jvmVersion = 8; - String javaSpecVer = System.getProperty("java.specification.version"); + String javaSpecVer = SystemUtil.getQuietly("java.specification.version"); if (StrUtil.isNotBlank(javaSpecVer)) { if (javaSpecVer.startsWith("1.")) { javaSpecVer = javaSpecVer.substring(2); diff --git a/hutool-core/src/main/java/cn/hutool/core/util/SystemUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/SystemUtil.java index c6cf8566f..eb9fe08b7 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/SystemUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/SystemUtil.java @@ -30,7 +30,9 @@ import java.util.Properties; */ public class SystemUtil { - /** Hutool自定义系统属性:是否解析日期字符串采用严格模式 */ + /** + * Hutool自定义系统属性:是否解析日期字符串采用严格模式 + */ public static String HUTOOL_DATE_LENIENT = "hutool.date.lenient"; /** @@ -43,7 +45,31 @@ public class SystemUtil { * @see System#getenv(String) */ public static String get(final String name, final String defaultValue) { - return ObjUtil.defaultIfNull(get(name, false), defaultValue); + return ObjUtil.defaultIfNull(get(name), defaultValue); + } + + /** + * 获得System属性 + * + * @param key 键 + * @return 属性值 + * @see System#getProperty(String) + * @see System#getenv(String) + */ + public static String get(final String key) { + return get(key, false); + } + + /** + * 获得System属性,忽略无权限问题 + * + * @param key 键 + * @return 属性值 + * @see System#getProperty(String) + * @see System#getenv(String) + */ + public static String getQuietly(final String key) { + return get(key, true); } /** @@ -62,7 +88,7 @@ public class SystemUtil { } catch (final SecurityException e) { if (false == quiet) { Console.error("Caught a SecurityException reading the system property '{}'; " + - "the SystemUtil property value will default to null.", name); + "the SystemUtil property value will default to null.", name); } } @@ -72,7 +98,7 @@ public class SystemUtil { } catch (final SecurityException e) { if (false == quiet) { Console.error("Caught a SecurityException reading the system env '{}'; " + - "the SystemUtil env value will default to null.", name); + "the SystemUtil env value will default to null.", name); } } } @@ -80,18 +106,6 @@ public class SystemUtil { return value; } - /** - * 获得System属性 - * - * @param key 键 - * @return 属性值 - * @see System#getProperty(String) - * @see System#getenv(String) - */ - public static String get(final String key) { - return get(key, null); - } - /** * 获得boolean类型值 *