Search in sources :

Example 46 with Dependency

use of com.google.gwt.inject.rebind.binding.Dependency in project google-gin by gwtplus.

the class PathFinder method findShortestPath.

/**
 * Find the shortest path from an unresolved edge in the roots to a key in the destinations.
 *
 * <p>Implemented as a Breadth-first search from the destination set back to the origin.
 *
 * @return the shortest path from the roots to any of the destinations specified that passes
 *     through edges meeting the criteria; can be empty if destination is already in the root
 *     set, or null if no path exists
 */
public List<Dependency> findShortestPath() {
    Preconditions.checkNotNull(graph, "Must call onGraph(DependencyGraph) before findShortestPath");
    Preconditions.checkState(!roots.isEmpty(), "Must call addRoots(Key<?>...) before findShortestPath");
    Preconditions.checkState(!destinations.isEmpty(), "Must call addDestinations(Key<?>...) before findShortestPath");
    visited = new LinkedHashMap<Key<?>, Dependency>();
    workQueue = new LinkedList<Key<?>>();
    // set, we can return early.
    for (Key<?> key : destinations) {
        visited.put(key, null);
        if (roots.contains(key)) {
            return getPathFor(key);
        }
        workQueue.add(key);
    }
    // Perform a BFS looking for a path back to a root edge
    while (!workQueue.isEmpty()) {
        Key<?> key = workQueue.remove();
        for (Dependency edge : graph.getDependenciesTargeting(key)) {
            if (isEdgeUsable(edge)) {
                Key<?> sourceKey = edge.getSource();
                if (!visited.containsKey(sourceKey)) {
                    workQueue.add(sourceKey);
                    visited.put(sourceKey, edge);
                    // Check for early termination
                    if (roots.contains(sourceKey)) {
                        return getPathFor(sourceKey);
                    }
                }
            }
        }
    }
    // and requiredOnly is true.
    return null;
}
Also used : Dependency(com.google.gwt.inject.rebind.binding.Dependency) Key(com.google.inject.Key)

Example 47 with Dependency

use of com.google.gwt.inject.rebind.binding.Dependency in project google-gin by gwtplus.

the class UnresolvedBindingValidator method getRequiredSourcesTargeting.

/**
 * Returns all of the source keys that have a required dependency on any key in the target set.
 */
