8230058: Replace exception from sun.rmi.runtime.Log#getSource() with StackWalker

Reviewed-by: mchung, rriggs
This commit is contained in:
Philippe Marschall 2019-08-23 14:04:38 -04:00 committed by Roger Riggs
parent e98ba531a2
commit ed17c3aafb

View File

@ -28,8 +28,10 @@ package sun.rmi.runtime;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.lang.StackWalker.StackFrame;
import java.rmi.server.LogStream; import java.rmi.server.LogStream;
import java.security.PrivilegedAction; import java.security.PrivilegedAction;
import java.util.Set;
import java.util.logging.Handler; import java.util.logging.Handler;
import java.util.logging.SimpleFormatter; import java.util.logging.SimpleFormatter;
import java.util.logging.Level; import java.util.logging.Level;
@ -62,6 +64,8 @@ public abstract class Log {
public static final Level BRIEF = Level.FINE; public static final Level BRIEF = Level.FINE;
public static final Level VERBOSE = Level.FINER; public static final Level VERBOSE = Level.FINER;
private static final StackWalker WALKER = StackWalker.getInstance(Set.of(), 4);
/* selects log implementation */ /* selects log implementation */
private static final LogFactory logFactory; private static final LogFactory logFactory;
static { static {
@ -217,16 +221,16 @@ public abstract class Log {
public void log(Level level, String message) { public void log(Level level, String message) {
if (isLoggable(level)) { if (isLoggable(level)) {
String[] source = getSource(); StackFrame sourceFrame = getSource();
logger.logp(level, source[0], source[1], logger.logp(level, sourceFrame.getClassName(), sourceFrame.getMethodName(),
Thread.currentThread().getName() + ": " + message); Thread.currentThread().getName() + ": " + message);
} }
} }
public void log(Level level, String message, Throwable thrown) { public void log(Level level, String message, Throwable thrown) {
if (isLoggable(level)) { if (isLoggable(level)) {
String[] source = getSource(); StackFrame sourceFrame = getSource();
logger.logp(level, source[0], source[1], logger.logp(level, sourceFrame.getClassName(), sourceFrame.getMethodName(),
Thread.currentThread().getName() + ": " + Thread.currentThread().getName() + ": " +
message, thrown); message, thrown);
} }
@ -390,9 +394,9 @@ public abstract class Log {
public void log(Level messageLevel, String message) { public void log(Level messageLevel, String message) {
if (isLoggable(messageLevel)) { if (isLoggable(messageLevel)) {
String[] source = getSource(); StackFrame sourceFrame = getSource();
stream.println(unqualifiedName(source[0]) + stream.println(unqualifiedName(sourceFrame.getClassName()) +
"." + source[1] + ": " + message); "." + sourceFrame.getMethodName() + ": " + message);
} }
} }
@ -403,9 +407,9 @@ public abstract class Log {
* RemoteServer.getLog * RemoteServer.getLog
*/ */
synchronized (stream) { synchronized (stream) {
String[] source = getSource(); StackFrame sourceFrame = getSource();
stream.println(unqualifiedName(source[0]) + "." + stream.println(unqualifiedName(sourceFrame.getClassName()) + "." +
source[1] + ": " + message); sourceFrame.getMethodName() + ": " + message);
thrown.printStackTrace(stream); thrown.printStackTrace(stream);
} }
} }
@ -441,13 +445,12 @@ public abstract class Log {
} }
/** /**
* Obtain class and method names of code calling a log method. * Obtain stack frame of code calling a log method.
*/ */
private static String[] getSource() { private static StackFrame getSource() {
StackTraceElement[] trace = (new Exception()).getStackTrace(); return WALKER.walk(s -> s
return new String[] { .skip(3)
trace[3].getClassName(), .findFirst()
trace[3].getMethodName() .get());
};
} }
} }