Search in sources :

Example 6 with BeanInfo

use of io.quarkus.arc.processor.BeanInfo in project quarkus by quarkusio.

the class JsonbProcessor method processJsonbAdapters.

@BuildStep
void processJsonbAdapters(BuildProducer<UnremovableBeanBuildItem> unremovableBeans, BuildProducer<AutoAddScopeBuildItem> autoScopes) {
    // An adapter with an injection point but no scope is @Singleton
    autoScopes.produce(AutoAddScopeBuildItem.builder().implementsInterface(JSONB_ADAPTER_NAME).requiresContainerServices().defaultScope(BuiltinScope.SINGLETON).build());
    // Make all adapters unremovable
    unremovableBeans.produce(new UnremovableBeanBuildItem(new Predicate<BeanInfo>() {

        @Override
        public boolean test(BeanInfo bean) {
            return bean.isClassBean() && bean.hasType(JSONB_ADAPTER_NAME);
        }
    }));
}
Also used : BeanInfo(io.quarkus.arc.processor.BeanInfo) UnremovableBeanBuildItem(io.quarkus.arc.deployment.UnremovableBeanBuildItem) Predicate(java.util.function.Predicate) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Example 7 with BeanInfo

use of io.quarkus.arc.processor.BeanInfo in project quarkus by quarkusio.

the class AutoAddScopeProcessor method annotationTransformer.

@BuildStep
void annotationTransformer(List<AutoAddScopeBuildItem> autoScopes, CustomScopeAnnotationsBuildItem scopes, List<AutoInjectAnnotationBuildItem> autoInjectAnnotations, BuildProducer<AnnotationsTransformerBuildItem> annotationsTransformers, BuildProducer<UnremovableBeanBuildItem> unremovableBeans, BeanArchiveIndexBuildItem beanArchiveIndex) throws Exception {
    if (autoScopes.isEmpty()) {
        return;
    }
    List<AutoAddScopeBuildItem> sortedAutoScopes = autoScopes.stream().sorted(Comparator.comparingInt(AutoAddScopeBuildItem::getPriority).reversed()).collect(Collectors.toList());
    Set<DotName> containerAnnotationNames = autoInjectAnnotations.stream().flatMap(a -> a.getAnnotationNames().stream()).collect(Collectors.toSet());
    containerAnnotationNames.add(DotNames.POST_CONSTRUCT);
    containerAnnotationNames.add(DotNames.PRE_DESTROY);
    containerAnnotationNames.add(DotNames.INJECT);
    Set<DotName> unremovables = new HashSet<>();
    annotationsTransformers.produce(new AnnotationsTransformerBuildItem(new AnnotationsTransformer() {

        @Override
        public boolean appliesTo(Kind kind) {
            return kind == Kind.CLASS;
        }

        @Override
        public int getPriority() {
            // Make sure this annotation transformer runs before all transformers with the default priority
            return DEFAULT_PRIORITY + 1000;
        }

        @Override
        public void transform(TransformationContext context) {
            if (scopes.isScopeIn(context.getAnnotations())) {
                // Skip classes annotated with a scope
                return;
            }
            ClassInfo clazz = context.getTarget().asClass();
            DotName scope = null;
            Boolean requiresContainerServices = null;
            String reason = null;
            for (AutoAddScopeBuildItem autoScope : sortedAutoScopes) {
                if (autoScope.isContainerServicesRequired()) {
                    if (requiresContainerServices == null) {
                        // Analyze the class hierarchy lazily
                        requiresContainerServices = requiresContainerServices(clazz, containerAnnotationNames, beanArchiveIndex.getIndex());
                    }
                    if (!requiresContainerServices) {
                        // Skip - no injection point detected
                        continue;
                    }
                }
                if (autoScope.test(clazz, context.getAnnotations(), beanArchiveIndex.getIndex())) {
                    if (scope != null) {
                        BiConsumer<DotName, String> consumer = autoScope.getScopeAlreadyAdded();
                        if (consumer != null) {
                            consumer.accept(scope, reason);
                        } else {
                            LOGGER.debugf("Scope %s was already added for reason: %s", scope, reason);
                        }
                        continue;
                    }
                    scope = autoScope.getDefaultScope();
                    reason = autoScope.getReason();
                    context.transform().add(scope).done();
                    if (autoScope.isUnremovable()) {
                        unremovables.add(clazz.name());
                    }
                    LOGGER.debugf("Automatically added scope %s to class %s: %s", scope, clazz, autoScope.getReason());
                }
            }
        }
    }));
    if (!unremovables.isEmpty()) {
        unremovableBeans.produce(new UnremovableBeanBuildItem(new Predicate<BeanInfo>() {

            @Override
            public boolean test(BeanInfo bean) {
                return bean.isClassBean() && unremovables.contains(bean.getBeanClass());
            }
        }));
    }
}
Also used : IndexView(org.jboss.jandex.IndexView) DotNames(io.quarkus.arc.processor.DotNames) Predicate(java.util.function.Predicate) Logger(org.jboss.logging.Logger) DotName(org.jboss.jandex.DotName) Set(java.util.Set) ClassInfo(org.jboss.jandex.ClassInfo) Collectors(java.util.stream.Collectors) BuildProducer(io.quarkus.deployment.annotations.BuildProducer) HashSet(java.util.HashSet) AnnotationsTransformer(io.quarkus.arc.processor.AnnotationsTransformer) BuildStep(io.quarkus.deployment.annotations.BuildStep) List(java.util.List) Kind(org.jboss.jandex.AnnotationTarget.Kind) BiConsumer(java.util.function.BiConsumer) Comparator(java.util.Comparator) BeanInfo(io.quarkus.arc.processor.BeanInfo) BeanInfo(io.quarkus.arc.processor.BeanInfo) DotName(org.jboss.jandex.DotName) Predicate(java.util.function.Predicate) AnnotationsTransformer(io.quarkus.arc.processor.AnnotationsTransformer) Kind(org.jboss.jandex.AnnotationTarget.Kind) HashSet(java.util.HashSet) ClassInfo(org.jboss.jandex.ClassInfo) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Example 8 with BeanInfo

