From 3dd78a671d3b71c60628b73bea54582f69562aa8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 10 Nov 2024 21:56:53 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=9A=A7=20Allow=20binding=20objects=20with?= =?utf8?q?=20@Named=20annotations?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../pterodactylus/util/inject/ObjectBinding.java | 24 +++++++++++++++ .../util/inject/ObjectBindingTest.java | 35 +++++++++++++++++----- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/pterodactylus/util/inject/ObjectBinding.java b/src/main/java/net/pterodactylus/util/inject/ObjectBinding.java index 5bf2864..7cc41d2 100644 --- a/src/main/java/net/pterodactylus/util/inject/ObjectBinding.java +++ b/src/main/java/net/pterodactylus/util/inject/ObjectBinding.java @@ -1,6 +1,10 @@ package net.pterodactylus.util.inject; +import com.google.inject.Key; import com.google.inject.Module; +import com.google.inject.name.Names; + +import static com.google.inject.name.Names.named; /** * Helper class for Guice configuration. @@ -25,6 +29,26 @@ public class ObjectBinding { return (binder) -> binder.bind(requestedClass).toInstance(instance); } + public $2 named(String name) { + return new $2<>(requestedClass, name); + } + + } + + public static class $2 { + + private $2(Class requestedClass, String name) { + this.requestedClass = requestedClass; + this.name = name; + } + + public Module is(T instance) { + return (binder) -> binder.bind(Key.get(requestedClass, named(name))).toInstance(instance); + } + + private final Class requestedClass; + private final String name; + } } diff --git a/src/test/java/net/pterodactylus/util/inject/ObjectBindingTest.java b/src/test/java/net/pterodactylus/util/inject/ObjectBindingTest.java index 273430a..5f1ada4 100644 --- a/src/test/java/net/pterodactylus/util/inject/ObjectBindingTest.java +++ b/src/test/java/net/pterodactylus/util/inject/ObjectBindingTest.java @@ -1,11 +1,16 @@ package net.pterodactylus.util.inject; -import com.google.inject.Guice; +import com.google.inject.ConfigurationException; import com.google.inject.Injector; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; +import com.google.inject.Key; +import com.google.inject.name.Names; import org.junit.jupiter.api.Test; +import static com.google.inject.Guice.createInjector; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.sameInstance; +import static org.junit.jupiter.api.Assertions.assertThrows; + /** * Unit test for {@link ObjectBinding}. * @@ -21,12 +26,28 @@ public class ObjectBindingTest { @Test public void objectIsBound() { TestClass testObject = new TestClass(); - Injector injector = Guice.createInjector(ObjectBinding.forClass(TestInterface.class).is(testObject)); - MatcherAssert.assertThat(injector.getInstance(TestInterface.class), Matchers.sameInstance(testObject)); + Injector injector = createInjector(ObjectBinding.forClass(TestInterface.class).is(testObject)); + assertThat(injector.getInstance(TestInterface.class), sameInstance(testObject)); } - public interface TestInterface { } + @Test + public void namedObjectIsBound() { + TestClass testObject = new TestClass(); + Injector injector = createInjector(ObjectBinding.forClass(TestInterface.class).named("foo").is(testObject)); + assertThat(injector.getInstance(Key.get(TestInterface.class, Names.named("foo"))), sameInstance(testObject)); + } - public static class TestClass implements TestInterface { } + @Test + public void namedObjectIsNotReturnedIfNotAskedForByName() { + TestClass testObject = new TestClass(); + Injector injector = createInjector(ObjectBinding.forClass(TestInterface.class).named("foo").is(testObject)); + assertThrows(ConfigurationException.class, () -> injector.getInstance(TestInterface.class)); + } + + public interface TestInterface { + } + + public static class TestClass implements TestInterface { + } } -- 2.7.4