Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/src/com/google/inject/internal/KotlinSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,10 @@ public boolean isValueClass(Class<?> clazz) {
public boolean isKotlinClass(Class<?> clazz) {
return false;
}

@Override
public StackTraceElement maybeDemangleSTE(StackTraceElement element, Class<?> clazz) {
return element;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@ public interface KotlinSupportInterface {

/** Returns whether the {@code clazz} is a Kotlin class. */
boolean isKotlinClass(Class<?> clazz);

/**
* Returns a demangled StackTraceElement if it corresponds to an inlined Kotlin function,
* otherwise returns the original StackTraceElement.
*/
StackTraceElement maybeDemangleSTE(StackTraceElement element, Class<?> clazz);
}
16 changes: 15 additions & 1 deletion core/src/com/google/inject/internal/util/CallerFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,19 @@
* for implementing it.
*/
interface CallerFinder {
StackTraceElement findCaller(Predicate<String> shouldBeSkipped);
/**
* A Tuple to hold the stack element alongside the class it originated from if StackWalker
* captured it.
*/
final class Caller {
final StackTraceElement element;
final Class<?> clazz;

Caller(StackTraceElement element, Class<?> clazz) {
this.element = element;
this.clazz = clazz;
}
}

Caller findCaller(Predicate<String> shouldBeSkipped);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ final class DirectStackWalkerFinder implements CallerFinder {
StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);

@Override
public StackTraceElement findCaller(Predicate<String> shouldBeSkipped) {
public CallerFinder.Caller findCaller(Predicate<String> shouldBeSkipped) {
return WALKER
.walk(s -> s.skip(2).filter(f -> !shouldBeSkipped.test(f.getClassName())).findFirst())
.map(StackWalker.StackFrame::toStackTraceElement)
.map(f -> new CallerFinder.Caller(f.toStackTraceElement(), f.getDeclaringClass()))
.orElseThrow(AssertionError::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
/** A CallerFinder that construcst a new Throwable and iterates through its stack trace. */
class NewThrowableFinder implements CallerFinder {
@Override
public StackTraceElement findCaller(Predicate<String> shouldBeSkipped) {
public CallerFinder.Caller findCaller(Predicate<String> shouldBeSkipped) {
StackTraceElement[] stackTraceElements = new Throwable().getStackTrace();
for (StackTraceElement element : stackTraceElements) {
String className = element.getClassName();
if (!shouldBeSkipped.test(className)) {
return element;
return new CallerFinder.Caller(element, /* clazz= */ null);
}
}
throw new AssertionError();
Expand Down
4 changes: 3 additions & 1 deletion core/src/com/google/inject/internal/util/SourceProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.inject.internal.KotlinSupport;
import java.util.List;

/**
Expand Down Expand Up @@ -81,7 +82,8 @@ private static List<String> asStrings(Class<?>... classes) {
* is not skipped.
*/
public StackTraceElement getCaller() {
return FINDER.findCaller(this::shouldBeSkipped);
CallerFinder.Caller caller = FINDER.findCaller(this::shouldBeSkipped);
return KotlinSupport.getInstance().maybeDemangleSTE(caller.element, caller.clazz);
}

/** Returns the non-skipped module class name. */
Expand Down
Loading