private Collection<Key<?>> getRequiredSourcesTargeting(DependencyGraph graph, Iterable<Key<?>> targets) {
    Collection<Key<?>> requiredSources = new LinkedHashSet<Key<?>>();
    for (Key<?> target : targets) {
        for (Dependency edge : graph.getDependenciesTargeting(target)) {
            if (!edge.isOptional()) {
                PrettyPrinter.log(logger, TreeLogger.DEBUG, "Removing the key %s because of %s", edge.getSource(), edge);
                requiredSources.add(edge.getSource());
            }
        }
    }
    return requiredSources;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Dependency(com.google.gwt.inject.rebind.binding.Dependency) Key(com.google.inject.Key)

Example 48 with Dependency

use of com.google.gwt.inject.rebind.binding.Dependency in project google-gin by gwtplus.

the class ReachabilityAnalyzer method traceGinjectorMethods.

/**
 * Traces out bindings that are reachable from a GInjector method.
 */
private void traceGinjectorMethods() {
    TypeLiteral<?> ginjectorInterface = rootBindings.getGinjectorInterface();
    for (MethodLiteral<?, Method> method : memberCollector.getMethods(ginjectorInterface)) {
        if (!guiceUtil.isMemberInject(method)) {
            // It's a constructor method, so just trace to the key that's
            // constructed.
            Key<?> key = guiceUtil.getKey(method);
            PrettyPrinter.log(logger, TreeLogger.DEBUG, "ROOT -> %s:%s [%s]", rootBindings, key, method);
            traceKey(key, rootBindings);
        } else {
            Key<?> sourceKey = guiceUtil.getKey(method);
            getReachableMemberInjects(rootBindings).add(sourceKey.getTypeLiteral());
            for (Dependency dependency : guiceUtil.getMemberInjectionDependencies(sourceKey, sourceKey.getTypeLiteral())) {
                Key<?> targetKey = dependency.getTarget();
                PrettyPrinter.log(logger, TreeLogger.DEBUG, "ROOT -> %s:%s [%s]", rootBindings, targetKey, method);
                traceKey(targetKey, rootBindings);
            }
        }
    }
}
Also used : Method(java.lang.reflect.Method) Dependency(com.google.gwt.inject.rebind.binding.Dependency)

Example 49 with Dependency

use of com.google.gwt.inject.rebind.binding.Dependency in project google-gin by gwtplus.

the class GinjectorBindings method addUnresolvedEntriesForInjectorInterface.

void addUnresolvedEntriesForInjectorInterface() {
    assertNotFinalized();
    for (MethodLiteral<?, Method> method : completeCollector.getMethods(ginjectorInterface)) {
        nameGenerator.markAsUsed(method.getName());
        Key<?> key = guiceUtil.getKey(method);
        logger.log(TreeLogger.TRACE, "Add unresolved key from injector interface: " + key);
        // need to provide all dependencies.
        if (guiceUtil.isMemberInject(method)) {
            memberInjectRequests.add(key.getTypeLiteral());
            addDependencies(guiceUtil.getMemberInjectionDependencies(Dependency.GINJECTOR, key.getTypeLiteral()));
        } else {
            addDependency(new Dependency(Dependency.GINJECTOR, key, method.toString()));
        }
    }
}
Also used : Method(java.lang.reflect.Method) Dependency(com.google.gwt.inject.rebind.binding.Dependency)

Example 50 with Dependency

use of com.google.gwt.inject.rebind.binding.Dependency in project google-gin by gwtplus.

the class GinjectorBindings method addStaticInjectionRequest.

void addStaticInjectionRequest(Class<?> type, Object source) {
    assertNotFinalized();
    staticInjectionRequests.add(type);
    // Calculate required bindings and add to dependencies
    for (InjectionPoint injectionPoint : InjectionPoint.forStaticMethodsAndFields(type)) {
        Member member = injectionPoint.getMember();
        if (member instanceof Method) {
            addDependencies(guiceUtil.getDependencies(Dependency.GINJECTOR, MethodLiteral.get((Method) member, TypeLiteral.get(member.getDeclaringClass()))));
        } else if (member instanceof Field) {
            FieldLiteral<?> field = FieldLiteral.get((Field) member, TypeLiteral.get(member.getDeclaringClass()));
            Key<?> key = guiceUtil.getKey(field);
            addDependency(new Dependency(Dependency.GINJECTOR, key, guiceUtil.isOptional(field), false, source.toString()));
        }
    }
}
Also used : Field(java.lang.reflect.Field) FieldLiteral(com.google.gwt.inject.rebind.reflect.FieldLiteral) InjectionPoint(com.google.inject.spi.InjectionPoint) Method(java.lang.reflect.Method) Dependency(com.google.gwt.inject.rebind.binding.Dependency) Member(java.lang.reflect.Member) Key(com.google.inject.Key)

Aggregations

Dependency (com.google.gwt.inject.rebind.binding.Dependency)50 DependencyExplorerOutput (com.google.gwt.inject.rebind.resolution.DependencyExplorer.DependencyExplorerOutput)10 GinjectorBindings (com.google.gwt.inject.rebind.GinjectorBindings)8 Key (com.google.inject.Key)6 Binding (com.google.gwt.inject.rebind.binding.Binding)5 ParentBinding (com.google.gwt.inject.rebind.binding.ParentBinding)4 Method (java.lang.reflect.Method)4 LinkedHashSet (java.util.LinkedHashSet)4 ExposedChildBinding (com.google.gwt.inject.rebind.binding.ExposedChildBinding)3 HashMap (java.util.HashMap)3 Context (com.google.gwt.inject.rebind.binding.Context)2 BindingCreationException (com.google.gwt.inject.rebind.resolution.ImplicitBindingCreator.BindingCreationException)2 ArrayList (java.util.ArrayList)2 FieldLiteral (com.google.gwt.inject.rebind.reflect.FieldLiteral)1 InjectionPoint (com.google.inject.spi.InjectionPoint)1 Field (java.lang.reflect.Field)1 Member (java.lang.reflect.Member)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1