Debian Codegraph @

src/com/github/weisj/jsvg/logging/impl/CallerFinder.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2025 Jannis Weis
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 */
22 package com.github.weisj.jsvg.logging.impl;
23
24 import java.lang.invoke.MethodHandle;
25 import java.lang.invoke.MethodHandles;
26 import java.lang.invoke.MethodType;
27 import java.util.Optional;
28 import java.util.function.Function;
29 import java.util.stream.Stream;
30
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
33
34 final class CallerFinder {
35 private static final boolean HAS_STACK_WALKER;
36 private static final MethodHandle STACK_WALKER_WALK;
37 private static final MethodHandle STACK_FRAME_GET_CLASS_NAME;
38 private static final MethodHandle STACK_FRAME_GET_METHOD_NAME;
39
40 private final String dispatcherClassName;
41 private boolean lookingForCaller = false;
42
43 static {
44 MethodHandle walk = null;
45 MethodHandle getClassName = null;
46 MethodHandle getMethodName = null;
47 boolean hasStackWalker = false;
48 try {
49 Class<?> stackWalkerClass = Class.forName("java.lang.StackWalker");
50 Class<?> optionClass = Class.forName("java.lang.StackWalker$Option");
51 @SuppressWarnings({"rawtypes", "unchecked"}) Object retainClassRef =
52 Enum.valueOf((Class<Enum>) optionClass, "RETAIN_CLASS_REFERENCE");
53 MethodHandle getInstance = MethodHandles.publicLookup().findStatic(
54 stackWalkerClass, "getInstance",
55 MethodType.methodType(stackWalkerClass, optionClass));
56 Object stackWalkerInstance = getInstance.invoke(retainClassRef);
57 walk = MethodHandles.lookup().findVirtual(
58 stackWalkerClass, "walk",
59 MethodType.methodType(Object.class, java.util.function.Function.class));
60 walk = walk.bindTo(stackWalkerInstance);
61
62 Class<?> stackFrameClass = Class.forName("java.lang.StackWalker$StackFrame");
63 getClassName = MethodHandles.publicLookup().findVirtual(
64 stackFrameClass, "getClassName", MethodType.methodType(String.class));
65 getMethodName = MethodHandles.publicLookup().findVirtual(
66 stackFrameClass, "getMethodName", MethodType.methodType(String.class));
67
68 hasStackWalker = true;
69 } catch (Throwable ignored) {
70 // If any of the above fails, we will not use the stack walker.
71 }
72 HAS_STACK_WALKER = hasStackWalker;
73 STACK_WALKER_WALK = walk;
74 STACK_FRAME_GET_CLASS_NAME = getClassName;
75 STACK_FRAME_GET_METHOD_NAME = getMethodName;
76 }
77
78 public CallerFinder(@NotNull String dispatcherClassName) {
79 this.dispatcherClassName = dispatcherClassName;
80 }
81
82 @SuppressWarnings("unchecked")
83 public Optional<StackTraceElement> findCaller() {
84 if (HAS_STACK_WALKER) {
85 try {
86 return (Optional<StackTraceElement>) STACK_WALKER_WALK.invoke(
87 (Function<Stream<?>, Optional<StackTraceElement>>) stream -> stream
88 .map(f -> {
89 try {
90 String className = (String) STACK_FRAME_GET_CLASS_NAME.invoke(f);
91 String methodName = (String) STACK_FRAME_GET_METHOD_NAME.invoke(f);
92 return new StackTraceElement(className, methodName, null, -1);
93 } catch (Throwable t) {
94 return null;
95 }
96 })
97 .filter(this::isCallerFrame)
98 .findFirst());
99 } catch (Throwable ignored) {
100 // If the stack walker fails, we will fall back to the traditional method.
101 }
102 }
103 // Fallback for JDK 8
104 StackTraceElement[] stack = new Throwable().getStackTrace();
105 for (StackTraceElement element : stack) {
106 if (isCallerFrame(element)) {
107 return Optional.of(element);
108 }
109 }
110 return Optional.empty();
111 }
112
113 private boolean isCallerFrame(@Nullable StackTraceElement frame) {
114 if (frame == null) return false;
115 String cname = frame.getClassName();
116 if (!lookingForCaller) {
117 lookingForCaller = cname.equals(dispatcherClassName);
118 return false;
119 }
120 return true;
121 }
122 }