Skip to content

Commit 8e798ba

Browse files
authored
feat: Add Java 21 support and ignore .trae directory (#468)
1 parent 98a13f4 commit 8e798ba

File tree

3 files changed

+27
-57
lines changed

3 files changed

+27
-57
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ dependency-reduced-pom.xml
99
node_modules/
1010
package-lock.json
1111
.DS_Store
12+
.trae/

clickhouse-native-jdbc/src/main/java/com/github/housepower/log/Util.java

Lines changed: 14 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -70,65 +70,28 @@ public static boolean safeGetBooleanSystemProperty(String key) {
7070
return value.equalsIgnoreCase("true");
7171
}
7272

73-
/**
74-
* In order to call {@link SecurityManager#getClassContext()}, which is a
75-
* protected method, we add this wrapper which allows the method to be visible
76-
* inside this package.
77-
*/
78-
private static final class ClassContextSecurityManager extends SecurityManager {
79-
protected Class<?>[] getClassContext() {
80-
return super.getClassContext();
81-
}
82-
}
83-
84-
private static ClassContextSecurityManager SECURITY_MANAGER;
85-
private static boolean SECURITY_MANAGER_CREATION_ALREADY_ATTEMPTED = false;
86-
87-
private static ClassContextSecurityManager getSecurityManager() {
88-
if (SECURITY_MANAGER != null)
89-
return SECURITY_MANAGER;
90-
else if (SECURITY_MANAGER_CREATION_ALREADY_ATTEMPTED)
91-
return null;
92-
else {
93-
SECURITY_MANAGER = safeCreateSecurityManager();
94-
SECURITY_MANAGER_CREATION_ALREADY_ATTEMPTED = true;
95-
return SECURITY_MANAGER;
96-
}
97-
}
98-
99-
private static ClassContextSecurityManager safeCreateSecurityManager() {
100-
try {
101-
return new ClassContextSecurityManager();
102-
} catch (SecurityException sm) {
103-
return null;
104-
}
105-
}
106-
10773
/**
10874
* Returns the name of the class which called the invoking method.
75+
* Uses StackWalker API (Java 9+) instead of deprecated SecurityManager.
10976
*
11077
* @return the name of the class which called the invoking method.
11178
*/
11279
public static Class<?> getCallingClass() {
113-
ClassContextSecurityManager securityManager = getSecurityManager();
114-
if (securityManager == null)
80+
try {
81+
StackWalker walker = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
82+
return walker.walk(stream -> {
83+
String thisClassName = Util.class.getName();
84+
return stream
85+
.map(StackWalker.StackFrame::getDeclaringClass)
86+
.dropWhile(clazz -> !thisClassName.equals(clazz.getName()))
87+
.skip(2) // Skip Util class and the immediate caller
88+
.findFirst()
89+
.orElse(null);
90+
});
91+
} catch (Exception e) {
92+
// Fallback to null if StackWalker fails
11593
return null;
116-
Class<?>[] trace = securityManager.getClassContext();
117-
String thisClassName = Util.class.getName();
118-
119-
// Advance until Util is found
120-
int i;
121-
for (i = 0; i < trace.length; i++) {
122-
if (thisClassName.equals(trace[i].getName()))
123-
break;
12494
}
125-
126-
// trace[i] = Util; trace[i+1] = caller; trace[i+2] = caller's caller
127-
if (i >= trace.length || i + 2 >= trace.length) {
128-
throw new IllegalStateException("Failed to find its caller in the stack; " + "this should not happen");
129-
}
130-
131-
return trace[i + 2];
13295
}
13396

13497
static public void report(String msg, Throwable t) {

pom.xml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
<deploy.repo.release.id>sonatype-nexus-staging</deploy.repo.release.id>
7474
<deploy.repo.release.url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</deploy.repo.release.url>
7575

76-
<java.version>1.8</java.version>
76+
<java.version>21</java.version>
7777
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
7878
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
7979
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
@@ -83,7 +83,7 @@
8383
<build.testJarPhase>none</build.testJarPhase>
8484

8585
<maven.assembly.plugin.version>3.1.1</maven.assembly.plugin.version>
86-
<maven.compiler.plugin.version>3.8.1</maven.compiler.plugin.version>
86+
<maven.compiler.plugin.version>3.11.0</maven.compiler.plugin.version>
8787
<maven.checkstyle.plugin.version>3.1.1</maven.checkstyle.plugin.version>
8888
<maven.scala.plugin.version>4.3.0</maven.scala.plugin.version>
8989
<maven.exec.plugin.version>1.6.0</maven.exec.plugin.version>
@@ -93,10 +93,10 @@
9393
<maven.javadoc.plugin.version>3.1.1</maven.javadoc.plugin.version>
9494
<maven.jacoco.plugin.version>0.8.2</maven.jacoco.plugin.version>
9595
<maven.os.plugin.version>1.7.0</maven.os.plugin.version>
96-
<maven.shade.plugin.version>3.2.1</maven.shade.plugin.version>
96+
<maven.shade.plugin.version>3.6.0</maven.shade.plugin.version>
9797
<maven.source.plugin.version>3.1.0</maven.source.plugin.version>
98-
<maven.surefire.plugin.version>3.0.0-M3</maven.surefire.plugin.version>
99-
<maven.failsafe.plugin.version>3.0.0-M3</maven.failsafe.plugin.version>
98+
<maven.surefire.plugin.version>3.2.5</maven.surefire.plugin.version>
99+
<maven.failsafe.plugin.version>3.2.5</maven.failsafe.plugin.version>
100100
<maven.release.plugin.version>2.5.3</maven.release.plugin.version>
101101
<maven.checkstyle.plugin.version>3.1.1</maven.checkstyle.plugin.version>
102102

@@ -351,7 +351,7 @@
351351
<arg>-deprecation</arg>
352352
<arg>-feature</arg>
353353
<arg>-explaintypes</arg>
354-
<arg>-target:jvm-1.8</arg>
354+
<arg>-target:jvm-21</arg>
355355
</args>
356356
<jvmArgs>
357357
<jvmArg>-Xms1024m</jvmArg>
@@ -566,6 +566,12 @@
566566
<jdk>11</jdk>
567567
</activation>
568568
</profile>
569+
<profile>
570+
<id>java-21</id>
571+
<activation>
572+
<jdk>21</jdk>
573+
</activation>
574+
</profile>
569575
<profile>
570576
<id>scala-2.11</id>
571577
<properties>

0 commit comments

Comments
 (0)