Search in sources :

Example 1 with AmbiguousResolutionException

use of javax.enterprise.inject.AmbiguousResolutionException 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 2 with AmbiguousResolutionException

use of javax.enterprise.inject.AmbiguousResolutionException 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)

Example 3 with AmbiguousResolutionException

use of javax.enterprise.inject.AmbiguousResolutionException in project core by weld.

the class Components method getDependents.

/**
 * @param bean
 * @param beans
 * @param probe
 * @param firstMatch
 * @return the set of dependents
 */
static Set<Dependency> getDependents(Bean<?> bean, Collection<Bean<?>> beans, Probe probe, boolean firstMatch) {
    Set<Dependency> dependents = new HashSet<Dependency>();
    for (Bean<?> candidate : beans) {
        if (candidate.equals(bean)) {
            continue;
        }
        BeanManager beanManager = probe.getBeanManager(candidate);
        if (beanManager == null) {
            // Don't process built-in beans
            continue;
        }
        Set<InjectionPoint> injectionPoints = candidate.getInjectionPoints();
        if (injectionPoints != null && !injectionPoints.isEmpty()) {
            for (InjectionPoint injectionPoint : injectionPoints) {
                if (injectionPoint.isDelegate()) {
                    // Do not include delegate injection points
                    continue;
                }
                Set<Bean<?>> foundBeans = beanManager.getBeans(injectionPoint.getType(), injectionPoint.getQualifiers().toArray(new Annotation[injectionPoint.getQualifiers().size()]));
                if (foundBeans.isEmpty()) {
                    continue;
                }
                Bean<?> candidateDependency;
                try {
                    candidateDependency = beanManager.resolve(foundBeans);
                } catch (AmbiguousResolutionException e) {
                    continue;
                }
                if (candidateDependency.getBeanClass().equals(InstanceImpl.class)) {
                    Bean<?> lazilyFetched = getInstanceResolvedBean(beanManager, injectionPoint);
                    if (lazilyFetched != null && lazilyFetched.equals(bean)) {
                        dependents.add(Dependency.createPotential(candidate, injectionPoint, INFO_FETCHING_LAZILY));
                        if (firstMatch) {
                            break;
                        } else {
                            continue;
                        }
                    }
                }
                boolean satisfies = false;
                if (isBuiltinBeanButNotExtension(candidateDependency)) {
                    satisfies = bean.equals(probe.getBean(Components.getBuiltinBeanId((AbstractBuiltInBean<?>) candidateDependency)));
                } else {
                    satisfies = bean.equals(candidateDependency);
                }
                if (satisfies) {
                    dependents.add(new Dependency(candidate, injectionPoint));
                    if (firstMatch) {
                        break;
                    } else {
                        continue;
                    }
                }
            }
        }
    }
    return dependents;
}
Also used : AbstractBuiltInBean(org.jboss.weld.bean.builtin.AbstractBuiltInBean) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) Annotation(java.lang.annotation.Annotation) 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) AmbiguousResolutionException(javax.enterprise.inject.AmbiguousResolutionException) BeanManager(javax.enterprise.inject.spi.BeanManager) HashSet(java.util.HashSet)

Aggregations

Annotation (java.lang.annotation.Annotation)3 AmbiguousResolutionException (javax.enterprise.inject.AmbiguousResolutionException)3 Bean (javax.enterprise.inject.spi.Bean)3 InjectionPoint (javax.enterprise.inject.spi.InjectionPoint)3 HashSet (java.util.HashSet)2 ForwardingBean (org.jboss.weld.bean.ForwardingBean)2 ManagedBean (org.jboss.weld.bean.ManagedBean)2 SessionBean (org.jboss.weld.bean.SessionBean)2 AbstractBuiltInBean (org.jboss.weld.bean.builtin.AbstractBuiltInBean)2 ExtensionBean (org.jboss.weld.bean.builtin.ExtensionBean)2 BeanManager (javax.enterprise.inject.spi.BeanManager)1 BeanManagerImpl (org.jboss.weld.manager.BeanManagerImpl)1