Examples of SourceWriter


Examples of com.google.gwt.codegen.server.SourceWriter

    if (isInterface) {
      builder.addImplementedInterface(targetClass.getQualifiedSourceName());
    } else {
      builder.setSuperclass(targetClass.getQualifiedSourceName());
    }
    SourceWriter writer = builder.createSourceWriter();
    writer.println();
    writer.println("private " + targetClass.getQualifiedSourceName() + " instance;");
    for (JMethod method : collectOverridableMethods(targetClass)) {
      writer.println();
      if (!isInterface) {
        writer.println("@Override");
      }
      writer.println(method.getReadableDeclaration(false, true, true, true, true) + " {");
      writer.indent();
      writer.println("ensureInstance();");
      if (method.getReturnType() != JPrimitiveType.VOID) {
        writer.print("return ");
      }
      writer.print("instance." + method.getName() + '(');
      boolean first = true;
      for (JParameter param : method.getParameters()) {
        if (first) {
          first = false;
        } else {
          writer.print(", ");
        }
        writer.print(param.getName());
      }
      writer.println(");");
      writer.outdent();
      writer.println("}");
    }
    writer.println();
    writer.println("private void ensureInstance() {");
    writer.indent();
    writer.println("if (instance != null) {");
    writer.indentln("return;");
    writer.println("}");
    writer.println("String locale = " + LocaleInfo.class.getCanonicalName()
        + ".getCurrentLocale().getLocaleName();");
    String targetClassName = targetClass.getQualifiedSourceName() + '_' + locale.getAsString();
    for (Map.Entry<String, Set<GwtLocale>> entry : localeMap.entrySet()) {
      String implClassName = entry.getKey();
      if (defaultClass.equals(implClassName) || targetClassName.equals(implClassName)) {
        continue;
      }
      String prefix = "if (";
      for (GwtLocale match : entry.getValue()) {
        writer.print(prefix + CodeGenUtils.asStringLiteral(match.toString()) + ".equals(locale)");
        prefix = "\n    || ";
      }
      writer.println(") {");
      writer.indent();
      writer.println("instance = new " + implClassName + "();");
      writer.println("return;");
      writer.outdent();
      writer.println("}");
    }
    writer.println("instance = new " + defaultClass + "();");
    writer.outdent();
    writer.println("}");
    writer.close();
  }
