Support Guice throwing providers

Support Guice throwing providers by filtering the bindings using
the Guice com.google.inject.internal.UniqueAnnotations
Add test
This commit is contained in:
Michaël REMOND
2022-03-21 16:47:12 +01:00
committed by GitHub
parent ee7284a830
commit 43277ab746
3 changed files with 46 additions and 0 deletions

View File

@@ -28,7 +28,11 @@ import com.google.inject.AbstractModule;
import com.google.inject.BindingAnnotation;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Names;
import com.google.inject.throwingproviders.CheckedProvider;
import com.google.inject.throwingproviders.CheckedProvides;
import com.google.inject.throwingproviders.ThrowingProviderBinder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
@@ -90,6 +94,14 @@ public class BindingAnnotationTests {
.isNotNull();
assertThat(injector.getInstance(Key.get(SomeNamedDepWithType2.class, Names.named("sameNameDifferentType"))))
.isNotNull();
assertThat(injector
.getInstance(Key.get(new TypeLiteral<TestCheckedProvider<SomeDependencyFromTestCheckedProvider>>() {
}))).isNotNull();
assertThat(injector.getInstance(
Key.get(new TypeLiteral<TestCheckedProvider<SomeOtherDependencyFromTestCheckedProvider>>() {
}))).isNotNull();
context.close();
}
@@ -158,6 +170,18 @@ public class BindingAnnotationTests {
}
public interface TestCheckedProvider<T> extends CheckedProvider<T> {
}
public static class SomeDependencyFromTestCheckedProvider {
}
public static class SomeOtherDependencyFromTestCheckedProvider {
}
@EnableGuiceModules
@Configuration
static class BindingAnnotationTestsConfig {
@@ -226,9 +250,21 @@ public class BindingAnnotationTests {
return new AbstractModule() {
@Override
protected void configure() {
install(ThrowingProviderBinder.forModule(this));
bind(String.class).annotatedWith(SomeBindingAnnotation.class).toInstance("annotated");
bind(String.class).annotatedWith(SomeOtherBindingAnnotation.class).toInstance("other");
}
@CheckedProvides(TestCheckedProvider.class)
SomeDependencyFromTestCheckedProvider some() throws Exception {
return new SomeDependencyFromTestCheckedProvider();
}
@CheckedProvides(TestCheckedProvider.class)
SomeOtherDependencyFromTestCheckedProvider other() throws Exception {
return new SomeOtherDependencyFromTestCheckedProvider();
}
};
}