diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java index 2320ad7..daa16b3 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java @@ -500,7 +500,8 @@ if (clazz != null) { return clazz; } - } catch (final Exception ignore) { + } catch (final Throwable ignore) { + // LOG4J2-832 catches Throwable instead of Exception // Ignore exception. } } diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyOracleTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyOracleTest.java new file mode 100644 index 0000000..1c35ec0 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyOracleTest.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.log4j.core.impl; + +import java.lang.reflect.Method; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.Test; + +//TODO: How can we get Maven to compile this conditionally? +//import sun.reflect.misc.MethodUtil; + +/** + * Depends on internal Oracle JRE classes. + * + * TODO: How can we conditionally compile and run this class only on Oracle JREs? + * + * @see "LOG4J2-832" + */ +public class ThrowableProxyOracleTest { + + static class AlwaysThrowsException { + public void throwException() { + throw new IllegalStateException(); + } + } + + /** + * Invokes a method via reflection that always throws an exception. It is invoked by using the + * {@link sun.reflect.misc.MethodUtil} class which uses a hidden "trampoline" object to perform the invocation. + *

+ * The logger must not fail when trying to use {@link ThrowableProxy} to inspect the frames of the stack trace. + *

+ */ + @Test + public void testTrampolineException() { + try { + Method firstMethod = AlwaysThrowsException.class.getMethod("throwException"); + // TODO: How can we get Maven to compile this conditionally? + // MethodUtil.invoke(firstMethod, new AlwaysThrowsException(), new Object[0]); + } catch (Throwable e) { + // e.printStackTrace(); + Logger logger = LogManager.getLogger(getClass()); + // Throws java.lang.Error if not fixed + logger.error(e.getMessage(), e); + } + } + +}