Search in sources :

Example 21 with GinjectorBindings

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

the class BindingPositioner method getInstallPosition.

/**
 * Returns the Ginjector where the binding for key should be placed, or null if the key was
 * removed from the dependency graph earlier.
 */
public GinjectorBindings getInstallPosition(Key<?> key) {
    Preconditions.checkNotNull(positions, "Must call position before calling getInstallPosition(Key<?>)");
    GinjectorBindings position = installOverrides.get(key);
    if (position == null) {
        position = positions.get(key);
    }
    return position;
}
Also used : GinjectorBindings(com.google.gwt.inject.rebind.GinjectorBindings)

Example 22 with GinjectorBindings

use of com.google.gwt.inject.rebind.GinjectorBindings 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 23 with GinjectorBindings

use of com.google.gwt.inject.rebind.GinjectorBindings 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)

Example 24 with GinjectorBindings

use of com.google.gwt.inject.rebind.GinjectorBindings 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 25 with GinjectorBindings

use of com.google.gwt.inject.rebind.GinjectorBindings 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)

Aggregations

GinjectorBindings (com.google.gwt.inject.rebind.GinjectorBindings)27 Binding (com.google.gwt.inject.rebind.binding.Binding)10 Dependency (com.google.gwt.inject.rebind.binding.Dependency)8 ParentBinding (com.google.gwt.inject.rebind.binding.ParentBinding)7 ExposedChildBinding (com.google.gwt.inject.rebind.binding.ExposedChildBinding)6 DependencyExplorerOutput (com.google.gwt.inject.rebind.resolution.DependencyExplorer.DependencyExplorerOutput)6 Key (com.google.inject.Key)5 NameGenerator (com.google.gwt.inject.rebind.util.NameGenerator)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 Capture (org.easymock.Capture)3 GinjectorNameGenerator (com.google.gwt.inject.rebind.GinjectorNameGenerator)2 InjectorMethod (com.google.gwt.inject.rebind.util.InjectorMethod)2 SourceWriteUtil (com.google.gwt.inject.rebind.util.SourceWriteUtil)2 GeneratorContext (com.google.gwt.core.ext.GeneratorContext)1 GinModule (com.google.gwt.inject.client.GinModule)1 GinScope (com.google.gwt.inject.rebind.GinScope)1 GinModuleAdapter (com.google.gwt.inject.rebind.adapter.GinModuleAdapter)1 GinjectorBinding (com.google.gwt.inject.rebind.binding.GinjectorBinding)1 SubPackageClass (com.google.gwt.inject.rebind.output.subpackage.SubPackageClass)1