/** * 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; }
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; }
/** * 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; }
/** * 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); } }
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) {
/** * 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; }
@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(); } }
@Override public String getMessage() { return new StringBuilder("Too many choices for desire: ") .append(format(getDesire().getInjectionPoint())) .append('\n') .append(format(context, desires)) .toString(); } }
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(); }
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; }
/** * 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; }
/** * 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); } }
for (Desire d : desires) { LogContext ipContext = LogContext.create(); if (d.getInjectionPoint() instanceof ConstructorParameterInjectionPoint) { ConstructorParameterInjectionPoint cd = (ConstructorParameterInjectionPoint) d.getInjectionPoint(); logger.trace("Injection point satisfactions in progress {}", cd); try { LogContext ipContext = LogContext.create(); try { final InjectionStrategy injectionStrategy = InjectionStrategy.forInjectionPoint(d.getInjectionPoint()); ipContext.put("org.grouplens.grapht.injectionPoint", d.getInjectionPoint().toString()); injectionStrategy.inject(d.getInjectionPoint(), instance, providers.get(d), settersAndArguments); } finally { ipContext.finish();
Type providerType = desire.getInjectionPoint().getType(); if (providerType instanceof ParameterizedType) { Desire providedDesire = Desires.create(desire.getInjectionPoint().getQualifier(), providedType, desire.getInjectionPoint().isNullable());
BindingResult result = null; Annotation qualifier = desire.getInjectionPoint().getQualifier();
InjectionContext newContext = context.extend(result.satisfaction, desire.getInjectionPoint());
.getDesireChain() .getInitialDesire() .getInjectionPoint()); walkGraphForReplacements(edge.getTail(), next, replacements); } else {