You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2022/01/08 15:06:56 UTC

[logging-log4j2] branch master updated: Add StackLocatorUtil.getCallerClassLoader(int) for the 1.2 bridge.

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/master by this push:
     new 2153bb8  Add StackLocatorUtil.getCallerClassLoader(int) for the 1.2 bridge.
2153bb8 is described below

commit 2153bb86e663219946dc2ef41abc83bdf88bd8f6
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jan 8 09:30:04 2022 -0500

    Add StackLocatorUtil.getCallerClassLoader(int) for the 1.2 bridge.
    
    - Port from release-2.x.
    - Add some Javadocs.
    - Use diamond.
---
 .../logging/log4j/util/StackLocatorUtilTest.java   |  5 +++++
 .../apache/logging/log4j/util/StackLocator.java    | 15 ++++++++++++++-
 .../logging/log4j/util/StackLocatorUtil.java       | 22 ++++++++++++++++++++++
 3 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/log4j-api-test/src/test/java/org/apache/logging/log4j/util/StackLocatorUtilTest.java b/log4j-api-test/src/test/java/org/apache/logging/log4j/util/StackLocatorUtilTest.java
index 69ced2a..07f5536 100644
--- a/log4j-api-test/src/test/java/org/apache/logging/log4j/util/StackLocatorUtilTest.java
+++ b/log4j-api-test/src/test/java/org/apache/logging/log4j/util/StackLocatorUtilTest.java
@@ -63,6 +63,11 @@ public class StackLocatorUtilTest {
     }
 
     @Test
+    public void testGetCallerClassLoader() throws Exception {
+        assertSame(StackLocatorUtilTest.class.getClassLoader(), StackLocatorUtil.getCallerClassLoader(1));
+    }
+
+    @Test
     public void testGetCallerClassNameViaStackTrace() throws Exception {
         final Class<?> expected = StackLocatorUtilTest.class;
         final Class<?> actual = Class.forName(new Throwable().getStackTrace()[0].getClassName());
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/StackLocator.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/StackLocator.java
index 2a92d8f..5772102 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/util/StackLocator.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/StackLocator.java
@@ -32,6 +32,11 @@ public final class StackLocator {
 
     private final static StackLocator INSTANCE = new StackLocator();
 
+    /**
+     * Gets the singleton instance.
+     *
+     * @return the singleton instance.
+     */
     public static StackLocator getInstance() {
         return INSTANCE;
     }
@@ -79,6 +84,14 @@ public final class StackLocator {
                 map(StackWalker.StackFrame::getDeclaringClass).orElse(null);
     }
 
+    /**
+     * Gets the Class of the method that called <em>this</em> method at the location up the call stack by the given stack
+     * frame depth.
+     *
+     * @param depth The stack frame count to walk.
+     * @return A class or null.
+     * @throws IndexOutOfBoundsException if depth is negative.
+     */
     @PerformanceSensitive
     public Class<?> getCallerClass(final int depth) {
         return walker.walk(s -> s.skip(depth).findFirst()).map(StackWalker.StackFrame::getDeclaringClass).orElse(null);
@@ -90,7 +103,7 @@ public final class StackLocator {
         if (PrivateSecurityManagerStackTraceUtil.isEnabled()) {
             return PrivateSecurityManagerStackTraceUtil.getCurrentStackTrace();
         }
-        final Stack<Class<?>> stack = new Stack<Class<?>>();
+        final Stack<Class<?>> stack = new Stack<>();
         final List<Class<?>> classes = walker.walk(s -> s.map(StackWalker.StackFrame::getDeclaringClass).collect(Collectors.toList()));
         stack.addAll(classes);
         return stack;
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/StackLocatorUtil.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/StackLocatorUtil.java
index d27fb9c..9eb7fb7 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/util/StackLocatorUtil.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/StackLocatorUtil.java
@@ -71,6 +71,28 @@ public final class StackLocatorUtil {
     }
 
     /**
+     * Gets the ClassLoader of the class that called <em>this</em> method at the location up the call stack by the given
+     * stack frame depth.
+     * <p>
+     * This method returns {@code null} if:
+     * </p>
+     * <ul>
+     * <li>{@code sun.reflect.Reflection.getCallerClass(int)} is not present.</li>
+     * <li>An exception is caught calling {@code sun.reflect.Reflection.getCallerClass(int)}.</li>
+     * <li>Some Class implementations may use null to represent the bootstrap class loader.</li>
+     * </ul>
+     *
+     * @param depth The stack frame count to walk.
+     * @return A class or null.
+     * @throws IndexOutOfBoundsException if depth is negative.
+     */
+    @PerformanceSensitive
+    public static ClassLoader getCallerClassLoader(final int depth) {
+        final Class<?> callerClass = stackLocator.getCallerClass(depth + 1);
+        return callerClass != null ? callerClass.getClassLoader() : null;
+    }
+
+    /**
      * Search for a calling class.
      *
      * @param sentinelClass Sentinel class at which to begin searching