Examples of SourceWriter


Examples of com.google.gwt.user.rebind.SourceWriter

   * Create instances of {@link SoundResourcePrototype}.
   */
  @Override
  public void writeAssignment(TreeLogger logger, JMethod method)
      throws UnableToCompleteException {
    SourceWriter sw = context.getSourceWriter();

    sw.print("new ");
    sw.print(SoundResourcePrototype.class.getName());
    sw.println("(");
    sw.indent();
    sw.print("FLASH_ELEMENT, \"");
    sw.print(method.getName());
    sw.print("\", ");
    sw.print(String.valueOf(builder.getIndex(method.getName())));
    sw.print(", ");
    sw.print(String.valueOf(builder.getDuration(method.getName())));
    sw.outdent();
    sw.println(")");
  }
View Full Code Here

Examples of com.google.gwt.user.rebind.SourceWriter

      urlExpression = builder.writeBundle(logger, context);
    } else {
      urlExpression = "null";
    }

    SourceWriter sw = context.getSourceWriter();
    sw.print("private final ");
    sw.print(Element.class.getName());
    sw.print(" FLASH_ELEMENT = ");
    sw.print(SoundResourcePrototype.class.getName());
    sw.print(".attach(");
    sw.print(urlExpression);
    sw.println(");");
  }
View Full Code Here

Examples of com.google.gwt.user.rebind.SourceWriter

    }

    URL resource = resources[0];
    String outputUrlExpression = context.addToOutput(resource, false);

    SourceWriter sw = context.getSourceWriter();
    // Write the expression to create the subtype.
    sw.println("new " + DataResource.class.getName() + "() {");
    sw.indent();

    // Convenience when examining the generated code.
    sw.println("// " + resource.toExternalForm());

    sw.println("public String getUrl() {");
    sw.indent();
    sw.println("return " + outputUrlExpression + ";");
    sw.outdent();
    sw.println("}");

    sw.println("public String getName() {");
    sw.indent();
    sw.println("return \"" + method.getName() + "\";");
    sw.outdent();
    sw.println("}");

    sw.outdent();
    sw.println("}");
  }
View Full Code Here

Examples of com.google.gwt.user.rebind.SourceWriter

      throw new UnableToCompleteException();
    }

    URL resource = resources[0];

    SourceWriter sw = context.getSourceWriter();
    // Write the expression to create the subtype.
    sw.println("new " + TextResource.class.getName() + "() {");
    sw.indent();

    // Convenience when examining the generated code.
    sw.println("// " + resource.toExternalForm());

    sw.println("public String getText() {");
    sw.indent();

    TreeLogger transformLogger =
        logger.branch(TreeLogger.DEBUG, "Applying Transformers", null);
    String toWrite =
        ResourceGeneratorUtil.applyTransformations(transformLogger, method,
            String.class, new String(Util.readURLAsChars(resource)));

    sw.println("return \"" + Generator.escape(toWrite).replaceAll("\0", "\\0")
        + "\";");
    sw.outdent();
    sw.println("}");

    sw.println("public String getName() {");
    sw.indent();
    sw.println("return \"" + method.getName() + "\";");
    sw.outdent();
    sw.println("}");

    sw.outdent();
    sw.println("}");
  }
View Full Code Here

