/** * Determine whether a desire is transient. * * @param d The desire to test. * @return {@code true} if the desire is transient. */ public static boolean desireIsTransient(@Nonnull Desire d) { InjectionPoint ip = d.getInjectionPoint(); return ip.getAttribute(Transient.class) != null; }
/** * Add a dependency of this component. * @param dep The dependency. * @return The builder (for chaining). */ public ComponentNodeBuilder addDependency(Desire dep) { Annotation q = dep.getInjectionPoint().getQualifier(); Class<?> type = dep.getDesiredType(); if (q == null) { dependencies.add(shortClassName(type)); } else { dependencies.add(shortAnnotation(q) + ": " + shortClassName(type)); } return this; }
@Override public BindingResult bind(InjectionContext context, DesireChain desires) throws ResolutionException { Desire desire = desires.getCurrentDesire(); if (Provider.class.equals(desire.getDesiredType())) { Type providerType = desire.getInjectionPoint().getType(); if (providerType instanceof ParameterizedType) { Desire providedDesire = Desires.create(desire.getInjectionPoint().getQualifier(), providedType, desire.getInjectionPoint().isNullable()); Desire jitDesire = desire.restrict(new ProviderInjectionSatisfaction(providedDesire));
if (provider != null) { return BindingResult.newBuilder() .setDesire(desire.restrict(Satisfactions.providerType(provider.value()))) .setCachePolicy(provider.cachePolicy()) .addFlag(BindingFlag.TERMINAL) if (Types.isInstantiable(impl.value())) { return BindingResult.newBuilder() .setDesire(desire.restrict(Satisfactions.type(impl.value()))) .setCachePolicy(impl.cachePolicy()) .build(); } else { return BindingResult.newBuilder() .setDesire(desire.restrict(impl.value())) .setCachePolicy(impl.cachePolicy()) .build(); if (dnull != null) { return BindingResult.newBuilder() .setDesire(desire.restrict(Satisfactions.nullOfType(desire.getDesiredType()))) .setCachePolicy(CachePolicy.NO_PREFERENCE) .addFlag(BindingFlag.TERMINAL)
@Override public Type getType() { return Types.parameterizedType(Provider.class, providedDesire.getDesiredType()); }
if (terminate && chain.getCurrentDesire().isInstantiable()) { logger.info("Satisfied {} with {}", desire, chain.getCurrentDesire().getSatisfaction()); policy = chain.getCurrentDesire().getSatisfaction().getDefaultCachePolicy(); if (policy.equals(CachePolicy.NO_PREFERENCE)) { policy = defaultPolicy; return new Resolution(chain.getCurrentDesire().getSatisfaction(), policy, chain, fixed, defer); } else if (binding == null) {
private static List<String> extractOrderKey(DAGEdge<Component,Dependency> node) { Desire desire = node.getLabel().getInitialDesire(); InjectionPoint ip = desire.getInjectionPoint(); List<String> key = new ArrayList<>(4); Member member = ip.getMember(); if (member instanceof Constructor) { key.add("0: constructor"); key.add(Integer.toString(ip.getParameterIndex())); } else if (member instanceof Method) { key.add("1: setter"); key.add(member.getName()); key.add(Integer.toString(ip.getParameterIndex())); } else if (member instanceof Field) { key.add("2: field"); key.add(member.getName()); } else if (ip instanceof SimpleInjectionPoint) { key.add("5: simple"); } else { key.add("9: unknown"); key.add(ip.getClass().getName()); } return key; }
@Override public boolean matches(Desire desire) { // bind rules match type by equality if (desire.getDesiredType().equals(depType)) { // if the type is equal, then rely on the qualifier matcher return qualifier.matches(desire.getInjectionPoint().getQualifier()); } // the type and {@link Qualifier}s are not a match, so return false return false; }
throw new ResolutionException(providerName + " does not provide " + type); builder.setDesire(desire.restrict(sat)) .addFlag(BindingFlag.TERMINAL); found = true; throw new ResolutionException(providerName + " not compatible with " + type); builder.setDesire(desire.restrict(sat)); found = true; } catch (ClassNotFoundException e) {
/** * Find a node with a satisfaction for a specified type. Does a breadth-first * search to find the closest matching one. * * @param type The type to look for. * @return A node whose satisfaction is compatible with {@code type}. */ @Nullable public static DAGNode<Component,Dependency> findSatisfyingNode(DAGNode<Component,Dependency> graph, final QualifierMatcher qmatch, final Class<?> type) { Optional<DAGEdge<Component, Dependency>> edge = graph.breadthFirstEdges() .filter(e -> type.isAssignableFrom(e.getTail() .getLabel() .getSatisfaction() .getErasedType())) .filter(e -> qmatch.apply(e.getLabel() .getInitialDesire() .getInjectionPoint() .getQualifier())) .findFirst(); return edge.map(DAGEdge::getTail) .orElse(null); } }
protected String format(InjectionContext ctx, DesireChain desires) { StringBuilder sb = new StringBuilder(); // type path sb.append("Context:\n"); sb.append(" Type path:\n"); for (Pair<Satisfaction, InjectionPoint> path: ctx) { Satisfaction sat = path.getLeft(); Class<?> type = sat == null ? null : sat.getErasedType(); sb.append(" ") .append(format(path.getRight(), type)) .append('\n'); } sb.append('\n'); // desire chain sb.append(" Prior desires:\n"); for (Desire desire: desires.getPreviousDesires()) { sb.append(" ") .append(format(desire.getInjectionPoint(), desire.getDesiredType())) .append('\n'); } return sb.toString(); }
if (dfltDouble != null) { bld = BindingResult.newBuilder() .setDesire(desire.restrict(Satisfactions.instance(dfltDouble.value()))); bld = BindingResult.newBuilder().setDesire(desire.restrict(Satisfactions.instance(dfltInt.value()))); bld = BindingResult.newBuilder().setDesire(desire.restrict(Satisfactions.instance(dfltBool.value()))); bld = BindingResult.newBuilder().setDesire(desire.restrict(Satisfactions.instance(dfltStr.value())));
for (DAGEdge<Component, Dependency> e: edges) { Desire dep = e.getLabel().getInitialDesire(); Annotation q = dep.getInjectionPoint().getQualifier(); DAGNode<Component, Dependency> targetNode = e.getTail(); if (q != null && q.annotationType().getAnnotation(Parameter.class) != null) {
/** * Add a dependency of this component. * @param dep The dependency. * @return The builder (for chaining). */ public ComponentNodeBuilder addDependency(Desire dep) { Annotation q = dep.getInjectionPoint().getQualifier(); Class<?> type = dep.getDesiredType(); if (q == null) { dependencies.add(shortClassName(type)); } else { dependencies.add(shortAnnotation(q) + ": " + shortClassName(type)); } return this; }
/** * Determine whether a desire is transient. * * @param d The desire to test. * @return {@code true} if the desire is transient. */ public static boolean desireIsTransient(@Nonnull Desire d) { InjectionPoint ip = d.getInjectionPoint(); return ip.getAttribute(Transient.class) != null; }
BindingResult result = null; Annotation qualifier = desire.getInjectionPoint().getQualifier(); result = getAnnotatedDefault(desire, desire.getDesiredType()); result = getMetaInfDefault(desire, desire.getDesiredType());
@SuppressWarnings("unchecked") private Constructor<?> getConstructor() { for (Desire d: desires) { if (d.getInjectionPoint() instanceof ConstructorParameterInjectionPoint) { // since we only allow one injectable constructor, any ConstructorParameterInjectionPoint // will have the same constructor as all other constructor parameter injection points Constructor<?> ctor = ((ConstructorParameterInjectionPoint) d.getInjectionPoint()).getMember(); logger.debug("Using constructor annotated with @Inject: {}", ctor); return ctor; } } try { logger.debug("Using default constructor for {}", type); return type.getDeclaredConstructor(); } catch (NoSuchMethodException e) { // this constructor is being invoked for a ClassSatisfaction or a // ProviderClassSatisfaction, both of which assert that the type is // instantiable, so this should never happen throw new RuntimeException("Unexpected exception", e); } }
@Override public String getMessage() { return new StringBuilder("Unable to satisfy desire: ") .append(format(desire.getInjectionPoint())) .append('\n') .append("Reason: ") .append(super.getMessage()) .toString(); } }
@Override public String getMessage() { StringBuilder sb = new StringBuilder("Unable to satisfy desire ") .append(format(desires.getCurrentDesire().getInjectionPoint())); List<Pair<Satisfaction, InjectionPoint>> path = context; if (!path.isEmpty()) { sb.append(" of ") .append(path.get(0).getLeft()); } sb.append('\n') .append(format(context, desires)); return sb.toString(); } }