Search in sources :

Example 1 with CreationalContextImpl

use of io.quarkus.arc.impl.CreationalContextImpl in project quarkus by quarkusio.

the class BeanGenerator method implementCreateForProducerMethod.

void implementCreateForProducerMethod(ClassOutput classOutput, ClassCreator beanCreator, BeanInfo bean, ProviderType providerType, String baseName, Map<InjectionPointInfo, String> injectionPointToProviderSupplierField, ReflectionRegistration reflectionRegistration, String targetPackage, boolean isApplicationClass, MethodCreator create) {
    AssignableResultHandle instanceHandle;
    MethodInfo producerMethod = bean.getTarget().get().asMethod();
    instanceHandle = create.createVariable(DescriptorUtils.extToInt(providerType.className()));
    // instance = declaringProviderSupplier.get().get(new CreationalContextImpl<>()).produce()
    ResultHandle ctxHandle = create.newInstance(MethodDescriptor.ofConstructor(CreationalContextImpl.class, Contextual.class), create.getThis());
    ResultHandle declaringProviderInstanceHandle;
    ResultHandle declaringProviderSupplierHandle = create.readInstanceField(FieldDescriptor.of(beanCreator.getClassName(), FIELD_NAME_DECLARING_PROVIDER_SUPPLIER, Supplier.class.getName()), create.getThis());
    ResultHandle declaringProviderHandle = create.invokeInterfaceMethod(MethodDescriptors.SUPPLIER_GET, declaringProviderSupplierHandle);
    if (Modifier.isStatic(producerMethod.flags())) {
        // for static producers, we don't need to resolve this this handle
        declaringProviderInstanceHandle = create.loadNull();
    } else {
        declaringProviderInstanceHandle = create.invokeInterfaceMethod(MethodDescriptors.INJECTABLE_REF_PROVIDER_GET, declaringProviderHandle, ctxHandle);
        if (bean.getDeclaringBean().getScope().isNormal()) {
            // We need to unwrap the client proxy
            declaringProviderInstanceHandle = create.invokeInterfaceMethod(MethodDescriptors.CLIENT_PROXY_GET_CONTEXTUAL_INSTANCE, declaringProviderInstanceHandle);
        }
    }
    List<InjectionPointInfo> injectionPoints = bean.getAllInjectionPoints();
    ResultHandle[] referenceHandles = new ResultHandle[injectionPoints.size()];
    List<TransientReference> transientReferences = new ArrayList<>();
    int paramIdx = 0;
    for (InjectionPointInfo injectionPoint : injectionPoints) {
        ResultHandle providerSupplierHandle = create.readInstanceField(FieldDescriptor.of(beanCreator.getClassName(), injectionPointToProviderSupplierField.get(injectionPoint), Supplier.class.getName()), create.getThis());
        ResultHandle providerHandle = create.invokeInterfaceMethod(MethodDescriptors.SUPPLIER_GET, providerSupplierHandle);
        ResultHandle childCtxHandle = create.invokeStaticMethod(MethodDescriptors.CREATIONAL_CTX_CHILD_CONTEXTUAL, providerHandle, create.getMethodParam(0));
        ResultHandle referenceHandle = create.invokeInterfaceMethod(MethodDescriptors.INJECTABLE_REF_PROVIDER_GET, providerHandle, childCtxHandle);
        referenceHandles[paramIdx++] = referenceHandle;
        // We need to destroy dependent beans for @TransientReference injection points
        if (injectionPoint.isDependentTransientReference()) {
            transientReferences.add(new TransientReference(providerHandle, referenceHandle, childCtxHandle));
        }
    }
    if (Modifier.isPrivate(producerMethod.flags())) {
        privateMembers.add(isApplicationClass, String.format("Producer method %s#%s()", producerMethod.declaringClass().name(), producerMethod.name()));
        ResultHandle paramTypesArray = create.newArray(Class.class, create.load(referenceHandles.length));
        ResultHandle argsArray = create.newArray(Object.class, create.load(referenceHandles.length));
        for (int i = 0; i < referenceHandles.length; i++) {
            create.writeArrayValue(paramTypesArray, i, create.loadClass(producerMethod.parameters().get(i).name().toString()));
            create.writeArrayValue(argsArray, i, referenceHandles[i]);
        }
        reflectionRegistration.registerMethod(producerMethod);
        create.assign(instanceHandle, create.invokeStaticMethod(MethodDescriptors.REFLECTIONS_INVOKE_METHOD, create.loadClass(producerMethod.declaringClass().name().toString()), create.load(producerMethod.name()), paramTypesArray, declaringProviderInstanceHandle, argsArray));
    } else {
        ResultHandle invokeMethodHandle;
        if (Modifier.isStatic(producerMethod.flags())) {
            invokeMethodHandle = create.invokeStaticMethod(MethodDescriptor.of(producerMethod), referenceHandles);
        } else {
            invokeMethodHandle = create.invokeVirtualMethod(MethodDescriptor.of(producerMethod), declaringProviderInstanceHandle, referenceHandles);
        }
        create.assign(instanceHandle, invokeMethodHandle);
    }
    if (bean.getScope().isNormal()) {
        create.ifNull(instanceHandle).trueBranch().throwException(IllegalProductException.class, "Normal scoped producer method may not return null: " + bean.getDeclaringBean().getImplClazz().name() + "." + bean.getTarget().get().asMethod().name() + "()");
    }
    // If the declaring bean is @Dependent we must destroy the instance afterwards
    if (BuiltinScope.DEPENDENT.is(bean.getDeclaringBean().getScope())) {
        create.invokeInterfaceMethod(MethodDescriptors.INJECTABLE_BEAN_DESTROY, declaringProviderHandle, declaringProviderInstanceHandle, ctxHandle);
    }
    // Destroy injected transient references
    destroyTransientReferences(create, transientReferences);
    create.returnValue(instanceHandle);
}
Also used : Contextual(javax.enterprise.context.spi.Contextual) ArrayList(java.util.ArrayList) AssignableResultHandle(io.quarkus.gizmo.AssignableResultHandle) MethodInfo(org.jboss.jandex.MethodInfo) ResultHandle(io.quarkus.gizmo.ResultHandle) AssignableResultHandle(io.quarkus.gizmo.AssignableResultHandle) CreationalContextImpl(io.quarkus.arc.impl.CreationalContextImpl)

