logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1481672 - in /logging/log4j/log4j2/trunk: core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java core/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyTest.java src/changes/changes.xml
Date Sun, 12 May 2013 23:46:59 GMT
Author: rgoers
Date: Sun May 12 23:46:59 2013
New Revision: 1481672

URL: http://svn.apache.org/r1481672
Log:
LOG4J2-245 - Avoid EmptyStack exception if getCallerClass and SecurityManager are not available.

Added:
    logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyTest.java
Modified:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java
    logging/log4j/log4j2/trunk/src/changes/changes.xml

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java?rev=1481672&r1=1481671&r2=1481672&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/impl/ThrowableProxy.java
Sun May 12 23:46:59 2013
@@ -317,7 +317,7 @@ public class ThrowableProxy extends Thro
      * @param stackTrace The stack trace being resolved.
      * @return The StackTracePackageElement array.
      */
-    private StackTracePackageElement[] resolvePackageData(final Stack<Class<?>>
stack, final Map<String,
+    StackTracePackageElement[] resolvePackageData(final Stack<Class<?>> stack,
final Map<String,
                                                           CacheEntry> map,
                                                           final StackTraceElement[] rootTrace,
                                                           final StackTraceElement[] stackTrace)
{
@@ -336,19 +336,19 @@ public class ThrowableProxy extends Thro
             stackLength = stackTrace.length;
         }
         final StackTracePackageElement[] packageArray = new StackTracePackageElement[stackLength];
-        Class<?> clazz = stack.peek();
+        Class<?> clazz = stack.isEmpty() ? null : stack.peek();
         ClassLoader lastLoader = null;
         for (int i = stackLength - 1; i >= 0; --i) {
             final String className = stackTrace[i].getClassName();
             // The stack returned from getCurrentStack will be missing entries for  java.lang.reflect.Method.invoke()
             // and its implementation. The Throwable might also contain stack entries that
are no longer
             // present as those methods have returned.
-            if (className.equals(clazz.getName())) {
+            if (clazz != null && className.equals(clazz.getName())) {
                 final CacheEntry entry = resolvePackageElement(clazz, true);
                 packageArray[i] = entry.element;
                 lastLoader = entry.loader;
                 stack.pop();
-                clazz = stack.peek();
+                clazz = stack.isEmpty() ? null : stack.peek();
             } else {
                 if (map.containsKey(className)) {
                     final CacheEntry entry = map.get(className);
@@ -486,7 +486,8 @@ public class ThrowableProxy extends Thro
             final Method[] methods = clazz.getMethods();
             for (final Method method : methods) {
                 final int modifier = method.getModifiers();
-                if (method.getName().equals("getCallerClass") && Modifier.isStatic(modifier))
{
+                if (method.getName().equals("getCallerClass") && Modifier.isStatic(modifier)
&&
+                    method.getParameterTypes().length == 1) {
                     getCallerClass = method;
                     return;
                 }
@@ -535,7 +536,7 @@ public class ThrowableProxy extends Thro
     /**
      * Cached StackTracePackageElement and the ClassLoader.
      */
-    private class CacheEntry {
+    class CacheEntry {
         private final StackTracePackageElement element;
         private final ClassLoader loader;
 

Added: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyTest.java?rev=1481672&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyTest.java
(added)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyTest.java
Sun May 12 23:46:59 2013
@@ -0,0 +1,41 @@
+/*
+ * 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 org.junit.Assert;
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Stack;
+
+/**
+ *
+ */
+public class ThrowableProxyTest {
+
+    @Test
+    public void testStack() {
+        final Map<String, ThrowableProxy.CacheEntry> map = new HashMap<String, ThrowableProxy.CacheEntry>();
+        final Stack<Class<?>> stack = new Stack<Class<?>>();
+        Throwable throwable = new IllegalStateException("This is a test");
+        ThrowableProxy proxy = new ThrowableProxy(throwable);
+        StackTracePackageElement[] callerPackageData = proxy.resolvePackageData(stack, map,
null,
+            throwable.getStackTrace());
+        Assert.assertNotNull("No package data returned", callerPackageData);;
+    }
+}

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1481672&r1=1481671&r2=1481672&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Sun May 12 23:46:59 2013
@@ -22,6 +22,9 @@
   </properties>
   <body>
     <release version="2.0-beta7" date="2013-??-??" description="Bug fixes and enhancements">
+      <action issue="LOG4J2-245" dev="rgoers" type="fix">
+        Avoid EmptyStack exception if getCallerClass and SecurityManager are not available.
+      </action>
       <action issue="LOG4J2-229" dev="ggregory" type="add" due-to="beamerblvd">
         New JDBC, JPA, and NoSQL database Appenders.
       </action>
@@ -29,7 +32,7 @@
         SocketServer.isActive should be volatile because it is accessed from different threads.
       </action>
       <action issue="LOG4J2-251" dev="sdeboy" type="add">
-        Provide configuration information (location, content type, content if possible) via
a registered Advertiser. 
+        Provide configuration information (location, content type, content if possible) via
a registered Advertiser.
       </action>
     </release>
     <release version="2.0-beta6" date="2013-05-05" description="Bug fixes and enhancements">



Mime
View raw message