Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ dependency-reduced-pom.xml
node_modules/
package-lock.json
.DS_Store
.trae/
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 12 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<deploy.repo.release.id>sonatype-nexus-staging</deploy.repo.release.id>
<deploy.repo.release.url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</deploy.repo.release.url>

<java.version>1.8</java.version>
<java.version>21</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
Expand All @@ -83,7 +83,7 @@
<build.testJarPhase>none</build.testJarPhase>

<maven.assembly.plugin.version>3.1.1</maven.assembly.plugin.version>
<maven.compiler.plugin.version>3.8.1</maven.compiler.plugin.version>
<maven.compiler.plugin.version>3.11.0</maven.compiler.plugin.version>
<maven.checkstyle.plugin.version>3.1.1</maven.checkstyle.plugin.version>
<maven.scala.plugin.version>4.3.0</maven.scala.plugin.version>
<maven.exec.plugin.version>1.6.0</maven.exec.plugin.version>
Expand All @@ -93,10 +93,10 @@
<maven.javadoc.plugin.version>3.1.1</maven.javadoc.plugin.version>
<maven.jacoco.plugin.version>0.8.2</maven.jacoco.plugin.version>
<maven.os.plugin.version>1.7.0</maven.os.plugin.version>
<maven.shade.plugin.version>3.2.1</maven.shade.plugin.version>
<maven.shade.plugin.version>3.6.0</maven.shade.plugin.version>
<maven.source.plugin.version>3.1.0</maven.source.plugin.version>
<maven.surefire.plugin.version>3.0.0-M3</maven.surefire.plugin.version>
<maven.failsafe.plugin.version>3.0.0-M3</maven.failsafe.plugin.version>
<maven.surefire.plugin.version>3.2.5</maven.surefire.plugin.version>
<maven.failsafe.plugin.version>3.2.5</maven.failsafe.plugin.version>
<maven.release.plugin.version>2.5.3</maven.release.plugin.version>
<maven.checkstyle.plugin.version>3.1.1</maven.checkstyle.plugin.version>

Expand Down Expand Up @@ -351,7 +351,7 @@
<arg>-deprecation</arg>
<arg>-feature</arg>
<arg>-explaintypes</arg>
<arg>-target:jvm-1.8</arg>
<arg>-target:jvm-21</arg>
</args>
<jvmArgs>
<jvmArg>-Xms1024m</jvmArg>
Expand Down Expand Up @@ -566,6 +566,12 @@
<jdk>11</jdk>
</activation>
</profile>
<profile>
<id>java-21</id>
<activation>
<jdk>21</jdk>
</activation>
</profile>
<profile>
<id>scala-2.11</id>
<properties>
Expand Down
Loading