Search in sources :

Example 1 with UnsatisfiedResolutionException

use of org.b3log.latke.ioc.inject.UnsatisfiedResolutionException in project latke by b3log.

the class InjectionPointValidator method checkDependency.

/**
 * Checks dependency resolution of the specified bean.
 *
 * @param <T> the class of the bean instance
 * @param bean the specified bean
 * @param configurator the specified configurator
 */
public static <T> void checkDependency(final Bean<T> bean, final Configurator configurator) {
    for (final InjectionPoint injectionPoint : bean.getInjectionPoints()) {
        final Type requiredType = injectionPoint.getType();
        final Set<Annotation> requiredQualifiers = injectionPoint.getQualifiers();
        Set<Annotation> bindedQualifiers;
        if (Reflections.isConcrete(requiredType)) {
            bindedQualifiers = configurator.getBindedQualifiers((Class<?>) requiredType);
            if (bindedQualifiers.containsAll(requiredQualifiers)) {
                continue;
            }
        }
        final Set<Class<?>> bindedBeanClasses = configurator.getBindedBeanClasses(requiredType);
        if (bindedBeanClasses == null) {
            throw new UnsatisfiedResolutionException("Has no eligible bean[type=" + requiredType.toString() + "] for injection point[" + injectionPoint + "]");
        } else if (bindedBeanClasses.size() == 1) {
            final Class<?> eligibleClass = bindedBeanClasses.iterator().next();
            bindedQualifiers = configurator.getBindedQualifiers(eligibleClass);
            if (!bindedQualifiers.containsAll(requiredQualifiers)) {
                throw new UnsatisfiedResolutionException("Has no eligible bean[type=" + requiredType.toString() + ", qualifiers=]" + requiredQualifiers + "] for injection point[" + injectionPoint + "]");
            }
        } else if (bindedBeanClasses.size() > 1) {
            final Set<Class<?>> eligibleClasses = new HashSet<Class<?>>();
            for (final Class<?> beanClass : bindedBeanClasses) {
                bindedQualifiers = configurator.getBindedQualifiers(beanClass);
                if (bindedQualifiers.containsAll(requiredQualifiers)) {
                    eligibleClasses.add(beanClass);
                }
            }
            if (eligibleClasses.isEmpty()) {
                throw new UnsatisfiedResolutionException("Has no eligible bean[type=" + requiredType.toString() + ", qualifiers=" + requiredQualifiers + "] for injection point[" + injectionPoint + "]");
            } else if (eligibleClasses.size() > 1) {
                throw new AmbiguousResolutionException("Has more than one eligible bean[type=" + requiredType.toString() + ", qualifiers=" + requiredQualifiers + "] for injection point[" + injectionPoint + "]");
            }
        }
    }
}
Also used : Type(java.lang.reflect.Type) InjectionPoint(org.b3log.latke.ioc.point.InjectionPoint) UnsatisfiedResolutionException(org.b3log.latke.ioc.inject.UnsatisfiedResolutionException) AmbiguousResolutionException(org.b3log.latke.ioc.inject.AmbiguousResolutionException) Annotation(java.lang.annotation.Annotation) HashSet(java.util.HashSet)

Aggregations

Annotation (java.lang.annotation.Annotation)1 Type (java.lang.reflect.Type)1 HashSet (java.util.HashSet)1 AmbiguousResolutionException (org.b3log.latke.ioc.inject.AmbiguousResolutionException)1 UnsatisfiedResolutionException (org.b3log.latke.ioc.inject.UnsatisfiedResolutionException)1 InjectionPoint (org.b3log.latke.ioc.point.InjectionPoint)1