From c18af5a01bc25335677be3d284bb146956310b77 Mon Sep 17 00:00:00 2001 From: gauss1314 Date: Sun, 22 Jun 2025 16:52:42 +0800 Subject: [PATCH] feat: Add Java 21 support and ignore .trae directory --- .gitignore | 1 + .../java/com/github/housepower/log/Util.java | 65 ++++--------------- pom.xml | 18 +++-- 3 files changed, 27 insertions(+), 57 deletions(-) diff --git a/.gitignore b/.gitignore index ef89f18c7..46298d57a 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ dependency-reduced-pom.xml node_modules/ package-lock.json .DS_Store +.trae/ diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/log/Util.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/log/Util.java index 5e50dc8c4..559262336 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/log/Util.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/log/Util.java @@ -70,65 +70,28 @@ public static boolean safeGetBooleanSystemProperty(String key) { return value.equalsIgnoreCase("true"); } - /** - * In order to call {@link SecurityManager#getClassContext()}, which is a - * protected method, we add this wrapper which allows the method to be visible - * inside this package. - */ - private static final class ClassContextSecurityManager extends SecurityManager { - protected Class[] getClassContext() { - return super.getClassContext(); - } - } - - private static ClassContextSecurityManager SECURITY_MANAGER; - private static boolean SECURITY_MANAGER_CREATION_ALREADY_ATTEMPTED = false; - - private static ClassContextSecurityManager getSecurityManager() { - if (SECURITY_MANAGER != null) - return SECURITY_MANAGER; - else if (SECURITY_MANAGER_CREATION_ALREADY_ATTEMPTED) - return null; - else { - SECURITY_MANAGER = safeCreateSecurityManager(); - SECURITY_MANAGER_CREATION_ALREADY_ATTEMPTED = true; - return SECURITY_MANAGER; - } - } - - private static ClassContextSecurityManager safeCreateSecurityManager() { - try { - return new ClassContextSecurityManager(); - } catch (SecurityException sm) { - return null; - } - } - /** * Returns the name of the class which called the invoking method. + * Uses StackWalker API (Java 9+) instead of deprecated SecurityManager. * * @return the name of the class which called the invoking method. */ public static Class getCallingClass() { - ClassContextSecurityManager securityManager = getSecurityManager(); - if (securityManager == null) + try { + StackWalker walker = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE); + return walker.walk(stream -> { + String thisClassName = Util.class.getName(); + return stream + .map(StackWalker.StackFrame::getDeclaringClass) + .dropWhile(clazz -> !thisClassName.equals(clazz.getName())) + .skip(2) // Skip Util class and the immediate caller + .findFirst() + .orElse(null); + }); + } catch (Exception e) { + // Fallback to null if StackWalker fails return null; - Class[] trace = securityManager.getClassContext(); - String thisClassName = Util.class.getName(); - - // Advance until Util is found - int i; - for (i = 0; i < trace.length; i++) { - if (thisClassName.equals(trace[i].getName())) - break; } - - // trace[i] = Util; trace[i+1] = caller; trace[i+2] = caller's caller - if (i >= trace.length || i + 2 >= trace.length) { - throw new IllegalStateException("Failed to find its caller in the stack; " + "this should not happen"); - } - - return trace[i + 2]; } static public void report(String msg, Throwable t) { diff --git a/pom.xml b/pom.xml index 27eef0d61..1264b9396 100644 --- a/pom.xml +++ b/pom.xml @@ -73,7 +73,7 @@ sonatype-nexus-staging https://oss.sonatype.org/service/local/staging/deploy/maven2/ - 1.8 + 21 UTF-8 UTF-8 UTF-8 @@ -83,7 +83,7 @@ none 3.1.1 - 3.8.1 + 3.11.0 3.1.1 4.3.0 1.6.0 @@ -93,10 +93,10 @@ 3.1.1 0.8.2 1.7.0 - 3.2.1 + 3.6.0 3.1.0 - 3.0.0-M3 - 3.0.0-M3 + 3.2.5 + 3.2.5 2.5.3 3.1.1 @@ -351,7 +351,7 @@ -deprecation -feature -explaintypes - -target:jvm-1.8 + -target:jvm-21 -Xms1024m @@ -566,6 +566,12 @@ 11 + + java-21 + + 21 + + scala-2.11