Search in sources :

Example 1 with SourceWriteUtil

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

the class GinjectorBindingsOutputterTest method testOutputStaticInjections.

// Verify that outputting static injections creates and dispatches to the
// correct fragment classes.
public void testOutputStaticInjections() throws Exception {
    PrintWriter printWriter = new PrintWriter(new ByteArrayOutputStream());
    GeneratorContext ctx = createMock(GeneratorContext.class, "ctx");
    expect(ctx.tryCreate((TreeLogger) anyObject(), (String) anyObject(), (String) anyObject())).andStubReturn(printWriter);
    Capture<FieldLiteral<SuperClass>> fieldCapture = new Capture<FieldLiteral<SuperClass>>();
    Capture<MethodLiteral<SuperClass, Method>> methodCapture = new Capture<MethodLiteral<SuperClass, Method>>();
    NameGenerator nameGenerator = createMock(NameGenerator.class, "nameGenerator");
    expect(nameGenerator.convertToValidMemberName("injectStatic_com.google.gwt.inject.rebind.output." + "GinjectorBindingsOutputterTest$SubClass")).andStubReturn("test_injectSubClass");
    expect(nameGenerator.convertToValidMemberName("injectStatic_com.google.gwt.inject.rebind.output.subpackage." + "SubPackageClass")).andStubReturn("test_injectSubPackageClass");
    SourceWriteUtil sourceWriteUtil = createMock(SourceWriteUtil.class, "sourceWriteUtil");
    expect(sourceWriteUtil.createFieldInjection(capture(fieldCapture), (String) anyObject(), (NameGenerator) anyObject(), (List<InjectorMethod>) anyObject())).andReturn(SourceSnippets.forText(""));
    MethodCallUtil methodCallUtil = createMock(MethodCallUtil.class, "methodCallUtil");
    expect(methodCallUtil.createMethodCallWithInjection(capture(methodCapture), (String) anyObject(), (NameGenerator) anyObject(), (List<InjectorMethod>) anyObject())).andReturn(SourceSnippets.forText(""));
    GinjectorBindings bindings = createMock(GinjectorBindings.class, "bindings");
    expect(bindings.getNameGenerator()).andStubReturn(nameGenerator);
    expect(bindings.getStaticInjectionRequests()).andStubReturn(Arrays.<Class<?>>asList(SubClass.class, SubPackageClass.class));
    String ginjectorPackageName = "com.google.gwt.inject.rebind.output";
    String ginjectorClassName = "GinjectorFragmentOutputterTest$FakeGinjector";
    GinjectorFragmentOutputter.Factory fragmentOutputterFactory = createMock(GinjectorFragmentOutputter.Factory.class, "fragmentOutputterFactory");
    GinjectorFragmentOutputter fragmentOutputter = createMock(GinjectorFragmentOutputter.class, "fragmentOutputter");
    GinjectorFragmentOutputter fragmentOutputterSubpackage = createMock(GinjectorFragmentOutputter.class, "fragmentOutputterSubpackage");
    expect(fragmentOutputterFactory.create(bindings, new FragmentPackageName(null, "com.google.gwt.inject.rebind.output"), ginjectorPackageName, ginjectorClassName)).andStubReturn(fragmentOutputter);
    expect(fragmentOutputterFactory.create(bindings, new FragmentPackageName(null, "com.google.gwt.inject.rebind.output.subpackage"), ginjectorPackageName, ginjectorClassName)).andStubReturn(fragmentOutputterSubpackage);
    fragmentOutputter.outputMethod((InjectorMethod) anyObject());
    fragmentOutputterSubpackage.outputMethod((InjectorMethod) anyObject());
    fragmentOutputter.invokeInInitializeStaticInjections("test_injectSubClass");
    fragmentOutputterSubpackage.invokeInInitializeStaticInjections("test_injectSubPackageClass");
    replay();
    GinjectorBindingsOutputter outputter = new GinjectorBindingsOutputter(ctx, null, fragmentOutputterFactory, new TestFragmentPackageNameFactory(), null, TreeLogger.NULL, methodCallUtil, null, null);
    GinjectorBindingsOutputter.FragmentMap fragments = new GinjectorBindingsOutputter.FragmentMap(bindings, ginjectorPackageName, ginjectorClassName, fragmentOutputterFactory);
    outputter.outputStaticInjections(bindings, fragments, sourceWriteUtil);
    verify();
    TypeLiteral<SuperClass> superClass = TypeLiteral.get(SuperClass.class);
    assertEquals(superClass, methodCapture.getValue().getDeclaringType());
    assertEquals(superClass, fieldCapture.getValue().getDeclaringType());
}
Also used : InjectorMethod(com.google.gwt.inject.rebind.util.InjectorMethod) NameGenerator(com.google.gwt.inject.rebind.util.NameGenerator) GeneratorContext(com.google.gwt.core.ext.GeneratorContext) Capture(org.easymock.Capture) SourceWriteUtil(com.google.gwt.inject.rebind.util.SourceWriteUtil) MethodCallUtil(com.google.gwt.inject.rebind.util.MethodCallUtil) SubPackageClass(com.google.gwt.inject.rebind.output.subpackage.SubPackageClass) MethodLiteral(com.google.gwt.inject.rebind.reflect.MethodLiteral) PrintWriter(java.io.PrintWriter) GinjectorBindings(com.google.gwt.inject.rebind.GinjectorBindings) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Method(java.lang.reflect.Method) InjectorMethod(com.google.gwt.inject.rebind.util.InjectorMethod) FieldLiteral(com.google.gwt.inject.rebind.reflect.FieldLiteral)

