Search in sources :

Example 1 with NameGenerator

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

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

the class GinjectorBindingsTest method setUp.

@Override
public void setUp() {
    control = createControl();
    niceControl = createNiceControl();
    nameGenerator = control.createMock("nameGenerator", NameGenerator.class);
    logger = niceControl.createMock("logger", TreeLogger.class);
    guiceUtil = control.createMock("guiceUtil", GuiceUtil.class);
    {
        @SuppressWarnings("unchecked") Provider<GinjectorBindings> tmpProvider = control.createMock("ginjectorBindingsProvider", Provider.class);
        ginjectorBindingsProvider = tmpProvider;
    }
    collector = control.createMock("collector", MemberCollector.class);
    errorManager = control.createMock("errorManager", ErrorManager.class);
    bindingResolver = control.createMock("bindingResolver", BindingResolver.class);
    methodCallUtil = control.createMock("methodCallUtil", MethodCallUtil.class);
    bindingFactory = new BindingFactoryImpl(errorManager, guiceUtil, DummyInjectorInterface.class, methodCallUtil);
}
Also used : MethodCallUtil(com.google.gwt.inject.rebind.util.MethodCallUtil) TreeLogger(com.google.gwt.core.ext.TreeLogger) GuiceUtil(com.google.gwt.inject.rebind.util.GuiceUtil) BindingFactoryImpl(com.google.gwt.inject.rebind.binding.BindingFactoryImpl) NameGenerator(com.google.gwt.inject.rebind.util.NameGenerator) BindingResolver(com.google.gwt.inject.rebind.resolution.BindingResolver) MemberCollector(com.google.gwt.inject.rebind.util.MemberCollector) Provider(com.google.inject.Provider)

Example 3 with NameGenerator

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

the class GinjectorBindingsOutputter method outputMemberInjections.

/**
 * Adds member injections to each fragment.
 */
private void outputMemberInjections(GinjectorBindings bindings, FragmentMap fragments, SourceWriteUtil sourceWriteUtil) {
    NameGenerator nameGenerator = bindings.getNameGenerator();
    for (TypeLiteral<?> type : bindings.getMemberInjectRequests()) {
        if (!reachabilityAnalyzer.isReachableMemberInject(bindings, type)) {
            continue;
        }
        List<InjectorMethod> memberInjectionHelpers = new ArrayList<InjectorMethod>();
        try {
            sourceWriteUtil.createMemberInjection(type, nameGenerator, memberInjectionHelpers);
            outputMethods(memberInjectionHelpers, fragments);
        } catch (NoSourceNameException e) {
            errorManager.logError(e.getMessage(), e);
        }
    }
}
Also used : InjectorMethod(com.google.gwt.inject.rebind.util.InjectorMethod) NoSourceNameException(com.google.gwt.inject.rebind.reflect.NoSourceNameException) ArrayList(java.util.ArrayList) GinjectorNameGenerator(com.google.gwt.inject.rebind.GinjectorNameGenerator) NameGenerator(com.google.gwt.inject.rebind.util.NameGenerator)

Example 4 with NameGenerator

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

the class GinjectorBindingsOutputter method outputInterfaceField.

/**
 * Writes code to store and retrieve the current injector interface, if one is
 * bound.
 */
private void outputInterfaceField(GinjectorBindings bindings, SourceWriteUtil sourceWriteUtil, SourceWriter writer) {
    // Only the root injector has an interface binding.
    if (bindings.getParent() != null) {
        return;
    }
    Class<?> boundGinjectorInterface = getBoundGinjector(bindings);
    if (boundGinjectorInterface == null) {
        // Sanity-check: if this fails, then we somehow didn't bind the injector
        // interface in the root module (the root module should always generate a
        // binding for the injector).
        errorManager.logError("Injector interface not bound in the root module.");
        return;
    }
    NameGenerator nameGenerator = bindings.getNameGenerator();
    String fieldName = nameGenerator.getGinjectorInterfaceFieldName();
    String getterName = nameGenerator.getGinjectorInterfaceGetterMethodName();
    writer.beginJavaDocComment();
    writer.print("The implementation of " + boundGinjectorInterface);
    writer.endJavaDocComment();
    writer.println("private final %s %s;", boundGinjectorInterface.getCanonicalName(), fieldName);
    sourceWriteUtil.writeMethod(writer, String.format("public %s %s()", boundGinjectorInterface.getCanonicalName(), getterName), String.format("return %s;", fieldName));
}
Also used : GinjectorNameGenerator(com.google.gwt.inject.rebind.GinjectorNameGenerator) NameGenerator(com.google.gwt.inject.rebind.util.NameGenerator)

Example 5 with NameGenerator

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

the class GinjectorFragmentContext method callMethod.

public String callMethod(String methodName, String methodFragmentPackage, Iterable<String> parameters) {
    NameGenerator nameGenerator = bindings.getNameGenerator();
    StringBuilder resultBuilder = new StringBuilder();
    FragmentPackageName methodFragmentPackageName = fragmentPackageNameFactory.create(methodFragmentPackage);
    if (!methodFragmentPackageName.equals(fragmentPackageName)) {
        String fragmentGetter = nameGenerator.getFragmentGetterMethodName(methodFragmentPackageName);
        resultBuilder.append("injector.").append(fragmentGetter).append("().");
    }
    return resultBuilder.append(methodName).append("(").append(SourceWriteUtil.join(", ", parameters)).append(")").toString();
}
Also used : 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