Search in sources :

Example 1 with SourceSnippetBuilder

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

the class GinjectorBindingsOutputter method outputStaticInjectionMethods.

/**
 * Outputs all the static injection methods for the given class.
 */
void outputStaticInjectionMethods(Class<?> type, FragmentMap fragments, NameGenerator nameGenerator, SourceWriteUtil sourceWriteUtil) {
    String methodName = nameGenerator.convertToValidMemberName("injectStatic_" + type.getName());
    SourceSnippetBuilder body = new SourceSnippetBuilder();
    for (InjectionPoint injectionPoint : InjectionPoint.forStaticMethodsAndFields(type)) {
        Member member = injectionPoint.getMember();
        try {
            List<InjectorMethod> staticInjectionHelpers = new ArrayList<InjectorMethod>();
            if (member instanceof Method) {
                MethodLiteral<?, Method> method = MethodLiteral.get((Method) member, TypeLiteral.get(member.getDeclaringClass()));
                body.append(methodCallUtil.createMethodCallWithInjection(method, null, nameGenerator, staticInjectionHelpers));
            } else if (member instanceof Field) {
                FieldLiteral<?> field = FieldLiteral.get((Field) member, TypeLiteral.get(member.getDeclaringClass()));
                body.append(sourceWriteUtil.createFieldInjection(field, null, nameGenerator, staticInjectionHelpers));
            }
            outputMethods(staticInjectionHelpers, fragments);
        } catch (NoSourceNameException e) {
            errorManager.logError(e.getMessage(), e);
        }
    }
    // Note that the top-level method that performs static injection will only
    // invoke a bunch of other injector methods.  Therefore, it doesn't matter
    // which package it goes in, and we don't need to invoke getUserPackageName
    // (which is good, because in practice users statically inject types that
    // have no user package name because they're private inner classes!)
    String packageName = type.getPackage().getName();
    InjectorMethod method = SourceSnippets.asMethod(false, "private void " + methodName + "()", packageName, body.build());
    GinjectorFragmentOutputter fragment = fragments.get(fragmentPackageNameFactory.create(packageName));
    fragment.outputMethod(method);
    fragment.invokeInInitializeStaticInjections(methodName);
}
Also used : InjectionPoint(com.google.inject.spi.InjectionPoint) InjectorMethod(com.google.gwt.inject.rebind.util.InjectorMethod) NoSourceNameException(com.google.gwt.inject.rebind.reflect.NoSourceNameException) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) InjectorMethod(com.google.gwt.inject.rebind.util.InjectorMethod) Field(java.lang.reflect.Field) FieldLiteral(com.google.gwt.inject.rebind.reflect.FieldLiteral) SourceSnippetBuilder(com.google.gwt.inject.rebind.util.SourceSnippetBuilder) Member(java.lang.reflect.Member)

Example 2 with SourceSnippetBuilder

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

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

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

Example 5 with SourceSnippetBuilder

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

the class ImplicitProviderBinding method getCreationStatements.

public SourceSnippet getCreationStatements(NameGenerator nameGenerator, List<InjectorMethod> methodsOutput) throws NoSourceNameException {
    String providerTypeName = ReflectUtil.getSourceName(providerType);
    String targetKeyName = ReflectUtil.getSourceName(targetKey.getTypeLiteral());
    return new SourceSnippetBuilder().append(providerTypeName).append(" result = new ").append(providerTypeName).append("() { \n").append("  public ").append(targetKeyName).append(" get() { \n").append("    return ").append(SourceSnippets.callGetter(targetKey)).append(";\n").append("  }\n").append("};").build();
}
Also used : SourceSnippetBuilder(com.google.gwt.inject.rebind.util.SourceSnippetBuilder)

Aggregations

SourceSnippetBuilder (com.google.gwt.inject.rebind.util.SourceSnippetBuilder)5 SourceSnippet (com.google.gwt.inject.rebind.util.SourceSnippet)3 NoSourceNameException (com.google.gwt.inject.rebind.reflect.NoSourceNameException)2 InjectionPoint (com.google.inject.spi.InjectionPoint)2 ArrayList (java.util.ArrayList)2 GeneratorContext (com.google.gwt.core.ext.GeneratorContext)1 Context (com.google.gwt.inject.rebind.binding.Context)1 FieldLiteral (com.google.gwt.inject.rebind.reflect.FieldLiteral)1 InjectorMethod (com.google.gwt.inject.rebind.util.InjectorMethod)1 InjectorWriteContext (com.google.gwt.inject.rebind.util.InjectorWriteContext)1 Field (java.lang.reflect.Field)1 Member (java.lang.reflect.Member)1 Method (java.lang.reflect.Method)1