Search in sources :

Example 11 with Binding

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

the class BindingResolverTest method testDependencyInOtherChild.

public void testDependencyInOtherChild() throws Exception {
    // Test one of the "weird" behaviors in Guice. Foo depends on Bar and Baz.  Because
    // Bar is bound in a sibling, we can't create Bar in the parent.  Therefore,
    // we create Bar (and Foo) in the origin
    GinjectorBindings root = createInjectorNode("root");
    GinjectorBindings childL = createInjectorNode("childL");
    GinjectorBindings childR = createInjectorNode("childR");
    setChildren(root, childL, childR);
    bind(baz(), root);
    bind(bar(), childL);
    expect(root.isBoundLocallyInChild(bar())).andReturn(true).anyTimes();
    Binding fooBinding = expectCreateBinding(foo(), required(foo(), bar()), required(foo(), baz()));
    Binding barBinding = expectCreateBinding(bar());
    childR.addBinding(bar(), barBinding);
    expectParentBinding(baz(), root, childR);
    childR.addBinding(foo(), fooBinding);
    replayAndResolve(childR, 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 12 with Binding

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

the class DependencyExplorer method visit.

private void visit(Key<?> key, DependencyGraph.Builder builder, DependencyExplorerOutput output, GinjectorBindings origin) {
    if (visited.add(key)) {
        GinjectorBindings accessibleSource = locateHighestAccessibleSource(key, origin);
        if (accessibleSource != null) {
            PrettyPrinter.log(logger, TreeLogger.DEBUG, "Using binding of %s in %s.", key, accessibleSource);
            output.preExistingBindings.put(key, accessibleSource);
        } else {
            try {
                Binding binding = bindingCreator.create(key);
                PrettyPrinter.log(logger, TreeLogger.DEBUG, "Implicitly bound %s in %s using %s.", key, origin, binding);
                for (Dependency edge : binding.getDependencies()) {
                    PrettyPrinter.log(logger, TreeLogger.DEBUG, "Following %s", edge);
                    builder.addEdge(edge);
                    visit(edge.getTarget(), builder, output, origin);
                }
                // Do this *after* visiting all dependencies so that that the ordering is post-order
                output.implicitBindings.put(key, binding);
            } catch (BindingCreationException e) {
                PrettyPrinter.log(logger, TreeLogger.DEBUG, "Implicit binding failed for %s: %s", key, e.getMessage());
                output.bindingErrors.put(key, e.getMessage());
            } catch (RuntimeException e) {
                logger.log(Type.ERROR, "Exception while visiting " + key);
                throw e;
            }
        }
    }
}
Also used : Binding(com.google.gwt.inject.rebind.binding.Binding) ExposedChildBinding(com.google.gwt.inject.rebind.binding.ExposedChildBinding) GinjectorBindings(com.google.gwt.inject.rebind.GinjectorBindings) Dependency(com.google.gwt.inject.rebind.binding.Dependency) BindingCreationException(com.google.gwt.inject.rebind.resolution.ImplicitBindingCreator.BindingCreationException)

Example 13 with Binding

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

the class GinjectorFragmentContext method callGetter.

public String callGetter(Key<?> key) {
    Binding keyBinding = bindings.getBinding(key);
    if (keyBinding == null) {
        errorManager.logError("No binding found for %s in %s", key, bindings);
        return "null /* No binding found */";
    }
    FragmentPackageName keyPackageName = fragmentPackageNameFactory.create(keyBinding.getGetterMethodPackage());
    String getterCall = bindings.getNameGenerator().getGetterMethodName(key) + "()";
    if (keyPackageName.equals(fragmentPackageName)) {
        return getterCall;
    } else {
        String fragmentGetterName = bindings.getNameGenerator().getFragmentGetterMethodName(keyPackageName);
        return String.format("injector.%s().%s", fragmentGetterName, getterCall);
    }
}
Also used : Binding(com.google.gwt.inject.rebind.binding.Binding)

Example 14 with Binding

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

the class GinjectorFragmentContext method callChildGetter.

public String callChildGetter(GinjectorBindings childBindings, Key<?> key) {
    Binding childKeyBinding = childBindings.getBinding(key);
    if (childKeyBinding == null) {
        errorManager.logError("No binding found for %s", key);
        return "null /* No binding found */";
    }
    FragmentPackageName childKeyPackageName = fragmentPackageNameFactory.create(childKeyBinding.getGetterMethodPackage());
    NameGenerator nameGenerator = bindings.getNameGenerator();
    NameGenerator childNameGenerator = childBindings.getNameGenerator();
    String childInjectorClassName = ginjectorNameGenerator.getClassName(childBindings);
    String childGetter = nameGenerator.getChildInjectorGetterMethodName(childInjectorClassName);
    String fragmentGetter = childNameGenerator.getFragmentGetterMethodName(childKeyPackageName);
    String getter = childNameGenerator.getGetterMethodName(key);
    return String.format("injector.%s().%s().%s()", childGetter, fragmentGetter, getter);
}
Also used : Binding(com.google.gwt.inject.rebind.binding.Binding) GinjectorNameGenerator(com.google.gwt.inject.rebind.GinjectorNameGenerator) NameGenerator(com.google.gwt.inject.rebind.util.NameGenerator)

Example 15 with Binding

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

the class GinjectorImplOutputter method outputInterfaceMethods.

private void outputInterfaceMethods(GinjectorBindings bindings, TypeLiteral<?> ginjectorInterface, SourceWriteUtil sourceWriteUtil, SourceWriter writer) throws NoSourceNameException, UnableToCompleteException {
    NameGenerator nameGenerator = bindings.getNameGenerator();
    // interface.
    for (MethodLiteral<?, Method> method : constructorInjectCollector.getMethods(ginjectorInterface)) {
        Key<?> methodKey = guiceUtil.getKey(method);
        Binding binding = bindings.getBinding(methodKey);
        if (binding == null) {
            // This should not happen, but fail with a meaningful message if it
            // does.
            logger.log(TreeLogger.Type.ERROR, "Unable to find a binding for the required key " + methodKey);
            throw new UnableToCompleteException();
        }
        if (!reachabilityAnalyzer.isReachable(binding)) {
            // Sanity-check reachability: every binding in the Ginjector ought to be
            // reachable.
            PrettyPrinter.log(logger, TreeLogger.Type.ERROR, "The key %s is required by the Ginjector, but is not reachable.", methodKey);
            throw new UnableToCompleteException();
        }
        FragmentPackageName fragmentPackageName = fragmentPackageNameFactory.create(binding.getGetterMethodPackage());
        String body = String.format("return %s.%s().%s();", ginjectorNameGenerator.getFieldName(bindings), nameGenerator.getFragmentGetterMethodName(fragmentPackageName), nameGenerator.getGetterMethodName(guiceUtil.getKey(method)));
        String readableDeclaration = ReflectUtil.signatureBuilder(method).removeAbstractModifier().build();
        sourceWriteUtil.writeMethod(writer, readableDeclaration, body.toString());
    }
    // injection on the given BarType.
    for (MethodLiteral<?, Method> method : memberInjectCollector.getMethods(ginjectorInterface)) {
        Key<?> injectee = guiceUtil.getKey(method);
        if (!reachabilityAnalyzer.isReachableMemberInject(bindings, injectee.getTypeLiteral())) {
            // Sanity-check reachability: every member injection in the Ginjector
            // ought to be reachable.
            PrettyPrinter.log(logger, TreeLogger.Type.ERROR, "Method injection of %s is required by the Ginjector, but is not reachable.", injectee.getTypeLiteral());
            throw new UnableToCompleteException();
        }
        FragmentPackageName fragmentPackageName = fragmentPackageNameFactory.create(ReflectUtil.getUserPackageName(injectee.getTypeLiteral()));
        String body = String.format("%s.%s().%s(param);", ginjectorNameGenerator.getFieldName(bindings), nameGenerator.getFragmentGetterMethodName(fragmentPackageName), nameGenerator.getMemberInjectMethodName(injectee.getTypeLiteral()));
        String readableDeclaration = ReflectUtil.signatureBuilder(method).withParameterNames(new String[] { "param" }).removeAbstractModifier().build();
        sourceWriteUtil.writeMethod(writer, readableDeclaration, body);
    }
}
Also used : Binding(com.google.gwt.inject.rebind.binding.Binding) UnableToCompleteException(com.google.gwt.core.ext.UnableToCompleteException) GinjectorNameGenerator(com.google.gwt.inject.rebind.GinjectorNameGenerator) NameGenerator(com.google.gwt.inject.rebind.util.NameGenerator) Method(java.lang.reflect.Method)

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