Examples of com.google.gwt.user.rebind.SourceWriter

    TreeLogger logger = context.parentLogger.branch(TreeLogger.DEBUG,
        "Writing binding function", null);
    context = new FragmentGeneratorContext(context);
    context.parentLogger = logger;

    SourceWriter sw = context.sw;
    TypeOracle typeOracle = context.typeOracle;
    Binding bindingAnnotation = JSWrapperGenerator.hasTag(logger, binding,
        Binding.class);

    // Write the java half to add assertions to the code. These will be elided
    // in web mode, and the method become a pure delegation, allowing it to
    // be removed completely
    sw.print("public void ");
    sw.print(binding.getName());
    sw.print("(");
    JParameter[] params = binding.getParameters();

    context.parameterName = "jso";
    sw.print(params[0].getType().getQualifiedSourceName());
    sw.print(" ");
    sw.print(context.parameterName);

    JClassType bindingType = null;
    if (params.length == 2) {
      // Infer the binding type from the second parameter of the binding
      // method.
      bindingType = params[1].getType().isClassOrInterface();
      context.objRef = "obj";

      sw.print(", ");
      sw.print(bindingType.getQualifiedSourceName());
      sw.print(" ");
      sw.print(context.objRef);
    } else if (bindingAnnotation != null
        && bindingAnnotation.value().length() > 0) {
      // Use the binding type specified in the the gwt.binding annotation.
      bindingType = typeOracle.findType(bindingAnnotation.value());
      if (bindingType == null) {
        logger.log(TreeLogger.ERROR, "Could not resolve binding type "
            + bindingType, null);
        throw new UnableToCompleteException();
      }
    }

    sw.println(") {");
    sw.indent();

    for (Task t : context.tasks) {
      if (t.imported != null) {
        String fieldName = t.getFieldName(logger);
        sw.print("assert JSONWrapperUtil.hasField(");
        sw.print(context.parameterName);
        sw.print(", \"");
        sw.print(fieldName);
        sw.print("\") : \"Backing JSO missing imported function ");
        sw.print(fieldName);
        sw.println("\";");
      }
    }

    sw.print(binding.getName());
    sw.print("Native (");
    sw.print(context.parameterName);
    if (params.length == 2) {
      sw.print(",");
      sw.print(context.objRef);
    }
    sw.println(");");
    sw.outdent();
    sw.println("}");

    // Write the native half to perform the actual binding operations
    sw.print("public native void ");
    sw.print(binding.getName());
    sw.print("Native (");
    sw.print(params[0].getType().getQualifiedSourceName());
    sw.print(" ");
    sw.print(context.parameterName);
    if (params.length == 2) {
      sw.print(", ");
      sw.print(bindingType.getQualifiedSourceName());
      sw.print(" ");
      sw.print(context.objRef);
    }
    sw.println(") /*-{");
    sw.indent();

    // A binding should have been declared void
    context.returnType = JPrimitiveType.VOID;

    if (context.maintainIdentity && params.length == 2) {
      // XXX link the Java object to the JSO?

      // Verify that the incoming object doesn't already have a wrapper object.
      // If there is a backreference, throw an exception.
      sw.print("if (");
      sw.print(context.parameterName);
      sw.print(".");
      sw.print(BACKREF);
      sw.println(") {");
      sw.indent();
      sw.println("@com.google.gwt.jsio.client.impl.JSONWrapperUtil::throwMultipleWrapperException()();");
      sw.outdent();
      sw.println("}");

      // Assign the backreference from the JSO object to the delegate
      sw.print(context.parameterName);
      sw.print(".");
      sw.print(BACKREF);
      sw.print(" = ");
      sw.print(context.objRef);
      sw.println(";");
    }

    writeEmptyFieldInitializers(context);

    if (bindingType != null) {
      // Extract the exported methods
      context.tasks = TaskFactory.extractMethods(logger, typeOracle,
          bindingType, TaskFactory.EXPORTER_POLICY).values();
      writeMethodBindings(context);
    } else {
      logger.log(TreeLogger.DEBUG,
          "Not binding methods to any particular type.", null);
    }

    sw.outdent();
    sw.println("}-*/;");
  }
View Full Code Here

Examples of com.google.gwt.user.rebind.SourceWriter

  protected void writeConstructor(FragmentGeneratorContext context,
      JMethod constructor) throws UnableToCompleteException {

    TreeLogger logger = context.parentLogger.branch(TreeLogger.DEBUG,
        "Writing constructor " + constructor.getName(), null);
    SourceWriter sw = context.sw;

    JParameter[] parameters = constructor.getParameters();
    if (parameters == null) {
      parameters = new JParameter[0];
    }

    // Method declaration
    sw.print("public native ");
    sw.print(constructor.getReturnType().getQualifiedSourceName());
    sw.print(" ");
    sw.print(constructor.getName());
    sw.print("(");
    for (int i = 0; i < parameters.length; i++) {
      JType returnType = parameters[i].getType();
      JParameterizedType pType = returnType.isParameterized();

      if (pType != null) {
        sw.print(pType.getRawType().getQualifiedSourceName());
      } else {
        sw.print(returnType.getQualifiedSourceName());
      }

      sw.print(" ");
      sw.print(parameters[i].getName());

      if (i < parameters.length - 1) {
        sw.print(", ");
      }
    }
    sw.print(")");
    sw.println(" /*-{");
    sw.indent();

    JType returnType = constructor.getReturnType();

    FragmentGeneratorContext subContext = new FragmentGeneratorContext(context);
    subContext.returnType = returnType;
    subContext.parameterName = "jsReturn";
    subContext.objRef = "jsReturn";

    sw.print("var ");
    sw.print(subContext.objRef);
    sw.print(" = ");

    Constructor constructorAnnotation = hasTag(logger, constructor,
        Constructor.class);
    Global globalAnnotation = hasTag(logger, constructor, Global.class);
    if (constructorAnnotation != null) {
      // If the imported method is acting as an invocation of a JavaScript
      // constructor, use the new Foo() syntax, otherwise treat is an an
      // invocation on a field on the underlying JSO.
      sw.print("new ");
      sw.print(constructorAnnotation.value());

      // Write the invocation's parameter list
      sw.print("(");
      for (int i = 0; i < parameters.length; i++) {
        // Create a sub-context to generate the wrap/unwrap logic
        JType subType = parameters[i].getType();
        FragmentGeneratorContext subParams = new FragmentGeneratorContext(
            context);
        subParams.returnType = subType;
        subParams.parameterName = parameters[i].getName();

        FragmentGenerator fragmentGenerator = context.fragmentGeneratorOracle.findFragmentGenerator(
            logger, context.typeOracle, subType);
        if (fragmentGenerator == null) {
          logger.log(TreeLogger.ERROR, "No fragment generator for "
              + returnType.getQualifiedSourceName(), null);
          throw new UnableToCompleteException();
        }

        fragmentGenerator.toJS(subParams);

        if (i < parameters.length - 1) {
          sw.print(", ");
        }
      }
      sw.print(")");

    } else if (globalAnnotation != null) {
      sw.print(globalAnnotation.value());

    } else {
      logger.log(TreeLogger.ERROR,
          "Writing a constructor, but no constructor-appropriate annotations",
          null);
      throw new UnableToCompleteException();
    }
    sw.println(";");

    writeEmptyFieldInitializers(subContext);

    sw.print("return ");
    sw.print(subContext.objRef);
    sw.println(";");

    sw.outdent();
    sw.println("}-*/;");
  }
