From b86ceda449b7ed129d3d705dd8fd61ca92236965 Mon Sep 17 00:00:00 2001 From: Sam Berlin Date: Mon, 1 Apr 2024 09:27:34 -0700 Subject: [PATCH] Update open-source Guice to use DirectStackWalkerFinder, now that the base level is JDK11. PiperOrigin-RevId: 620868575 --- core/src/com/google/inject/BUILD | 16 +++++++++++++++- .../internal/util/DirectStackWalkerFinder.java | 17 +++++++++++++++++ .../inject/internal/util/SourceProvider.java | 2 +- 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 core/src/com/google/inject/internal/util/DirectStackWalkerFinder.java diff --git a/core/src/com/google/inject/BUILD b/core/src/com/google/inject/BUILD index 5be458bc1b..e13ef8e71c 100644 --- a/core/src/com/google/inject/BUILD +++ b/core/src/com/google/inject/BUILD @@ -35,6 +35,15 @@ IMPLEMENTED_BY_SRCS = [ "ImplementedBy.java", ] +CALLER_FINDER_STACK_WALKER_SRCS = ["internal/util/DirectStackWalkerFinder.java"] + +java_library( + name = "caller_finder_impl", + srcs = CALLER_FINDER_STACK_WALKER_SRCS, + javacopts = JAVAC_OPTS, + deps = [":caller_finder_common"], +) + java_library( name = "caller_finder_common", srcs = CALLER_FINDER_COMMON_SRCS, @@ -65,13 +74,18 @@ java_library( name = "inject", srcs = glob( ["**/*.java"], - exclude = IMPLEMENTED_BY_SRCS + PROVIDED_BY_SRCS + ANNOTATION_SRCS + CALLER_FINDER_COMMON_SRCS, + exclude = IMPLEMENTED_BY_SRCS + + PROVIDED_BY_SRCS + + ANNOTATION_SRCS + + CALLER_FINDER_COMMON_SRCS + + CALLER_FINDER_STACK_WALKER_SRCS, ), javacopts = JAVAC_OPTS, tags = ["maven_coordinates=com.google.inject:guice:" + POM_VERSION], exports = [ ":annotations", ":caller_finder_common", + ":caller_finder_impl", ":implemented_by", ":provided_by", ], diff --git a/core/src/com/google/inject/internal/util/DirectStackWalkerFinder.java b/core/src/com/google/inject/internal/util/DirectStackWalkerFinder.java new file mode 100644 index 0000000000..c593de0b70 --- /dev/null +++ b/core/src/com/google/inject/internal/util/DirectStackWalkerFinder.java @@ -0,0 +1,17 @@ +package com.google.inject.internal.util; + +import java.util.function.Predicate; + +/** A CallerFinder directly compiled against StackWalker. Requires compiling against jdk11+. */ +final class DirectStackWalkerFinder implements CallerFinder { + private static final StackWalker WALKER = + StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE); + + @Override + public StackTraceElement findCaller(Predicate shouldBeSkipped) { + return WALKER + .walk(s -> s.skip(2).filter(f -> !shouldBeSkipped.test(f.getClassName())).findFirst()) + .map(StackWalker.StackFrame::toStackTraceElement) + .orElseThrow(AssertionError::new); + } +} diff --git a/core/src/com/google/inject/internal/util/SourceProvider.java b/core/src/com/google/inject/internal/util/SourceProvider.java index db069a67ef..2db4c2637e 100644 --- a/core/src/com/google/inject/internal/util/SourceProvider.java +++ b/core/src/com/google/inject/internal/util/SourceProvider.java @@ -96,6 +96,6 @@ public Object getFromClassNames(List moduleClassNames) { } private static CallerFinder loadCallerFinder() { - return new NewThrowableFinder(); + return new DirectStackWalkerFinder(); } }