Search in sources :

Example 1 with NoSourceNameException

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

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

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

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

NoSourceNameException (com.google.gwt.inject.rebind.reflect.NoSourceNameException)4 InjectorMethod (com.google.gwt.inject.rebind.util.InjectorMethod)2 SourceSnippetBuilder (com.google.gwt.inject.rebind.util.SourceSnippetBuilder)2 ArrayList (java.util.ArrayList)2 GeneratorContext (com.google.gwt.core.ext.GeneratorContext)1 GinjectorNameGenerator (com.google.gwt.inject.rebind.GinjectorNameGenerator)1 Context (com.google.gwt.inject.rebind.binding.Context)1 FieldLiteral (com.google.gwt.inject.rebind.reflect.FieldLiteral)1 InjectorWriteContext (com.google.gwt.inject.rebind.util.InjectorWriteContext)1 NameGenerator (com.google.gwt.inject.rebind.util.NameGenerator)1 SourceSnippet (com.google.gwt.inject.rebind.util.SourceSnippet)1 SourceWriteUtil (com.google.gwt.inject.rebind.util.SourceWriteUtil)1 ClassSourceFileComposerFactory (com.google.gwt.user.rebind.ClassSourceFileComposerFactory)1 SourceWriter (com.google.gwt.user.rebind.SourceWriter)1 InjectionPoint (com.google.inject.spi.InjectionPoint)1 Field (java.lang.reflect.Field)1 Member (java.lang.reflect.Member)1 Method (java.lang.reflect.Method)1