This is an automated email from the ASF dual-hosted git repository. pkarwasz pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 0df43d9853de75f95e6d71db2ad8d465c5426e66 Author: Gary Gregory <[email protected]> AuthorDate: Sat Feb 5 16:16:10 2022 -0500 Update for Carter's comments. --- .../org/apache/log4j/spi/ThrowableInformation.java | 29 ++++++++++++---------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/log4j-1.2-api/src/main/java/org/apache/log4j/spi/ThrowableInformation.java b/log4j-1.2-api/src/main/java/org/apache/log4j/spi/ThrowableInformation.java index de81a1a..cf0002c 100644 --- a/log4j-1.2-api/src/main/java/org/apache/log4j/spi/ThrowableInformation.java +++ b/log4j-1.2-api/src/main/java/org/apache/log4j/spi/ThrowableInformation.java @@ -16,6 +16,7 @@ */ package org.apache.log4j.spi; +import java.io.Serializable; import java.lang.reflect.Method; import java.util.List; @@ -25,14 +26,25 @@ import org.apache.logging.log4j.core.util.Throwables; /** * Log4j's internal representation of throwables. */ -public class ThrowableInformation implements java.io.Serializable { +public class ThrowableInformation implements Serializable { static final long serialVersionUID = -4748765566864322735L; private transient Throwable throwable; private transient Category category; private String[] rep; - private Method toStringList; + private static final Method TO_STRING_LIST; + + static { + Method method = null; + try { + final Class<?> throwables = Class.forName("org.apache.logging.log4j.core.util.Throwables"); + method = throwables.getMethod("toStringList", Throwable.class); + } catch (ClassNotFoundException | NoSuchMethodException ex) { + // Ignore the exception if Log4j-core is not present. + } + TO_STRING_LIST = method; + } /** * Constructs new instance. @@ -49,14 +61,6 @@ public class ThrowableInformation implements java.io.Serializable { */ public ThrowableInformation(Throwable throwable) { this.throwable = throwable; - Method method = null; - try { - final Class<?> throwables = Class.forName("org.apache.logging.log4j.core.util.Throwables"); - method = throwables.getMethod("toStringList", Throwable.class); - } catch (ClassNotFoundException | NoSuchMethodException ex) { - // Ignore the exception if Log4j-core is not present. - } - this.toStringList = method; } /** @@ -78,11 +82,10 @@ public class ThrowableInformation implements java.io.Serializable { } public synchronized String[] getThrowableStrRep() { - if (toStringList != null && throwable != null) { + if (TO_STRING_LIST != null && throwable != null) { try { @SuppressWarnings("unchecked") - final - List<String> elements = (List<String>) toStringList.invoke(null, throwable); + final List<String> elements = (List<String>) TO_STRING_LIST.invoke(null, throwable); if (elements != null) { return elements.toArray(new String[0]); }
