Search in sources :

Example 1 with JClassType

use of com.google.gwt.core.ext.typeinfo.JClassType in project che by eclipse.

the class ExtensionRegistryGenerator method generateImports.

/**
     * Inject imports
     *
     * @param extensions
     * @param composerFactory
     */
private void generateImports(List<JClassType> extensions, ClassSourceFileComposerFactory composerFactory) {
    // imports
    composerFactory.addImport(GWT.class.getCanonicalName());
    composerFactory.addImport(Extension.class.getCanonicalName());
    composerFactory.addImport(ExtensionRegistry.class.getCanonicalName());
    composerFactory.addImport(Inject.class.getCanonicalName());
    composerFactory.addImport(Provider.class.getCanonicalName());
    composerFactory.addImport(List.class.getCanonicalName());
    composerFactory.addImport(ArrayList.class.getCanonicalName());
    composerFactory.addImport(Map.class.getCanonicalName());
    composerFactory.addImport(HashMap.class.getCanonicalName());
    // import for extensions
    for (JClassType jClassType : extensions) {
        composerFactory.addImport(jClassType.getQualifiedSourceName());
    }
}
Also used : Extension(org.eclipse.che.ide.api.extension.Extension) Inject(com.google.inject.Inject) JClassType(com.google.gwt.core.ext.typeinfo.JClassType) GWT(com.google.gwt.core.client.GWT) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ExtensionRegistry(org.eclipse.che.ide.api.extension.ExtensionRegistry) Provider(com.google.inject.Provider)

Example 2 with JClassType

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

the class JavaScriptSerializerGenerator method generate.

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException {
    TypeOracle oracle = context.getTypeOracle();
    List<JClassType> classes = new ArrayList<JClassType>();
    // locate all the types annotated with JavaScriptSerializable
    for (JClassType classType : oracle.getTypes()) {
        if (isAnnotatedSerializable(classType))
            classes.add(classType);
    }
    ClassSourceFileComposerFactory sourceFile = new ClassSourceFileComposerFactory(genPackageName, genClassName);
    sourceFile.addImplementedInterface(JavaScriptSerializer.class.getCanonicalName());
    sourceFile.addImport("com.google.gwt.core.client.JavaScriptObject");
    sourceFile.addImport("org.rstudio.core.client.js.JsObject;");
    PrintWriter printWriter = context.tryCreate(logger, genPackageName, genClassName);
    if (printWriter != null) {
        SourceWriter sourceWriter = sourceFile.createSourceWriter(context, printWriter);
        sourceWriter.println(genClassName + "() {");
        sourceWriter.println("}");
        printSerializers(classes, sourceWriter);
        printDeserializers(classes, sourceWriter);
        sourceWriter.commit(logger);
    }
    return sourceFile.getCreatedClassName();
}
Also used : JavaScriptSerializer(org.rstudio.core.client.js.JavaScriptSerializer) JClassType(com.google.gwt.core.ext.typeinfo.JClassType) ClassSourceFileComposerFactory(com.google.gwt.user.rebind.ClassSourceFileComposerFactory) TypeOracle(com.google.gwt.core.ext.typeinfo.TypeOracle) ArrayList(java.util.ArrayList) SourceWriter(com.google.gwt.user.rebind.SourceWriter) PrintWriter(java.io.PrintWriter)

Example 3 with JClassType

use of com.google.gwt.core.ext.typeinfo.JClassType 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 4 with JClassType

use of com.google.gwt.core.ext.typeinfo.JClassType 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)

Example 5 with JClassType

use of com.google.gwt.core.ext.typeinfo.JClassType in project libgdx by libgdx.

the class ReflectionCacheGenerator method generate.

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException {
    TypeOracle oracle = context.getTypeOracle();
    assert (oracle != null);
    JClassType type = oracle.findType(typeName);
    if (type == null) {
        logger.log(ERROR, "Couldn't find type '" + typeName + "'");
        throw new UnableToCompleteException();
    }
    if (type.isInterface() == null) {
        logger.log(ERROR, "Type '" + typeName + "' must be an interface");
        throw new UnableToCompleteException();
    }
    ReflectionCacheSourceCreator source = new ReflectionCacheSourceCreator(logger, context, type);
    return source.create();
}
Also used : JClassType(com.google.gwt.core.ext.typeinfo.JClassType) TypeOracle(com.google.gwt.core.ext.typeinfo.TypeOracle) UnableToCompleteException(com.google.gwt.core.ext.UnableToCompleteException)

Aggregations

JClassType (com.google.gwt.core.ext.typeinfo.JClassType)14 UnableToCompleteException (com.google.gwt.core.ext.UnableToCompleteException)7 TypeOracle (com.google.gwt.core.ext.typeinfo.TypeOracle)6 ClassSourceFileComposerFactory (com.google.gwt.user.rebind.ClassSourceFileComposerFactory)4 SourceWriter (com.google.gwt.user.rebind.SourceWriter)4 PrintWriter (java.io.PrintWriter)4 GWT (com.google.gwt.core.client.GWT)3 ArrayList (java.util.ArrayList)3 JField (com.google.gwt.core.ext.typeinfo.JField)2 JType (com.google.gwt.core.ext.typeinfo.JType)2 NotFoundException (com.google.gwt.core.ext.typeinfo.NotFoundException)2 Extension (org.eclipse.che.ide.api.extension.Extension)2 BadPropertyValueException (com.google.gwt.core.ext.BadPropertyValueException)1 ConfigurationProperty (com.google.gwt.core.ext.ConfigurationProperty)1 PropertyOracle (com.google.gwt.core.ext.PropertyOracle)1 JConstructor (com.google.gwt.core.ext.typeinfo.JConstructor)1 JMethod (com.google.gwt.core.ext.typeinfo.JMethod)1 Resource (com.google.gwt.dev.resource.Resource)1 ClientBundleWithLookup (com.google.gwt.resources.client.ClientBundleWithLookup)1 DataResource (com.google.gwt.resources.client.DataResource)1