mirror of
https://gitee.com/jzsw-it/yexuejc-base.git
synced 2025-10-25 19:31:22 +08:00
[update] StrUtil.printStackTrace 获取异常堆栈优化
Some checks failed
yexuejc-base package jre11 / package_job (push) Failing after 38s
Some checks failed
yexuejc-base package jre11 / package_job (push) Failing after 38s
This commit is contained in:
@@ -1,20 +1,27 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
import com.yexuejc.base.constant.ExpCode;
|
||||
import com.yexuejc.base.exception.BaseException;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.yexuejc.base.constant.ExpCode;
|
||||
import com.yexuejc.base.exception.BaseException;
|
||||
|
||||
/**
|
||||
* 字符串工具类
|
||||
*
|
||||
@@ -25,12 +32,12 @@ import java.util.regex.Pattern;
|
||||
*/
|
||||
public final class StrUtil {
|
||||
private static final Logger logger = Logger.getLogger(StrUtil.class.getName());
|
||||
|
||||
|
||||
private StrUtil() {
|
||||
}
|
||||
|
||||
private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
|
||||
|
||||
|
||||
// ThreadLocal缓存MessageDigest实例,提高性能
|
||||
private static final ThreadLocal<MessageDigest> MD5_DIGEST = ThreadLocal.withInitial(() -> {
|
||||
try {
|
||||
@@ -39,7 +46,7 @@ public final class StrUtil {
|
||||
throw new RuntimeException(new BaseException(e, ExpCode.MD5_ALGORITHM_UNAVAILABLE));
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
private static final ThreadLocal<MessageDigest> SHA256_DIGEST = ThreadLocal.withInitial(() -> {
|
||||
try {
|
||||
return MessageDigest.getInstance("SHA-256");
|
||||
@@ -47,7 +54,7 @@ public final class StrUtil {
|
||||
throw new RuntimeException(new BaseException(e, ExpCode.SHA256_ALGORITHM_UNAVAILABLE));
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// 使用SecureRandom替代Random提高安全性
|
||||
private static final ThreadLocal<SecureRandom> SECURE_RANDOM = ThreadLocal.withInitial(SecureRandom::new);
|
||||
|
||||
@@ -65,8 +72,7 @@ public final class StrUtil {
|
||||
return ((Optional<?>) obj).isEmpty();
|
||||
} else if (obj instanceof CharSequence) {
|
||||
return ((CharSequence) obj).length() == 0;
|
||||
} else if (obj.getClass()
|
||||
.isArray()) {
|
||||
} else if (obj.getClass().isArray()) {
|
||||
return Array.getLength(obj) == 0;
|
||||
} else if (obj instanceof Collection) {
|
||||
return ((Collection<?>) obj).isEmpty();
|
||||
@@ -85,9 +91,7 @@ public final class StrUtil {
|
||||
* @return
|
||||
*/
|
||||
public static String genUUID() {
|
||||
return UUID.randomUUID()
|
||||
.toString()
|
||||
.replaceAll("-", "");
|
||||
return UUID.randomUUID().toString().replaceAll("-", "");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -122,9 +126,7 @@ public final class StrUtil {
|
||||
* @return
|
||||
*/
|
||||
public static String genNum() {
|
||||
int hashCode = UUID.randomUUID()
|
||||
.toString()
|
||||
.hashCode();
|
||||
int hashCode = UUID.randomUUID().toString().hashCode();
|
||||
StringBuilder num = new StringBuilder();
|
||||
if (hashCode < 0) {
|
||||
hashCode = -hashCode;
|
||||
@@ -132,8 +134,7 @@ public final class StrUtil {
|
||||
} else {
|
||||
num.append("1");
|
||||
}
|
||||
return num.append(String.format("%010d", hashCode))
|
||||
.substring(0, 8);
|
||||
return num.append(String.format("%010d", hashCode)).substring(0, 8);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -183,13 +184,13 @@ public final class StrUtil {
|
||||
* @return SHA256值
|
||||
*/
|
||||
public static String toSHA256(final String str) {
|
||||
return toSHA(str, "SHA-256");
|
||||
return toSHA(str, "SHA-256");
|
||||
}
|
||||
|
||||
/**
|
||||
* SHA加密
|
||||
*
|
||||
* @param str 要加密的字符串
|
||||
* @param str 要加密的字符串
|
||||
* @param algorithm 算法名称
|
||||
* @return SHA值
|
||||
*/
|
||||
@@ -340,10 +341,7 @@ public final class StrUtil {
|
||||
for (String key : keys) {
|
||||
Object value = sortedParams.get(key);
|
||||
if (isNotEmpty(key) && isNotEmpty(value)) {
|
||||
content.append(index == 0 ? "" : "&")
|
||||
.append(key)
|
||||
.append("=")
|
||||
.append(value);
|
||||
content.append(index == 0 ? "" : "&").append(key).append("=").append(value);
|
||||
++index;
|
||||
}
|
||||
}
|
||||
@@ -464,17 +462,14 @@ public final class StrUtil {
|
||||
if (cause != null) {
|
||||
StringBuilder msg = new StringBuilder();
|
||||
if (isNotEmpty(cause.getMessage())) {
|
||||
msg.append(cause.getMessage())
|
||||
.append(NEW_LINE);
|
||||
msg.append(cause.getMessage()).append(NEW_LINE);
|
||||
}
|
||||
String causedMsg = printStackTrace(cause, (eMessage) -> {
|
||||
if (isNotEmpty(eMessage)) {
|
||||
msg.append(eMessage)
|
||||
.append(NEW_LINE);
|
||||
msg.append(eMessage).append(NEW_LINE);
|
||||
}
|
||||
});
|
||||
return msg.append(causedMsg)
|
||||
.toString();
|
||||
return msg.append(causedMsg).toString();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
@@ -493,30 +488,24 @@ public final class StrUtil {
|
||||
|
||||
private static String printStackTrace(Throwable cause, Consumer<String> consumer) {
|
||||
if (cause != null) {
|
||||
String err = "\tat %s.%s(%s.java:%d)";
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String cClass = cause.getClass()
|
||||
.getName();
|
||||
String cClass = cause.getClass().getName();
|
||||
String eMessage = cause.getMessage();
|
||||
StackTraceElement[] stackTrace = cause.getStackTrace();
|
||||
Throwable caused = cause.getCause();
|
||||
while (caused != null) {
|
||||
cClass = caused.getClass()
|
||||
.getName();
|
||||
eMessage = caused.getMessage();
|
||||
stackTrace = caused.getStackTrace();
|
||||
caused = caused.getCause();
|
||||
consumer.accept(eMessage);
|
||||
Throwable c = cause.getCause();
|
||||
while (c != null) {
|
||||
cClass = c.getClass().getName();
|
||||
eMessage = c.getMessage();
|
||||
stackTrace = c.getStackTrace();
|
||||
c = c.getCause();
|
||||
}
|
||||
sb.append("Caused by: ")
|
||||
.append(cClass)
|
||||
.append(": ")
|
||||
.append(eMessage)
|
||||
.append(NEW_LINE);
|
||||
sb.append("Caused by: ").append(cClass).append(": ").append(eMessage).append(System.lineSeparator());
|
||||
for (StackTraceElement element : stackTrace) {
|
||||
sb.append("\tat ");
|
||||
sb.append(String.format(ERROR_MESSAGE_FORMAT, element.getClassName(), element.getMethodName(), element.getFileName(),
|
||||
element.getLineNumber()));
|
||||
sb.append(NEW_LINE);
|
||||
String className = element.getClassName();
|
||||
String simpleClassName = className.substring(className.lastIndexOf('.') + 1);
|
||||
sb.append(String.format(err, className, element.getMethodName(), simpleClassName, element.getLineNumber()))
|
||||
.append(System.lineSeparator());
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user