use of org.b3log.latke.ioc.point.InjectionPoint in project latke by b3log.
the class InjectionPointValidator method checkValidity.
/**
* Checks the validity of the specified bean.
*
* @param <T> the class of the bean instance
* @param bean the specified bean
*/
public static <T> void checkValidity(final Bean<T> bean) {
final Set<InjectionPoint> injectionPoints = bean.getInjectionPoints();
final Set<Constructor> constructors = new HashSet<Constructor>();
for (final InjectionPoint injectionPoint : injectionPoints) {
final Member member = injectionPoint.getMember();
if (member instanceof Constructor) {
constructors.add((Constructor) member);
}
}
if (constructors.size() > 1) {
throw new RuntimeException("Only one constructor can be injected!");
}
// TODO: TypeVarible check.
}
use of org.b3log.latke.ioc.point.InjectionPoint 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 + "]");
}
}
}
}
Aggregations