Search in sources :

Example 1 with JField

use of com.google.gwt.core.ext.typeinfo.JField in project rstudio by rstudio.

the class JavaScriptSerializerGenerator method printSerializers.

private void printSerializers(List<JClassType> classes, SourceWriter w) {
    // print the method that dispatches to the appropriate serializer
    w.println("public <T> JavaScriptObject serialize(T source)");
    w.println("{");
    w.indent();
    for (JClassType classType : classes) {
        if (classType.isAbstract())
            continue;
        w.println();
        w.println("if (source.getClass().getName() == " + classType.getQualifiedSourceName() + ".class.getName())");
        w.println("{");
        w.indent();
        w.println("return serializeJso((" + classType.getQualifiedSourceName() + ") source);");
        w.outdent();
        w.println("}");
        w.println();
    }
    w.println("return null;");
    w.outdent();
    w.println("}");
    // print individual serializers
    for (JClassType classType : classes) {
        w.print("private final native JavaScriptObject serializeJso(");
        w.println(classType.getQualifiedSourceName() + " source) /*-{");
        w.indent();
        w.println("return {");
        w.indent();
        w.println("\"class_name\":\"" + classType.getQualifiedSourceName() + "\",");
        w.println("\"class_data\": {");
        w.indent();
        JField[] fields = classType.getFields();
        for (int i = 0; i < fields.length; i++) {
            JField field = fields[i];
            if (!field.isStatic()) {
                w.print("\"" + field.getName() + "\": ");
                if (isAnnotatedSerializable(field)) {
                    w.print("this.@" + genPackageName + "." + genClassName + "::serializeJso(L");
                    w.print(field.getType().getQualifiedSourceName().replace(".", "/"));
                    w.print(";)(");
                }
                w.println("source.@" + classType.getQualifiedSourceName() + "::" + field.getName());
                if (isAnnotatedSerializable(field)) {
                    w.print(")");
                }
                if (i < (fields.length - 1))
                    w.print(", ");
                w.println();
            }
        }
        w.outdent();
        w.println("}");
        w.outdent();
        w.println("};");
        w.outdent();
        w.println("}-*/;");
        w.println();
    }
}
Also used : JClassType(com.google.gwt.core.ext.typeinfo.JClassType) JField(com.google.gwt.core.ext.typeinfo.JField)

Example 2 with JField

use of com.google.gwt.core.ext.typeinfo.JField in project rstudio by rstudio.

the class JavaScriptSerializerGenerator method printDeserializers.

private void printDeserializers(List<JClassType> classes, SourceWriter w) {
    w.println("private final native String classFromJso(" + "JavaScriptObject jso) /*-{");
    w.indent();
    w.println("return jso.class_name;");
    w.outdent();
    w.println("}-*/;");
    w.println();
    // print the method that dispatches to the appropriate deserializer
    w.println("public <T> T deserialize (JavaScriptObject jso)");
    w.println("{");
    w.indent();
    for (JClassType classType : classes) {
        // ignore abstract classes
        if (classType.isAbstract())
            continue;
        // determine class name from string
        w.println();
        w.println("if (classFromJso(jso) == \"" + classType.getQualifiedSourceName() + "\")");
        w.println("{");
        w.indent();
        w.println(classType.getQualifiedSourceName() + " ret = new " + classType.getQualifiedSourceName() + "();");
        w.println("deserializeJso(ret, jso);");
        w.println("return (T) ret;");
        w.outdent();
        w.println("}");
        w.println();
    }
    w.println("return null;");
    w.outdent();
    w.println("}");
    // emit individual deserializer methods (overloads)
    for (JClassType classType : classes) {
        if (classType.isAbstract())
            continue;
        w.println();
        w.println("private final native void deserializeJso(" + classType.getQualifiedSourceName() + " dest, " + "JavaScriptObject source) /*-{");
        w.indent();
        for (JField field : classType.getFields()) {
            if (!field.isStatic()) {
                w.print("dest.@" + classType.getQualifiedSourceName() + "::");
                w.print(field.getName() + " = ");
                if (isAnnotatedSerializable(field)) {
                    w.print("this.@" + genPackageName + "." + genClassName + "::deserialize(");
                    w.print("Lcom/google/gwt/core/client/JavaScriptObject;)(");
                }
                w.print("source.class_data[\"" + field.getName() + "\"]");
                if (isAnnotatedSerializable(field))
                    w.print(")");
                w.println(";");
            }
        }
        w.outdent();
        w.println("}-*/;");
    }
}
Also used : JClassType(com.google.gwt.core.ext.typeinfo.JClassType) JField(com.google.gwt.core.ext.typeinfo.JField)

Aggregations

JClassType (com.google.gwt.core.ext.typeinfo.JClassType)2 JField (com.google.gwt.core.ext.typeinfo.JField)2