private static boolean satIsNull(Satisfaction sat) { return sat.visit(new AbstractSatisfactionVisitor<Boolean>(false) { @Override public Boolean visitNull() { return true; } }); } }
@Override public <T> T visit(SatisfactionVisitor<T> visitor) { return visitor.visitNull(); }
/** * 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; }
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(); } }
/** * 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); } }
/** * 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; }
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; } } }
Component label = node.getLabel(); if (label.getSatisfaction().hasInstance()) { logger.trace("node {} shareable because it has an instance", node); return true; Class<?> type = label.getSatisfaction().getErasedType(); logger.trace("node {} has satisfaction type {}", node, type); if (type.getAnnotation(Shareable.class) != null) { return label.getSatisfaction().visit(new AbstractSatisfactionVisitor<Boolean>() { @Override public Boolean visitDefault() {
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; }
@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; } }
@Nullable public <T> T tryGetInstance(Class<T> type) throws InjectionException { Desire d = Desires.create(null, type, true); DAGEdge<Component, Dependency> e = graph.getOutgoingEdgeWithLabel(l -> l.hasInitialDesire(d)); if (e != null) { return type.cast(instantiator.instantiate(e.getTail())); } else { DAGNode<Component, Dependency> node = GraphtUtils.findSatisfyingNode(graph, Qualifiers.matchDefault(), type); if (node != null) { return type.cast(instantiator.instantiate(node)); } else { return null; } } }
/** * Get the component of a particular type, if one is already instantiated. This is useful to extract pre-built * models from serialized recommender engines, for example. * @param type The required component type. * @param <T> The required component type. * @return The component instance, or {@code null} if no instance can be retreived (either because no such * component is configured, or it is not yet instantiated). */ @Nullable public <T> T getComponent(Class<T> type) { DAGNode<Component, Dependency> node = GraphtUtils.findSatisfyingNode(graph, Qualifiers.matchDefault(), type); if (node == null) { return null; } Satisfaction sat = node.getLabel().getSatisfaction(); if (sat instanceof InstanceSatisfaction) { return type.cast(((InstanceSatisfaction) sat).getInstance()); } else { return null; } }
public <T> T tryGetInstance(Class<? extends Annotation> qual, Class<T> type) throws InjectionException { return tryGetInstance(Qualifiers.match(qual), type); }
@Override public <T> T getInstance(Class<T> type) throws InjectionException { T obj = tryGetInstance(Qualifiers.matchDefault(), type); if (obj == null) { throw new ResolutionException("no resolution available for " + type); } else { return obj; } }
public DAGNode<Component,Dependency> buildGraph() throws ResolutionException { DependencySolver solver = buildDependencySolver(); for (Class<?> root: roots) { solver.resolve(Desires.create(null, root, true)); } return solver.getGraph(); }
/** * 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"); } }
@Override public void describe(DAGNode<Component, Dependency> node, DescriptionWriter description) { node.getLabel().getSatisfaction().visit(new LabelDescriptionVisitor(description)); description.putField("cachePolicy", node.getLabel().getCachePolicy().name()); List<DAGNode<Component, Dependency>> edges = node.getOutgoingEdges() .stream() .sorted(GraphtUtils.DEP_EDGE_ORDER) .map(DAGEdge::getTail) .collect(Collectors.toList()); description.putList("dependencies", edges, INSTANCE); } }
@Nullable @Override public <T> T tryGetInstance(Annotation qualifier, Class<T> type) throws InjectionException { return tryGetInstance(Qualifiers.match(qualifier), type); }
/** * 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); }
@Override public <T> T getInstance(Annotation qualifier, Class<T> type) throws InjectionException { T obj = tryGetInstance(Qualifiers.match(qualifier), type); if (obj == null) { throw new ResolutionException("no resolution available for " + type + " with qualifier " + qualifier); } else { return obj; } }