Search in sources :

Example 21 with Dependency

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

the class EagerCycleFinder method reportCycle.

private void reportCycle(Dependency cycleEdge) {
    cycleDetected = true;
    // Get the edges in the cycle
    List<Dependency> cycle = describeCycle(cycleEdge);
    // Using the edges, determine the keys in the cycle
    PathFinder pathFinder = new PathFinder().onGraph(graph).addRoots(Dependency.GINJECTOR);
    for (Dependency edge : cycle) {
        pathFinder.addDestinations(edge.getTarget());
    }
    List<Dependency> path = pathFinder.findShortestPath();
    if (path != null && !path.isEmpty()) {
        cycle = rootCycleAt(cycle, path.get(path.size() - 1).getTarget());
    }
    reportError(path, cycle);
}
Also used : Dependency(com.google.gwt.inject.rebind.binding.Dependency)

Example 22 with Dependency

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

the class GuiceUtil method getMemberInjectionDependencies.

/**
 * Collects and returns all keys required to member-inject the given class.
 *
 * @param typeKey key causing member injection
 * @param type class for which required keys are calculated
 * @return keys required to inject given class
 */
public Collection<Dependency> getMemberInjectionDependencies(Key<?> typeKey, TypeLiteral<?> type) {
    Set<Dependency> required = new LinkedHashSet<Dependency>();
    for (MethodLiteral<?, Method> method : memberCollector.getMethods(type)) {
        required.addAll(getDependencies(typeKey, method));
    }
    for (FieldLiteral<?> field : memberCollector.getFields(type)) {
        Key<?> key = getKey(field);
        required.add(new Dependency(typeKey, key, isOptional(field), false, "member injection of " + field));
    }
    return required;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Dependency(com.google.gwt.inject.rebind.binding.Dependency) Method(java.lang.reflect.Method)

Example 23 with Dependency

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

the class PrettyPrinter method formatArgTo.

/**
 * Formats a list of dependencies as a dependency path; see the class
 * comments.
 */
private static void formatArgTo(List<Dependency> path, StringBuilder builder) {
    if (path.isEmpty()) {
        return;
    }
    builder.append("\n");
    boolean first = true;
    // For sanity-checking.
    Key<?> previousTarget = null;
    for (Dependency dependency : path) {
        Key<?> source = dependency.getSource();
        Key<?> target = dependency.getTarget();
        // Sanity-check.
        if (previousTarget != null && !previousTarget.equals(source)) {
            throw new IllegalArgumentException("Dependency list is not a path.");
        }
        // ...
        if (first) {
            if (source == Dependency.GINJECTOR) {
                formatArgTo(target, builder);
                builder.append(String.format(" [%s]%n", dependency.getContext()));
            } else {
                formatArgTo(source, builder);
                builder.append("\n -> ");
                formatArgTo(target, builder);
                builder.append(String.format(" [%s]%n", dependency.getContext()));
            }
            first = false;
        } else {
            builder.append(" -> ");
            formatArgTo(target, builder);
            builder.append(String.format(" [%s]%n", dependency.getContext()));
        }
        previousTarget = target;
    }
}
Also used : Dependency(com.google.gwt.inject.rebind.binding.Dependency)

Example 24 with Dependency

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

the class ReachabilityAnalyzer method traceKey.

/**
 * Marks the binding of the given key in the given {@link GinjectorBindings}
 * as reachable, and traces out its dependencies.
 */
private void traceKey(Key<?> key, GinjectorBindings bindings) {
    Binding binding = bindings.getBinding(key);
    // Make sure the binding is present: optional bindings might be missing.
    if (binding != null) {
        if (!reachable.add(binding)) {
            // The binding was already marked as reachable.
            return;
        }
        getReachableMemberInjects(bindings).addAll(binding.getMemberInjectRequests());
        for (Dependency dependency : binding.getDependencies()) {
            if (dependency.getSource().equals(key)) {
                Key<?> target = dependency.getTarget();
                PrettyPrinter.log(logger, TreeLogger.DEBUG, "%s:%s -> %s:%s [%s]", bindings, key, bindings, dependency.getTarget(), binding);
                traceKey(target, bindings);
            }
        }
        // dependency graph.
        if (binding instanceof ParentBinding) {
            ParentBinding parentBinding = (ParentBinding) binding;
            PrettyPrinter.log(logger, TreeLogger.DEBUG, "%s:%s -> %s:%s [inherited]", bindings, key, parentBinding.getParentBindings(), key);
            traceKey(key, parentBinding.getParentBindings());
        } else if (binding instanceof ExposedChildBinding) {
            ExposedChildBinding exposedChildBinding = (ExposedChildBinding) binding;
            PrettyPrinter.log(logger, TreeLogger.DEBUG, "%s:%s -> %s:%s [exposed]", bindings, key, exposedChildBinding.getChildBindings(), key);
            traceKey(key, exposedChildBinding.getChildBindings());
        }
    }
}
Also used : ExposedChildBinding(com.google.gwt.inject.rebind.binding.ExposedChildBinding) Binding(com.google.gwt.inject.rebind.binding.Binding) ParentBinding(com.google.gwt.inject.rebind.binding.ParentBinding) ParentBinding(com.google.gwt.inject.rebind.binding.ParentBinding) ExposedChildBinding(com.google.gwt.inject.rebind.binding.ExposedChildBinding) Dependency(com.google.gwt.inject.rebind.binding.Dependency)

Example 25 with Dependency

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

the class GuiceUtil method getDependencies.

/**
 * Collects and returns all keys required to inject the given method.
 *
 * @param typeKey the key that depends on injecting the arguments of method
 * @param method method for which required keys are calculated
 * @return required keys
 */
public Collection<Dependency> getDependencies(Key<?> typeKey, MethodLiteral<?, ?> method) {
    String context;
    if (method.isConstructor()) {
        context = "@Inject constructor of " + method.getDeclaringType();
    } else if (typeKey == Dependency.GINJECTOR) {
        context = "Member injector " + method;
    } else {
        context = "Member injection via " + method;
    }
    Set<Dependency> required = new LinkedHashSet<Dependency>();
    for (Key<?> key : method.getParameterKeys()) {
        required.add(new Dependency(typeKey, key, isOptional(method), false, context));
    }
    return required;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Dependency(com.google.gwt.inject.rebind.binding.Dependency)

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