View Full Code Here

Examples of com.google.gwt.user.rebind.SourceWriter

  @Override
  void fromJS(FragmentGeneratorContext context)
      throws UnableToCompleteException {
    context.parentLogger.branch(TreeLogger.DEBUG,
        "Building primitive value getter statement", null);
    SourceWriter sw = context.sw;

    sw.print(context.parameterName);
  }
View Full Code Here

Examples of com.google.gwt.user.rebind.SourceWriter

 
  @Override
  void toJS(FragmentGeneratorContext context) throws UnableToCompleteException {
    context.parentLogger.branch(TreeLogger.DEBUG,
        "Building primitive value setter statement", null);
    SourceWriter sw = context.sw;

    sw.print(context.parameterName);
  }
View Full Code Here

Examples of com.google.gwt.user.rebind.SourceWriter

  /**
   * Simply prints a JSNI reference to the exported function.
   */
  private static void writeIdentityInvocation(FragmentGeneratorContext context,
      JMethod m) throws UnableToCompleteException {
    SourceWriter sw = context.sw;
    JParameter[] parameters = m.getParameters();

    sw.print("@");
    sw.print(m.getEnclosingType().getQualifiedSourceName());
    sw.print("::");
    sw.print(m.getName());
    sw.print("(");

    // Argument list for the Java invocation
    for (int i = 0; i < parameters.length; i++) {
      sw.print(parameters[i].getType().getJNISignature());
    }

    sw.print(")");
  }
View Full Code Here

Examples of com.google.gwt.user.rebind.SourceWriter

  private static void writeLinkageInvocation(FragmentGeneratorContext context,
      JMethod m) throws UnableToCompleteException {
    TreeLogger logger = context.parentLogger.branch(TreeLogger.DEBUG,
        "Writing function() for " + m.getName(), null);

    SourceWriter sw = context.sw;
    JParameter[] parameters = m.getParameters();
    FragmentGeneratorOracle fgo = context.fragmentGeneratorOracle;
    FragmentGenerator returnFragmentGenerator = fgo.findFragmentGenerator(
        logger, context.typeOracle, m.getReturnType());

    sw.print("function(");
    for (int i = 0; i < parameters.length; i++) {
      sw.print("arg");
      sw.print(String.valueOf(i));
      if (i < parameters.length - 1) {
        sw.print(", ");
      }
    }
    sw.println(") {");
    sw.indent();

    if (returnFragmentGenerator.isIdentity()) {
      sw.print("return ");
    } else {
      sw.print("var javaReturn = ");
    }

    // Don't need to reference the instance on a static method
    if (!m.isStatic()) {
      sw.print(context.parameterName);
      sw.print(".");
    }

    sw.print("@");
    sw.print(m.getEnclosingType().getQualifiedSourceName());
    sw.print("::");
    sw.print(m.getName());
    sw.print("(");

    // Argument list for the Java invocation
    for (int i = 0; i < parameters.length; i++) {
      sw.print(parameters[i].getType().getJNISignature());
    }

    sw.println(")(");
    // Indent the parameters, each on its own like to improve readability
    sw.indent();
    sw.indent();

    for (int i = 0; i < parameters.length; i++) {
      // Create a sub-context to generate the wrap/unwrap logic
      JType returnType = parameters[i].getType();
      FragmentGeneratorContext subParams = new FragmentGeneratorContext(context);
      subParams.returnType = returnType;
      subParams.parameterName = "arg" + i;

      FragmentGenerator fragmentGenerator = fgo.findFragmentGenerator(logger,
          context.typeOracle, returnType);
      if (fragmentGenerator == null) {
        logger.log(TreeLogger.ERROR, "No fragment generator for "
            + returnType.getQualifiedSourceName(), null);
        throw new UnableToCompleteException();
      }

      fragmentGenerator.fromJS(subParams);

      if (i < parameters.length - 1) {
        sw.println(", ");
      }
    }

    sw.outdent();
    sw.outdent();
    sw.println(");");

    if (!returnFragmentGenerator.isIdentity()) {
      FragmentGeneratorContext returnContext = new FragmentGeneratorContext(
          context);
      returnContext.parameterName = "javaReturn";
      returnContext.returnType = m.getReturnType();
      sw.print("return ");
      returnFragmentGenerator.toJS(returnContext);
      sw.println(";");
    }

    sw.outdent();
    sw.print("}");
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.