Search in sources :

Example 31 with Bean

use of javax.enterprise.inject.spi.Bean in project core by weld.

the class JsonObjects method createFullBeanJson.

/**
 * @param bean
 * @param transientDependencies
 * @param transientDependents
 * @param probe
 * @return the full bean representation
 */
static JsonObjectBuilder createFullBeanJson(Bean<?> bean, boolean transientDependencies, boolean transientDependents, BeanManagerImpl beanManager, Probe probe) {
    JsonObjectBuilder beanBuilder = createBasicBeanJson(bean, probe);
    // NAME
    if (bean.getName() != null) {
        beanBuilder.add(NAME, bean.getName());
    }
    // STEREOTYPES
    if (bean.getStereotypes() != null && !bean.getStereotypes().isEmpty()) {
        JsonArrayBuilder stereotypesBuilder = Json.arrayBuilder();
        for (Class<?> stereotype : Components.getSortedProbeComponetCandidates(bean.getStereotypes())) {
            stereotypesBuilder.add(Json.objectBuilder().add(CLASS, stereotype.getName()).add(PROBE_COMPONENT, Components.isProbeComponent(stereotype)));
        }
        beanBuilder.add(STEREOTYPES, stereotypesBuilder);
    }
    if (bean instanceof SessionBean) {
        // SESSION BEAN
        SessionBean<?> sessionBean = (SessionBean<?>) bean;
        if (sessionBean.getEjbDescriptor().getEjbName() != null) {
            beanBuilder.add(EJB_NAME, true);
        }
        beanBuilder.add(SESSION_BEAN_TYPE, Components.getSessionBeanType(sessionBean.getEjbDescriptor()).toString());
    } else if (bean instanceof AbstractProducerBean) {
        // PRODUCERS
        AbstractProducerBean<?, ?, ?> producerBean = (AbstractProducerBean<?, ?, ?>) bean;
        if (producerBean.getDeclaringBean() != null) {
            beanBuilder.add(DECLARING_BEAN, createSimpleBeanJson(producerBean.getDeclaringBean(), probe));
        }
        if (producerBean.getProducer() instanceof ProducerMethodProducer) {
            ProducerMethodProducer<?, ?> producer = (ProducerMethodProducer<?, ?>) producerBean.getProducer();
            if (producer.getDisposalMethod() != null) {
                beanBuilder.add(DISPOSAL_METHOD, annotatedMethodToString(producer.getDisposalMethod().getAnnotated(), bean.getBeanClass()));
            }
            beanBuilder.add(PRODUCER_METHOD, annotatedMethodToString((AnnotatedMethod<?>) producer.getAnnotated(), bean.getBeanClass()));
        } else if (producerBean.getProducer() instanceof ProducerFieldProducer) {
            ProducerFieldProducer<?, ?> producer = (ProducerFieldProducer<?, ?>) producerBean.getProducer();
            if (producer.getDisposalMethod() != null) {
                beanBuilder.add(DISPOSAL_METHOD, annotatedMethodToString(producer.getDisposalMethod().getAnnotated(), bean.getBeanClass()));
            }
            beanBuilder.add(PRODUCER_FIELD, annotatedFieldToString(producer.getAnnotated(), bean.getBeanClass()));
        }
    }
    // DEPENDENCIES
    JsonArrayBuilder dependencies = createDependencies(null, bean, probe, transientDependencies);
    if (dependencies != null) {
        beanBuilder.add(DEPENDENCIES, dependencies);
    }
    // DEPENDENTS
    JsonArrayBuilder dependents = createDependents(null, bean, probe, transientDependents);
    if (dependents != null) {
        beanBuilder.add(DEPENDENTS, dependents);
    }
    // DECLARED OBSERVERS
    JsonArrayBuilder declaredObservers = Json.arrayBuilder();
    for (ObserverMethod<?> observerMethod : probe.getObservers()) {
        if (observerMethod instanceof ObserverMethodImpl) {
            ObserverMethodImpl<?, ?> observerMethodImpl = (ObserverMethodImpl<?, ?>) observerMethod;
            if (bean.equals(observerMethodImpl.getDeclaringBean())) {
                JsonObjectBuilder observerBuilder = createSimpleObserverJson(observerMethodImpl, probe);
                observerBuilder.add(RECEPTION, observerMethodImpl.getReception().toString());
                observerBuilder.add(TX_PHASE, observerMethodImpl.getTransactionPhase().toString());
                observerBuilder.add(ANNOTATED_METHOD, annotatedMethodToString(observerMethodImpl.getMethod().getAnnotated(), bean.getBeanClass()));
                declaredObservers.add(observerBuilder);
            }
        }
    }
    beanBuilder.add(DECLARED_OBSERVERS, declaredObservers);
    // DECLARED PRODUCERS
    beanBuilder.add(DECLARED_PRODUCERS, createDeclaredProducers(bean, probe));
    // ENABLEMENT
    BeanKind kind = BeanKind.from(bean);
    if (BeanKind.INTERCEPTOR.equals(kind) || BeanKind.DECORATOR.equals(kind) || bean.isAlternative()) {
        JsonObjectBuilder enablementBuilder = Json.objectBuilder();
        AnnotationApiAbstraction annotationApi = beanManager.getServices().get(AnnotationApiAbstraction.class);
        Object priority = bean.getBeanClass().getAnnotation(annotationApi.PRIORITY_ANNOTATION_CLASS);
        if (priority != null) {
            int priorityValue = annotationApi.getPriority(priority);
            enablementBuilder.add(PRIORITY, priorityValue);
            enablementBuilder.add(PRIORITY_RANGE, Components.PriorityRange.of(priorityValue).toString());
            if (!probe.getLocalEnablementOfBean(bean.getBeanClass()).isEmpty()) {
                enablementBuilder.add(WARNING, WARNING_CONFLICTING_ENABLEMENT);
                JsonArrayBuilder conflictingBdas = Json.arrayBuilder();
                for (String bdaId : probe.getLocalEnablementOfBean(bean.getBeanClass())) {
                    conflictingBdas.add(createSimpleBdaJson(bdaId));
                }
                enablementBuilder.add(CONFLICTS, conflictingBdas);
            }
        } else {
            JsonArrayBuilder bdasBuilder = Json.arrayBuilder();
            Collection<BeanManagerImpl> beanManagers = Container.instance(beanManager).beanDeploymentArchives().values();
            for (BeanManagerImpl manager : beanManagers) {
                ModuleEnablement enablement = manager.getEnabled();
                if ((BeanKind.INTERCEPTOR.equals(kind) && enablement.isInterceptorEnabled(bean.getBeanClass())) || (BeanKind.DECORATOR.equals(kind) && enablement.isDecoratorEnabled(bean.getBeanClass())) || isSelectedAlternative(enablement, bean)) {
                    bdasBuilder.add(createSimpleBdaJson(manager.getId()));
                }
            }
            enablementBuilder.add(BDAS, bdasBuilder);
        }
        beanBuilder.add(ENABLEMENT, enablementBuilder);
    }
    // INTERCEPTOR BINDINGS
    if (BeanKind.INTERCEPTOR.equals(kind)) {
        Interceptor<?> interceptor = (Interceptor<?>) bean;
        JsonArrayBuilder bindings = Json.arrayBuilder(true);
        for (Annotation binding : interceptor.getInterceptorBindings()) {
            bindings.add(binding.toString());
        }
        beanBuilder.add(BINDINGS, bindings);
    }
    // DECORATOR
    if (BeanKind.DECORATOR.equals(kind)) {
        Decorator<?> decorator = (Decorator<?>) bean;
        beanBuilder.add(DELEGATE_TYPE, Formats.formatType(decorator.getDelegateType(), false));
        beanBuilder.add(DELEGATE_QUALIFIERS, createQualifiers(decorator.getDelegateQualifiers(), false));
        JsonArrayBuilder decoratedTypes = Json.arrayBuilder(true);
        for (Type type : decorator.getDecoratedTypes()) {
            decoratedTypes.add(Formats.formatType(type, false));
        }
        beanBuilder.add(DECORATED_TYPES, decoratedTypes);
        // ASSOCIATED TO BEANS
        Set<Bean<?>> decoratedBeans = findDecoratedBeans(decorator, beanManager, probe);
        if (!decoratedBeans.isEmpty()) {
            JsonArrayBuilder decoratedBeansBuilder = Json.arrayBuilder();
            for (Bean<?> decoratedBean : decoratedBeans) {
                decoratedBeansBuilder.add(createSimpleBeanJson(decoratedBean, probe));
            }
            beanBuilder.add(ASSOCIATED_TO, decoratedBeansBuilder);
        }
    }
    if (bean instanceof AbstractClassBean) {
        AbstractClassBean<?> abstractClassBean = (AbstractClassBean<?>) bean;
        InterceptionModel interceptionModel = abstractClassBean.getInterceptors();
        // CLASS-LEVEL BINDINGS
        if (interceptionModel != null) {
            Set<Annotation> classInterceptorBindings = interceptionModel.getClassInterceptorBindings();
            if (!classInterceptorBindings.isEmpty()) {
                JsonArrayBuilder bindingsBuilder = Json.arrayBuilder();
                for (Annotation binding : Components.getSortedProbeComponetAnnotationCandidates(classInterceptorBindings)) {
                    bindingsBuilder.add(Json.objectBuilder().add(VALUE, binding.toString()).add(PROBE_COMPONENT, Components.isProbeAnnotation(binding)));
                }
                beanBuilder.add(CLASS_INTERCEPTOR_BINDINGS, bindingsBuilder);
            }
        }
        // ASSOCIATED DECORATORS
        List<Decorator<?>> decorators = abstractClassBean.getDecorators();
        if (!decorators.isEmpty()) {
            JsonArrayBuilder decoratorsBuilder = Json.arrayBuilder();
            for (Decorator<?> decorator : decorators) {
                decoratorsBuilder.add(createSimpleBeanJson(decorator, probe));
            }
            beanBuilder.add(DECORATORS, decoratorsBuilder);
        }
    }
    return beanBuilder;
}
Also used : AnnotationApiAbstraction(org.jboss.weld.util.AnnotationApiAbstraction) SessionBean(org.jboss.weld.bean.SessionBean) ProducerMethodProducer(org.jboss.weld.injection.producer.ProducerMethodProducer) SessionBean(org.jboss.weld.bean.SessionBean) AbstractClassBean(org.jboss.weld.bean.AbstractClassBean) AbstractBuiltInBean(org.jboss.weld.bean.builtin.AbstractBuiltInBean) AbstractProducerBean(org.jboss.weld.bean.AbstractProducerBean) Bean(javax.enterprise.inject.spi.Bean) ObserverMethodImpl(org.jboss.weld.event.ObserverMethodImpl) ProducerFieldProducer(org.jboss.weld.injection.producer.ProducerFieldProducer) AbstractClassBean(org.jboss.weld.bean.AbstractClassBean) JsonArrayBuilder(org.jboss.weld.probe.Json.JsonArrayBuilder) JsonObjectBuilder(org.jboss.weld.probe.Json.JsonObjectBuilder) Interceptor(javax.enterprise.inject.spi.Interceptor) BeanKind(org.jboss.weld.probe.Components.BeanKind) ModuleEnablement(org.jboss.weld.bootstrap.enablement.ModuleEnablement) AbstractProducerBean(org.jboss.weld.bean.AbstractProducerBean) InterceptionModel(org.jboss.weld.interceptor.spi.model.InterceptionModel) Annotation(java.lang.annotation.Annotation) Decorator(javax.enterprise.inject.spi.Decorator) ParameterizedType(java.lang.reflect.ParameterizedType) EventType(org.jboss.weld.probe.BootstrapStats.EventType) ProcessAnnotatedType(javax.enterprise.inject.spi.ProcessAnnotatedType) ProcessSyntheticAnnotatedType(javax.enterprise.inject.spi.ProcessSyntheticAnnotatedType) Type(java.lang.reflect.Type) BeanManagerImpl(org.jboss.weld.manager.BeanManagerImpl) ProxyObject(org.jboss.weld.bean.proxy.ProxyObject)

