use of com.google.gwt.inject.rebind.util.NameGenerator 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.util.NameGenerator 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);
}
}
use of com.google.gwt.inject.rebind.util.NameGenerator 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.util.NameGenerator in project google-gin by gwtplus.
the class GinjectorBindingsOutputter method outputFragments.
/**
* For each fragment in the given {@link FragmentMap}, writes the field that
* stores it and a getter for that field, and adds code to invoke the
* fragment's initializers.
*/
private void outputFragments(GinjectorBindings bindings, FragmentMap fragments, StringBuilder initializeEagerSingletonsBody, StringBuilder initializeStaticInjectionsBody, SourceWriteUtil sourceWriteUtil, SourceWriter writer) {
String implClassName = ginjectorNameGenerator.getClassName(bindings);
NameGenerator nameGenerator = bindings.getNameGenerator();
for (FragmentPackageName fragmentPackageName : fragments.getFragmentPackages()) {
String fragmentCanonicalClassName = nameGenerator.getFragmentCanonicalClassName(implClassName, fragmentPackageName);
String fieldName = nameGenerator.getFragmentFieldName(fragmentPackageName);
String getterName = nameGenerator.getFragmentGetterMethodName(fragmentPackageName);
// Create the field.
writer.beginJavaDocComment();
writer.print("Injector fragment for %s", fragmentPackageName);
writer.endJavaDocComment();
writer.print("private %s %s = null;", fragmentCanonicalClassName, fieldName);
// Write the getter.
writer.beginJavaDocComment();
writer.print("Getter for injector fragment for %s", fragmentPackageName);
writer.endJavaDocComment();
sourceWriteUtil.writeMethod(writer, "public " + fragmentCanonicalClassName + " " + getterName + "()", String.format("if (%2$s == null) {\n" + " %2$s = new %1$s(this);\n" + "}\n\n" + "return %2$s;", fragmentCanonicalClassName, fieldName));
if (fragments.get(fragmentPackageName).hasEagerSingletonInitialization()) {
initializeEagerSingletonsBody.append(getterName + "().initializeEagerSingletons();\n");
}
if (fragments.get(fragmentPackageName).hasStaticInjectionInitialization()) {
initializeStaticInjectionsBody.append(getterName + "().initializeStaticInjections();\n");
}
}
}
use of com.google.gwt.inject.rebind.util.NameGenerator 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();
}
Aggregations