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;
}
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;
}
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);
}
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();
}
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);
}
}
Aggregations