Search in sources :

Example 1 with InjectionPointInfo

use of io.quarkus.arc.processor.InjectionPointInfo in project camel-quarkus by apache.

the class InjectionPointsProcessor method injectedComponents.

@Record(ExecutionTime.RUNTIME_INIT)
@BuildStep
void injectedComponents(CombinedIndexBuildItem index, InjectionPointsRecorder recorder, BeanRegistrationPhaseBuildItem beanRegistrationPhase, BuildProducer<SyntheticBeanBuildItem> syntheticBeans, BuildProducer<CamelRuntimeTaskBuildItem> runtimeTasks, BuildProducer<BeanRegistrationPhaseBuildItem.BeanConfiguratorBuildItem> beanConfigurator) {
    final Collection<ClassInfo> components = index.getIndex().getAllKnownImplementors(INTERFACE_NAME_COMPONENT);
    final Set<String> created = new HashSet<>();
    for (InjectionPointInfo injectionPoint : beanRegistrationPhase.getContext().get(BuildExtension.Key.INJECTION_POINTS)) {
        if (injectionPoint.getTarget().kind() == AnnotationTarget.Kind.FIELD) {
            FieldInfo target = injectionPoint.getTarget().asField();
            if (!created.add(target.type().name().toString())) {
                continue;
            }
            if (components.stream().anyMatch(ci -> Objects.equals(ci.name(), target.type().name()))) {
                final AnnotationInstance named = target.annotation(ANNOTATION_NAME_NAMED);
                final String componentName = named == null ? target.name() : named.value().asString();
                final Supplier<?> creator = recorder.componentSupplier(componentName, target.type().toString());
                LOGGER.debugf("Creating synthetic component bean [name=%s, type=%s]", componentName, target.type().name());
                syntheticBeans.produce(syntheticBean(target.type().name(), creator));
            }
        }
        if (injectionPoint.getTarget().kind() == AnnotationTarget.Kind.METHOD) {
            final MethodInfo target = injectionPoint.getTarget().asMethod();
            final List<Type> types = target.parameters();
            for (int i = 0; i < types.size(); i++) {
                Type type = types.get(0);
                if (!created.add(type.name().toString())) {
                    continue;
                }
                if (components.stream().anyMatch(ci -> Objects.equals(ci.name(), type.name()))) {
                    final AnnotationInstance named = target.annotation(ANNOTATION_NAME_NAMED);
                    final String componentName = named == null ? target.parameterName(i) : named.value().asString();
                    final Supplier<?> creator = recorder.componentSupplier(componentName, type.toString());
                    LOGGER.debugf("Creating synthetic component bean [name=%s, type=%s]", componentName, type.name());
                    syntheticBeans.produce(syntheticBean(type.name(), creator));
                }
            }
        }
    }
    // Ensure the task is executed before the runtime is assembled
    runtimeTasks.produce(new CamelRuntimeTaskBuildItem("components-injection"));
    // Methods using BeanRegistrationPhaseBuildItem should return/produce a BeanConfiguratorBuildItem
    // otherwise the build step may be processed at the wrong time.
    // 
    // See BeanRegistrationPhaseBuildItem javadoc
    beanConfigurator.produce(new BeanRegistrationPhaseBuildItem.BeanConfiguratorBuildItem());
}
Also used : Endpoint(org.apache.camel.Endpoint) BeanRegistrationPhaseBuildItem(io.quarkus.arc.deployment.BeanRegistrationPhaseBuildItem) Type(org.jboss.jandex.Type) InjectionPointInfo(io.quarkus.arc.processor.InjectionPointInfo) CamelRuntimeTaskBuildItem(org.apache.camel.quarkus.core.deployment.spi.CamelRuntimeTaskBuildItem) MethodInfo(org.jboss.jandex.MethodInfo) FieldInfo(org.jboss.jandex.FieldInfo) AnnotationInstance(org.jboss.jandex.AnnotationInstance) ClassInfo(org.jboss.jandex.ClassInfo) HashSet(java.util.HashSet) Record(io.quarkus.deployment.annotations.Record) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Example 2 with InjectionPointInfo

use of io.quarkus.arc.processor.InjectionPointInfo in project myfaces by apache.

the class ManagedPropertyBuildStep method build.

public static void build(BeanRegistrationPhaseBuildItem beanRegistrationPhase, BuildProducer<BeanRegistrationPhaseBuildItem.BeanConfiguratorBuildItem> beanConfigurators) {
    for (InjectionPointInfo injectionPoint : beanRegistrationPhase.getContext().get(BuildExtension.Key.INJECTION_POINTS)) {
        if (injectionPoint.hasDefaultedQualifier()) {
            // Defaulted qualifier means no @ManagedProperty
            continue;
        }
        AnnotationInstance managedProperty = injectionPoint.getRequiredQualifier(DotName.createSimple(ManagedProperty.class.getName()));
        if (managedProperty != null) {
            AnnotationValue value = managedProperty.value("value");
            if (value == null) {
                continue;
            }
            Type requiredType = injectionPoint.getRequiredType();
            beanConfigurators.produce(new BeanRegistrationPhaseBuildItem.BeanConfiguratorBuildItem(beanRegistrationPhase.getContext().configure(requiredType.name()).qualifiers(managedProperty).scope(BuiltinScope.DEPENDENT.getInfo()).types(requiredType).creator(ManagedPropertyBeanCreator.class).name(UUID.randomUUID().toString().replace("-", "")).defaultBean().param(ManagedPropertyBeanCreator.EXPRESSION, value.asString())));
        }
    }
}
Also used : Type(org.jboss.jandex.Type) InjectionPointInfo(io.quarkus.arc.processor.InjectionPointInfo) ManagedPropertyBeanCreator(org.apache.myfaces.core.extensions.quarkus.runtime.producer.ManagedPropertyBeanCreator) AnnotationValue(org.jboss.jandex.AnnotationValue) AnnotationInstance(org.jboss.jandex.AnnotationInstance) BeanRegistrationPhaseBuildItem(io.quarkus.arc.deployment.BeanRegistrationPhaseBuildItem)

Aggregations

BeanRegistrationPhaseBuildItem (io.quarkus.arc.deployment.BeanRegistrationPhaseBuildItem)2 InjectionPointInfo (io.quarkus.arc.processor.InjectionPointInfo)2 AnnotationInstance (org.jboss.jandex.AnnotationInstance)2 Type (org.jboss.jandex.Type)2 BuildStep (io.quarkus.deployment.annotations.BuildStep)1 Record (io.quarkus.deployment.annotations.Record)1 HashSet (java.util.HashSet)1 Endpoint (org.apache.camel.Endpoint)1 CamelRuntimeTaskBuildItem (org.apache.camel.quarkus.core.deployment.spi.CamelRuntimeTaskBuildItem)1 ManagedPropertyBeanCreator (org.apache.myfaces.core.extensions.quarkus.runtime.producer.ManagedPropertyBeanCreator)1 AnnotationValue (org.jboss.jandex.AnnotationValue)1 ClassInfo (org.jboss.jandex.ClassInfo)1 FieldInfo (org.jboss.jandex.FieldInfo)1 MethodInfo (org.jboss.jandex.MethodInfo)1