8230058: Replace exception from sun.rmi.runtime.Log#getSource() with StackWalker
authorrriggs
Fri, 23 Aug 2019 14:04:38 -0400
changeset 57866 1e85670cb9ee
parent 57865 489b8e142559
child 57867 36f5e20be69a
8230058: Replace exception from sun.rmi.runtime.Log#getSource() with StackWalker Reviewed-by: mchung, rriggs Contributed-by: kustos@gmx.net
src/java.rmi/share/classes/sun/rmi/runtime/Log.java
--- a/src/java.rmi/share/classes/sun/rmi/runtime/Log.java	Fri Aug 23 10:39:34 2019 -0700
+++ b/src/java.rmi/share/classes/sun/rmi/runtime/Log.java	Fri Aug 23 14:04:38 2019 -0400
@@ -28,8 +28,10 @@
 import java.io.ByteArrayOutputStream;
 import java.io.PrintStream;
 import java.io.OutputStream;
+import java.lang.StackWalker.StackFrame;
 import java.rmi.server.LogStream;
 import java.security.PrivilegedAction;
+import java.util.Set;
 import java.util.logging.Handler;
 import java.util.logging.SimpleFormatter;
 import java.util.logging.Level;
@@ -62,6 +64,8 @@
     public static final Level BRIEF = Level.FINE;
     public static final Level VERBOSE = Level.FINER;
 
+    private static final StackWalker WALKER = StackWalker.getInstance(Set.of(), 4);
+
     /* selects log implementation */
     private static final LogFactory logFactory;
     static {
@@ -217,16 +221,16 @@
 
         public void log(Level level, String message) {
             if (isLoggable(level)) {
-                String[] source = getSource();
-                logger.logp(level, source[0], source[1],
+                StackFrame sourceFrame = getSource();
+                logger.logp(level, sourceFrame.getClassName(), sourceFrame.getMethodName(),
                            Thread.currentThread().getName() + ": " + message);
             }
         }
 
         public void log(Level level, String message, Throwable thrown) {
             if (isLoggable(level)) {
-                String[] source = getSource();
-                logger.logp(level, source[0], source[1],
+                StackFrame sourceFrame = getSource();
+                logger.logp(level, sourceFrame.getClassName(), sourceFrame.getMethodName(),
                     Thread.currentThread().getName() + ": " +
                            message, thrown);
             }
@@ -390,9 +394,9 @@
 
         public void log(Level messageLevel, String message) {
             if (isLoggable(messageLevel)) {
-                String[] source = getSource();
-                stream.println(unqualifiedName(source[0]) +
-                               "." + source[1] + ": " + message);
+                StackFrame sourceFrame = getSource();
+                stream.println(unqualifiedName(sourceFrame.getClassName()) +
+                               "." + sourceFrame.getMethodName() + ": " + message);
             }
         }
 
@@ -403,9 +407,9 @@
                  * RemoteServer.getLog
                  */
                 synchronized (stream) {
-                    String[] source = getSource();
-                    stream.println(unqualifiedName(source[0]) + "." +
-                                   source[1] + ": " + message);
+                    StackFrame sourceFrame = getSource();
+                    stream.println(unqualifiedName(sourceFrame.getClassName()) + "." +
+                                    sourceFrame.getMethodName() + ": " + message);
                     thrown.printStackTrace(stream);
                 }
             }
@@ -441,13 +445,12 @@
     }
 
     /**
-     * Obtain class and method names of code calling a log method.
+     * Obtain stack frame of code calling a log method.
      */
-    private static String[] getSource() {
-        StackTraceElement[] trace = (new Exception()).getStackTrace();
-        return new String[] {
-            trace[3].getClassName(),
-            trace[3].getMethodName()
-        };
+    private static StackFrame getSource() {
+        return WALKER.walk(s -> s
+                                 .skip(3)
+                                 .findFirst()
+                                 .get());
     }
 }