View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.util;
18  
19  import java.lang.reflect.Method;
20  import java.util.Stack;
21  
22  /**
23   * <em>Consider this class private.</em> Provides various methods to determine the caller class. <h3>Background</h3>
24   * <p>
25   * This method, available only in the Oracle/Sun/OpenJDK implementations of the Java Virtual Machine, is a much more
26   * efficient mechanism for determining the {@link Class} of the caller of a particular method. When it is not available,
27   * a {@link SecurityManager} is the second-best option. When this is also not possible, the {@code StackTraceElement[]}
28   * returned by {@link Throwable#getStackTrace()} must be used, and its {@code String} class name converted to a
29   * {@code Class} using the slow {@link Class#forName} (which can add an extra microsecond or more for each invocation
30   * depending on the runtime ClassLoader hierarchy).
31   * </p>
32   * <p>
33   * During Java 8 development, the {@code sun.reflect.Reflection.getCallerClass(int)} was removed from OpenJDK, and this
34   * change was back-ported to Java 7 in version 1.7.0_25 which changed the behavior of the call and caused it to be off
35   * by one stack frame. This turned out to be beneficial for the survival of this API as the change broke hundreds of
36   * libraries and frameworks relying on the API which brought much more attention to the intended API removal.
37   * </p>
38   * <p>
39   * After much community backlash, the JDK team agreed to restore {@code getCallerClass(int)} and keep its existing
40   * behavior for the rest of Java 7. However, the method is deprecated in Java 8, and current Java 9 development has not
41   * addressed this API. Therefore, the functionality of this class cannot be relied upon for all future versions of Java.
42   * It does, however, work just fine in Sun JDK 1.6, OpenJDK 1.6, Oracle/OpenJDK 1.7, and Oracle/OpenJDK 1.8. Other Java
43   * environments may fall back to using {@link Throwable#getStackTrace()} which is significantly slower due to
44   * examination of every virtual frame of execution.
45   * </p>
46   */
47  public final class StackLocator {
48  
49      // Checkstyle Suppress: the lower-case 'u' ticks off CheckStyle...
50      // CHECKSTYLE:OFF
51      static final int JDK_7u25_OFFSET;
52      // CHECKSTYLE:OFF
53  
54      private static final Method GET_CALLER_CLASS;
55  
56      private static final StackLocator INSTANCE;
57  
58      static {
59          Method getCallerClass;
60          int java7u25CompensationOffset = 0;
61          try {
62              final Class<?> sunReflectionClass = LoaderUtil.loadClass("sun.reflect.Reflection");
63              getCallerClass = sunReflectionClass.getDeclaredMethod("getCallerClass", int.class);
64              Object o = getCallerClass.invoke(null, 0);
65              getCallerClass.invoke(null, 0);
66              if (o == null || o != sunReflectionClass) {
67                  getCallerClass = null;
68                  java7u25CompensationOffset = -1;
69              } else {
70                  o = getCallerClass.invoke(null, 1);
71                  if (o == sunReflectionClass) {
72                      System.out.println("WARNING: Java 1.7.0_25 is in use which has a broken implementation of Reflection.getCallerClass(). " +
73                          " Please consider upgrading to Java 1.7.0_40 or later.");
74                      java7u25CompensationOffset = 1;
75                  }
76              }
77          } catch (final Exception | LinkageError e) {
78              System.out.println("WARNING: sun.reflect.Reflection.getCallerClass is not supported. This will impact performance.");
79              getCallerClass = null;
80              java7u25CompensationOffset = -1;
81          }
82  
83          GET_CALLER_CLASS = getCallerClass;
84          JDK_7u25_OFFSET = java7u25CompensationOffset;
85  
86          INSTANCE = new StackLocator();
87      }
88  
89      public static StackLocator getInstance() {
90          return INSTANCE;
91      }
92  
93      private StackLocator() {
94      }
95  
96      // TODO: return Object.class instead of null (though it will have a null ClassLoader)
97      // (MS) I believe this would work without any modifications elsewhere, but I could be wrong
98  
99      // migrated from ReflectiveCallerClassUtility
100     @PerformanceSensitive
101     public Class<?> getCallerClass(final int depth) {
102         if (depth < 0) {
103             throw new IndexOutOfBoundsException(Integer.toString(depth));
104         }
105         // note that we need to add 1 to the depth value to compensate for this method, but not for the Method.invoke
106         // since Reflection.getCallerClass ignores the call to Method.invoke()
107         try {
108             return (Class<?>) GET_CALLER_CLASS.invoke(null, depth + 1 + JDK_7u25_OFFSET);
109         } catch (final Exception e) {
110             // theoretically this could happen if the caller class were native code
111             // TODO: return Object.class
112             return null;
113         }
114     }
115 
116     // migrated from Log4jLoggerFactory
117     @PerformanceSensitive
118     public Class<?> getCallerClass(final String fqcn, final String pkg) {
119         boolean next = false;
120         Class<?> clazz;
121         for (int i = 2; null != (clazz = getCallerClass(i)); i++) {
122             if (fqcn.equals(clazz.getName())) {
123                 next = true;
124                 continue;
125             }
126             if (next && clazz.getName().startsWith(pkg)) {
127                 return clazz;
128             }
129         }
130         // TODO: return Object.class
131         return null;
132     }
133 
134     // added for use in LoggerAdapter implementations mainly
135     @PerformanceSensitive
136     public Class<?> getCallerClass(final Class<?> anchor) {
137         boolean next = false;
138         Class<?> clazz;
139         for (int i = 2; null != (clazz = getCallerClass(i)); i++) {
140             if (anchor.equals(clazz)) {
141                 next = true;
142                 continue;
143             }
144             if (next) {
145                 return clazz;
146             }
147         }
148         return Object.class;
149     }
150 
151     // migrated from ThrowableProxy
152     @PerformanceSensitive
153     public Stack<Class<?>> getCurrentStackTrace() {
154         // benchmarks show that using the SecurityManager is much faster than looping through getCallerClass(int)
155         if (PrivateSecurityManagerStackTraceUtil.isEnabled()) {
156             return PrivateSecurityManagerStackTraceUtil.getCurrentStackTrace();
157         }
158         // slower version using getCallerClass where we cannot use a SecurityManager
159         final Stack<Class<?>> classes = new Stack<>();
160         Class<?> clazz;
161         for (int i = 1; null != (clazz = getCallerClass(i)); i++) {
162             classes.push(clazz);
163         }
164         return classes;
165     }
166 
167     public StackTraceElement calcLocation(final String fqcnOfLogger) {
168         if (fqcnOfLogger == null) {
169             return null;
170         }
171         // LOG4J2-1029 new Throwable().getStackTrace is faster than Thread.currentThread().getStackTrace().
172         final StackTraceElement[] stackTrace = new Throwable().getStackTrace();
173         boolean found = false;
174         for (int i = 0; i < stackTrace.length; i++) {
175             final String className = stackTrace[i].getClassName();
176             if (fqcnOfLogger.equals(className)) {
177 
178                 found = true;
179                 continue;
180             }
181             if (found) {
182                 return stackTrace[i];
183             }
184         }
185         return null;
186     }
187 
188     public StackTraceElement getStackTraceElement(final int depth) {
189         // (MS) I tested the difference between using Throwable.getStackTrace() and Thread.getStackTrace(), and
190         // the version using Throwable was surprisingly faster! at least on Java 1.8. See ReflectionBenchmark.
191         final StackTraceElement[] elements = new Throwable().getStackTrace();
192         int i = 0;
193         for (final StackTraceElement element : elements) {
194             if (isValid(element)) {
195                 if (i == depth) {
196                     return element;
197                 }
198                 ++i;
199             }
200         }
201         throw new IndexOutOfBoundsException(Integer.toString(depth));
202     }
203 
204     private boolean isValid(final StackTraceElement element) {
205         // ignore native methods (oftentimes are repeated frames)
206         if (element.isNativeMethod()) {
207             return false;
208         }
209         final String cn = element.getClassName();
210         // ignore OpenJDK internal classes involved with reflective invocation
211         if (cn.startsWith("sun.reflect.")) {
212             return false;
213         }
214         final String mn = element.getMethodName();
215         // ignore use of reflection including:
216         // Method.invoke
217         // InvocationHandler.invoke
218         // Constructor.newInstance
219         if (cn.startsWith("java.lang.reflect.") && (mn.equals("invoke") || mn.equals("newInstance"))) {
220             return false;
221         }
222         // ignore use of Java 1.9+ reflection classes
223         if (cn.startsWith("jdk.internal.reflect.")) {
224             return false;
225         }
226         // ignore Class.newInstance
227         if (cn.equals("java.lang.Class") && mn.equals("newInstance")) {
228             return false;
229         }
230         // ignore use of Java 1.7+ MethodHandle.invokeFoo() methods
231         if (cn.equals("java.lang.invoke.MethodHandle") && mn.startsWith("invoke")) {
232             return false;
233         }
234         // any others?
235         return true;
236     }
237 }