Search in sources :

Example 36 with Named

use of javax.inject.Named in project wildfly-swarm by wildfly-swarm.

the class InterfaceExtension method afterBeanDiscovery.

@SuppressWarnings("unused")
void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager beanManager) throws Exception {
    List<SimpleKey> configuredInterfaces = this.configView.simpleSubkeys(ROOT);
    for (SimpleKey interfaceName : configuredInterfaces) {
        Set<Bean<?>> ifaces = beanManager.getBeans(Interface.class, AnyLiteral.INSTANCE);
        if (ifaces.stream().noneMatch(e -> e.getQualifiers().stream().anyMatch(anno -> anno instanceof Named && ((Named) anno).value().equals(interfaceName + "-interface")))) {
            Interface iface = new Interface(interfaceName.name(), "0.0.0.0");
            applyConfiguration(iface);
            CommonBean<Interface> interfaceBean = CommonBeanBuilder.newBuilder(Interface.class).beanClass(InterfaceExtension.class).scope(ApplicationScoped.class).addQualifier(AnyLiteral.INSTANCE).addQualifier(new NamedLiteral(interfaceName.name() + "-interface")).createSupplier(() -> iface).addType(Interface.class).addType(Object.class).build();
            abd.addBean(interfaceBean);
        }
    }
}
Also used : AfterBeanDiscovery(javax.enterprise.inject.spi.AfterBeanDiscovery) Set(java.util.Set) AnyLiteral(org.jboss.weld.literal.AnyLiteral) SimpleKey(org.wildfly.swarm.spi.api.config.SimpleKey) List(java.util.List) Interface(org.wildfly.swarm.container.Interface) CommonBean(org.wildfly.swarm.spi.api.cdi.CommonBean) ConfigKey(org.wildfly.swarm.spi.api.config.ConfigKey) Observes(javax.enterprise.event.Observes) NamedLiteral(org.jboss.weld.literal.NamedLiteral) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Named(javax.inject.Named) ConfigView(org.wildfly.swarm.spi.api.config.ConfigView) Bean(javax.enterprise.inject.spi.Bean) BeanManager(javax.enterprise.inject.spi.BeanManager) CommonBeanBuilder(org.wildfly.swarm.spi.api.cdi.CommonBeanBuilder) Named(javax.inject.Named) NamedLiteral(org.jboss.weld.literal.NamedLiteral) Interface(org.wildfly.swarm.container.Interface) SimpleKey(org.wildfly.swarm.spi.api.config.SimpleKey) CommonBean(org.wildfly.swarm.spi.api.cdi.CommonBean) Bean(javax.enterprise.inject.spi.Bean)

Example 37 with Named

use of javax.inject.Named in project eclipse.platform.runtime by eclipse.

the class InjectorImpl method findBinding.

private Binding findBinding(IObjectDescriptor descriptor) {
    Class<?> desiredClass = getProviderType(descriptor.getDesiredType());
    if (desiredClass == null)
        desiredClass = getDesiredClass(descriptor.getDesiredType());
    synchronized (bindings) {
        if (!bindings.containsKey(desiredClass))
            return null;
        Set<Binding> collection = bindings.get(desiredClass);
        String desiredQualifierName = null;
        if (descriptor.hasQualifier(Named.class)) {
            Named namedAnnotation = descriptor.getQualifier(Named.class);
            desiredQualifierName = namedAnnotation.value();
        } else {
            Annotation[] annotations = descriptor.getQualifiers();
            if (annotations != null) {
                for (Annotation annotation : annotations) {
                    desiredQualifierName = annotation.annotationType().getName();
                    break;
                }
            }
        }
        for (Binding collectionBinding : collection) {
            if (eq(collectionBinding.getQualifierName(), desiredQualifierName))
                return collectionBinding;
        }
        desiredQualifierName = desiredClass.getName();
        for (Binding collectionBinding : collection) {
            Class<?> bindingClass = collectionBinding.getDescribedClass();
            if (bindingClass == null)
                continue;
            String simpleClassName = bindingClass.getName();
            if (eq(simpleClassName, desiredQualifierName))
                return collectionBinding;
        }
    }
    return null;
}
Also used : IBinding(org.eclipse.e4.core.di.IBinding) Named(javax.inject.Named) Annotation(java.lang.annotation.Annotation)

Example 38 with Named

use of javax.inject.Named in project eclipse.platform.runtime by eclipse.

the class AnnotationsInjectionTest method testFieldMethodOrder.

/**
 * Tests that fields are injected before methods.
 */
