Search in sources :

Example 1 with JsonArrayBuilder

use of org.jboss.weld.probe.Json.JsonArrayBuilder 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 2 with JsonArrayBuilder

use of org.jboss.weld.probe.Json.JsonArrayBuilder in project core by weld.

the class JsonObjects method createSimpleBeanJsonWithDependencies.

/**
 * The simple representation plus dependencies (non-transient).
 *
 * @param bean
 * @param probe
 * @return the simple representation plus dependencies/dependents (non-transient)
 */
static JsonObjectBuilder createSimpleBeanJsonWithDependencies(Bean<?> bean, Probe probe) {
    JsonObjectBuilder builder = createSimpleBeanJson(bean, probe);
    builder.add(DECLARING_BEAN, createDeclaringBean(bean, probe));
    // DEPENDENCIES
    JsonArrayBuilder dependencies = createDependencies(null, bean, probe, false);
    if (dependencies != null) {
        builder.add(DEPENDENCIES, dependencies);
    }
    // DEPENDENTS
    JsonArrayBuilder dependents = createDependents(null, bean, probe, false);
    if (dependents != null) {
        builder.add(DEPENDENTS, dependents);
    }
    return builder;
}
Also used : JsonArrayBuilder(org.jboss.weld.probe.Json.JsonArrayBuilder) JsonObjectBuilder(org.jboss.weld.probe.Json.JsonObjectBuilder)

Example 3 with JsonArrayBuilder

use of org.jboss.weld.probe.Json.JsonArrayBuilder in project core by weld.

the class JsonObjects method createDependencies.

/**
 * @param parent
 * @param bean
 * @param probe
 * @param isTransient
 * @return
 */
static JsonArrayBuilder createDependencies(Bean<?> parent, Bean<?> bean, Probe probe, boolean isTransient) {
    final BeanManagerImpl beanManager = probe.getBeanManager(bean);
    // Don't create dependencies for built-in beans
    if (beanManager == null) {
        return null;
    }
    JsonArrayBuilder dependenciesBuilder = Json.arrayBuilder(true);
    for (Dependency dep : Components.getDependencies(bean, beanManager, probe)) {
        if (dep.isUnsatisfied()) {
            dependenciesBuilder.add(createDependency(null, dep, probe).add(IS_UNSATISFIED, true));
            continue;
        }
        if (dep.isAmbiguous()) {
            dependenciesBuilder.add(createDependency(null, dep, probe).add(IS_AMBIGUOUS, true));
            continue;
        }
        if (Components.isBuiltinBeanButNotExtension(dep.getBean())) {
            dependenciesBuilder.add(createBuiltInDependency(dep, probe, beanManager, DEPENDENCIES));
            continue;
        }
        // Handle circular dependencies
        if (dep.getBean().equals(parent)) {
            dependenciesBuilder.add(createDependency(dep.getBean(), dep, probe));
            continue;
        }
        JsonObjectBuilder dependency = createDependency(dep.getBean(), dep, probe);
        dependency.add(DECLARING_BEAN, createDeclaringBean(dep.getBean(), probe));
        dependenciesBuilder.add(dependency);
        if (isTransient) {
            dependency.add(DEPENDENCIES, createDependencies(bean, dep.getBean(), probe, true));
        }
    }
    return dependenciesBuilder.isEmpty() ? null : dependenciesBuilder;
}
Also used : BeanManagerImpl(org.jboss.weld.manager.BeanManagerImpl) JsonArrayBuilder(org.jboss.weld.probe.Json.JsonArrayBuilder) Dependency(org.jboss.weld.probe.Components.Dependency) JsonObjectBuilder(org.jboss.weld.probe.Json.JsonObjectBuilder)

Example 4 with JsonArrayBuilder

use of org.jboss.weld.probe.Json.JsonArrayBuilder in project core by weld.

the class JsonObjects method createContextualInstanceJson.

