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);
}
}
}
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;
}
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];
}
}
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);
}
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);
}
Aggregations