@Test
public void testFieldMethodOrder() {
    final AssertionFailedError[] error = new AssertionFailedError[1];
    class TestData {
        // empty
    }
    class Injected {

        @Inject
        @Named("valueField")
        Object injectedField;

        Object methodValue;

        @Inject
        public void injectedMethod(@Optional @Named("valueMethod") Object arg) {
            try {
                assertTrue(injectedField != null);
            } catch (AssertionFailedError e) {
                error[0] = e;
            }
            methodValue = arg;
        }
    }
    IEclipseContext context = EclipseContextFactory.create();
    TestData fieldValue = new TestData();
    TestData methodValue = new TestData();
    context.set("valueField", fieldValue);
    context.set("valueMethod", methodValue);
    Injected object = new Injected();
    ContextInjectionFactory.inject(object, context);
    if (error[0] != null) {
        throw error[0];
    }
    assertEquals(fieldValue, object.injectedField);
    assertEquals(methodValue, object.methodValue);
    // removing method value, the field should still have value
    context.remove("valueMethod");
    if (error[0] != null) {
        throw error[0];
    }
    assertEquals(fieldValue, object.injectedField);
    assertNull(object.methodValue);
    context.dispose();
    if (error[0] != null) {
        throw error[0];
    }
}
Also used : Named(javax.inject.Named) Optional(org.eclipse.e4.core.di.annotations.Optional) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) AssertionFailedError(junit.framework.AssertionFailedError) Test(org.junit.Test)

Example 39 with Named

use of javax.inject.Named in project eclipse.platform.runtime by eclipse.

the class AnnotationsInjectionTest method testInvoke.

@Test
public void testInvoke() {
    class TestData {

        public String value;

        public TestData(String tmp) {
            value = tmp;
        }
    }
    class Injected {

        public String myString;

        public Injected() {
        // placeholder
        }

        @Execute
        public String something(@Named("testing123") TestData data) {
            myString = data.value;
            return "true";
        }
    }
    IEclipseContext context = EclipseContextFactory.create();
    TestData methodValue = new TestData("abc");
    context.set("testing123", methodValue);
    Injected object = new Injected();
    assertNull(object.myString);
    assertEquals("true", ContextInjectionFactory.invoke(object, Execute.class, context, null));
    assertEquals("abc", object.myString);
}
Also used : Named(javax.inject.Named) Execute(org.eclipse.e4.core.di.annotations.Execute) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Example 40 with Named

use of javax.inject.Named in project eclipse.platform.runtime by eclipse.

the class AnnotationsInjectionTest method testContextSetOneArg.

@Test
public void testContextSetOneArg() {
    class TestData {
        // empty
    }
    class Injected {

        int contextSetCalled = 0;

        int setMethodCalled = 0;

        public TestData value;

        @Inject
        public void settings(IEclipseContext context) {
            contextSetCalled++;
        }

        @Inject
        public void injectedMethod(@Named("testing123") TestData arg) {
            setMethodCalled++;
            value = arg;
        }
    }
    IEclipseContext context = EclipseContextFactory.create();
    TestData methodValue = new TestData();
    context.set("testing123", methodValue);
    Injected object = new Injected();
    ContextInjectionFactory.inject(object, context);
    assertEquals(1, object.setMethodCalled);
    assertEquals(1, object.contextSetCalled);
    TestData methodValue2 = new TestData();
    context.set("testing123", methodValue2);
    assertEquals(2, object.setMethodCalled);
    assertEquals(methodValue2, object.value);
    assertEquals(1, object.contextSetCalled);
}
Also used : Named(javax.inject.Named) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) Test(org.junit.Test)

Aggregations

Named (javax.inject.Named)126 Produces (javax.enterprise.inject.Produces)36 Test (org.junit.Test)29 ApplicationScoped (javax.enterprise.context.ApplicationScoped)26 Provides (com.google.inject.Provides)22 Singleton (javax.inject.Singleton)17 Properties (java.util.Properties)16 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)15 Api (com.google.api.server.spi.config.Api)15 SimpleLevelOverridingApi (com.google.api.server.spi.testing.SimpleLevelOverridingApi)15 JsonNode (com.fasterxml.jackson.databind.JsonNode)14 PropertiesComponent (org.apache.camel.component.properties.PropertiesComponent)12 Annotation (java.lang.annotation.Annotation)11 Provides (dagger.Provides)9 ArrayList (java.util.ArrayList)8 List (java.util.List)7 Inject (javax.inject.Inject)7 HashMap (java.util.HashMap)6 ApiConfigAnnotationReader (com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader)4 Type (java.lang.reflect.Type)4