Example 32 with Bean

use of javax.enterprise.inject.spi.Bean in project core by weld.

the class Reports method addInvalidDependencies.

private static void addInvalidDependencies(Probe probe, HtmlTag body) {
    body.add(HtmlTag.aname(DEPS));
    body.add(HtmlTag.h2(TITLE_DEPS));
    HtmlTag table = HtmlTag.stripedTable().appendTo(HtmlTag.div(DEPS).appendTo(body));
    HtmlTag.tr().add(HtmlTag.th(""), HtmlTag.th("Declaring Bean"), HtmlTag.th("Problem")).appendTo(table);
    int idx = 0;
    for (Bean<?> bean : probe.getBeans()) {
        final BeanManagerImpl beanManager = probe.getBeanManager(bean);
        // Don't process built-in beans
        if (beanManager == null) {
            continue;
        }
        Set<InjectionPoint> injectionPoints = bean.getInjectionPoints();
        if (injectionPoints != null && !injectionPoints.isEmpty()) {
            for (InjectionPoint injectionPoint : injectionPoints) {
                if (injectionPoint.isDelegate()) {
                    // Do not process delegate injection points
                    continue;
                }
                Set<Bean<?>> beans = beanManager.getBeans(injectionPoint.getType(), injectionPoint.getQualifiers().toArray(new Annotation[injectionPoint.getQualifiers().size()]));
                if (beans.isEmpty()) {
                    // Unsatisfied
                    HtmlTag.tr().add(HtmlTag.td(++idx + "."), HtmlTag.td(bean.toString()).add(HtmlTag.td("Unsatisfied dependency at " + injectionPoint))).appendTo(table);
                    try {
                        beanManager.resolve(beans);
                    } catch (AmbiguousResolutionException e) {
                        // Ambiguous
                        HtmlTag.tr().add(HtmlTag.td(++idx + "."), HtmlTag.td(bean.toString()).add(HtmlTag.td("Ambiguous dependency at " + injectionPoint))).appendTo(table);
                    }
                }
            }
        }
    }
}
Also used : BeanManagerImpl(org.jboss.weld.manager.BeanManagerImpl) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) AmbiguousResolutionException(javax.enterprise.inject.AmbiguousResolutionException) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) Annotation(java.lang.annotation.Annotation) Bean(javax.enterprise.inject.spi.Bean)

