Search in sources :

Example 1 with Arc

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;
}
Also used : Arc(io.quarkus.arc.Arc) ArcContainer(io.quarkus.arc.ArcContainer) ResultHandle(io.quarkus.gizmo.ResultHandle) InstanceHandle(io.quarkus.arc.InstanceHandle) Annotation(java.lang.annotation.Annotation)

Example 2 with Arc

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;
}
Also used : Arc(io.quarkus.arc.Arc) ArcContainer(io.quarkus.arc.ArcContainer) ResultHandle(io.quarkus.gizmo.ResultHandle) InstanceHandle(io.quarkus.arc.InstanceHandle) Annotation(java.lang.annotation.Annotation)

Example 3 with Arc

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());
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Singleton(javax.inject.Singleton) AtomicReference(java.util.concurrent.atomic.AtomicReference) Retention(java.lang.annotation.Retention) Inject(javax.inject.Inject) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) TypeLiteral(javax.enterprise.util.TypeLiteral) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Observes(javax.enterprise.event.Observes) ArcContainer(io.quarkus.arc.ArcContainer) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Instance(javax.enterprise.inject.Instance) AnnotatedConstructor(javax.enterprise.inject.spi.AnnotatedConstructor) Arc(io.quarkus.arc.Arc) Default(javax.enterprise.inject.Default) ArcTestContainer(io.quarkus.arc.test.ArcTestContainer) Set(java.util.Set) AnnotatedParameter(javax.enterprise.inject.spi.AnnotatedParameter) AnnotatedField(javax.enterprise.inject.spi.AnnotatedField) Test(org.junit.jupiter.api.Test) Dependent(javax.enterprise.context.Dependent) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Annotation(java.lang.annotation.Annotation) Assertions(org.junit.jupiter.api.Assertions) Bean(javax.enterprise.inject.spi.Bean) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) BeanManager(javax.enterprise.inject.spi.BeanManager) RetentionPolicy(java.lang.annotation.RetentionPolicy) Inject(javax.inject.Inject) ArcContainer(io.quarkus.arc.ArcContainer) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) AnnotatedParameter(javax.enterprise.inject.spi.AnnotatedParameter) Annotation(java.lang.annotation.Annotation) AnnotatedConstructor(javax.enterprise.inject.spi.AnnotatedConstructor) TypeLiteral(javax.enterprise.util.TypeLiteral) Singleton(javax.inject.Singleton) AnnotatedField(javax.enterprise.inject.spi.AnnotatedField) Test(org.junit.jupiter.api.Test)

Aggregations

Arc (io.quarkus.arc.Arc)3 ArcContainer (io.quarkus.arc.ArcContainer)3 Annotation (java.lang.annotation.Annotation)3 InstanceHandle (io.quarkus.arc.InstanceHandle)2 ResultHandle (io.quarkus.gizmo.ResultHandle)2 ArcTestContainer (io.quarkus.arc.test.ArcTestContainer)1 Retention (java.lang.annotation.Retention)1 RetentionPolicy (java.lang.annotation.RetentionPolicy)1 Set (java.util.Set)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Dependent (javax.enterprise.context.Dependent)1 Observes (javax.enterprise.event.Observes)1 Default (javax.enterprise.inject.Default)1 Instance (javax.enterprise.inject.Instance)1 AnnotatedConstructor (javax.enterprise.inject.spi.AnnotatedConstructor)1 AnnotatedField (javax.enterprise.inject.spi.AnnotatedField)1 AnnotatedParameter (javax.enterprise.inject.spi.AnnotatedParameter)1 Bean (javax.enterprise.inject.spi.Bean)1 BeanManager (javax.enterprise.inject.spi.BeanManager)1 InjectionPoint (javax.enterprise.inject.spi.InjectionPoint)1