Search in sources :

Example 26 with Binding

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

the class BindingResolverTest method testManyChildren.

public void testManyChildren() throws Exception {
    GinjectorBindings root = createInjectorNode("root");
    GinjectorBindings child1 = createInjectorNode("child1");
    GinjectorBindings child2 = createInjectorNode("child2");
    GinjectorBindings child3 = createInjectorNode("child3");
    setChildren(root, child1, child2, child3);
    bind(bar(), child1);
    bindChild(bar(), root, child1);
    bind(baz(), child2);
    bindChild(baz(), root, child2);
    Binding fooBinding = expectCreateBinding(foo(), required(foo(), bar()), required(foo(), baz()));
    root.addBinding(foo(), fooBinding);
    expectParentBinding(foo(), root, child3);
    replayAndResolve(child3, required(Dependency.GINJECTOR, foo()));
}
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) GinjectorBindings(com.google.gwt.inject.rebind.GinjectorBindings)

Example 27 with Binding

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

the class BindingResolverTest method testResolveCycleThroughProvider.

public void testResolveCycleThroughProvider() throws Exception {
    // Foo -> Bar ->Provider<Foo> -> Foo, cycle is OK because of Provider.
    // Provider<Foo> is in the "unpositioned pending Foo" set.
    StandardTree tree = createExampleTree();
    Binding fooBinding = expectCreateBinding(foo(), required(foo(), bar()));
    Binding barBinding = expectCreateBinding(bar(), required(bar(), providerFoo()));
    Binding providerFooBinding = expectCreateBinding(providerFoo(), requiredLazy(providerFoo(), foo()));
    tree.root.addBinding(foo(), fooBinding);
    tree.root.addBinding(bar(), barBinding);
    tree.root.addBinding(providerFoo(), providerFooBinding);
    expectParentBinding(foo(), tree.root, tree.childLL);
    replayAndResolve(tree.childLL, required(Dependency.GINJECTOR, foo()));
}
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)

Example 28 with Binding

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

the class BindingResolverTest method testResolveDependenciesInChildL_ExposedToRoot.

public void testResolveDependenciesInChildL_ExposedToRoot() throws Exception {
    StandardTree tree = createExampleTree();
    // Bar is bound in the child, but exposed to the root.  Foo should still be in root
    bind(bar(), tree.childL);
    bindChild(bar(), tree.root, tree.childL);
    bind(baz(), tree.root);
    Binding fooBinding = expectCreateBinding(foo(), required(foo(), bar()), required(foo(), baz()));
    tree.root.addBinding(foo(), fooBinding);
    expectParentBinding(foo(), tree.root, tree.childLL);
    replayAndResolve(tree.childLL, required(Dependency.GINJECTOR, foo()));
}
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)

Example 29 with Binding

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

the class UnresolvedBindingValidator method getAllInvalidKeys.

/**
 * Returns a map from keys that are invalid to errors explaining why each key is invalid.
 */
private Map<Key<?>, String> getAllInvalidKeys(DependencyExplorerOutput output) {
    Map<Key<?>, String> invalidKeys = new LinkedHashMap<Key<?>, String>();
    // node (if its optional).
    for (Entry<Key<?>, String> error : output.getBindingErrors()) {
        invalidKeys.put(error.getKey(), "Unable to create or inherit binding: " + error.getValue());
    }
    GinjectorBindings origin = output.getGraph().getOrigin();
    for (Key<?> key : output.getImplicitlyBoundKeys()) {
        if (origin.isBoundLocallyInChild(key)) {
            GinjectorBindings child = origin.getChildWhichBindsLocally(key);
            Binding childBinding = child.getBinding(key);
            PrettyPrinter.log(logger, TreeLogger.DEBUG, "Marking the key %s as bound in the ginjector %s (implicitly), and in the child" + " %s (%s)", key, origin, child, childBinding.getContext());
            // TODO(schmitt): Determine path to binding in child ginjector (requires
            // different DependencyExplorerOutput).
            invalidKeys.put(key, PrettyPrinter.format("Already bound in child Ginjector %s. Consider exposing it?", child));
        }
    }
    return invalidKeys;
}
Also used : Binding(com.google.gwt.inject.rebind.binding.Binding) ParentBinding(com.google.gwt.inject.rebind.binding.ParentBinding) GinjectorBindings(com.google.gwt.inject.rebind.GinjectorBindings) Key(com.google.inject.Key) LinkedHashMap(java.util.LinkedHashMap)

Example 30 with Binding

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

the class GinjectorBindingsOutputter method outputBindings.

/**
 * Outputs the top-level injector for the given {@link GinjectorBindings},
 * along with all of its fragments.
 *
 * <p>The top-level injector contains one field for each fragment of the
 * injector, which stores a reference to an instance of that fragment.  In
 * addition, it contains a getter for every public type created by one of its
 * fragments, each of which forwards to a getter in the corresponding
 * fragment.  In addition to being the injector's public interface, these
 * getters are used by each fragment of the injector to retrieve objects
 * created by other fragments.
 */