Example 33 with Bean

use of javax.enterprise.inject.spi.Bean in project core by weld.

the class Reports method addBeans.

private static void addBeans(Probe probe, HtmlTag body, List<BeanDeploymentArchive> bdas) {
    body.add(HtmlTag.aname(BEANS));
    body.add(HtmlTag.h2(TITLE_BEANS));
    HtmlTag table = HtmlTag.stripedTable().appendTo(HtmlTag.div(BEANS).appendTo(body));
    HtmlTag.tr().add(HtmlTag.th(""), HtmlTag.th("Archive"), HtmlTag.th("Kind"), HtmlTag.th("Bean Class"), HtmlTag.th("Types"), HtmlTag.th("Qualifiers"), HtmlTag.th("Scope")).appendTo(table);
    int idx = 0;
    List<Bean<?>> beans = probe.getBeans();
    for (int i = 0; i < bdas.size(); i++) {
        String bdaId = bdas.get(i).getId();
        for (Bean<?> bean : beans) {
            final BeanManagerImpl beanManager = probe.getBeanManager(bean);
            // Don't process built-in beans
            if (beanManager == null) {
                continue;
            }
            if (bdaId.equals(beanManager.getId())) {
                HtmlTag types = HtmlTag.td();
                for (Iterator<Type> iterator = JsonObjects.sortTypes(bean.getTypes()).iterator(); iterator.hasNext(); ) {
                    // Omit java.lang.Object
                    Type type = iterator.next();
                    String formatted = Strings.escape(Formats.formatType(type, false));
                    if (!Object.class.equals(type)) {
                        types.add(HtmlTag.div().attr(HtmlTag.TITLE, formatted).add(abbreviateType(formatted)));
                        if (iterator.hasNext()) {
                            types.add(HtmlTag.BR);
                        }
                    }
                }
                HtmlTag qualifiers = HtmlTag.td();
                if (bean.getQualifiers() != null && !bean.getQualifiers().isEmpty()) {
                    for (Iterator<Annotation> iterator = bean.getQualifiers().iterator(); iterator.hasNext(); ) {
                        Annotation qualifier = iterator.next();
                        if (Any.class.equals(qualifier.annotationType())) {
                            // Omit javax.enterprise.inject.Any
                            continue;
                        } else if (Default.class.equals(qualifier.annotationType())) {
                            qualifiers.add(JsonObjects.simplifiedAnnotation(qualifier));
                        } else {
                            qualifiers.add(HtmlTag.div().attr(HtmlTag.TITLE, qualifier.toString()).add(abbreviateAnnotation(qualifier.toString())));
                        }
                        if (iterator.hasNext()) {
                            qualifiers.add(HtmlTag.BR);
                        }
                    }
                }
                String beanClass = Formats.formatType(bean.getBeanClass(), false);
                HtmlTag.tr().add(HtmlTag.td(++idx + "."), HtmlTag.td().add(HtmlTag.a("#" + BDAS).add(" " + (i + 1))), HtmlTag.td(BeanKind.from(bean).toString()), HtmlTag.td().add(HtmlTag.div().attr(HtmlTag.TITLE, beanClass).add(abbreviateType(beanClass))), types, qualifiers, HtmlTag.td(JsonObjects.simplifiedScope(bean.getScope()))).appendTo(table);
            }
        }
    }
}
Also used : SafeString(org.jboss.weld.probe.HtmlTag.SafeString) Default(javax.enterprise.inject.Default) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) Annotation(java.lang.annotation.Annotation) Bean(javax.enterprise.inject.spi.Bean) Type(java.lang.reflect.Type) BeanManagerImpl(org.jboss.weld.manager.BeanManagerImpl)