View Full Code Here

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

        composerFactory.addImport(Serializer.class.getName());
        composerFactory.addImport(SerialMode.class.getName());
       
        composerFactory.setSuperclass(typeName);
        // TODO is the SERIALIZER required for DE RPC?
        SourceWriter sourceWriter = composerFactory.createSourceWriter(context, printWriter);
        sourceWriter.print("private Serializer SERIALIZER = new " + realize + "();");
        sourceWriter.print("protected Serializer getSerializer() {return SERIALIZER;}");
        sourceWriter.print("public SerialMode getMode() {return SerialMode." + annotation.mode().name() + ";}");
                sourceWriter.print("public SerialMode getPushMode() {return SerialMode." + annotation.pushmode().name() + ";}");
        sourceWriter.commit(logger);
       
        if (annotation.mode() == SerialMode.DE_RPC) {
          RpcDataArtifact data = new RpcDataArtifact(type.getQualifiedSourceName());
          for (JType t : typesSentToBrowser.getSerializableTypes()) {
            if (!(t instanceof JClassType)) {
View Full Code Here

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

  public String generate(TreeLogger logger, GeneratorContext context,
      String typeName) throws UnableToCompleteException {
    try {
      JClassType eventBinderType = context.getTypeOracle().getType(typeName);
      JClassType targetType = getTargetType(eventBinderType, context.getTypeOracle());
      SourceWriter writer = createSourceWriter(logger, context, eventBinderType, targetType);
      if (writer != null) { // Otherwise the class was already created
        new EventBinderWriter(
            logger,
            context.getTypeOracle().getType(GenericEvent.class.getCanonicalName()))
                .writeDoBindEventHandlers(targetType, writer, context.getTypeOracle());
        writer.commit(logger);
      }
      return getFullyQualifiedGeneratedClassName(eventBinderType);
    } catch (NotFoundException e) {
      logger.log(Type.ERROR, "Error generating " + typeName, e);
      throw new UnableToCompleteException();
View Full Code Here

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

      JClassType userPrefsType = GadgetUtils.getUserPrefsType(logger,
          typeOracle, sourceType);

      // We really use a SourceWriter since it's convenient
      SourceWriter sw = f.createSourceWriter(context, out);
      sw.println("public " + generatedSimpleSourceName + "() {");
      sw.indent();
      sw.println("nativeInit();");
      sw.println("init((" + userPrefsType.getQualifiedSourceName()
          + ")GWT.create(" + userPrefsType.getQualifiedSourceName()
          + ".class));");
      sw.outdent();
      sw.println("}");

      sw.println("private native void nativeInit() /*-{");
      sw.indent();
      for (JClassType interfaceType : sourceType.getImplementedInterfaces()) {
        generateFeatureInitializer(logger, typeOracle, sw, sourceType,
            interfaceType);
      }
      sw.outdent();
      sw.println("}-*/;");

      // Write out the manifest
      OutputStream manifestOut = context.tryCreateResource(logger, typeName
          + ".gadget.xml");
      if (manifestOut == null) {
        logger.log(TreeLogger.ERROR, "Gadget manifest was already created",
            null);
        throw new UnableToCompleteException();
      }

      generateGadgetManifest(logger, typeOracle, sourceType, new PrintWriter(
          new OutputStreamWriter(manifestOut)));
      context.commitResource(logger, manifestOut);

      sw.commit(logger);
    }

    return f.getCreatedClassName();
  }
View Full Code Here

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

    if (out != null) {
      JClassType preferenceType = typeOracle.findType(Preference.class.getName().replace('$', '.'));
      assert preferenceType != null;

      // We really use a SourceWriter since it's convenient
      SourceWriter sw = f.createSourceWriter(context, out);

      for (JMethod m : sourceType.getOverridableMethods()) {
        JClassType extendsPreferenceType = m.getReturnType().isClassOrInterface();
        if (extendsPreferenceType == null
            || !preferenceType.isAssignableFrom(extendsPreferenceType)) {
          logger.log(TreeLogger.ERROR, "Cannot assign "
              + extendsPreferenceType.getQualifiedSourceName() + " to "
              + preferenceType.getQualifiedSourceName(), null);
          throw new UnableToCompleteException();
        }

        // private final FooProperty __propName = new FooProperty() {...}
        sw.println("private final "
            + extendsPreferenceType.getParameterizedQualifiedSourceName()
            + " __" + m.getName() + " = ");
        sw.indent();
        writeInstantiation(logger, sw, extendsPreferenceType, m);
        sw.println(";");
        sw.outdent();

        // public FooProperty property() { return __property; }
        sw.print("public ");
        sw.print(m.getReadableDeclaration(true, true, true, true, true));
        sw.println("{");
        sw.indent();
        sw.println("return __" + m.getName() + ";");
        sw.outdent();
        sw.println("}");
      }

      sw.commit(logger);
    }

    return f.getCreatedClassName();
  }
View Full Code Here

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

  @Override
  public void writeAssignment(TreeLogger logger, JMethod method)
      throws UnableToCompleteException {
    String name = method.getName();

    SourceWriter sw = context.getSourceWriter();
    sw.println("new " + ImageResourcePrototype.class.getName() + "(");
    sw.indent();
    sw.println('"' + name + "\",");

    ImageBundleBuilder.ImageRect rect;
    if (!externalImageRects.containsKey(name)) {
      // This is a reference to a field
      sw.println("imageResourceBundleUrl,");
      rect = builder.getMapping(method.getName());
    } else {
      sw.println(externalLocationExpressions.get(name).toString() + ", ");
      rect = externalImageRects.get(name);
    }

    if (rect == null) {
      logger.log(TreeLogger.ERROR, "No ImageRect ever computed for " + name,
          null);
      throw new UnableToCompleteException();
    }

    sw.println(rect.left + ", 0, " + rect.width + ", " + rect.height);

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

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

  public void writeFields(TreeLogger logger) throws UnableToCompleteException {
    if (imageStripCount > 0) {
      String bundleUrlExpression = builder.writeBundledImage(logger.branch(
          TreeLogger.DEBUG, "Writing image strip", null), context);

      SourceWriter sw = context.getSourceWriter();
      sw.println("private String imageResourceBundleUrl = "
          + bundleUrlExpression + ";");
    }
  }
View Full Code Here

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

    // If an implementation already exists, we don't need to do any work
    if (out != null) {

      // We really use a SourceWriter since it's convenient
      final SourceWriter sw = f.createSourceWriter(context, out);

      JMethod[] methods = sourceType.getMethods();

      Map<Class<? extends ResourceGenerator>, List<JMethod>> resourceGenerators = new HashMap<Class<? extends ResourceGenerator>, List<JMethod>>();

      ResourceContext resourceContext = createResourceContext(logger, context,
          sourceType, sw);

      // First assemble all of the ResourceGenerators that we may need for the
      // type
      for (JMethod m : methods) {
        JClassType returnType = m.getReturnType().isClassOrInterface();
        if (returnType == null) {
          logger.log(TreeLogger.ERROR, "Cannot implement " + m.getName()
              + ": not a class or interface.", null);
          throw new UnableToCompleteException();
        }

        Class<? extends ResourceGenerator> clazz = findResourceGenerator(
            logger, typeOracle, m);
        List<JMethod> generatorMethods;
        if (resourceGenerators.containsKey(clazz)) {
          generatorMethods = resourceGenerators.get(clazz);
        } else {
          generatorMethods = new ArrayList<JMethod>();
          resourceGenerators.put(clazz, generatorMethods);
        }

        generatorMethods.add(m);
      }

      // Run the ResourceGenerator code
      for (Map.Entry<Class<? extends ResourceGenerator>, List<JMethod>> entry : resourceGenerators.entrySet()) {
        Class<? extends ResourceGenerator> generatorClass = entry.getKey();
        List<JMethod> generatorMethods = entry.getValue();

        // Create the ResourceGenerator
        ResourceGenerator rg;
        try {
          rg = generatorClass.newInstance();
          rg.init(logger.branch(TreeLogger.DEBUG,
              "Initializing ResourceGenerator", null), resourceContext);
        } catch (InstantiationException e) {
          logger.log(TreeLogger.ERROR,
              "Unable to initialize ResourceGenerator", e);
          throw new UnableToCompleteException();
        } catch (IllegalAccessException e) {
          logger.log(TreeLogger.ERROR,
              "Unable to instantiate ResourceGenerator. "
                  + "Does it have a public default constructor?", e);
          throw new UnableToCompleteException();
        }

        // Prepare the ResourceGenerator by telling it all methods that it is
        // expected to produce.
        for (JMethod m : generatorMethods) {
          rg.prepare(logger.branch(TreeLogger.DEBUG, "Preparing method "
              + m.getName(), null), m);
        }

        // Write all field values
        rg.writeFields(logger.branch(TreeLogger.DEBUG, "Writing fields", null));

        // Create the instance variables in the IRB subclass by calling
        // writeAssignment() on the ResourceGenerator
        for (JMethod m : generatorMethods) {
          // Strip off all but the access modifiers
          sw.print(m.getReadableDeclaration(false, true, true, true, true));
          sw.println(" {");
          sw.indent();

          String fieldName = (m.getName() + "_instance").toUpperCase();
          fieldNames.add(fieldName);
          sw.println("return " + fieldName + ";");

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

          sw.print("private final "
              + m.getReturnType().getQualifiedSourceName() + " " + fieldName
              + " = ");

          rg.writeAssignment(logger.branch(TreeLogger.DEBUG,
              "Writing assignment for " + m.getName(), null), m);

          sw.println(";");
        }

        // Finalize the ResourceGenerator
        rg.finish(logger.branch(TreeLogger.DEBUG,
            "Finishing ResourceGenerator", null));
      }

      // Complete the IRB contract
      sw.println("public ResourcePrototype[] getResources() {");
      sw.indent();
      sw.println("return new ResourcePrototype[] {");
      sw.indent();
      for (String fieldName : fieldNames) {
        sw.println(fieldName + ",");
      }
      sw.outdent();
      sw.println("};");
      sw.outdent();
      sw.println("}");

      // Allow map-style lookup
      sw.println("public native ResourcePrototype "
          + "getResource(String name) /*-{");
      sw.indent();
      sw.println("switch (name) {");
      sw.indent();
      for (JMethod m : methods) {
        sw.println("case '" + m.getName() + "': return this.@"
            + f.getCreatedClassName() + "::"
            + (m.getName() + "_instance").toUpperCase() + ";");
      }
      sw.outdent();
      sw.println("}");
      sw.println("return null;");
      sw.outdent();
      sw.println("}-*/;");

      // Write the generated code to disk
      sw.commit(logger);
    }

    // Return the name of the concrete class
    return f.getCreatedClassName();
  }
View Full Code Here

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

  @Override
  public void writeAssignment(TreeLogger logger, JMethod method)
      throws UnableToCompleteException {
    String name = method.getName();

    SourceWriter sw = context.getSourceWriter();
    sw.println("new " + ExternalTextResourcePrototype.class.getName() + "(");
    sw.indent();
    sw.println('"' + name + "\",");
    // These are field names
    sw.println("EXTERNAL_TEXT_URL, EXTERNAL_TEXT_CACHE,");
    sw.println(offsets.get(method.getName()).toString());
    sw.outdent();
    sw.print(")");
  }
View Full Code Here

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

    urlExpression = context.addToOutput(
        context.getResourceBundleType().getQualifiedSourceName().replace('.',
            '_')
            + "_jsonbundle.txt", "text/plain", data.toString().getBytes(), true);

    SourceWriter sw = context.getSourceWriter();
    sw.println("final String EXTERNAL_TEXT_URL = " + urlExpression + ";");
    sw.println("final " + TextResource.class.getName()
        + "[] EXTERNAL_TEXT_CACHE = new " + TextResource.class.getName() + "["
        + currentIndex + "];");
  }
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.