Example 2 with CreationalContextImpl

use of io.quarkus.arc.impl.CreationalContextImpl in project quarkus by quarkusio.

the class BeanGenerator method implementCreateForProducerField.

void implementCreateForProducerField(ClassOutput classOutput, ClassCreator beanCreator, BeanInfo bean, ProviderType providerType, String baseName, Map<InjectionPointInfo, String> injectionPointToProviderSupplierField, ReflectionRegistration reflectionRegistration, String targetPackage, boolean isApplicationClass, MethodCreator create) {
    AssignableResultHandle instanceHandle = create.createVariable(DescriptorUtils.extToInt(providerType.className()));
    // instance = declaringProviderSupplier.get().get(new CreationalContextImpl<>()).field
    FieldInfo producerField = bean.getTarget().get().asField();
    ResultHandle declaringProviderSupplierHandle = create.readInstanceField(FieldDescriptor.of(beanCreator.getClassName(), FIELD_NAME_DECLARING_PROVIDER_SUPPLIER, Supplier.class.getName()), create.getThis());
    ResultHandle declaringProviderHandle = create.invokeInterfaceMethod(MethodDescriptors.SUPPLIER_GET, declaringProviderSupplierHandle);
    ResultHandle ctxHandle = create.newInstance(MethodDescriptor.ofConstructor(CreationalContextImpl.class, Contextual.class), create.getThis());
    ResultHandle declaringProviderInstanceHandle;
    if (Modifier.isStatic(producerField.flags())) {
        declaringProviderInstanceHandle = create.loadNull();
    } else {
        declaringProviderInstanceHandle = create.invokeInterfaceMethod(MethodDescriptors.INJECTABLE_REF_PROVIDER_GET, declaringProviderHandle, ctxHandle);
        if (bean.getDeclaringBean().getScope().isNormal()) {
            // We need to unwrap the client proxy
            declaringProviderInstanceHandle = create.invokeInterfaceMethod(MethodDescriptors.CLIENT_PROXY_GET_CONTEXTUAL_INSTANCE, declaringProviderInstanceHandle);
        }
    }
    if (Modifier.isPrivate(producerField.flags())) {
        privateMembers.add(isApplicationClass, String.format("Producer field %s#%s", producerField.declaringClass().name(), producerField.name()));
        reflectionRegistration.registerField(producerField);
        create.assign(instanceHandle, create.invokeStaticMethod(MethodDescriptors.REFLECTIONS_READ_FIELD, create.loadClass(producerField.declaringClass().name().toString()), create.load(producerField.name()), declaringProviderInstanceHandle));
    } else {
        ResultHandle readFieldHandle;
        if (Modifier.isStatic(producerField.flags())) {
            readFieldHandle = create.readStaticField(FieldDescriptor.of(producerField));
        } else {
            readFieldHandle = create.readInstanceField(FieldDescriptor.of(producerField), declaringProviderInstanceHandle);
        }
        create.assign(instanceHandle, readFieldHandle);
    }
    if (bean.getScope().isNormal()) {
        create.ifNull(instanceHandle).trueBranch().throwException(IllegalProductException.class, "Normal scoped producer field may not be null: " + bean.getDeclaringBean().getImplClazz().name() + "." + bean.getTarget().get().asField().name());
    }
    // If the declaring bean is @Dependent we must destroy the instance afterwards
    if (BuiltinScope.DEPENDENT.is(bean.getDeclaringBean().getScope())) {
        create.invokeInterfaceMethod(MethodDescriptors.INJECTABLE_BEAN_DESTROY, declaringProviderHandle, declaringProviderInstanceHandle, ctxHandle);
    }
    create.returnValue(instanceHandle);
}
Also used : Contextual(javax.enterprise.context.spi.Contextual) AssignableResultHandle(io.quarkus.gizmo.AssignableResultHandle) ResultHandle(io.quarkus.gizmo.ResultHandle) AssignableResultHandle(io.quarkus.gizmo.AssignableResultHandle) CreationalContextImpl(io.quarkus.arc.impl.CreationalContextImpl) FieldInfo(org.jboss.jandex.FieldInfo)

