Search in sources :

Example 31 with Binding

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

the class GinjectorFragmentContext method callParentGetter.

public String callParentGetter(Key<?> key, GinjectorBindings parentBindings) {
    Binding parentKeyBinding = parentBindings.getBinding(key);
    if (parentKeyBinding == null) {
        errorManager.logError("No binding found for %s in %s", key, parentBindings);
        return "null /* No binding found */";
    }
    FragmentPackageName parentKeyPackageName = fragmentPackageNameFactory.create(parentKeyBinding.getGetterMethodPackage());
    StringBuilder result = new StringBuilder().append("injector");
    // Walk up the injector hierarchy until we hit the requested parent.
    GinjectorBindings current = bindings;
    while (current != null && current != parentBindings) {
        result.append(".getParent()");
        current = current.getParent();
    }
    if (current == null) {
        // This should never happen; it indicates that the given parent injector
        // isn't actually a parent of the current bindings object.
        errorManager.logError("Internal Gin error: %s is not a parent of %s.", parentBindings, bindings);
        return "null /* Internal error: unreachable parent bindings */";
    }
    NameGenerator parentNameGenerator = parentBindings.getNameGenerator();
    String fragmentGetter = parentNameGenerator.getFragmentGetterMethodName(parentKeyPackageName);
    String getter = parentNameGenerator.getGetterMethodName(key);
    return result.append(String.format(".%s().%s()", fragmentGetter, getter)).toString();
}
Also used : Binding(com.google.gwt.inject.rebind.binding.Binding) GinjectorBindings(com.google.gwt.inject.rebind.GinjectorBindings) GinjectorNameGenerator(com.google.gwt.inject.rebind.GinjectorNameGenerator) NameGenerator(com.google.gwt.inject.rebind.util.NameGenerator)

Example 32 with Binding

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

the class ReachabilityAnalyzer method doTraceEagerSingletons.

private void doTraceEagerSingletons(GinjectorBindings bindings) {
    for (Map.Entry<Key<?>, Binding> entry : bindings.getBindings()) {
        Key<?> key = entry.getKey();
        Binding binding = entry.getValue();
        GinScope scope = bindings.determineScope(key);
        if (scope == GinScope.EAGER_SINGLETON) {
            PrettyPrinter.log(logger, TreeLogger.DEBUG, "ROOT -> %s:%s [eager singleton: %s]", bindings, key, binding);
            traceKey(key, bindings);
        }
    }
    for (GinjectorBindings child : bindings.getChildren()) {
        doTraceEagerSingletons(child);
    }
}
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) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Key(com.google.inject.Key) GinScope(com.google.gwt.inject.rebind.GinScope)

Example 33 with Binding

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

the class BindingInstaller method installBindings.

/**
 * Installs all of the implicit bindings as described {@link BindingInstaller above}.
 *
 * @param output {@link DependencyExplorerOutput} with information about the unresolved bindings
 *     for the current ginjector.
 */
public void installBindings(DependencyExplorerOutput output) {
    positions.position(output);
    // Install each implicit binding in the correct position
    for (Map.Entry<Key<?>, Binding> entry : output.getImplicitBindings()) {
        installBinding(output.getGraph(), entry.getKey(), entry.getValue());
    }
    // Make sure that each of the dependencies needed directly from the origin are available
    GinjectorBindings origin = output.getGraph().getOrigin();
    inheritBindingsForDeps(origin, origin.getDependencies());
}
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) Map(java.util.Map) Key(com.google.inject.Key)

Example 34 with Binding

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

the class GinjectorBindings method determineScope.

public GinScope determineScope(Key<?> key) {
    assertFinalized();
    GinScope scope = scopes.get(key);
    if (scope == null) {
        Class<?> raw = key.getTypeLiteral().getRawType();
        Binding binding = bindings.get(key);
        if (binding != null && (binding instanceof ExposedChildBinding || binding instanceof ParentBinding)) {
            // If this is just a "copy" of a binding higher/lower in the injector
            // tree, we prefer to treat the binding like it's unscoped, and refer to
            // the "real" binding every time we need the value.
            scope = GinScope.NO_SCOPE;
        } else if (raw.getAnnotation(Singleton.class) != null || raw.getAnnotation(javax.inject.Singleton.class) != null) {
            // Look for scope annotation as a fallback
            scope = GinScope.SINGLETON;
        } else if (RemoteServiceProxyBinding.isRemoteServiceProxy(key.getTypeLiteral())) {
            // Special case for remote services
            scope = GinScope.SINGLETON;
        } else {
            scope = GinScope.NO_SCOPE;
        }
    }
    logger.log(TreeLogger.TRACE, "scope for " + key + ": " + scope);
    return scope;
}
Also used : RemoteServiceProxyBinding(com.google.gwt.inject.rebind.binding.RemoteServiceProxyBinding) 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) Singleton(com.google.inject.Singleton) ExposedChildBinding(com.google.gwt.inject.rebind.binding.ExposedChildBinding)

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