From: David ‘Bombe’ Roden Date: Sun, 10 Nov 2024 20:56:53 +0000 (+0100) Subject: 🚧 Allow binding objects with @Named annotations X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=3dd78a671d3b71c60628b73bea54582f69562aa8;p=rhynodge.git 🚧 Allow binding objects with @Named annotations --- 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 { + } }