Skip to content

Commit 2712230

Browse files
java-team-github-botGuice Team
authored andcommitted
Fix formatting of source with module stack. Add NullInjectedIntoNonNullable tests.
PiperOrigin-RevId: 346890324
1 parent e399b21 commit 2712230

File tree

5 files changed

+183
-1
lines changed

5 files changed

+183
-1
lines changed

core/src/com/google/inject/internal/Messages.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ private static Object appendModules(Object source, ElementSource elementSource)
182182
if (modules.length() == 0) {
183183
return source;
184184
} else {
185-
return source + modules;
185+
return source + " (installed by: " + modules + ")";
186186
}
187187
}
188188

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.google.inject.errors;
2+
3+
import static com.google.inject.errors.ErrorMessageTestUtils.assertGuiceErrorEqualsIgnoreLineNumber;
4+
import static org.junit.Assert.assertThrows;
5+
import static org.junit.Assume.assumeTrue;
6+
7+
import com.google.inject.AbstractModule;
8+
import com.google.inject.Guice;
9+
import com.google.inject.Injector;
10+
import com.google.inject.Module;
11+
import com.google.inject.Provides;
12+
import com.google.inject.ProvisionException;
13+
import com.google.inject.internal.InternalFlags;
14+
import com.google.inject.internal.InternalFlags.IncludeStackTraceOption;
15+
import java.lang.annotation.Retention;
16+
import java.lang.annotation.RetentionPolicy;
17+
import java.lang.reflect.Field;
18+
import javax.inject.Inject;
19+
import javax.inject.Qualifier;
20+
import org.junit.Before;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.junit.runners.JUnit4;
24+
25+
@RunWith(JUnit4.class)
26+
public final class NullInjectedIntoNonNullableTest {
27+
28+
@Qualifier
29+
@Retention(RetentionPolicy.RUNTIME)
30+
@interface Bar {}
31+
32+
static class Foo {
33+
@Inject
34+
Foo(@Bar String string) {}
35+
}
36+
37+
static class FromProvidesMethodModule extends AbstractModule {
38+
@Provides
39+
@Bar
40+
String provideString() {
41+
return null;
42+
}
43+
}
44+
45+
static class FromProviderModule extends AbstractModule {
46+
@Override
47+
protected void configure() {
48+
bind(String.class).annotatedWith(Bar.class).toProvider(() -> null);
49+
}
50+
}
51+
52+
static class IntermediateModule extends AbstractModule {
53+
public static final String NULL = null;
54+
55+
private static final Module MODULE =
56+
new AbstractModule() {
57+
@Override
58+
protected void configure() {
59+
Field field = null;
60+
try {
61+
field = IntermediateModule.class.getField("NULL");
62+
} catch (NoSuchFieldException error) {
63+
throw new AssertionError("NULL field missing!");
64+
}
65+
binder()
66+
.withSource(field)
67+
.bind(String.class)
68+
.annotatedWith(Bar.class)
69+
.toProvider(() -> NULL);
70+
}
71+
};
72+
73+
@Override
74+
protected void configure() {
75+
install(MODULE);
76+
}
77+
}
78+
79+
@Before
80+
public void ensureStackTraceIsIncluded() {
81+
assumeTrue(InternalFlags.getIncludeStackTraceOption() != IncludeStackTraceOption.OFF);
82+
}
83+
84+
@Test
85+
public void nullReturnedFromProvidesMethod() {
86+
Injector injector = Guice.createInjector(new FromProvidesMethodModule());
87+
88+
ProvisionException exception =
89+
assertThrows(ProvisionException.class, () -> injector.getInstance(Foo.class));
90+
assertGuiceErrorEqualsIgnoreLineNumber(
91+
exception.getMessage(), "null_returned_from_provides_method.txt");
92+
}
93+
94+
@Test
95+
public void nullReturnedFromProvider() {
96+
Injector injector = Guice.createInjector(new FromProviderModule());
97+
98+
ProvisionException exception =
99+
assertThrows(ProvisionException.class, () -> injector.getInstance(Foo.class));
100+
assertGuiceErrorEqualsIgnoreLineNumber(
101+
exception.getMessage(), "null_returned_from_provider.txt");
102+
}
103+
104+
@Test
105+
public void nullReturnedFromProviderWithModuleStack() {
106+
Injector injector = Guice.createInjector(new IntermediateModule());
107+
108+
ProvisionException exception =
109+
assertThrows(ProvisionException.class, () -> injector.getInstance(Foo.class));
110+
assertGuiceErrorEqualsIgnoreLineNumber(
111+
exception.getMessage(), "null_returned_from_provider_with_module_stack.txt");
112+
}
113+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Unable to provision, see the following errors:
2+
3+
1) [Guice/NullInjectedIntoNonNullable]: null returned by binding at NullInjectedIntoNonNullableTest$FromProviderModule.configure(NullInjectedIntoNonNullableTest.java:44)
4+
but the 1st parameter string of NullInjectedIntoNonNullableTest$Foo.<init>(NullInjectedIntoNonNullableTest.java:30) is not @Nullable
5+
at NullInjectedIntoNonNullableTest$FromProviderModule.configure(NullInjectedIntoNonNullableTest.java:44)
6+
at NullInjectedIntoNonNullableTest$Foo.<init>(NullInjectedIntoNonNullableTest.java:30)
7+
\_ for 1st parameter string
8+
while locating NullInjectedIntoNonNullableTest$Foo
9+
10+
Learn more:
11+
https://github.com/google/guice/wiki/NULL_INJECTED_INTO_NON_NULLABLE
12+
13+
1 error
14+
15+
======================
16+
Full classname legend:
17+
======================
18+
NullInjectedIntoNonNullableTest$Foo: "com.google.inject.errors.NullInjectedIntoNonNullableTest$Foo"
19+
NullInjectedIntoNonNullableTest$FromProviderModule: "com.google.inject.errors.NullInjectedIntoNonNullableTest$FromProviderModule"
20+
========================
21+
End of classname legend:
22+
========================
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Unable to provision, see the following errors:
2+
3+
1) [Guice/NullInjectedIntoNonNullable]: null returned by binding at NullInjectedIntoNonNullableTest$IntermediateModule.NULL (installed by: NullInjectedIntoNonNullableTest$IntermediateModule -> NullInjectedIntoNonNullableTest$IntermediateModule$1)
4+
but the 1st parameter string of NullInjectedIntoNonNullableTest$Foo.<init>(NullInjectedIntoNonNullableTest.java:30) is not @Nullable
5+
at NullInjectedIntoNonNullableTest$IntermediateModule.NULL(NullInjectedIntoNonNullableTest.java:48)
6+
\_ installed by: NullInjectedIntoNonNullableTest$IntermediateModule -> NullInjectedIntoNonNullableTest$IntermediateModule$1
7+
at NullInjectedIntoNonNullableTest$Foo.<init>(NullInjectedIntoNonNullableTest.java:30)
8+
\_ for 1st parameter string
9+
while locating NullInjectedIntoNonNullableTest$Foo
10+
11+
Learn more:
12+
https://github.com/google/guice/wiki/NULL_INJECTED_INTO_NON_NULLABLE
13+
14+
1 error
15+
16+
======================
17+
Full classname legend:
18+
======================
19+
NullInjectedIntoNonNullableTest$Foo: "com.google.inject.errors.NullInjectedIntoNonNullableTest$Foo"
20+
NullInjectedIntoNonNullableTest$IntermediateModule: "com.google.inject.errors.NullInjectedIntoNonNullableTest$IntermediateModule"
21+
NullInjectedIntoNonNullableTest$IntermediateModule$1: "com.google.inject.errors.NullInjectedIntoNonNullableTest$IntermediateModule$1"
22+
========================
23+
End of classname legend:
24+
========================
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Unable to provision, see the following errors:
2+
3+
1) [Guice/NullInjectedIntoNonNullable]: null returned by binding at NullInjectedIntoNonNullableTest$FromProvidesMethodModule.provideString()
4+
but the 1st parameter string of NullInjectedIntoNonNullableTest$Foo.<init>(NullInjectedIntoNonNullableTest.java:30) is not @Nullable
5+
at NullInjectedIntoNonNullableTest$FromProvidesMethodModule.provideString(NullInjectedIntoNonNullableTest.java:37)
6+
at NullInjectedIntoNonNullableTest$FromProvidesMethodModule.provideString(NullInjectedIntoNonNullableTest.java:37)
7+
at NullInjectedIntoNonNullableTest$Foo.<init>(NullInjectedIntoNonNullableTest.java:30)
8+
\_ for 1st parameter string
9+
while locating NullInjectedIntoNonNullableTest$Foo
10+
11+
Learn more:
12+
https://github.com/google/guice/wiki/NULL_INJECTED_INTO_NON_NULLABLE
13+
14+
1 error
15+
16+
======================
17+
Full classname legend:
18+
======================
19+
NullInjectedIntoNonNullableTest$Foo: "com.google.inject.errors.NullInjectedIntoNonNullableTest$Foo"
20+
NullInjectedIntoNonNullableTest$FromProvidesMethodModule: "com.google.inject.errors.NullInjectedIntoNonNullableTest$FromProvidesMethodModule"
21+
========================
22+
End of classname legend:
23+
========================

0 commit comments

Comments
 (0)