Search in sources :

Example 6 with NameGenerator

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);
}
Also used : Binding(com.google.gwt.inject.rebind.binding.Binding) GinjectorNameGenerator(com.google.gwt.inject.rebind.GinjectorNameGenerator) NameGenerator(com.google.gwt.inject.rebind.util.NameGenerator)

Example 7 with NameGenerator

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);
    }
}
Also used : Binding(com.google.gwt.inject.rebind.binding.Binding) UnableToCompleteException(com.google.gwt.core.ext.UnableToCompleteException) GinjectorNameGenerator(com.google.gwt.inject.rebind.GinjectorNameGenerator) NameGenerator(com.google.gwt.inject.rebind.util.NameGenerator) Method(java.lang.reflect.Method)

Example 8 with NameGenerator

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);
}
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 9 with NameGenerator

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");
        }
    }
}
Also used : GinjectorNameGenerator(com.google.gwt.inject.rebind.GinjectorNameGenerator) NameGenerator(com.google.gwt.inject.rebind.util.NameGenerator)

Example 10 with NameGenerator

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

Aggregations

NameGenerator (com.google.gwt.inject.rebind.util.NameGenerator)10 GinjectorNameGenerator (com.google.gwt.inject.rebind.GinjectorNameGenerator)8 Binding (com.google.gwt.inject.rebind.binding.Binding)4 GinjectorBindings (com.google.gwt.inject.rebind.GinjectorBindings)3 InjectorMethod (com.google.gwt.inject.rebind.util.InjectorMethod)3 MethodCallUtil (com.google.gwt.inject.rebind.util.MethodCallUtil)2 SourceWriteUtil (com.google.gwt.inject.rebind.util.SourceWriteUtil)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 GeneratorContext (com.google.gwt.core.ext.GeneratorContext)1 TreeLogger (com.google.gwt.core.ext.TreeLogger)1 UnableToCompleteException (com.google.gwt.core.ext.UnableToCompleteException)1 BindingFactoryImpl (com.google.gwt.inject.rebind.binding.BindingFactoryImpl)1 GinjectorBinding (com.google.gwt.inject.rebind.binding.GinjectorBinding)1 SubPackageClass (com.google.gwt.inject.rebind.output.subpackage.SubPackageClass)1 FieldLiteral (com.google.gwt.inject.rebind.reflect.FieldLiteral)1 MethodLiteral (com.google.gwt.inject.rebind.reflect.MethodLiteral)1 NoSourceNameException (com.google.gwt.inject.rebind.reflect.NoSourceNameException)1 BindingResolver (com.google.gwt.inject.rebind.resolution.BindingResolver)1 GuiceUtil (com.google.gwt.inject.rebind.util.GuiceUtil)1