Skip to content

Commit

Permalink
If all reasonable sources are filtered via skipSource, then show the …
Browse files Browse the repository at this point in the history
…source as "unknown source" instead of InjectorShell.build (which should be an internal detail).

PiperOrigin-RevId: 358853443
  • Loading branch information
sameb authored and Xiaoming Jia committed Feb 22, 2021
1 parent df622ab commit bfc2e48
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
12 changes: 11 additions & 1 deletion core/src/com/google/inject/spi/Elements.java
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,17 @@ private ElementSource getElementSource() {
if (stackTraceOption == IncludeStackTraceOption.COMPLETE
|| stackTraceOption == IncludeStackTraceOption.ONLY_FOR_DECLARING_SOURCE) {
// With the above conditions and assignments 'callStack' is non-null
declaringSource = sourceProvider.get(callStack);
StackTraceElement callingSource = sourceProvider.get(callStack);
// If we've traversed past all reasonable sources and into our internal code, then we
// don't know the source.
if (callingSource
.getClassName()
.equals("com.google.inject.internal.InjectorShell$Builder")
&& callingSource.getMethodName().equals("build")) {
declaringSource = SourceProvider.UNKNOWN_SOURCE;
} else {
declaringSource = callingSource;
}
} else { // or if (stackTraceOption == IncludeStackTraceOptions.OFF)
// As neither 'declaring source' nor 'call stack' is available use 'module source'
declaringSource = sourceProvider.getFromClassNames(moduleSource.getModuleClassNames());
Expand Down
47 changes: 47 additions & 0 deletions core/test/com/google/inject/spi/SourcesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.google.inject.spi;

import static com.google.common.truth.Truth.assertThat;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Module;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests sources are set correctly in elements. */
@RunWith(JUnit4.class)
public final class SourcesTest {

@Test
public void entirelyFilteredSourceShowsAsUnknown() {
ElementSource source =
(ElementSource)
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
binder().skipSources(getClass()).bind(String.class).toInstance("Foo");
}
})
.getBinding(String.class)
.getSource();
assertThat(source.getDeclaringSource()).isEqualTo("[unknown source]");
}

@Test
public void unfilteredShowsCorrectly() {
Module m =
new AbstractModule() {
@Override
protected void configure() {
binder().bind(String.class).toInstance("Foo");
}
};
ElementSource source =
(ElementSource) Guice.createInjector(m).getBinding(String.class).getSource();
StackTraceElement ste = (StackTraceElement) source.getDeclaringSource();
assertThat(ste.getClassName()).isEqualTo(m.getClass().getName());
assertThat(ste.getMethodName()).isEqualTo("configure");
}
}

0 comments on commit bfc2e48

Please sign in to comment.