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