use of io.quarkus.arc.processor.BeanInfo in project quarkus by quarkusio.

the class LookupConditionsProcessor method suppressConditionsGenerators.

@BuildStep
void suppressConditionsGenerators(BuildProducer<SuppressConditionGeneratorBuildItem> generators, BeanArchiveIndexBuildItem beanArchiveIndex) {
    IndexView index = beanArchiveIndex.getIndex();
    generators.produce(new SuppressConditionGeneratorBuildItem(new Function<BeanInfo, Consumer<BytecodeCreator>>() {

        @Override
        public Consumer<BytecodeCreator> apply(BeanInfo bean) {
            Optional<AnnotationTarget> maybeTarget = bean.getTarget();
            if (maybeTarget.isPresent()) {
                AnnotationTarget target = maybeTarget.get();
                List<AnnotationInstance> ifPropertyList = findAnnotations(target, LOOK_UP_IF_PROPERTY, LOOK_UP_IF_CONTAINER, index);
                List<AnnotationInstance> unlessPropertyList = findAnnotations(target, LOOK_UP_UNLESS_PROPERTY, LOOK_UP_UNLESS_PROPERTY_CONTAINER, index);
                if (!ifPropertyList.isEmpty() || !unlessPropertyList.isEmpty()) {
                    return new Consumer<BytecodeCreator>() {

                        @Override
                        public void accept(BytecodeCreator suppressed) {
                            for (AnnotationInstance ifProperty : ifPropertyList) {
                                String propertyName = ifProperty.value(NAME).asString();
                                String expectedStringValue = ifProperty.value(STRING_VALUE).asString();
                                AnnotationValue lookupIfMissingValue = ifProperty.value(LOOKUP_IF_MISSING);
                                boolean lookupIfMissing = lookupIfMissingValue != null && lookupIfMissingValue.asBoolean();
                                ResultHandle result = suppressed.invokeStaticMethod(SUPPRESS_IF_PROPERTY, suppressed.load(propertyName), suppressed.load(expectedStringValue), suppressed.load(lookupIfMissing));
                                suppressed.ifTrue(result).trueBranch().returnValue(suppressed.load(true));
                            }
                            for (AnnotationInstance unlessProperty : unlessPropertyList) {
                                String propertyName = unlessProperty.value(NAME).asString();
                                String expectedStringValue = unlessProperty.value(STRING_VALUE).asString();
                                AnnotationValue lookupIfMissingValue = unlessProperty.value(LOOKUP_IF_MISSING);
                                boolean lookupIfMissing = lookupIfMissingValue != null && lookupIfMissingValue.asBoolean();
                                ResultHandle result = suppressed.invokeStaticMethod(SUPPRESS_UNLESS_PROPERTY, suppressed.load(propertyName), suppressed.load(expectedStringValue), suppressed.load(lookupIfMissing));
                                suppressed.ifTrue(result).trueBranch().returnValue(suppressed.load(true));
                            }
                        }
                    };
                }
            }
            return null;
        }
    }));
}
Also used : AnnotationTarget(org.jboss.jandex.AnnotationTarget) IndexView(org.jboss.jandex.IndexView) BeanInfo(io.quarkus.arc.processor.BeanInfo) BytecodeCreator(io.quarkus.gizmo.BytecodeCreator) Function(java.util.function.Function) Consumer(java.util.function.Consumer) AnnotationValue(org.jboss.jandex.AnnotationValue) ResultHandle(io.quarkus.gizmo.ResultHandle) AnnotationInstance(org.jboss.jandex.AnnotationInstance) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Example 9 with BeanInfo