private void outputBindings(GinjectorBindings bindings, FragmentMap fragments, SourceWriter writer) {
    NameGenerator nameGenerator = bindings.getNameGenerator();
    // The initialize*() methods contain code that needs to run before the root
    // injector is returned to the client, but after the injector hierarchy is
    // fully constructed.
    // Collects the text of the body of initializeEagerSingletons().
    StringBuilder initializeEagerSingletonsBody = new StringBuilder();
    // Collects the text of the body of initializeStaticInjections().
    StringBuilder initializeStaticInjectionsBody = new StringBuilder();
    SourceWriteUtil sourceWriteUtil = sourceWriteUtilFactory.create(bindings);
    // Output child modules.
    for (GinjectorBindings child : bindings.getChildren()) {
        String className = ginjectorNameGenerator.getClassName(child);
        String canonicalClassName = ginjectorNameGenerator.getCanonicalClassName(child);
        String fieldName = ginjectorNameGenerator.getFieldName(child);
        String getterName = nameGenerator.getChildInjectorGetterMethodName(className);
        writer.beginJavaDocComment();
        writer.print("Child injector for %s", child.getModule());
        writer.endJavaDocComment();
        writer.println("private %s %s = null;", canonicalClassName, fieldName);
        writer.beginJavaDocComment();
        writer.print("Getter for child injector for %s", child.getModule());
        writer.endJavaDocComment();
        sourceWriteUtil.writeMethod(writer, String.format("public %s %s()", canonicalClassName, getterName), String.format("if (%2$s == null) {\n" + "    %2$s = new %1$s(this);\n" + "}\n\n" + "return %2$s;", canonicalClassName, fieldName));
        // Ensure that the initializer initializes this child, if necessary.
        outputSubInitialize(child, getterName, initializeEagerSingletonsBody, initializeStaticInjectionsBody);
    }
    initializeEagerSingletonsBody.append("\n");
    initializeStaticInjectionsBody.append("\n");
    outputInterfaceField(bindings, sourceWriteUtil, writer);
    outputMemberInjections(bindings, fragments, sourceWriteUtil);
    outputStaticInjections(bindings, fragments, sourceWriteUtil);
    // Output the bindings in the fragments.
    for (Map.Entry<Key<?>, Binding> entry : bindings.getBindings()) {
        Binding binding = entry.getValue();
        if (!reachabilityAnalyzer.isReachable(binding)) {
            continue;
        }
        FragmentPackageName fragmentPackageName = fragmentPackageNameFactory.create(binding.getGetterMethodPackage());
        Key<?> key = entry.getKey();
        List<InjectorMethod> helperMethods = new ArrayList();
        fragments.get(fragmentPackageName).writeBindingGetter(key, binding, bindings.determineScope(key), helperMethods);
        outputMethods(helperMethods, fragments);
    }
    // Output the fragment members.
    outputFragments(bindings, fragments, initializeEagerSingletonsBody, initializeStaticInjectionsBody, sourceWriteUtil, writer);
    writeConstructor(bindings, sourceWriteUtil, writer);
    writeInitializers(bindings, initializeEagerSingletonsBody, initializeStaticInjectionsBody, sourceWriteUtil, writer);
}
Also used : GinjectorBinding(com.google.gwt.inject.rebind.binding.GinjectorBinding) Binding(com.google.gwt.inject.rebind.binding.Binding) GinjectorBindings(com.google.gwt.inject.rebind.GinjectorBindings) InjectorMethod(com.google.gwt.inject.rebind.util.InjectorMethod) ArrayList(java.util.ArrayList) GinjectorNameGenerator(com.google.gwt.inject.rebind.GinjectorNameGenerator) NameGenerator(com.google.gwt.inject.rebind.util.NameGenerator) SourceWriteUtil(com.google.gwt.inject.rebind.util.SourceWriteUtil) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Key(com.google.inject.Key)

Aggregations

Binding (com.google.gwt.inject.rebind.binding.Binding)34 ParentBinding (com.google.gwt.inject.rebind.binding.ParentBinding)28 ExposedChildBinding (com.google.gwt.inject.rebind.binding.ExposedChildBinding)25 GinjectorBindings (com.google.gwt.inject.rebind.GinjectorBindings)10 Key (com.google.inject.Key)6 Dependency (com.google.gwt.inject.rebind.binding.Dependency)5 GinjectorNameGenerator (com.google.gwt.inject.rebind.GinjectorNameGenerator)4 NameGenerator (com.google.gwt.inject.rebind.util.NameGenerator)4 BindingCreationException (com.google.gwt.inject.rebind.resolution.ImplicitBindingCreator.BindingCreationException)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 Context (com.google.gwt.inject.rebind.binding.Context)2 HashMap (java.util.HashMap)2 UnableToCompleteException (com.google.gwt.core.ext.UnableToCompleteException)1 GinScope (com.google.gwt.inject.rebind.GinScope)1 GinjectorBinding (com.google.gwt.inject.rebind.binding.GinjectorBinding)1 RemoteServiceProxyBinding (com.google.gwt.inject.rebind.binding.RemoteServiceProxyBinding)1 InjectorMethod (com.google.gwt.inject.rebind.util.InjectorMethod)1 SourceWriteUtil (com.google.gwt.inject.rebind.util.SourceWriteUtil)1 Singleton (com.google.inject.Singleton)1