Example 34 with Bean

use of javax.enterprise.inject.spi.Bean in project core by weld.

the class Components method getNumberOfEnabledBeans.

/**
 * @param manager
 * @return the number of enabled beans, without built-in, extensions, interceptors, decorators
 */
static Integer getNumberOfEnabledBeans(BeanManagerImpl manager) {
    List<Bean<?>> enabledBeans = manager.getBeans();
    int count = 0;
    for (Bean<?> bean : enabledBeans) {
        if (!Components.isBuiltinBeanButNotExtension(bean)) {
            count++;
        }
    }
    return count;
}
Also used : InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) SessionBean(org.jboss.weld.bean.SessionBean) ExtensionBean(org.jboss.weld.bean.builtin.ExtensionBean) ForwardingBean(org.jboss.weld.bean.ForwardingBean) ManagedBean(org.jboss.weld.bean.ManagedBean) AbstractBuiltInBean(org.jboss.weld.bean.builtin.AbstractBuiltInBean) Bean(javax.enterprise.inject.spi.Bean)

Example 35 with Bean

use of javax.enterprise.inject.spi.Bean in project core by weld.

the class Components method getDependencies.

/**
 * @param bean
 * @param beanManager
 * @param probe
 * @return the set of dependencies
 */
static Set<Dependency> getDependencies(Bean<?> bean, BeanManager beanManager, Probe probe) {
    Set<Dependency> dependencies = new HashSet<Dependency>();
    Set<InjectionPoint> injectionPoints = bean.getInjectionPoints();
    if (injectionPoints != null && !injectionPoints.isEmpty()) {
        for (InjectionPoint injectionPoint : injectionPoints) {
            if (injectionPoint.isDelegate()) {
                // Do not include delegate injection points
                continue;
            }
            Set<Bean<?>> beans = beanManager.getBeans(injectionPoint.getType(), injectionPoint.getQualifiers().toArray(new Annotation[injectionPoint.getQualifiers().size()]));
            if (beans.isEmpty()) {
                dependencies.add(Dependency.createUnsatisfied(injectionPoint, null));
            } else {
                try {
                    Bean<?> dependency = beanManager.resolve(beans);
                    if (isBuiltinBeanButNotExtension(dependency)) {
                        dependency = probe.getBean(Components.getBuiltinBeanId((AbstractBuiltInBean<?>) dependency));
                    }
                    dependencies.add(new Dependency(dependency, injectionPoint));
                } catch (AmbiguousResolutionException e) {
                    dependencies.add(Dependency.createAmbiguous(injectionPoint, null));
                }
            }
        }
    }
    return dependencies;
}
Also used : InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) AmbiguousResolutionException(javax.enterprise.inject.AmbiguousResolutionException) Annotation(java.lang.annotation.Annotation) HashSet(java.util.HashSet) SessionBean(org.jboss.weld.bean.SessionBean) ExtensionBean(org.jboss.weld.bean.builtin.ExtensionBean) ForwardingBean(org.jboss.weld.bean.ForwardingBean) ManagedBean(org.jboss.weld.bean.ManagedBean) AbstractBuiltInBean(org.jboss.weld.bean.builtin.AbstractBuiltInBean) Bean(javax.enterprise.inject.spi.Bean)

Aggregations

Bean (javax.enterprise.inject.spi.Bean)259 Test (org.junit.Test)119 CreationalContext (javax.enterprise.context.spi.CreationalContext)110 BeanManager (javax.enterprise.inject.spi.BeanManager)76 URL (java.net.URL)68 Path (org.uberfire.backend.vfs.Path)67 KieModuleService (org.kie.workbench.common.services.shared.project.KieModuleService)66 Package (org.guvnor.common.services.project.model.Package)43 HashSet (java.util.HashSet)31 Annotation (java.lang.annotation.Annotation)18 ArrayList (java.util.ArrayList)14 Module (org.guvnor.common.services.project.model.Module)13 AnnotationLiteral (javax.enterprise.util.AnnotationLiteral)11 Type (java.lang.reflect.Type)9 Set (java.util.Set)9 AfterDeploymentValidation (javax.enterprise.inject.spi.AfterDeploymentValidation)9 BeanManagerImpl (org.apache.webbeans.container.BeanManagerImpl)9 Any (javax.enterprise.inject.Any)8 AbstractBuiltInBean (org.jboss.weld.bean.builtin.AbstractBuiltInBean)8 Weld (org.jboss.weld.environment.se.Weld)8