static String createContextualInstanceJson(Bean<?> bean, Object contextualInstance, Probe probe) {
    try {
        JsonObjectBuilder builder = createSimpleBeanJson(bean, probe);
        builder.add(SCOPE, simplifiedScope(bean.getScope()));
        builder.add(CONTEXT_ID, Components.getInspectableScopeId(bean.getScope()));
        builder.add(CLASS, contextualInstance.getClass().getName());
        builder.add(HASH, contextualInstance.hashCode());
        builder.add(AS_STRING, Strings.abbreviate(contextualInstance.toString(), CONTEXTUAL_INSTANCE_TO_STRING_LIMIT));
        JsonArrayBuilder propertiesBuilder = Json.arrayBuilder();
        Class<?> definingClass = contextualInstance.getClass();
        if (ProxyObject.class.isAssignableFrom(definingClass)) {
            definingClass = definingClass.getSuperclass();
        }
        BeanInfo bi = java.beans.Introspector.getBeanInfo(definingClass);
        PropertyDescriptor[] properties = bi.getPropertyDescriptors();
        for (PropertyDescriptor propertyDescriptor : properties) {
            Method readMethod = propertyDescriptor.getReadMethod();
            if (readMethod == null) {
                continue;
            }
            Object value;
            try {
                value = readMethod.invoke(contextualInstance);
            } catch (IllegalAccessException e) {
                value = toString(e);
            } catch (IllegalArgumentException e) {
                value = toString(e);
            } catch (InvocationTargetException e) {
                value = toString(e);
            }
            propertiesBuilder.add(Json.objectBuilder().add(NAME, propertyDescriptor.getDisplayName()).add(VALUE, value != null ? Strings.abbreviate(value.toString(), CONTEXTUAL_INSTANCE_PROPERTY_VALUE_LIMIT) : "null"));
        }
        builder.add(PROPERTIES, propertiesBuilder);
        return builder.build();
    } catch (IntrospectionException e) {
        ProbeLogger.LOG.introspectionProblem(bean, e);
        return null;
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) ProxyObject(org.jboss.weld.bean.proxy.ProxyObject) JsonArrayBuilder(org.jboss.weld.probe.Json.JsonArrayBuilder) ObserverMethod(javax.enterprise.inject.spi.ObserverMethod) ContainerLifecycleEventObserverMethod(org.jboss.weld.event.ContainerLifecycleEventObserverMethod) AnnotatedMethod(javax.enterprise.inject.spi.AnnotatedMethod) Method(java.lang.reflect.Method) JsonObjectBuilder(org.jboss.weld.probe.Json.JsonObjectBuilder) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 5 with JsonArrayBuilder

use of org.jboss.weld.probe.Json.JsonArrayBuilder in project core by weld.

the class JsonObjects method createBasicBeanJson.

/**
 * The basic representation consists of the simple representation plus the scope, bean types and qualifiers.
 *
 * @param bean
 * @param probe
 * @return the basic bean representation
 */
static JsonObjectBuilder createBasicBeanJson(Bean<?> bean, Probe probe) {
    JsonObjectBuilder beanBuilder = createSimpleBeanJson(bean, probe);
    // SCOPE
    beanBuilder.add(SCOPE, simplifiedScope(bean.getScope()));
    // BEAN TYPES
    JsonArrayBuilder typesBuilder = Json.arrayBuilder();
    for (Type type : sortTypes(bean.getTypes())) {
        // Omit java.lang.Object
        if (Object.class.equals(type)) {
            continue;
        }
        typesBuilder.add(Formats.formatType(type, false));
    }
    beanBuilder.add(TYPES, typesBuilder);
    // QUALIFIERS
    if (bean.getQualifiers() != null && !bean.getQualifiers().isEmpty()) {
        beanBuilder.add(QUALIFIERS, createQualifiers(bean.getQualifiers(), true));
    }
    // ALTERNATIVE
    if (bean.isAlternative()) {
        beanBuilder.add(IS_ALTERNATIVE, true);
    }
    // UNUSED
    if (probe.isUnused(bean)) {
        beanBuilder.add(UNUSED, true);
    }
    return beanBuilder;
}
Also used : 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) JsonArrayBuilder(org.jboss.weld.probe.Json.JsonArrayBuilder) JsonObjectBuilder(org.jboss.weld.probe.Json.JsonObjectBuilder)

Aggregations

JsonArrayBuilder (org.jboss.weld.probe.Json.JsonArrayBuilder)16 JsonObjectBuilder (org.jboss.weld.probe.Json.JsonObjectBuilder)16 ProxyObject (org.jboss.weld.bean.proxy.ProxyObject)5 AbstractBuiltInBean (org.jboss.weld.bean.builtin.AbstractBuiltInBean)3 ObserverMethodImpl (org.jboss.weld.event.ObserverMethodImpl)3 BeanManagerImpl (org.jboss.weld.manager.BeanManagerImpl)3 EventType (org.jboss.weld.probe.BootstrapStats.EventType)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 Annotation (java.lang.annotation.Annotation)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 ArrayList (java.util.ArrayList)2 AnnotatedMethod (javax.enterprise.inject.spi.AnnotatedMethod)2 Bean (javax.enterprise.inject.spi.Bean)2 ProcessAnnotatedType (javax.enterprise.inject.spi.ProcessAnnotatedType)2 ProcessSyntheticAnnotatedType (javax.enterprise.inject.spi.ProcessSyntheticAnnotatedType)2 AbstractClassBean (org.jboss.weld.bean.AbstractClassBean)2 AbstractProducerBean (org.jboss.weld.bean.AbstractProducerBean)2 SessionBean (org.jboss.weld.bean.SessionBean)2 ModuleEnablement (org.jboss.weld.bootstrap.enablement.ModuleEnablement)2