@Nullable private Optional<Object> getDiskCachedObject(Path file, DAGNode<Component,Dependency> node) { if (file != null && Files.exists(file)) { logger.debug("reading object for {} from cache (key {})", node.getLabel().getSatisfaction(), key); Object obj = readCompressedObject(file, node.getLabel().getSatisfaction().getErasedType()); logger.debug("read object {} from key {}", obj, key); return Optional.fromNullable(obj); } else { return null; } }
Class<?> type = label.getSatisfaction().getErasedType(); logger.trace("node {} has satisfaction type {}", node, type); if (type.getAnnotation(Shareable.class) != null) {
/** * 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); } }
/** * Check a graph for placeholder satisfactions. * * @param graph The graph to check. * @throws RecommenderConfigurationException if the graph has a placeholder satisfaction. */ public static void checkForPlaceholders(DAGNode<Component, Dependency> graph, Logger logger) throws RecommenderConfigurationException { Set<DAGNode<Component, Dependency>> placeholders = getPlaceholderNodes(graph); Satisfaction sat = null; for (DAGNode<Component,Dependency> node: placeholders) { Component csat = node.getLabel(); // special-case DAOs for non-checking if (DataAccessObject.class.isAssignableFrom(csat.getSatisfaction().getErasedType())) { logger.debug("found DAO placeholder {}", csat.getSatisfaction()); } else { // all other placeholders are bad if (sat == null) { sat = csat.getSatisfaction(); } logger.error("placeholder {} not removed", csat.getSatisfaction()); } } if (sat != null) { throw new RecommenderConfigurationException("placeholder " + sat + " not removed"); } }
/** * Create a provided node from the current node, and queue an edge for it. * * @param pid The ID of the provider node for targeting the provision edge. * @return The provided node and the edge connect it to the provider node. */ private Pair<GVNode, GVEdge> providedNode(String pid) { GVNode pNode = ComponentNodeBuilder.create(nodeId, satisfaction.getErasedType()) .setShareable(GraphtUtils.isShareable(currentNode)) .setShared(!unsharedNodes.contains(currentNode)) .setIsProvided(true) .build(); GVEdge pEdge = EdgeBuilder.create(pNode.getTarget() + ":e", pid) .set("style", "dotted") .set("dir", "back") .set("arrowhead", "vee") .build(); return Pair.of(pNode, pEdge); }
public DAGNode<Component, Dependency> processNode(@Nonnull DAGNode<Component, Dependency> node, @Nonnull DAGNode<Component, Dependency> original) { Component label = node.getLabel(); if (!label.getSatisfaction().hasInstance()) { Satisfaction instanceSat = Satisfactions.nullOfType(label.getSatisfaction().getErasedType()); Component newLbl = Component.create(instanceSat, label.getCachePolicy()); // build new node with replacement label DAGNodeBuilder<Component,Dependency> bld = DAGNode.newBuilder(newLbl); // retain all non-transient edges for (DAGEdge<Component,Dependency> edge: node.getOutgoingEdges()) { if (!GraphtUtils.edgeIsTransient(edge)) { bld.addEdge(edge.getTail(), edge.getLabel()); } } DAGNode<Component,Dependency> repl = bld.build(); logger.debug("simulating instantiation of {}", node); return repl; } else { return node; } } }
public DAGNode<Component, Dependency> processNode(@Nonnull DAGNode<Component, Dependency> node, @Nonnull DAGNode<Component, Dependency> original) { Component label = node.getLabel(); Satisfaction satisfaction = label.getSatisfaction(); if (satisfaction.hasInstance()) { return node; } Object obj = instantiator.apply(node); Satisfaction instanceSat; if (obj == null) { instanceSat = Satisfactions.nullOfType(satisfaction.getErasedType()); } else { instanceSat = Satisfactions.instance(obj); } Component newLabel = Component.create(instanceSat, label.getCachePolicy()); // build new node with replacement label DAGNodeBuilder<Component,Dependency> bld = DAGNode.newBuilder(newLabel); // retain all non-transient edges for (DAGEdge<Component, Dependency> edge: node.getOutgoingEdges()) { if (!GraphtUtils.edgeIsTransient(edge)) { bld.addEdge(edge.getTail(), edge.getLabel()); } } return bld.build(); } }
instanceSat = Satisfactions.nullOfType(satisfaction.getErasedType()); } else { instanceSat = Satisfactions.instance(obj);
@Override public Desire restrict(Satisfaction satis) { return new ReflectionDesire(satis.getErasedType(), injectPoint, satis); }
@Override public MatchElement apply(Pair<Satisfaction, InjectionPoint> n) { // we must check for nulls in case it is a synthetic satisfaction Satisfaction sat = n.getLeft(); boolean typeMatches; if (type == null) { typeMatches = sat == null || sat.getErasedType() == null || sat.getType().equals(Void.TYPE); } else { typeMatches = sat != null && sat.getErasedType() != null && type.isAssignableFrom(sat.getErasedType()); } if (typeMatches && qualifier.matches(n.getRight().getQualifier())) { return new MatchElem(sat == null ? null : sat.getErasedType(), type, qualifier); } else { return null; } }
/** * Construct a singleton injection context with no attributes. * @param satisfaction The satisfaction. * @return The injection context. */ public static InjectionContext singleton(Satisfaction satisfaction) { return singleton(satisfaction, new SimpleInjectionPoint(null, satisfaction.getErasedType(), true)); }
@Nullable private Optional<Object> getDiskCachedObject(Path file, DAGNode<Component,Dependency> node) { if (file != null && Files.exists(file)) { logger.debug("reading object for {} from cache (key {})", node.getLabel().getSatisfaction(), key); Object obj = readCompressedObject(file, node.getLabel().getSatisfaction().getErasedType()); logger.debug("read object {} from key {}", obj, key); return Optional.fromNullable(obj); } else { return 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(); }
@Override public void toSatisfaction(@Nonnull Satisfaction sat) { Preconditions.notNull("satisfaction", sat); BindRuleBuilder brb = startRule().setSatisfaction(sat); generateBindings(brb, sat.getErasedType()); }
/** * Check a graph for placeholder satisfactions. * * @param graph The graph to check. * @throws RecommenderConfigurationException if the graph has a placeholder satisfaction. */ public static void checkForPlaceholders(DAGNode<Component, Dependency> graph, Logger logger) throws RecommenderConfigurationException { Set<DAGNode<Component, Dependency>> placeholders = getPlaceholderNodes(graph); Satisfaction sat = null; for (DAGNode<Component,Dependency> node: placeholders) { Component csat = node.getLabel(); // special-case DAOs for non-checking if (DataAccessObject.class.isAssignableFrom(csat.getSatisfaction().getErasedType())) { logger.debug("found DAO placeholder {}"); } else { // all other placeholders are bad if (sat == null) { sat = csat.getSatisfaction(); } logger.error("placeholder {} not removed", csat.getSatisfaction()); } } if (sat != null) { throw new RecommenderConfigurationException("placeholder " + sat + " not removed"); } }
/** * 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); } }
if (!graph.getLabel().getSatisfaction().getErasedType().equals(Void.TYPE)) { throw new IllegalArgumentException("only full dependency graphs can be rewritten");
/** * Create a provided node from the current node, and queue an edge for it. * * @param pid The ID of the provider node for targeting the provision edge. * @return The provided node and the edge connect it to the provider node. */ private Pair<GVNode, GVEdge> providedNode(String pid) { GVNode pNode = ComponentNodeBuilder.create(nodeId, satisfaction.getErasedType()) .setShareable(GraphtUtils.isShareable(currentNode)) .setShared(!unsharedNodes.contains(currentNode)) .setIsProvided(true) .build(); GVEdge pEdge = EdgeBuilder.create(pNode.getTarget() + ":e", pid) .set("style", "dotted") .set("dir", "back") .set("arrowhead", "vee") .build(); return Pair.of(pNode, pEdge); }
public DAGNode<Component, Dependency> processNode(@Nonnull DAGNode<Component, Dependency> node, @Nonnull DAGNode<Component, Dependency> original) { Component label = node.getLabel(); if (!label.getSatisfaction().hasInstance()) { Satisfaction instanceSat = Satisfactions.nullOfType(label.getSatisfaction().getErasedType()); Component newLbl = Component.create(instanceSat, label.getCachePolicy()); // build new node with replacement label DAGNodeBuilder<Component,Dependency> bld = DAGNode.newBuilder(newLbl); // retain all non-transient edges for (DAGEdge<Component,Dependency> edge: node.getOutgoingEdges()) { if (!GraphtUtils.edgeIsTransient(edge)) { bld.addEdge(edge.getTail(), edge.getLabel()); } } DAGNode<Component,Dependency> repl = bld.build(); logger.debug("simulating instantiation of {}", node); return repl; } else { return node; } } }
public DAGNode<Component, Dependency> processNode(@Nonnull DAGNode<Component, Dependency> node, @Nonnull DAGNode<Component, Dependency> original) { Component label = node.getLabel(); Satisfaction satisfaction = label.getSatisfaction(); if (satisfaction.hasInstance()) { return node; } Object obj = instantiator.apply(node); Satisfaction instanceSat; if (obj == null) { instanceSat = Satisfactions.nullOfType(satisfaction.getErasedType()); } else { instanceSat = Satisfactions.instance(obj); } Component newLabel = Component.create(instanceSat, label.getCachePolicy()); // build new node with replacement label DAGNodeBuilder<Component,Dependency> bld = DAGNode.newBuilder(newLabel); // retain all non-transient edges for (DAGEdge<Component, Dependency> edge: node.getOutgoingEdges()) { if (!GraphtUtils.edgeIsTransient(edge)) { bld.addEdge(edge.getTail(), edge.getLabel()); } } return bld.build(); } }