use of io.quarkus.arc.Arc in project quarkus by quarkusio.
the class RepositoryDataAccessImplementor method getRepositoryInstance.
/**
* Implements getting repository from Arc container.
* <code>Arc.container().instance(Repository.class, new Annotation[0]).get()</code>
*/
private ResultHandle getRepositoryInstance(BytecodeCreator creator) {
ResultHandle arcContainer = creator.invokeStaticMethod(ofMethod(Arc.class, "container", ArcContainer.class));
ResultHandle instanceHandle = creator.invokeInterfaceMethod(ofMethod(ArcContainer.class, "instance", InstanceHandle.class, Class.class, Annotation[].class), arcContainer, creator.loadClassFromTCCL(repositoryClassName), creator.newArray(Annotation.class, 0));
ResultHandle instance = creator.invokeInterfaceMethod(ofMethod(InstanceHandle.class, "get", Object.class), instanceHandle);
creator.ifNull(instance).trueBranch().throwException(RuntimeException.class, repositoryClassName + " instance was not found");
return instance;
}
use of io.quarkus.arc.Arc in project quarkus by quarkusio.
the class RepositoryDataAccessImplementor method getRepositoryInstance.
/**
* Implements getting repository from Arc container.
* <code>Arc.container().instance(Repository.class, new Annotation[0]).get()</code>
*/
private ResultHandle getRepositoryInstance(BytecodeCreator creator) {
ResultHandle arcContainer = creator.invokeStaticMethod(ofMethod(Arc.class, "container", ArcContainer.class));
ResultHandle instanceHandle = creator.invokeInterfaceMethod(ofMethod(ArcContainer.class, "instance", InstanceHandle.class, Class.class, Annotation[].class), arcContainer, creator.loadClass(repositoryClassName), creator.newArray(Annotation.class, 0));
ResultHandle instance = creator.invokeInterfaceMethod(ofMethod(InstanceHandle.class, "get", Object.class), instanceHandle);
creator.ifNull(instance).trueBranch().throwException(RuntimeException.class, repositoryClassName + " instance was not found");
return instance;
}
use of io.quarkus.arc.Arc in project quarkus by quarkusio.
the class InjectionPointMetadataTest method testInjectionPointMetadata.
@SuppressWarnings({ "unchecked", "rawtypes", "serial" })
@Test
public void testInjectionPointMetadata() {
ArcContainer arc = Arc.container();
Controller controller = arc.instance(Controller.class).get();
// Field
InjectionPoint injectionPoint = controller.controlled.injectionPoint;
assertNotNull(injectionPoint);
assertEquals(Controlled.class, injectionPoint.getType());
Set<Annotation> qualifiers = injectionPoint.getQualifiers();
assertEquals(1, qualifiers.size());
assertEquals(Default.class, qualifiers.iterator().next().annotationType());
Bean<?> bean = injectionPoint.getBean();
assertNotNull(bean);
assertTrue(bean.getTypes().stream().anyMatch(t -> t.equals(Controller.class)));
assertNotNull(injectionPoint.getAnnotated());
assertTrue(injectionPoint.getAnnotated() instanceof AnnotatedField);
AnnotatedField<Controller> annotatedField = (AnnotatedField<Controller>) injectionPoint.getAnnotated();
assertEquals("controlled", annotatedField.getJavaMember().getName());
assertEquals(Controlled.class, annotatedField.getBaseType());
assertEquals(2, annotatedField.getAnnotations().size());
assertTrue(annotatedField.isAnnotationPresent(Inject.class));
assertTrue(annotatedField.isAnnotationPresent(FooAnnotation.class));
assertFalse(annotatedField.isAnnotationPresent(Deprecated.class));
assertTrue(annotatedField.getAnnotation(Singleton.class) == null);
assertTrue(annotatedField.getAnnotations(Singleton.class).isEmpty());
// Method
InjectionPoint methodInjectionPoint = controller.controlledMethod.injectionPoint;
assertNotNull(methodInjectionPoint);
assertEquals(Controlled.class, methodInjectionPoint.getType());
assertTrue(methodInjectionPoint.getAnnotated() instanceof AnnotatedParameter);
assertEquals(bean, methodInjectionPoint.getBean());
AnnotatedParameter<Controller> methodParam = (AnnotatedParameter<Controller>) methodInjectionPoint.getAnnotated();
assertEquals(0, methodParam.getPosition());
assertEquals(Controller.class, methodParam.getDeclaringCallable().getJavaMember().getDeclaringClass());
assertEquals("setControlled", methodParam.getDeclaringCallable().getJavaMember().getName());
// Constructor
InjectionPoint ctorInjectionPoint = controller.controlledCtor.injectionPoint;
assertNotNull(ctorInjectionPoint);
assertEquals(Controlled.class, ctorInjectionPoint.getType());
assertTrue(ctorInjectionPoint.getAnnotated() instanceof AnnotatedParameter);
assertEquals(bean, ctorInjectionPoint.getBean());
AnnotatedParameter<Controller> ctorParam = (AnnotatedParameter<Controller>) ctorInjectionPoint.getAnnotated();
assertEquals(1, ctorParam.getPosition());
assertTrue(ctorParam.isAnnotationPresent(Singleton.class));
assertTrue(ctorParam.getAnnotation(Singleton.class) != null);
assertTrue(!ctorParam.getAnnotations(Singleton.class).isEmpty());
assertEquals(1, ctorParam.getAnnotations().size());
assertTrue(ctorParam.getDeclaringCallable() instanceof AnnotatedConstructor);
assertEquals(Controller.class, ctorParam.getDeclaringCallable().getJavaMember().getDeclaringClass());
// Instance
InjectionPoint instanceInjectionPoint = controller.instanceControlled.get().injectionPoint;
assertNotNull(instanceInjectionPoint);
assertEquals(Controlled.class, instanceInjectionPoint.getType());
qualifiers = instanceInjectionPoint.getQualifiers();
assertEquals(1, qualifiers.size());
assertEquals(Default.class, qualifiers.iterator().next().annotationType());
bean = instanceInjectionPoint.getBean();
assertNotNull(bean);
assertTrue(bean.getTypes().stream().anyMatch(t -> t.equals(Controller.class)));
assertNotNull(instanceInjectionPoint.getAnnotated());
assertTrue(instanceInjectionPoint.getAnnotated() instanceof AnnotatedField);
annotatedField = (AnnotatedField) instanceInjectionPoint.getAnnotated();
assertEquals("instanceControlled", annotatedField.getJavaMember().getName());
assertEquals(new TypeLiteral<Instance<Controlled>>() {
}.getType(), annotatedField.getBaseType());
assertTrue(annotatedField.isAnnotationPresent(Inject.class));
assertTrue(annotatedField.getAnnotation(Singleton.class) == null);
assertTrue(annotatedField.getAnnotations(Singleton.class).isEmpty());
assertEquals(1, annotatedField.getAnnotations().size());
}
Aggregations