Skip to content

Commit fed8cb1

Browse files
lukesandbergGuice Team
authored andcommitted
Add a regression test for a bug from an earlier iteration of the MethodHandles implementation
If a `javax.inject.Provider` was bound in a scope, we would enforce that it was a `com.google.inject.Provider` which was wrong. This is due to logic in `MoreTypes.canonicalizeForKey` which canonicalizes `javax.inject.Provider` to the guice interface. This behavior makes sense for injection points since guice only ever hands out `com.google.inject.Provider` instances but we don't always canonicalize them at every phase of injection which is what lead to the bug. Now we no longer use `Dependency` objects to enforce types so we don't risk this issue anymore PiperOrigin-RevId: 741129931
1 parent 17aa84c commit fed8cb1

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

core/test/com/google/inject/BoundProviderTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.inject;
1818

19+
import static com.google.inject.name.Names.named;
20+
1921
import junit.framework.TestCase;
2022

2123
/** @author [email protected] (Bob Lee) */
@@ -48,6 +50,9 @@ public void testSingletonFooProvider() throws CreationException {
4850
@Override
4951
protected void configure() {
5052
bind(Foo.class).toProvider(SingletonFooProvider.class);
53+
bind(Foo.class)
54+
.annotatedWith(named("javax"))
55+
.toProvider(JavaxInjectSingletonFooProvider.class);
5156
}
5257
});
5358

@@ -59,6 +64,16 @@ protected void configure() {
5964
assertNotNull(a.bar);
6065
assertNotNull(b.bar);
6166
assertSame(a.bar, b.bar);
67+
68+
var javaxKey = Key.get(Foo.class, named("javax"));
69+
a = injector.getInstance(javaxKey);
70+
b = injector.getInstance(javaxKey);
71+
72+
assertEquals(0, a.i);
73+
assertEquals(1, b.i);
74+
assertNotNull(a.bar);
75+
assertNotNull(b.bar);
76+
assertSame(a.bar, b.bar);
6277
}
6378

6479
static class Bar {}
@@ -105,4 +120,25 @@ public Foo get() {
105120
return new Foo(this.bar, count++);
106121
}
107122
}
123+
124+
// Use a jakarta.inject.Provider as well. Dependency.get() always canonicalizes the jakarta.inject
125+
// provider to the Guice provider, which at one point caused a bug in the MethodHandle
126+
// implementation which attempted to enforce types derived from the
127+
// Dependency objects
128+
@Singleton
129+
static class JavaxInjectSingletonFooProvider implements jakarta.inject.Provider<Foo> {
130+
131+
final Bar bar;
132+
int count = 0;
133+
134+
@Inject
135+
JavaxInjectSingletonFooProvider(Bar bar) {
136+
this.bar = bar;
137+
}
138+
139+
@Override
140+
public Foo get() {
141+
return new Foo(this.bar, count++);
142+
}
143+
}
108144
}

0 commit comments

Comments
 (0)