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

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

Introduction

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

Prototype

void print(String s);

Source Link

Usage

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

License:Apache License

private void generateProxyCallCreator(final TreeLogger logger, final SourceWriter w)
        throws UnableToCompleteException {
    String callName = getJsonCallClassName(logger);
    w.println();/*w w w  .j  a  v a 2 s.c  om*/
    w.println("@Override");
    w.print("protected <T> ");
    w.print(callName);
    w.print("<T> newJsonCall(final AbstractJsonProxy proxy, ");
    w.print("final String methodName, final String reqData, ");
    w.println("final ResultDeserializer<T> ser) {");
    w.indent();

    w.print("return new ");
    w.print(callName);
    w.println("<T>(proxy, methodName, reqData, ser);");

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

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;//w w  w .  j a  v  a2s  .c  om
    //    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 generateSingleton(final SourceWriter w) {
    w.print("public static final ");
    w.print(getDeserializerSimpleName(targetType));
    w.print(" INSTANCE = new ");
    w.print(getDeserializerSimpleName(targetType));
    w.println("();");
    w.println();// www . j  a  v  a 2s .  c o m
}

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

License:Apache License

private void generateInstanceMembers(SourceWriter w) {
    w.print("private final ");
    w.print(serializerCreator.serializerFor(targetType));
    w.print(" ");
    w.print("serializer");
    w.print(" = ");
    serializerCreator.generateSerializerReference(targetType, w, true);
    w.println(";");
    w.println();//from w  ww  . j  a v a  2s .  c o  m
}

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   w w w . j  a va  2  s.  c  om

    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.ResultDeserializerCreator.java

License:Apache License

public void generateDeserializerReference(JType targetType, SourceWriter w) {
    if (SerializerCreator.isBoxedPrimitive(targetType)) {
        w.print(PrimitiveResultDeserializers.class.getCanonicalName());
        w.print(".");
        w.print(targetType.getSimpleSourceName().toUpperCase());
        w.print("_INSTANCE");
    } else if (targetType.isArray() != null) {
        w.print(deserializerFor(targetType.isArray()));
    } else {//from   w w w  .  ja  v  a  2 s  . c  om
        serializerCreator.generateSerializerReference(targetType, w, false);
    }
}

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

License:Apache License

private void generateSingleton(final SourceWriter w) {
    w.print("public static final ");
    w.print("javax.inject.Provider<" + getSerializerSimpleName() + ">");

    w.print(" INSTANCE_PROVIDER = new javax.inject.Provider<");
    w.print(getSerializerSimpleName());/*from   w ww.j ava2s. com*/
    w.println(">(){");
    w.print("public " + getSerializerSimpleName() + " get(){return INSTANCE;} ");
    w.println("};");
    w.println();

    w.print("public static final ");
    w.print(getSerializerSimpleName());
    w.print(" INSTANCE = new ");
    w.print(getSerializerSimpleName());
    w.println("();");
    w.println();
}

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

License:Apache License

private void generateInstanceMembers(final SourceWriter w) {
    for (final JField f : sortFields(targetType)) {
        final JType ft = f.getType();
        if (needsTypeParameter(ft)) {
            final String serType = serializerFor(ft);
            w.print("private final ");
            w.print(serType);/*from w w w  .j a  v  a 2  s  .c  o  m*/
            w.print(" ");
            w.print("ser_" + f.getName());
            w.print(" = ");
            boolean useProviders = true;
            generateSerializerReference(ft, w, useProviders);
            w.println(";");
        }
    }
    w.println();
}

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

License:Apache License

void generateSerializerReference(final JType type, final SourceWriter w, boolean useProviders) {
    String serializerFor = serializerFor(type);
    if (type.isArray() != null) {
        final JType componentType = type.isArray().getComponentType();
        if (componentType.isPrimitive() != null || isBoxedPrimitive(componentType)) {
            w.print(PrimitiveArraySerializer.class.getCanonicalName());
            w.print(".INSTANCE");
        } else {/*from   w w w.j a v a  2 s .  c om*/
            w.print("new " + serializerFor + "(");
            generateSerializerReference(componentType, w, useProviders);
            w.print(")");
        }

    } else if (needsTypeParameter(type)) {
        w.print("new " + serializerFor + "(");
        final JClassType[] typeArgs = type.isParameterized().getTypeArgs();
        int n = 0;
        if (isStringMap(type)) {
            n++;
        }
        boolean first = true;
        for (; n < typeArgs.length; n++) {
            if (first) {
                first = false;
            } else {
                w.print(", ");
            }
            generateSerializerReference(typeArgs[n], w, useProviders);
        }
        w.print(")");

    } else {
        //      String sourceName = type.getQualifiedSourceName();

        w.print(serializerFor + ".INSTANCE" + (useProviders ? "_PROVIDER" : ""));
    }
}

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();//from  ww  w  .j av a2s  .c  o  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();
    }
}