use of io.quarkus.arc.processor.BeanInfo in project quarkus by quarkusio.

the class ArcDevConsoleProcessor method collectBeanInfo.

@BuildStep(onlyIf = IsDevelopment.class)
public DevConsoleTemplateInfoBuildItem collectBeanInfo(ValidationPhaseBuildItem validationPhaseBuildItem, CompletedApplicationClassPredicateBuildItem predicate) {
    BeanDeploymentValidator.ValidationContext validationContext = validationPhaseBuildItem.getContext();
    DevBeanInfos beanInfos = new DevBeanInfos();
    for (BeanInfo bean : validationContext.beans()) {
        beanInfos.addBean(DevBeanInfo.from(bean, predicate));
    }
    for (BeanInfo bean : validationContext.removedBeans()) {
        beanInfos.addRemovedBean(DevBeanInfo.from(bean, predicate));
    }
    for (ObserverInfo observer : validationContext.get(BuildExtension.Key.OBSERVERS)) {
        beanInfos.addObserver(DevObserverInfo.from(observer, predicate));
    }
    for (InterceptorInfo interceptor : validationContext.get(BuildExtension.Key.INTERCEPTORS)) {
        beanInfos.addInterceptor(DevInterceptorInfo.from(interceptor, predicate));
    }
    Collection<InterceptorInfo> removedInterceptors = validationContext.get(BuildExtension.Key.REMOVED_INTERCEPTORS);
    if (removedInterceptors != null) {
        for (InterceptorInfo interceptor : removedInterceptors) {
            beanInfos.addRemovedInterceptor(DevInterceptorInfo.from(interceptor, predicate));
        }
    }
    for (DecoratorInfo decorator : validationContext.get(BuildExtension.Key.DECORATORS)) {
        beanInfos.addDecorator(DevDecoratorInfo.from(decorator, predicate));
    }
    Collection<DecoratorInfo> removedDecorators = validationContext.get(BuildExtension.Key.REMOVED_DECORATORS);
    if (removedDecorators != null) {
        for (DecoratorInfo decorator : removedDecorators) {
            beanInfos.addRemovedDecorator(DevDecoratorInfo.from(decorator, predicate));
        }
    }
    beanInfos.sort();
    return new DevConsoleTemplateInfoBuildItem("devBeanInfos", beanInfos);
}
Also used : ObserverInfo(io.quarkus.arc.processor.ObserverInfo) BeanInfo(io.quarkus.arc.processor.BeanInfo) DecoratorInfo(io.quarkus.arc.processor.DecoratorInfo) BeanDeploymentValidator(io.quarkus.arc.processor.BeanDeploymentValidator) InterceptorInfo(io.quarkus.arc.processor.InterceptorInfo) DevConsoleTemplateInfoBuildItem(io.quarkus.devconsole.spi.DevConsoleTemplateInfoBuildItem) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Example 10 with BeanInfo

