Example usage for com.google.gwt.user.rebind SourceWriter outdent

List of usage examples for com.google.gwt.user.rebind SourceWriter outdent

Introduction

In this page you can find the example usage for com.google.gwt.user.rebind SourceWriter outdent.

Prototype

void outdent();

Source Link

Usage

From source file:org.rapla.rest.gwtjsonrpc.rebind.ProxyCreator.java

License:Apache License

private void generateProxyMethod(@SuppressWarnings("unused") final TreeLogger logger, final JMethod method,
        final SourceWriter w) {
    final JParameter[] params = method.getParameters();
    final JType callback = method.getReturnType();// params[params.length - 1];
    JType resultType = callback;//from   w w w .  j  a v a 2  s . c o m
    //    final JClassType resultType =
    //        callback.isParameterized().getTypeArgs()[0];
    final String[] serializerFields = new String[params.length];
    String resultField = "";

    w.println();
    for (int i = 0; i < params.length /*- 1*/; i++) {
        final JType pType = params[i].getType();
        if (SerializerCreator.needsTypeParameter(pType)) {
            serializerFields[i] = "serializer_" + instanceField++;
            w.print("private static final ");
            if (pType.isArray() != null)
                w.print(serializerCreator.serializerFor(pType));
            else
                w.print(JsonSerializer.class.getName());
            w.print(" ");
            w.print(serializerFields[i]);
            w.print(" = ");
            serializerCreator.generateSerializerReference(pType, w, false);
            w.println(";");
        }
    }
    JClassType parameterizedResult = null;
    if (resultType.isParameterized() != null) {
        resultField = "serializer_" + instanceField++;
        w.print("private static final ");
        w.print(ResultDeserializer.class.getName());
        w.print(" ");
        w.print(resultField);
        w.print(" = ");
        parameterizedResult = resultType.isParameterized().getTypeArgs()[0];
        serializerCreator.generateSerializerReference(parameterizedResult, w, false);
        w.println(";");
    }

    w.print("public ");
    w.print(method.getReturnType().getQualifiedSourceName());
    w.print(" ");
    w.print(method.getName());
    w.print("(");
    boolean needsComma = false;
    final NameFactory nameFactory = new NameFactory();
    for (int i = 0; i < params.length; i++) {
        final JParameter param = params[i];

        if (needsComma) {
            w.print(", ");
        } else {
            needsComma = true;
        }

        final JType paramType = param.getType().getErasedType();
        w.print(paramType.getQualifiedSourceName());
        w.print(" ");

        nameFactory.addName(param.getName());
        w.print(param.getName());
    }

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

    if (returnsCallbackHandle(method)) {
        w.print("return new ");
        w.print(CallbackHandle.class.getName());
        w.print("(");
        if (SerializerCreator.needsTypeParameter(resultType)) {
            w.print(resultField);
        } else {
            deserializerCreator.generateDeserializerReference(resultType, w);
        }
        w.print(", " + "null" // callback.getName()
        );
        w.println(");");
        w.outdent();
        w.println("}");
        return;
    }

    //    final HostPageCache hpc = method.getAnnotation(HostPageCache.class);
    //    if (hpc != null) {
    //      final String objName = nameFactory.createName("cached");
    //      w.print("final JavaScriptObject " + objName + " = ");
    //      w.print(AbstractJsonProxy.class.getName());
    //      w.print(".");
    //      w.print(hpc.once() ? "hostPageCacheGetOnce" : "hostPageCacheGetMany");
    //      w.println("(\"" + hpc.name() + "\");");
    //      w.println("if (" + objName + " != null) {");
    //      w.indent();
    //      w.print(JsonUtil.class.getName());
    //      w.print(".invoke(");
    //      if (SerializerCreator.needsTypeParameter(resultType)) {
    //        w.print(resultField);
    //      } else {
    //        deserializerCreator.generateDeserializerReference(resultType, w);
    //      }
    //     // w.print(", " + callback.getName());
    //      w.print(", " + "null");
    //      w.print(", " + objName);
    //      w.println(");");
    //      w.println("return;");
    //      w.outdent();
    //      w.println("}");
    //    }

    final String reqDataStr;
    if (params.length == 0) {
        reqDataStr = "\"[]\"";
    } else {
        final String reqData = nameFactory.createName("reqData");
        w.println("final StringBuilder " + reqData + " = new StringBuilder();");
        needsComma = false;
        w.println(reqData + ".append('[');");
        for (int i = 0; i < params.length; i++) {
            if (needsComma) {
                w.println(reqData + ".append(\",\");");
            } else {
                needsComma = true;
            }

            final JType pType = params[i].getType();
            final String pName = params[i].getName();
            if (pType == JPrimitiveType.CHAR || SerializerCreator.isBoxedCharacter(pType)) {
                w.println(reqData + ".append(\"\\\"\");");
                w.println(reqData + ".append(" + JsonSerializer.class.getSimpleName() + ".escapeChar(" + pName
                        + "));");
                w.println(reqData + ".append(\"\\\"\");");
            } else if ((SerializerCreator.isJsonPrimitive(pType) || SerializerCreator.isBoxedPrimitive(pType))
                    && !SerializerCreator.isJsonString(pType)) {
                w.println(reqData + ".append(" + pName + ");");
            } else {
                w.println("if (" + pName + " != null) {");
                w.indent();
                if (SerializerCreator.needsTypeParameter(pType)) {
                    w.print(serializerFields[i]);
                } else {
                    serializerCreator.generateSerializerReference(pType, w, false);
                }
                w.println(".printJson(" + reqData + ", " + pName + ");");
                w.outdent();
                w.println("} else {");
                w.indent();
                w.println(reqData + ".append(" + JsonSerializer.class.getName() + ".JS_NULL);");
                w.outdent();
                w.println("}");
            }
        }
        w.println(reqData + ".append(']');");
        reqDataStr = reqData + ".toString()";
    }

    String resultClass = futureResultClassName;
    if (parameterizedResult != null) {
        resultClass += "<" + parameterizedResult.getQualifiedSourceName() + ">";
    }
    w.println(resultClass + " result = new " + resultClass + "();");
    w.print("doInvoke(");
    w.print("\"" + method.getName() + "\"");
    w.print(", " + reqDataStr);
    w.print(", ");
    if (resultType.isParameterized() != null) {
        w.print(resultField);
    } else {
        deserializerCreator.generateDeserializerReference(resultType, w);
    }

    //w.print(", " + callback.getName());
    w.print(", result");

    w.println(");");
    w.println("return result;");

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

From source file:org.rapla.rest.gwtjsonrpc.rebind.ResultDeserializerCreator.java

License:Apache License

private void generateFromResult(SourceWriter w) {
    final String ctn = componentType.getQualifiedSourceName();

    w.println("@Override");
    w.print("public " + ctn + "[] ");
    w.println("fromResult(JavaScriptObject responseObject) {");
    w.indent();/*from  ww w  .ja  v a2  s . c  o m*/

    w.print("final " + ctn + "[] tmp = new " + ctn);
    w.println("[getResultSize(responseObject)];");

    w.println("serializer.fromJson(getResult(responseObject), tmp);");
    w.println("return tmp;");
    w.outdent();

    w.println("}");
}

From source file:org.rapla.rest.gwtjsonrpc.rebind.SerializerCreator.java

License:Apache License

private void generateGetSets(final SourceWriter w) {
    for (final JField f : sortFields(targetType)) {
        if (f.isPrivate()) {
            w.print("private static final native ");
            w.print(f.getType().getQualifiedSourceName());
            w.print(" objectGet_" + f.getName());
            w.print("(");
            w.print(targetType.getQualifiedSourceName() + " instance");
            w.print(")");
            w.println("/*-{ ");
            w.indent();//  ww  w  .  j  a v a 2 s . co  m

            w.print("return instance.@");
            w.print(targetType.getQualifiedSourceName());
            w.print("::");
            w.print(f.getName());
            w.println(";");

            w.outdent();
            w.println("}-*/;");

            w.print("private static final native void ");
            w.print(" objectSet_" + f.getName());
            w.print("(");
            w.print(targetType.getQualifiedSourceName() + " instance, ");
            w.print(f.getType().getQualifiedSourceName() + " value");
            w.print(")");
            w.println("/*-{ ");
            w.indent();

            w.print("instance.@");
            w.print(targetType.getQualifiedSourceName());
            w.print("::");
            w.print(f.getName());
            w.println(" = value;");

            w.outdent();
            w.println("}-*/;");
        }

        if (f.getType() == JPrimitiveType.CHAR || isBoxedCharacter(f.getType())) {
            w.print("private static final native String");
            w.print(" jsonGet0_" + f.getName());
            w.print("(final JavaScriptObject instance)");
            w.println("/*-{ ");
            w.indent();
            w.print("return instance.");
            w.print(f.getName());
            w.println(";");
            w.outdent();
            w.println("}-*/;");

            w.print("private static final ");
            w.print(f.getType() == JPrimitiveType.CHAR ? "char" : "Character");
            w.print(" jsonGet_" + f.getName());
            w.print("(JavaScriptObject instance)");
            w.println(" {");
            w.indent();
            w.print("return ");
            w.print(JsonSerializer.class.getName());
            w.print(".toChar(");
            w.print("jsonGet0_" + f.getName());
            w.print("(instance)");
            w.println(");");
            w.outdent();
            w.println("}");
        } else {
            w.print("private static final native ");
            if (f.getType().isArray() != null) {
                w.print("JavaScriptObject");
            } else if (isJsonPrimitive(f.getType())) {
                w.print(f.getType().getQualifiedSourceName());
            } else if (isBoxedPrimitive(f.getType())) {
                w.print(boxedTypeToPrimitiveTypeName(f.getType()));
            } else {
                w.print("Object");
            }
            w.print(" jsonGet_" + f.getName());
            w.print("(JavaScriptObject instance)");
            w.println("/*-{ ");
            w.indent();

            w.print("return instance.");
            w.print(f.getName());
            w.println(";");

            w.outdent();
            w.println("}-*/;");
        }

        w.println();
    }
}

From source file:org.rapla.rest.gwtjsonrpc.rebind.SerializerCreator.java

License:Apache License

private void generateEnumFromJson(final SourceWriter w) {
    w.print("public ");
    w.print(targetType.getQualifiedSourceName());
    w.println(" fromJson(Object in) {");
    w.indent();/*from ww w .  java  2s . c om*/
    w.print("return in != null");
    w.print(" ? " + targetType.getQualifiedSourceName() + ".valueOf((String)in)");
    w.print(" : null");
    w.println(";");
    w.outdent();
    w.println("}");
    w.println();
}

From source file:org.rapla.rest.gwtjsonrpc.rebind.SerializerCreator.java

License:Apache License

private void generatePrintJson(final SourceWriter w) {
    final JField[] fieldList = sortFields(targetType);
    w.print("protected int printJsonImpl(int fieldCount, StringBuilder sb, ");
    w.println("Object instance) {");
    w.indent();/*w w  w .j a va 2s. c om*/

    w.print("final ");
    w.print(targetType.getQualifiedSourceName());
    w.print(" src = (");
    w.print(targetType.getQualifiedSourceName());
    w.println(")instance;");

    if (needsSuperSerializer(targetType)) {
        w.print("fieldCount = super.printJsonImpl(fieldCount, sb, (");
        w.print(targetType.getSuperclass().getQualifiedSourceName());
        w.println(")src);");
    }

    final String docomma = "if (fieldCount++ > 0) sb.append(\",\");";
    for (final JField f : fieldList) {
        final String doget;
        if (f.isPrivate()) {
            doget = "objectGet_" + f.getName() + "(src)";
        } else {
            doget = "src." + f.getName();
        }

        final String doname = "sb.append(\"\\\"" + f.getName() + "\\\":\");";
        if (f.getType() == JPrimitiveType.CHAR || isBoxedCharacter(f.getType())) {
            w.println(docomma);
            w.println(doname);
            w.println("sb.append(\"\\\"\");");
            w.println("sb.append(" + JsonSerializer.class.getSimpleName() + ".escapeChar(" + doget + "));");
            w.println("sb.append(\"\\\"\");");
        } else if (isJsonString(f.getType())) {
            w.println("if (" + doget + " != null) {");
            w.indent();
            w.println(docomma);
            w.println(doname);
            w.println("sb.append(" + JsonSerializer.class.getSimpleName() + ".escapeString(" + doget + "));");
            w.outdent();
            w.println("}");
            w.println();
        } else if (f.getType().isPrimitive() != null) {
            w.println(docomma);
            w.println(doname);
            w.println("sb.append(" + doget + ");");
            w.println();
        } else if (isJsonPrimitive(f.getType()) || isBoxedPrimitive(f.getType())) {
            w.println("if (" + doget + " != null) {");
            w.println(docomma);
            w.println(doname);
            w.println("sb.append(" + doget + ");");
            w.println();
            w.println("}");
        } else {
            w.println("if (" + doget + " != null) {");
            w.indent();
            w.println(docomma);
            w.println(doname);
            if (needsTypeParameter(f.getType())) {
                w.print("ser_" + f.getName());
            } else {
                w.print(serializerFor(f.getType()) + ".INSTANCE");
            }
            w.println(".printJson(sb, " + doget + ");");
            w.outdent();
            w.println("}");
            w.println();
        }
    }

    w.println("return fieldCount;");
    w.outdent();
    w.println("}");
    w.println();
}

From source file:org.rapla.rest.gwtjsonrpc.rebind.SerializerCreator.java

License:Apache License

private void generateFromJson(final SourceWriter w) {
    w.print("public ");
    w.print(targetType.getQualifiedSourceName());
    w.println(" fromJson(Object in) {");
    w.indent();//from   ww w .j  a v  a  2s. co  m
    if (targetType.isAbstract()) {
        w.println("throw new UnsupportedOperationException();");
    } else {
        w.println("if (in == null) return null;");
        w.println("final JavaScriptObject jso = (JavaScriptObject)in;");
        w.print("final ");
        w.print(targetType.getQualifiedSourceName());
        w.print(" dst = new ");
        w.println(targetType.getQualifiedSourceName() + "();");
        w.println("fromJsonImpl(jso, dst);");
        w.println("return dst;");
    }
    w.outdent();
    w.println("}");
    w.println();

    w.print("protected void fromJsonImpl(JavaScriptObject jso,");
    w.print(targetType.getQualifiedSourceName());
    w.println(" dst) {");
    w.indent();

    if (needsSuperSerializer(targetType)) {
        w.print("super.fromJsonImpl(jso, (");
        w.print(targetType.getSuperclass().getQualifiedSourceName());
        w.println(")dst);");
    }

    for (final JField f : sortFields(targetType)) {
        final String doget = "jsonGet_" + f.getName() + "(jso)";
        final String doset0, doset1;

        if (f.isPrivate()) {
            doset0 = "objectSet_" + f.getName() + "(dst, ";
            doset1 = ")";
        } else {
            doset0 = "dst." + f.getName() + " = ";
            doset1 = "";
        }

        JType type = f.getType();
        if (type.isArray() != null) {
            final JType ct = type.isArray().getComponentType();
            w.println("if (" + doget + " != null) {");
            w.indent();

            w.print("final ");
            w.print(ct.getQualifiedSourceName());
            w.print("[] tmp = new ");
            w.print(ct.getQualifiedSourceName());
            w.print("[");
            w.print(ObjectArraySerializer.class.getName());
            w.print(".size(" + doget + ")");
            w.println("];");

            w.println("ser_" + f.getName() + ".fromJson(" + doget + ", tmp);");

            w.print(doset0);
            w.print("tmp");
            w.print(doset1);
            w.println(";");

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

        } else if (isJsonPrimitive(type)) {
            w.print(doset0);
            w.print(doget);
            w.print(doset1);
            w.println(";");

        } else if (isBoxedPrimitive(type)) {
            w.print(doset0);
            w.print("( " + doget + " != null) ? ");
            //w.print("new " + type.getQualifiedSourceName() + "(");
            w.print(doget);
            //w.print(")");
            w.print(":null");
            w.print(doset1);
            w.println(";");

        } else {
            w.print(doset0);
            if (needsTypeParameter(type)) {
                w.print("ser_" + f.getName());
            } else {
                String serializerFor = serializerFor(type);
                w.print(serializerFor + ".INSTANCE");
            }
            w.print(".fromJson(" + doget + ")");
            w.print(doset1);
            w.println(";");
        }
    }

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

From source file:org.rstudio.core.rebind.command.CommandBundleGenerator.java

License:Open Source License

/**
 * Generates the impl class and returns its name.
 *//* w  w  w .  j a  v  a2 s .c o  m*/
public String generate() throws Exception {
    ImageResourceInfo images = generateImageBundle();

    simpleName_ = bundleType_.getName().replace('.', '_') + "__Impl";

    PrintWriter printWriter = context_.tryCreate(logger_, packageName_, simpleName_);
    if (printWriter != null) {
        // I don't fully understand why images is sometimes null but we better
        // not get into a situation where we are generating this type but don't
        // know what images we can use. Empirically it seems like images is
        // always null only when printWriter is also null.
        assert images != null;

        ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(packageName_, simpleName_);
        factory.setSuperclass(bundleType_.getName());
        factory.addImport("org.rstudio.core.client.command.AppCommand");
        factory.addImport("org.rstudio.core.client.command.MenuCallback");
        factory.addImport("org.rstudio.core.client.command.ShortcutManager");
        SourceWriter writer = factory.createSourceWriter(context_, printWriter);

        emitConstructor(writer, images);
        emitCommandFields(writer);
        emitMenus(writer);
        emitShortcuts(writer);
        emitCommandAccessors(writer);

        // Close the class and commit it
        writer.outdent();
        writer.println("}");
        context_.commit(logger_, printWriter);
    }
    return packageName_ + "." + simpleName_;
}

From source file:org.rstudio.core.rebind.command.CommandBundleGenerator.java

License:Open Source License

private void emitShortcuts(SourceWriter writer) throws UnableToCompleteException {
    writer.println("private void __registerShortcuts() {");
    writer.indent();/*from  w  w w  .  ja va  2  s . co  m*/
    NodeList nodes = getConfigDoc("/commands/shortcuts");
    for (int i = 0; i < nodes.getLength(); i++) {
        NodeList groups = nodes.item(i).getChildNodes();
        for (int j = 0; j < groups.getLength(); j++) {
            if (groups.item(j).getNodeType() != Node.ELEMENT_NODE)
                continue;
            String groupName = ((Element) groups.item(j)).getAttribute("name");
            new ShortcutsEmitter(logger_, groupName, (Element) groups.item(j)).generate(writer);
        }
    }
    writer.outdent();
    writer.println("}");
}

From source file:org.rstudio.core.rebind.command.CommandBundleGenerator.java

License:Open Source License

private void emitCommandAccessors(SourceWriter writer) {
    for (JMethod method : commandMethods_) {
        String name = method.getName();
        writer.println("public AppCommand " + name + "() {");
        writer.indent();/*from  w  w  w .  jav  a2s .c o  m*/
        writer.println("return " + name + "_;");
        writer.outdent();
        writer.println("}");
    }
}

From source file:org.rstudio.core.rebind.command.CommandBundleGenerator.java

License:Open Source License

private ImageResourceInfo generateImageBundle() {
    String className = bundleType_.getSimpleSourceName() + "__AutoGenResources";
    String pathToInstance = packageName_ + "." + className + ".INSTANCE";
    ImageResourceInfo iri = new ImageResourceInfo(pathToInstance);

    PrintWriter printWriter = context_.tryCreate(logger_, packageName_, className);
    if (printWriter == null)
        return null;

    ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(packageName_, className);
    factory.addImport("com.google.gwt.core.client.GWT");
    factory.addImport("com.google.gwt.resources.client.*");
    factory.makeInterface();//from w w  w  .  j  a va  2 s  .  com
    factory.addImplementedInterface("ClientBundle");
    SourceWriter writer = factory.createSourceWriter(context_, printWriter);

    Set<String> resourceNames = context_.getResourcesOracle().getPathNames();
    for (JMethod method : commandMethods_) {
        String commandId = method.getName();

        String key = packageName_.replace('.', '/') + "/" + commandId + ".png";
        if (resourceNames.contains(key)) {
            writer.println("ImageResource " + commandId + "();");
            iri.addImage(commandId);
        }
    }
    writer.println("public static final " + className + " INSTANCE = " + "(" + className + ")GWT.create("
            + className + ".class);");

    writer.outdent();
    writer.println("}");
    context_.commit(logger_, printWriter);

    return iri;

}