Search in sources :

Example 1 with SourceSnippet

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

the class FactoryBinding method callAssistedCreate.

private SourceSnippet callAssistedCreate(AssistData assisted, NameGenerator nameGenerator, List<InjectorMethod> methodsOutput) throws NoSourceNameException {
    String returnTypeName = ReflectUtil.getSourceName(assisted.implementation);
    String packageName = ReflectUtil.getUserPackageName(assisted.implementation);
    String factoryTypeName = ReflectUtil.getSourceName(factoryType);
    String assistedInjectMethodName = nameGenerator.getAssistedInjectMethodName(factoryKey, assisted.method.getName());
    String assistedInjectSignature = ReflectUtil.signatureBuilder(assisted.method).withMethodName(assistedInjectMethodName).removeAbstractModifier().build();
    SourceSnippet memberInjectCall = SourceSnippets.callMemberInject(assisted.implementation, "result");
    SourceSnippet methodCall = methodCallUtil.createMethodCallWithInjection(assisted.constructor, null, assisted.parameterNames, nameGenerator, methodsOutput);
    SourceSnippet assistedInjectMethodBody = new SourceSnippetBuilder().append(returnTypeName).append(" result = ").append(methodCall).append("\n").append(memberInjectCall).append("\nreturn result;").build();
    methodsOutput.add(SourceSnippets.asMethod(false, assistedInjectSignature, packageName, assistedInjectMethodBody));
    List<String> parameterNames = new ArrayList<String>();
    for (int i = 0; i < assisted.method.getParameterKeys().size(); ++i) {
        parameterNames.add(ReflectUtil.formatParameterName(i));
    }
    return SourceSnippets.callMethod(assistedInjectMethodName, packageName, parameterNames);
}
Also used : SourceSnippet(com.google.gwt.inject.rebind.util.SourceSnippet) SourceSnippetBuilder(com.google.gwt.inject.rebind.util.SourceSnippetBuilder) ArrayList(java.util.ArrayList) InjectionPoint(com.google.inject.spi.InjectionPoint)

Example 2 with SourceSnippet

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

the class GinjectorFragmentOutputter method writeBindingGetter.

/**
 * Writes a method describing the getter for the given key, along with any
 * other code necessary to support it.  Produces a list of helper methods that
 * still need to be written.
 */
void writeBindingGetter(Key<?> key, Binding binding, GinScope scope, List<InjectorMethod> helperMethodsOutput) {
    Context bindingContext = binding.getContext();
    SourceSnippetBuilder getterBuilder = new SourceSnippetBuilder();
    SourceSnippet creationStatements;
    String getter = nameGenerator.getGetterMethodName(key);
    String typeName;
    try {
        typeName = ReflectUtil.getSourceName(key.getTypeLiteral());
        creationStatements = binding.getCreationStatements(nameGenerator, helperMethodsOutput);
    } catch (NoSourceNameException e) {
        errorManager.logError("Error trying to write getter for [%s] -> [%s];" + " binding declaration: %s", e, key, binding, bindingContext);
        return;
    }
    // Name of the field that we might need.
    String field = nameGenerator.getSingletonFieldName(key);
    switch(scope) {
        case EAGER_SINGLETON:
            initializeEagerSingletonsBody.append("// Eager singleton bound at:\n");
            appendBindingContextCommentToMethod(bindingContext, initializeEagerSingletonsBody);
            initializeEagerSingletonsBody.append(getter).append("();\n");
        // $FALL-THROUGH$
        case SINGLETON:
            writer.println("private " + typeName + " " + field + " = null;");
            writer.println();
            getterBuilder.append(String.format("\nif (%s == null) {\n", field)).append(creationStatements).append("\n").append(String.format("    %s = result;\n", field)).append("}\n").append(String.format("return %s;\n", field));
            break;
        case NO_SCOPE:
            sourceWriteUtil.writeBindingContextJavadoc(writer, bindingContext, key);
            getterBuilder.append(creationStatements).append("\n").append("return result;\n");
            break;
        default:
            throw new IllegalStateException();
    }
    outputMethod(SourceSnippets.asMethod(false, String.format("public %s %s()", typeName, getter), fragmentPackageName.toString(), getterBuilder.build()));
}
Also used : GeneratorContext(com.google.gwt.core.ext.GeneratorContext) InjectorWriteContext(com.google.gwt.inject.rebind.util.InjectorWriteContext) Context(com.google.gwt.inject.rebind.binding.Context) SourceSnippet(com.google.gwt.inject.rebind.util.SourceSnippet) SourceSnippetBuilder(com.google.gwt.inject.rebind.util.SourceSnippetBuilder) NoSourceNameException(com.google.gwt.inject.rebind.reflect.NoSourceNameException)

Example 3 with SourceSnippet

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

the class FactoryBinding method getCreationStatements.

public SourceSnippet getCreationStatements(NameGenerator nameGenerator, List<InjectorMethod> methodsOutput) throws NoSourceNameException {
    String factoryTypeName = ReflectUtil.getSourceName(factoryType);
    SourceSnippetBuilder sb = new SourceSnippetBuilder();
    sb.append(factoryTypeName).append(" result = new ").append(factoryTypeName).append("() {");
    for (AssistData assisted : assistData) {
        // While it might seem that we could just create the return type directly
        // in the factory, that won't work.  The problem is that the return type
        // might have to be created in a different package from the factory: for
        // instance, it might inject a package-private object from its own
        // package.
        // 
        // So here's the strategy: we generate a separate injector method that,
        // given the assisted parameters, creates the return value and performs
        // member injection on it, named "assistedCreate_FACTORY_RETURNTYPE".
        // Then we create a factory method that dispatches to that injector method
        // (which, again, may be in some other injector fragment).
        String returnName = ReflectUtil.getSourceName(assisted.implementation);
        String signature = ReflectUtil.signatureBuilder(assisted.method).removeAbstractModifier().build();
        SourceSnippet methodCall = methodCallUtil.createMethodCallWithInjection(assisted.constructor, null, assisted.parameterNames, nameGenerator, methodsOutput);
        SourceSnippet assistedCreateCall = callAssistedCreate(assisted, nameGenerator, methodsOutput);
        sb.append("\n\n    ").append(signature).append(" {").append("\n      return ").append(assistedCreateCall).append(";").append(// End method.
        "\n    }");
    }
    // End factory implementation.
    sb.append("\n};");
    return sb.build();
}
Also used : SourceSnippet(com.google.gwt.inject.rebind.util.SourceSnippet) SourceSnippetBuilder(com.google.gwt.inject.rebind.util.SourceSnippetBuilder)

Aggregations

SourceSnippet (com.google.gwt.inject.rebind.util.SourceSnippet)3 SourceSnippetBuilder (com.google.gwt.inject.rebind.util.SourceSnippetBuilder)3 GeneratorContext (com.google.gwt.core.ext.GeneratorContext)1 Context (com.google.gwt.inject.rebind.binding.Context)1 NoSourceNameException (com.google.gwt.inject.rebind.reflect.NoSourceNameException)1 InjectorWriteContext (com.google.gwt.inject.rebind.util.InjectorWriteContext)1 InjectionPoint (com.google.inject.spi.InjectionPoint)1 ArrayList (java.util.ArrayList)1