Example 3 with CreationalContextImpl

use of io.quarkus.arc.impl.CreationalContextImpl in project quarkus by quarkusio.

the class BuiltInBeansAreResolvableTest method testInstanceBean.

@SuppressWarnings({ "serial", "unchecked" })
@Test
public void testInstanceBean() {
    BeanManager bm = Arc.container().beanManager();
    // verify all selections have a backing bean
    Set<Bean<?>> instanceBeans = bm.getBeans(Instance.class);
    assertEquals(1, instanceBeans.size());
    Set<Bean<?>> typedInstanceBeans = bm.getBeans(new TypeLiteral<Instance<DummyBean>>() {
    }.getType());
    assertEquals(1, typedInstanceBeans.size());
    Set<Bean<?>> typedQualifiedInstanceBean = bm.getBeans(new TypeLiteral<Instance<DummyBean>>() {
    }.getType(), new DummyQualifier.Literal());
    assertEquals(1, typedQualifiedInstanceBean.size());
    InjectionPoint dummyIp = new InjectionPoint() {

        @Override
        public Type getType() {
            return new TypeLiteral<Instance<DummyBean>>() {
            }.getType();
        }

        @Override
        public Set<Annotation> getQualifiers() {
            return new HashSet<>();
        }

        @Override
        public Bean<?> getBean() {
            return null;
        }

        @Override
        public Member getMember() {
            return null;
        }

        @Override
        public Annotated getAnnotated() {
            return null;
        }

        @Override
        public boolean isDelegate() {
            return false;
        }

        @Override
        public boolean isTransient() {
            return false;
        }
    };
    InjectionPoint dummyIpWithQualifiers = new InjectionPoint() {

        @Override
        public Type getType() {
            return new TypeLiteral<Instance<DummyBean>>() {
            }.getType();
        }

        @Override
        public Set<Annotation> getQualifiers() {
            return new HashSet<>(Arrays.asList(Any.Literal.INSTANCE, new DummyQualifier.Literal()));
        }

        @Override
        public Bean<?> getBean() {
            return null;
        }

        @Override
        public Member getMember() {
            return null;
        }

        @Override
        public Annotated getAnnotated() {
            return null;
        }

        @Override
        public boolean isDelegate() {
            return false;
        }

        @Override
        public boolean isTransient() {
            return false;
        }
    };
    Instance<DummyBean> dummyInstance = (Instance<DummyBean>) bm.getInjectableReference(dummyIp, new CreationalContextImpl<>(null));
    // not resolvable, no qualifier
    assertFalse(dummyInstance.isResolvable());
    dummyInstance.select(new DummyQualifier.Literal()).get().ping();
    Instance<DummyBean> dummyInstanceWithQualifier = (Instance<DummyBean>) bm.getInjectableReference(dummyIpWithQualifiers, new CreationalContextImpl<>(null));
    // resolvable, qualifier is present
    assertTrue(dummyInstanceWithQualifier.isResolvable());
    dummyInstanceWithQualifier.get().ping();
}
Also used : InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) Instance(javax.enterprise.inject.Instance) CreationalContextImpl(io.quarkus.arc.impl.CreationalContextImpl) Annotation(java.lang.annotation.Annotation) Bean(javax.enterprise.inject.spi.Bean) TypeLiteral(javax.enterprise.util.TypeLiteral) TypeLiteral(javax.enterprise.util.TypeLiteral) AnnotationLiteral(javax.enterprise.util.AnnotationLiteral) BeanManager(javax.enterprise.inject.spi.BeanManager) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 4 with CreationalContextImpl

