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

@@ -34,6 +34,12 @@
<version>5.1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-throwingproviders</artifactId>
<version>5.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>

View File

@@ -155,6 +155,10 @@ class ModuleRegistryConfiguration implements BeanDefinitionRegistryPostProcessor
&& entry.getKey().getAnnotationType().getName().startsWith("com.google.inject.multibindings")) {
continue;
}
if (entry.getKey().getAnnotationType() != null && entry.getKey().getAnnotationType().getName()
.startsWith("com.google.inject.internal.UniqueAnnotations")) {
continue;
}
Binding<?> binding = entry.getValue();
Key<?> key = entry.getKey();

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();
}
};
}