Example 2 with SourceWriteUtil

use of com.google.gwt.inject.rebind.util.SourceWriteUtil 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 3 with SourceWriteUtil

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

the class GinjectorImplOutputter method writeInterface.

private void writeInterface(TypeLiteral<?> ginjectorInterface, String packageName, String implClassName, PrintWriter printWriter, GinjectorBindings rootBindings) throws UnableToCompleteException {
    ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName, implClassName);
    SourceWriter writer = null;
    try {
        composerFactory.addImplementedInterface(ReflectUtil.getSourceName(ginjectorInterface));
        writer = composerFactory.createSourceWriter(ctx, printWriter);
        String rootInjectorClass = ginjectorNameGenerator.getClassName(rootBindings);
        String rootFieldName = ginjectorNameGenerator.getFieldName(rootBindings);
        writer.beginJavaDocComment();
        writer.print("Top-level injector instance for injector " + rootBindings.getModule() + ".");
        writer.endJavaDocComment();
        writer.println("private final %1$s %2$s = new %1$s(this);", rootInjectorClass, rootFieldName);
        SourceWriteUtil sourceWriteUtil = sourceWriteUtilFactory.create(rootBindings);
        String staticInjectionInitialization = rootBindings.hasStaticInjectionRequestInSubtree() ? String.format("%s.initializeStaticInjections();\n", rootFieldName) : "";
        String eagerSingletonsInitialization = rootBindings.hasEagerSingletonBindingInSubtree() ? String.format("%s.initializeEagerSingletons();\n", rootFieldName) : "";
        sourceWriteUtil.writeMethod(writer, "public " + implClassName + "()", String.format(// See http://code.google.com/p/google-guice/wiki/Bootstrap
        "%s%s", staticInjectionInitialization, eagerSingletonsInitialization));
        outputInterfaceMethods(rootBindings, ginjectorInterface, sourceWriteUtil, writer);
    } catch (NoSourceNameException e) {
        // TODO(schmitt): Collect errors and log list of them.
        logger.log(TreeLogger.Type.ERROR, e.getMessage(), e);
    }
    if (writer != null) {
        writer.commit(logger);
    }
}
Also used : SourceWriteUtil(com.google.gwt.inject.rebind.util.SourceWriteUtil) ClassSourceFileComposerFactory(com.google.gwt.user.rebind.ClassSourceFileComposerFactory) NoSourceNameException(com.google.gwt.inject.rebind.reflect.NoSourceNameException) SourceWriter(com.google.gwt.user.rebind.SourceWriter)

Aggregations

SourceWriteUtil (com.google.gwt.inject.rebind.util.SourceWriteUtil)3 GinjectorBindings (com.google.gwt.inject.rebind.GinjectorBindings)2 InjectorMethod (com.google.gwt.inject.rebind.util.InjectorMethod)2 NameGenerator (com.google.gwt.inject.rebind.util.NameGenerator)2 GeneratorContext (com.google.gwt.core.ext.GeneratorContext)1 GinjectorNameGenerator (com.google.gwt.inject.rebind.GinjectorNameGenerator)1 Binding (com.google.gwt.inject.rebind.binding.Binding)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 MethodCallUtil (com.google.gwt.inject.rebind.util.MethodCallUtil)1 ClassSourceFileComposerFactory (com.google.gwt.user.rebind.ClassSourceFileComposerFactory)1 SourceWriter (com.google.gwt.user.rebind.SourceWriter)1 Key (com.google.inject.Key)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintWriter (java.io.PrintWriter)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1