Skip to content

Commit

Permalink
Allow static provider methods to be deduped
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=308639369
  • Loading branch information
lukesandberg authored and cpovirk committed Apr 27, 2020
1 parent 581922a commit 1174710
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ private <T> ProviderMethod<T> createProviderMethod(
return ProviderMethod.create(
key,
method,
isStaticModule() ? null : delegate,
isStaticModule() || Modifier.isStatic(method.getModifiers()) ? null : delegate,
ImmutableSet.copyOf(point.getDependencies()),
scopeAnnotation,
skipFastClassGeneration,
Expand Down
38 changes: 38 additions & 0 deletions core/test/com/google/inject/spi/ProviderMethodsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,44 @@ Integer fail() {
}
}

static final class DeduplicateModule extends AbstractModule {
@Provides
String provideString() {
return "";
}
}

public void testDeduplicateProviderMethodsBindings_sameInstance() {
Module module = new DeduplicateModule();
Guice.createInjector(Stage.PRODUCTION, module, module);
}

public void testDeduplicateProviderMethodsBindings_differentInstances() {
try {
Guice.createInjector(Stage.PRODUCTION, new DeduplicateModule(), new DeduplicateModule());
fail();
} catch (CreationException expected) {
assertContains(expected.getMessage(), "A binding to java.lang.String was already configured");
}
}

static final class DeduplicateStaticModule extends AbstractModule {
@Provides
static String provideString() {
return "";
}
}

public void testDeduplicateProviderMethodsBindings_sameInstance_staticMethod() {
Module module = new DeduplicateStaticModule();
Guice.createInjector(Stage.PRODUCTION, module, module);
}

public void testDeduplicateProviderMethodsBindings_differentInstances_staticMethod() {
Guice.createInjector(
Stage.PRODUCTION, new DeduplicateStaticModule(), new DeduplicateStaticModule());
}

private void runNullableTest(Injector injector, Dependency<?> dependency, Module module) {
switch (InternalFlags.getNullableProvidesOption()) {
case ERROR:
Expand Down

0 comments on commit 1174710

Please sign in to comment.