use of io.quarkus.arc.processor.BeanInfo in project quarkus by quarkusio.

the class GrpcServerProcessor method validateBindableServices.

@BuildStep
void validateBindableServices(ValidationPhaseBuildItem validationPhase, BuildProducer<ValidationPhaseBuildItem.ValidationErrorBuildItem> errors) {
    Type mutinyBeanType = Type.create(GrpcDotNames.MUTINY_BEAN, org.jboss.jandex.Type.Kind.CLASS);
    Type mutinyServiceType = Type.create(GrpcDotNames.MUTINY_SERVICE, org.jboss.jandex.Type.Kind.CLASS);
    Type bindableServiceType = Type.create(GrpcDotNames.BINDABLE_SERVICE, org.jboss.jandex.Type.Kind.CLASS);
    Predicate<Set<Type>> predicate = new Predicate<>() {

        @Override
        public boolean test(Set<Type> types) {
            return types.contains(bindableServiceType) || types.contains(mutinyServiceType);
        }
    };
    for (BeanInfo bean : validationPhase.getContext().beans().classBeans().matchBeanTypes(predicate)) {
        validateBindableService(bean, mutinyBeanType, errors);
    }
    // Validate the removed beans as well - detect beans not annotated with @GrpcService
    for (BeanInfo bean : validationPhase.getContext().removedBeans().classBeans().matchBeanTypes(predicate)) {
        validateBindableService(bean, mutinyBeanType, errors);
    }
}
Also used : Type(org.jboss.jandex.Type) Set(java.util.Set) HashSet(java.util.HashSet) BeanInfo(io.quarkus.arc.processor.BeanInfo) Predicate(java.util.function.Predicate) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Aggregations

BeanInfo (io.quarkus.arc.processor.BeanInfo)16 BuildStep (io.quarkus.deployment.annotations.BuildStep)12 AnnotationInstance (org.jboss.jandex.AnnotationInstance)8 MethodInfo (org.jboss.jandex.MethodInfo)7 ClassInfo (org.jboss.jandex.ClassInfo)5 Type (org.jboss.jandex.Type)4 ArrayList (java.util.ArrayList)3 Predicate (java.util.function.Predicate)3 AnnotationStore (io.quarkus.arc.processor.AnnotationStore)2 DecoratorInfo (io.quarkus.arc.processor.DecoratorInfo)2 InjectionPointInfo (io.quarkus.arc.processor.InjectionPointInfo)2 InterceptorInfo (io.quarkus.arc.processor.InterceptorInfo)2 BuildProducer (io.quarkus.deployment.annotations.BuildProducer)2 Record (io.quarkus.deployment.annotations.Record)2 ClassOutput (io.quarkus.gizmo.ClassOutput)2 ResultHandle (io.quarkus.gizmo.ResultHandle)2 MediatorBuildItem (io.quarkus.smallrye.reactivemessaging.deployment.items.MediatorBuildItem)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 DeploymentException (javax.enterprise.inject.spi.DeploymentException)2