use of io.quarkus.arc.impl.CreationalContextImpl in project quarkus by quarkusio.

the class RequestContextTest method testGet.

@Test
public void testGet() {
    ManagedContext requestContext = Arc.container().requestContext();
    requestContext.activate();
    try {
        InjectableBean<Boom> boomBean = Arc.container().instance(Boom.class).getBean();
        assertThatIllegalArgumentException().isThrownBy(() -> requestContext.get(boomBean));
        assertThatIllegalArgumentException().isThrownBy(() -> requestContext.get(boomBean, new CreationalContextImpl<>(boomBean)));
    } finally {
        requestContext.terminate();
    }
}
Also used : ManagedContext(io.quarkus.arc.ManagedContext) CreationalContextImpl(io.quarkus.arc.impl.CreationalContextImpl) Test(org.junit.jupiter.api.Test)

Example 5 with CreationalContextImpl

use of io.quarkus.arc.impl.CreationalContextImpl in project quarkus by quarkusio.

the class SingletonContextGetTest method testGet.

@Test
public void testGet() {
    InjectableContext appContext = Arc.container().getActiveContext(Singleton.class);
    InjectableBean<Boom> boomBean = Arc.container().instance(Boom.class).getBean();
    assertThatIllegalArgumentException().isThrownBy(() -> appContext.get(boomBean));
    assertThatIllegalArgumentException().isThrownBy(() -> appContext.get(boomBean, new CreationalContextImpl<>(boomBean)));
}
Also used : InjectableContext(io.quarkus.arc.InjectableContext) CreationalContextImpl(io.quarkus.arc.impl.CreationalContextImpl) Test(org.junit.jupiter.api.Test)

Aggregations

CreationalContextImpl (io.quarkus.arc.impl.CreationalContextImpl)6 Test (org.junit.jupiter.api.Test)4 InjectableContext (io.quarkus.arc.InjectableContext)2 AssignableResultHandle (io.quarkus.gizmo.AssignableResultHandle)2 ResultHandle (io.quarkus.gizmo.ResultHandle)2 Contextual (javax.enterprise.context.spi.Contextual)2 ManagedContext (io.quarkus.arc.ManagedContext)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Instance (javax.enterprise.inject.Instance)1 Bean (javax.enterprise.inject.spi.Bean)1 BeanManager (javax.enterprise.inject.spi.BeanManager)1 InjectionPoint (javax.enterprise.inject.spi.InjectionPoint)1 AnnotationLiteral (javax.enterprise.util.AnnotationLiteral)1 TypeLiteral (javax.enterprise.util.TypeLiteral)1 FieldInfo (org.jboss.jandex.FieldInfo)1 MethodInfo (org.jboss.jandex.MethodInfo)1