refactor: 使用 CharSequenceUtil 替换部分 StrUtil 使用以解决 Sonar 问题

This commit is contained in:
2025-07-20 08:45:24 +08:00
parent 35e79620e4
commit ddd4e38dca
7 changed files with 22 additions and 23 deletions

View File

@@ -16,7 +16,7 @@
package top.continew.starter.json.jackson.util; package top.continew.starter.json.jackson.util;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.extra.spring.SpringUtil; import cn.hutool.extra.spring.SpringUtil;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
@@ -168,7 +168,7 @@ public class JSONUtils {
* @return 解析后的 Java 对象 * @return 解析后的 Java 对象
*/ */
public static <T> T parseObject(String str, Class<T> clazz) { public static <T> T parseObject(String str, Class<T> clazz) {
if (StrUtil.isEmpty(str)) { if (CharSequenceUtil.isEmpty(str)) {
return null; return null;
} }
try { try {
@@ -186,7 +186,7 @@ public class JSONUtils {
* @return 解析后的 List<T> * @return 解析后的 List<T>
*/ */
public static <T> List<T> parseArray(String str, Class<T> clazz) { public static <T> List<T> parseArray(String str, Class<T> clazz) {
if (StrUtil.isEmpty(str)) { if (CharSequenceUtil.isEmpty(str)) {
return new ArrayList<>(); return new ArrayList<>();
} }
try { try {
@@ -204,7 +204,7 @@ public class JSONUtils {
* @return 是否为 JSON 格式 * @return 是否为 JSON 格式
*/ */
public static boolean isTypeJSON(String str) { public static boolean isTypeJSON(String str) {
if (StrUtil.isEmpty(str)) { if (CharSequenceUtil.isEmpty(str)) {
return false; return false;
} }
try { try {
@@ -223,7 +223,7 @@ public class JSONUtils {
* @return 解析后的 Java 对象 * @return 解析后的 Java 对象
*/ */
public static <T> T toBean(String str, Class<T> clazz) { public static <T> T toBean(String str, Class<T> clazz) {
if (StrUtil.isEmpty(str)) { if (CharSequenceUtil.isEmpty(str)) {
return null; return null;
} }
try { try {

View File

@@ -20,9 +20,9 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import top.continew.starter.core.constant.StringConstants;
import top.continew.starter.core.util.CollUtils; import top.continew.starter.core.util.CollUtils;
import top.continew.starter.license.exception.LicenseException; import top.continew.starter.license.exception.LicenseException;
import top.continew.starter.license.model.LicenseExtraModel; import top.continew.starter.license.model.LicenseExtraModel;
@@ -128,7 +128,7 @@ public class ServerInfoUtils {
* @return {@link String} * @return {@link String}
*/ */
private static String getLinuxCpuSerial() { private static String getLinuxCpuSerial() {
String result = StrUtil.EMPTY; String result = StringConstants.EMPTY;
String cpuIdCmd = "dmidecode"; String cpuIdCmd = "dmidecode";
BufferedReader bufferedReader = null; BufferedReader bufferedReader = null;
try { try {
@@ -160,8 +160,7 @@ public class ServerInfoUtils {
* @return {@link String} * @return {@link String}
*/ */
private static String getWindowCpuSerial() { private static String getWindowCpuSerial() {
StringBuilder result = new StringBuilder(StringConstants.EMPTY);
StringBuilder result = new StringBuilder(StrUtil.EMPTY);
File file = null; File file = null;
BufferedReader input = null; BufferedReader input = null;
try { try {
@@ -205,11 +204,11 @@ public class ServerInfoUtils {
try { try {
Process process = new ProcessBuilder("sh", "-c", command).start(); Process process = new ProcessBuilder("sh", "-c", command).start();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
return reader.lines().findFirst().orElse(StrUtil.EMPTY); return reader.lines().findFirst().orElse(StringConstants.EMPTY);
} }
} catch (IOException e) { } catch (IOException e) {
log.error("获取 Linux 主板序列号失败: {}", e.getMessage()); log.error("获取 Linux 主板序列号失败: {}", e.getMessage());
return StrUtil.EMPTY; return StringConstants.EMPTY;
} }
} }
@@ -219,7 +218,7 @@ public class ServerInfoUtils {
* @return {@link String} * @return {@link String}
*/ */
private static String getWindowMainBoardSerial() { private static String getWindowMainBoardSerial() {
StringBuilder result = new StringBuilder(StrUtil.EMPTY); StringBuilder result = new StringBuilder(StringConstants.EMPTY);
File file = null; File file = null;
BufferedReader input = null; BufferedReader input = null;
try { try {

View File

@@ -16,7 +16,7 @@
package top.continew.starter.storage.enums; package top.continew.starter.storage.enums;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.text.CharSequenceUtil;
import top.continew.starter.core.enums.BaseEnum; import top.continew.starter.core.enums.BaseEnum;
import java.util.Arrays; import java.util.Arrays;
@@ -69,7 +69,7 @@ public enum FileType implements BaseEnum<Integer> {
*/ */
public static FileType getByExtension(String extension) { public static FileType getByExtension(String extension) {
return Arrays.stream(FileType.values()) return Arrays.stream(FileType.values())
.filter(t -> t.getExtensions().contains(StrUtil.emptyIfNull(extension).toLowerCase())) .filter(t -> t.getExtensions().contains(CharSequenceUtil.emptyIfNull(extension).toLowerCase()))
.findFirst() .findFirst()
.orElse(FileType.UNKNOWN); .orElse(FileType.UNKNOWN);
} }

View File

@@ -19,7 +19,7 @@ package top.continew.starter.storage.util;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.file.FileNameUtil; import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.text.CharSequenceUtil;
import top.continew.starter.core.constant.StringConstants; import top.continew.starter.core.constant.StringConstants;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
@@ -121,7 +121,7 @@ public class StorageUtils {
// 获取文件的扩展名 // 获取文件的扩展名
String extName = FileNameUtil.extName(fileName); String extName = FileNameUtil.extName(fileName);
// 去掉扩展名 // 去掉扩展名
String baseName = StrUtil.subBefore(fileName, StringConstants.DOT, true); String baseName = CharSequenceUtil.subBefore(fileName, StringConstants.DOT, true);
// 拼接新的路径:原始路径 + .缩略图后缀 + .扩展名 // 拼接新的路径:原始路径 + .缩略图后缀 + .扩展名
return baseName + "." + suffix + "." + extName; return baseName + "." + suffix + "." + extName;
} }

View File

@@ -19,8 +19,8 @@ package top.continew.starter.storage.strategy;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.file.FileNameUtil; import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil; import cn.hutool.crypto.digest.DigestUtil;
import top.continew.starter.core.constant.StringConstants; import top.continew.starter.core.constant.StringConstants;
import top.continew.starter.core.exception.BusinessException; import top.continew.starter.core.exception.BusinessException;
@@ -123,7 +123,7 @@ public class LocalStorageStrategy implements StorageStrategy<LocalClient> {
// 格式化文件名 防止上传后重复 // 格式化文件名 防止上传后重复
String formatFileName = StorageUtils.formatFileName(fileName); String formatFileName = StorageUtils.formatFileName(fileName);
// 判断文件路径是否为空 为空给默认路径 格式 2024/12/30/ // 判断文件路径是否为空 为空给默认路径 格式 2024/12/30/
if (StrUtil.isEmpty(path)) { if (CharSequenceUtil.isEmpty(path)) {
path = StorageUtils.localDefaultPath(); path = StorageUtils.localDefaultPath();
} }
// 判断文件夹是否存在 不存在则创建 // 判断文件夹是否存在 不存在则创建

View File

@@ -19,8 +19,8 @@ package top.continew.starter.storage.strategy;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.file.FileNameUtil; import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -156,7 +156,7 @@ public class OssStorageStrategy implements StorageStrategy<OssClient> {
// 格式化文件名 防止上传后重复 // 格式化文件名 防止上传后重复
String formatFileName = StorageUtils.formatFileName(fileName); String formatFileName = StorageUtils.formatFileName(fileName);
// 判断文件路径是否为空 为空给默认路径 格式 2024/12/30/ // 判断文件路径是否为空 为空给默认路径 格式 2024/12/30/
if (StrUtil.isEmpty(path)) { if (CharSequenceUtil.isEmpty(path)) {
path = StorageUtils.ossDefaultPath(); path = StorageUtils.ossDefaultPath();
} }
ThumbnailResp thumbnailResp = null; ThumbnailResp thumbnailResp = null;

View File

@@ -16,7 +16,7 @@
package top.continew.starter.storage.util; package top.continew.starter.storage.util;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.text.CharSequenceUtil;
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.regions.Region;
import top.continew.starter.core.constant.StringConstants; import top.continew.starter.core.constant.StringConstants;
import top.continew.starter.storage.constant.StorageConstant; import top.continew.starter.storage.constant.StorageConstant;
@@ -39,7 +39,7 @@ public class OssUtils {
* @return {@link Region } * @return {@link Region }
*/ */
public static Region getRegion(String region) { public static Region getRegion(String region) {
return StrUtil.isEmpty(region) ? Region.US_EAST_1 : Region.of(region); return CharSequenceUtil.isEmpty(region) ? Region.US_EAST_1 : Region.of(region);
} }
/** /**
@@ -51,7 +51,7 @@ public class OssUtils {
*/ */
public static String getUrl(String endpoint, String bucketName) { public static String getUrl(String endpoint, String bucketName) {
// 如果是云服务商,直接返回域名或终端点 // 如果是云服务商,直接返回域名或终端点
if (StrUtil.containsAny(endpoint, StorageConstant.CLOUD_SERVICE_PREFIX)) { if (CharSequenceUtil.containsAny(endpoint, StorageConstant.CLOUD_SERVICE_PREFIX)) {
return "http://" + bucketName + StringConstants.DOT + endpoint; return "http://" + bucketName + StringConstants.DOT + endpoint;
} else { } else {
return "http://" + endpoint + StringConstants.SLASH + bucketName; return "http://" + endpoint + StringConstants.SLASH + bucketName;