001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.util;
018
019import java.lang.reflect.Method;
020import java.util.Stack;
021
022/**
023 * <em>Consider this class private.</em> Provides various methods to determine the caller class. <h3>Background</h3>
024 * <p>
025 * This method, available only in the Oracle/Sun/OpenJDK implementations of the Java Virtual Machine, is a much more
026 * efficient mechanism for determining the {@link Class} of the caller of a particular method. When it is not available,
027 * a {@link SecurityManager} is the second-best option. When this is also not possible, the {@code StackTraceElement[]}
028 * returned by {@link Throwable#getStackTrace()} must be used, and its {@code String} class name converted to a
029 * {@code Class} using the slow {@link Class#forName} (which can add an extra microsecond or more for each invocation
030 * depending on the runtime ClassLoader hierarchy).
031 * </p>
032 * <p>
033 * During Java 8 development, the {@code sun.reflect.Reflection.getCallerClass(int)} was removed from OpenJDK, and this
034 * 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
035 * by one stack frame. This turned out to be beneficial for the survival of this API as the change broke hundreds of
036 * libraries and frameworks relying on the API which brought much more attention to the intended API removal.
037 * </p>
038 * <p>
039 * After much community backlash, the JDK team agreed to restore {@code getCallerClass(int)} and keep its existing
040 * behavior for the rest of Java 7. However, the method is deprecated in Java 8, and current Java 9 development has not
041 * addressed this API. Therefore, the functionality of this class cannot be relied upon for all future versions of Java.
042 * 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
043 * environments may fall back to using {@link Throwable#getStackTrace()} which is significantly slower due to
044 * examination of every virtual frame of execution.
045 * </p>
046 */
047public final class StackLocator {
048
049    // Checkstyle Suppress: the lower-case 'u' ticks off CheckStyle...
050    // CHECKSTYLE:OFF
051    static final int JDK_7u25_OFFSET;
052    // CHECKSTYLE:OFF
053
054    private static final Method GET_CALLER_CLASS;
055
056    private static final StackLocator INSTANCE;
057
058    static {
059        Method getCallerClass;
060        int java7u25CompensationOffset = 0;
061        try {
062            final Class<?> sunReflectionClass = LoaderUtil.loadClass("sun.reflect.Reflection");
063            getCallerClass = sunReflectionClass.getDeclaredMethod("getCallerClass", int.class);
064            Object o = getCallerClass.invoke(null, 0);
065            getCallerClass.invoke(null, 0);
066            if (o == null || o != sunReflectionClass) {
067                getCallerClass = null;
068                java7u25CompensationOffset = -1;
069            } else {
070                o = getCallerClass.invoke(null, 1);
071                if (o == sunReflectionClass) {
072                    System.out.println("WARNING: Java 1.7.0_25 is in use which has a broken implementation of Reflection.getCallerClass(). " +
073                        " Please consider upgrading to Java 1.7.0_40 or later.");
074                    java7u25CompensationOffset = 1;
075                }
076            }
077        } catch (final Exception | LinkageError e) {
078            System.out.println("WARNING: sun.reflect.Reflection.getCallerClass is not supported. This will impact performance.");
079            getCallerClass = null;
080            java7u25CompensationOffset = -1;
081        }
082
083        GET_CALLER_CLASS = getCallerClass;
084        JDK_7u25_OFFSET = java7u25CompensationOffset;
085
086        INSTANCE = new StackLocator();
087    }
088
089    public static StackLocator getInstance() {
090        return INSTANCE;
091    }
092
093    private StackLocator() {
094    }
095
096    // TODO: return Object.class instead of null (though it will have a null ClassLoader)
097    // (MS) I believe this would work without any modifications elsewhere, but I could be wrong
098
099    // 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 && !fqcnOfLogger.equals(className)) {
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}