if (InjectableType.Disabled.equals(depOwner.getInjectableType())) { priorities = Collections.singleton(ResolutionPriority.Disabled); reportProblems = false;
private static void validateDependentScopedInjectable(final Injectable injectable, final Set<Injectable> visiting, final Set<Injectable> visited, final Collection<String> problems, final boolean onlyConstuctorDeps) { if (InjectableType.Disabled.equals(injectable.getInjectableType())) { visited.add(injectable); return; } if (visiting.contains(injectable)) { problems.add(createCycleMessage(visiting, injectable)); return; } visiting.add(injectable); for (final Dependency dep : injectable.getDependencies()) { if (onlyConstuctorDeps && !dep.getDependencyType().equals(DependencyType.Constructor)) { continue; } final Injectable resolved = GraphUtil.getResolvedDependency(dep, injectable); if (!visited.contains(resolved)) { if (dep.getDependencyType().equals(DependencyType.ProducerMember)) { validateDependentScopedInjectable(resolved, visiting, visited, problems, true); } else if (resolved.getWiringElementTypes().contains(WiringElementType.PseudoScopedBean)) { validateDependentScopedInjectable(resolved, visiting, visited, problems, false); } } } visiting.remove(injectable); visited.add(injectable); }
private static String createCycleMessage(final Set<Injectable> visiting, final Injectable injectable) { final StringBuilder builder = new StringBuilder(); boolean cycleStarted = false; boolean hasProducer = false; for (final Injectable visitingInjectable : visiting) { if (visitingInjectable.equals(injectable)) { cycleStarted = true; } if (cycleStarted) { builder.append("\t"); visitingInjectable.getQualifier().stream() .forEach(anno -> builder.append(anno.toString()).append(' ')); builder.append(visitingInjectable.getInjectedType().getFullyQualifiedName()) .append("\n"); if (visitingInjectable.getInjectableType().equals(InjectableType.Producer)) { hasProducer = true; } } } if (hasProducer) { builder.insert(0, "A cycle was found containing a producer and no other normal scoped types:\n"); } else { builder.insert(0, "A cycle of only pseudo-scoped beans was found:\n"); } return builder.toString(); } }
private void removeLinksToProducedTypes(final InjectableImpl specialized, final Set<InjectableImpl> toBeRemoved) { final Collection<InjectableReference> producedReferences = new ArrayList<>(); for (final MetaMethod method : specialized.type.getDeclaredMethodsAnnotatedWith(Produces.class)) { producedReferences.add(lookupInjectableReference(method.getReturnType(), qualFactory.forSource(method))); } for (final MetaField field : specialized.type.getDeclaredFields()) { if (field.isAnnotationPresent(Produces.class)) { producedReferences.add(lookupInjectableReference(field.getType(), qualFactory.forSource(field))); } } for (final InjectableReference reference : producedReferences) { final Iterator<InjectableBase> linkIter = reference.linked.iterator(); while (linkIter.hasNext()) { final InjectableBase link = linkIter.next(); if (link instanceof InjectableImpl && ((InjectableImpl) link).injectableType.equals(InjectableType.Producer)) { final InjectableImpl concreteLink = (InjectableImpl) link; final ProducerInstanceDependencyImpl producerMemberDep = GraphUtil.findProducerInstanceDep(concreteLink); if (producerMemberDep.producingMember.getDeclaringClass().equals(specialized.type)) { linkIter.remove(); toBeRemoved.add(concreteLink); } } } } }
private void removeUnreachableInjectables(final ReachabilityStrategy strategy) { logger.debug("Removing unreachable injectables from dependency graph using {} strategy.", strategy); final Set<String> reachableNames = new HashSet<>(); final Queue<Injectable> processingQueue = new LinkedList<>(); final Predicate<Injectable> reachabilityRoot = reachabilityRootPredicate(strategy); for (final Injectable injectable : injectablesByName.values()) { if (reachabilityRoot.test(injectable) && !reachableNames.contains(injectable.getFactoryName()) && !InjectableType.Disabled.equals(injectable.getInjectableType())) { processingQueue.add(injectable); do { final Injectable processedInjectable = processingQueue.poll(); reachableNames.add(processedInjectable.getFactoryName()); logger.trace("Marked as reachable: {}", processedInjectable); for (final Dependency dep : processedInjectable.getDependencies()) { final Injectable resolvedDep = GraphUtil.getResolvedDependency(dep, processedInjectable); if (!reachableNames.contains(resolvedDep.getFactoryName())) { processingQueue.add(resolvedDep); } } } while (processingQueue.size() > 0); } } final int initialSize = injectablesByName.size(); injectablesByName.keySet().retainAll(reachableNames); logger.debug("Removed {} unreachable injectables.", initialSize - injectablesByName.size()); }
private void removeSpecializedAndSpecializingLinks(final InjectableImpl specialization, final Set<InjectableImpl> toBeRemoved, final MetaClass specializingType, final MetaClass enclosingType, final Iterator<InjectableBase> linkedIter, final InjectableImpl linkedInjectable) { if (linkedInjectable.injectableType.equals(InjectableType.Producer)) { final MetaClass foundProducerType = GraphUtil.findProducerInstanceDep(linkedInjectable).injectable.type.getErased(); if (foundProducerType.equals(enclosingType.getErased()) || foundProducerType.equals(specializingType.getErased())) { linkedIter.remove(); } if (foundProducerType.equals(enclosingType.getErased())) { toBeRemoved.add(linkedInjectable); specialization.qualifier = qualFactory.combine(specialization.qualifier, linkedInjectable.qualifier); } } }
private void resolveSpecializations() { logger.debug("Processing {} specializations...", specializations.size()); final Set<InjectableImpl> toBeRemoved = new HashSet<>(); GraphUtil.sortSuperTypesBeforeSubtypes(specializations); for (final InjectableImpl specialization : specializations) { if (specialization.injectableType.equals(InjectableType.Producer)) { resolveProducerSpecialization(specialization, toBeRemoved); } else { resolveTypeSpecialization(specialization, toBeRemoved); } } logger.debug("Removed {} beans that were specialized.", toBeRemoved.size()); logger.trace("Types removed by specialization: {}", toBeRemoved); injectablesByName.values().removeAll(toBeRemoved); }
@Override public boolean isContextual() { return InjectableType.ContextualProvider.equals(injectableType); }
@Override public boolean matches(final Injectable injectable) { return InjectableType.JsType.equals(injectable.getInjectableType()); } },
@Override public boolean matches(final Injectable injectable) { return injectable.getWiringElementTypes().contains(WiringElementType.AlternativeBean) && !InjectableType.Disabled.equals(injectable.getInjectableType()); } },
@Override public boolean matches(final Injectable injectable) { return InjectableType.Disabled.equals(injectable.getInjectableType()); } };
@Override public boolean matches(final Injectable injectable) { return injectable.getInjectableType().equals(InjectableType.JsType) && injectable.getWiringElementTypes().contains(WiringElementType.SharedSingleton); } },
private void addDependency(final Injectable injectable, final Dependency dependency) { assert (injectable instanceof InjectableImpl); if (InjectableType.Disabled.equals(injectable.getInjectableType()) && (!DependencyType.ProducerMember.equals(dependency.getDependencyType()) || !injectable.getDependencies().isEmpty())) { throw new RuntimeException("The injectable, " + injectable + ", is disabled." + " A disabled injectable may only have a single dependency if it is produced by a disabled bean."); } final InjectableImpl injectableAsImpl = (InjectableImpl) injectable; injectableAsImpl.dependencies.add(BaseDependency.class.cast(dependency)); }
private int getScore(final InjectableImpl c) { if (c.injectableType.equals(InjectableType.Producer)) { return getDistanceFromObject(findProducerInstanceDep(c).producingMember.getDeclaringClass()); } else { return getDistanceFromObject(c.type); } }