Package com.google.gwt.inject.rebind.util

Examples of com.google.gwt.inject.rebind.util.SourceSnippetBuilder


   * 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);
  }
View Full Code Here


   */
  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()));
  }
View Full Code Here

  public SourceSnippet getCreationStatements(NameGenerator nameGenerator,
      List<InjectorMethod> methodsOutput) throws NoSourceNameException {
    String type = ReflectUtil.getSourceName(key.getTypeLiteral());

    return new SourceSnippetBuilder()
        .append(type).append(" result = ").append(valueToOutput).append(";")
        .build();
  }
View Full Code Here

  }

  @Override protected final SourceSnippet getCreationStatement(NameGenerator nameGenerator,
      List<InjectorMethod> methodsOutput) throws NoSourceNameException {

    return new SourceSnippetBuilder()
        .append("Object created = GWT.create(").append(getTypeNameToCreate()).append(".class);\n")
        // Gin cannot deal with cases where the type returned by GWT.create is
        // not equal or a subtype of the requested type. Assert this here, in
        // production code (without asserts) the line below the assert will
        // throw a ClassCastException instead.
View Full Code Here

  public SourceSnippet getCreationStatements(NameGenerator nameGenerator,
      List<InjectorMethod> methodsOutput) throws NoSourceNameException {
    String type = ReflectUtil.getSourceName(ginjectorInterface);

    return new SourceSnippetBuilder()
        .append(type).append(" result = ").append(SourceSnippets.callGinjectorInterfaceGetter())
        .append(";")
        .build();
  }
View Full Code Here

    addParamTypes(constructor);
  }

  @Override protected SourceSnippet getCreationStatement(NameGenerator nameGenerator,
      List<InjectorMethod> methodsOutput) throws NoSourceNameException {
    return new SourceSnippetBuilder()
        .append(getTypeName()).append(" result = ")
        .append(
            methodCallUtil.createConstructorInjection(constructor, nameGenerator, methodsOutput))
        .build();
  }
View Full Code Here

    dependencies.addAll(guiceUtil.getMemberInjectionDependencies(Key.get(type), type));
  }

  public SourceSnippet getCreationStatements(NameGenerator nameGenerator,
      List<InjectorMethod> methodsOutput) throws NoSourceNameException {
    return new SourceSnippetBuilder()
        .append(getCreationStatement(nameGenerator, methodsOutput))
        .append("\n")
        .append(SourceSnippets.callMemberInject(type, "result"))
        .append("\n")
        .build();
View Full Code Here

  public SourceSnippet getCreationStatements(NameGenerator nameGenerator,
      List<InjectorMethod> methodsOutput) throws NoSourceNameException {
    String type = ReflectUtil.getSourceName(key.getTypeLiteral());

    return new SourceSnippetBuilder()
        .append(type).append(" result = ")
        .append(SourceSnippets.callParentGetter(key, parentBindings)).append(";")
        .build();
  }
View Full Code Here

  public SourceSnippet getCreationStatements(NameGenerator nameGenerator,
      List<InjectorMethod> methodsOutput) throws NoSourceNameException {
    String typeName = ReflectUtil.getSourceName(key.getTypeLiteral());

    return new SourceSnippetBuilder()
        .append(typeName).append(" result = ")
        .append(SourceSnippets.callChildGetter(childBindings, key)).append(";")
        .build();
  }
View Full Code Here

  }

  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("\n    }"); // End method.
    }

    sb.append("\n};"); // End factory implementation.

    return sb.build();
  }
View Full Code Here

TOP

Related Classes of com.google.gwt.inject.rebind.util.SourceSnippetBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.