Skip to content

Commit

Permalink
Update open-source Guice to use DirectStackWalkerFinder, now that the…
Browse files Browse the repository at this point in the history
… base level is JDK11.

PiperOrigin-RevId: 620868575
  • Loading branch information
sameb authored and Guice Team committed Apr 1, 2024
1 parent 2d4a395 commit b86ceda
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
16 changes: 15 additions & 1 deletion core/src/com/google/inject/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
],
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> shouldBeSkipped) {
return WALKER
.walk(s -> s.skip(2).filter(f -> !shouldBeSkipped.test(f.getClassName())).findFirst())
.map(StackWalker.StackFrame::toStackTraceElement)
.orElseThrow(AssertionError::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,6 @@ public Object getFromClassNames(List<String> moduleClassNames) {
}

private static CallerFinder loadCallerFinder() {
return new NewThrowableFinder();
return new DirectStackWalkerFinder();
}
}

0 comments on commit b86ceda

Please sign in to comment.