Search in sources :

Example 1 with Injector

use of eu.okaeri.injector.Injector in project okaeri-injector by OkaeriPoland.

the class TestInjector method test_always_provide_last.

@Test
public void test_always_provide_last() {
    String string1 = "some value";
    String string2 = "new value";
    Injector injector = OkaeriInjector.create().registerInjectable("test1", string1).registerInjectable("test1", string2);
    Optional<? extends Injectable<String>> injectable = injector.getInjectableExact("test1", String.class);
    assertEquals(true, injectable.isPresent());
    assertEquals(string2, injectable.get().getObject());
}
Also used : OkaeriInjector(eu.okaeri.injector.OkaeriInjector) Injector(eu.okaeri.injector.Injector) Test(org.junit.jupiter.api.Test)

Example 2 with Injector

use of eu.okaeri.injector.Injector in project okaeri-injector by OkaeriPoland.

the class TestInjector method test_constructor.

@Test
public void test_constructor() {
    String name = "some app";
    Injector injector = OkaeriInjector.create().registerInjectable("name", name);
    App app = injector.createInstance(App.class);
    assertEquals(name, app.getName());
}
Also used : App(eu.okaeri.injectortest.element.App) OkaeriInjector(eu.okaeri.injector.OkaeriInjector) Injector(eu.okaeri.injector.Injector) Test(org.junit.jupiter.api.Test)

Example 3 with Injector

use of eu.okaeri.injector.Injector in project okaeri-injector by OkaeriPoland.

the class TestInjector method test_double_type_injector.

@Test
public void test_double_type_injector() {
    Api api = new Api("test-api1");
    Api api2 = new Api("test-api2");
    Injector injector = OkaeriInjector.create().registerInjectable("api", api).registerInjectable("api2", api2);
    Worker worker = injector.createInstance(Worker.class);
    assertEquals(api, worker.getApi());
    assertEquals(3, worker.getCounter());
    Worker2 worker2 = injector.createInstance(Worker2.class);
    assertEquals(api2, worker2.getApi());
}
Also used : Worker2(eu.okaeri.injectortest.element.Worker2) OkaeriInjector(eu.okaeri.injector.OkaeriInjector) Injector(eu.okaeri.injector.Injector) Worker(eu.okaeri.injectortest.element.Worker) Api(eu.okaeri.injectortest.element.Api) Test(org.junit.jupiter.api.Test)

Example 4 with Injector

use of eu.okaeri.injector.Injector in project okaeri-injector by OkaeriPoland.

the class TestInjector method test_simple_injector.

@Test
public void test_simple_injector() {
    Api api = new Api("test-api");
    Injector injector = OkaeriInjector.create().registerInjectable(api);
    Worker worker = injector.createInstance(Worker.class);
    assertEquals(api, worker.getApi());
    assertEquals(3, worker.getCounter());
}
Also used : OkaeriInjector(eu.okaeri.injector.OkaeriInjector) Injector(eu.okaeri.injector.Injector) Worker(eu.okaeri.injectortest.element.Worker) Api(eu.okaeri.injectortest.element.Api) Test(org.junit.jupiter.api.Test)

Example 5 with Injector

use of eu.okaeri.injector.Injector in project okaeri-platform by OkaeriPoland.

the class ListenerComponentResolver method make.

@Override
public Object make(@NonNull ComponentCreator creator, @NonNull BeanManifest manifest, @NonNull Injector injector) {
    long start = System.currentTimeMillis();
    Class<?> manifestType = manifest.getType();
    Object instance = injector.createInstance(manifestType);
    Listener listener = (Listener) instance;
    this.plugin.getProxy().getPluginManager().registerListener(this.plugin, listener);
    long took = System.currentTimeMillis() - start;
    creator.log(ComponentHelper.buildComponentMessage().type("Added listener").name(listener.getClass().getSimpleName()).took(took).meta("methods", Arrays.stream(listener.getClass().getDeclaredMethods()).filter(method -> method.getAnnotation(EventHandler.class) != null).map(Method::getName).collect(Collectors.toList())).build());
    creator.increaseStatistics("listeners", 1);
    return listener;
}
Also used : Listener(net.md_5.bungee.api.plugin.Listener) Arrays(java.util.Arrays) Inject(eu.okaeri.injector.annotation.Inject) Component(eu.okaeri.platform.core.annotation.Component) NonNull(lombok.NonNull) Injector(eu.okaeri.injector.Injector) Collectors(java.util.stream.Collectors) BeanManifest(eu.okaeri.platform.core.component.manifest.BeanManifest) ComponentHelper(eu.okaeri.platform.core.component.ComponentHelper) ComponentCreator(eu.okaeri.platform.core.component.creator.ComponentCreator) ComponentResolver(eu.okaeri.platform.core.component.creator.ComponentResolver) Plugin(net.md_5.bungee.api.plugin.Plugin) EventHandler(net.md_5.bungee.event.EventHandler) Method(java.lang.reflect.Method) Listener(net.md_5.bungee.api.plugin.Listener) EventHandler(net.md_5.bungee.event.EventHandler) Method(java.lang.reflect.Method)

Aggregations

Injector (eu.okaeri.injector.Injector)13 OkaeriInjector (eu.okaeri.injector.OkaeriInjector)7 Inject (eu.okaeri.injector.annotation.Inject)6 ComponentHelper (eu.okaeri.platform.core.component.ComponentHelper)6 ComponentCreator (eu.okaeri.platform.core.component.creator.ComponentCreator)6 ComponentResolver (eu.okaeri.platform.core.component.creator.ComponentResolver)6 BeanManifest (eu.okaeri.platform.core.component.manifest.BeanManifest)6 Method (java.lang.reflect.Method)6 Collectors (java.util.stream.Collectors)6 NonNull (lombok.NonNull)6 Test (org.junit.jupiter.api.Test)6 ConfigManager (eu.okaeri.configs.ConfigManager)3 Configurer (eu.okaeri.configs.configurer.Configurer)3 OkaeriSerdesPack (eu.okaeri.configs.serdes.OkaeriSerdesPack)3 LocaleConfig (eu.okaeri.i18n.configs.LocaleConfig)3 LocaleConfigManager (eu.okaeri.i18n.configs.LocaleConfigManager)3 LocaleProvider (eu.okaeri.i18n.provider.LocaleProvider)3 Api (eu.okaeri.injectortest.element.Api)3 Worker (eu.okaeri.injectortest.element.Worker)3 ConfigurerProvider (eu.okaeri.persistence.document.ConfigurerProvider)3