]> source.dussan.org Git - vaadin-framework.git/commitdiff
#8304 Generator for Serializer mappings
authorArtur Signell <artur@vaadin.com>
Tue, 21 Feb 2012 14:48:53 +0000 (16:48 +0200)
committerArtur Signell <artur@vaadin.com>
Tue, 21 Feb 2012 14:48:53 +0000 (16:48 +0200)
src/com/vaadin/terminal/gwt/DefaultWidgetSet.gwt.xml
src/com/vaadin/terminal/gwt/client/communication/SerializerMapImpl.java [deleted file]
src/com/vaadin/terminal/gwt/widgetsetutils/SerializerMapGenerator.java [new file with mode: 0644]

index cd9bdfad93a905a7204a31280174f7dd84abfc0c..2b8a7610c99af94dff737277911b1068d04fc1cc 100644 (file)
                <when-type-is class="com.google.gwt.core.client.impl.SchedulerImpl" />
        </replace-with>
        
+
+       <generate-with
+               class="com.vaadin.terminal.gwt.widgetsetutils.SerializerMapGenerator">
+               <when-type-is class="com.vaadin.terminal.gwt.client.communication.SerializerMap" />
+       </generate-with>
+       
        <replace-with class="com.vaadin.terminal.gwt.client.VDebugConsole">
                <when-type-is class="com.vaadin.terminal.gwt.client.Console" />
        </replace-with>
diff --git a/src/com/vaadin/terminal/gwt/client/communication/SerializerMapImpl.java b/src/com/vaadin/terminal/gwt/client/communication/SerializerMapImpl.java
deleted file mode 100644 (file)
index eb37db3..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-package com.vaadin.terminal.gwt.client.communication;\r
-\r
-import com.google.gwt.core.client.GWT;\r
-import com.vaadin.terminal.gwt.client.ComponentState_Serializer;\r
-import com.vaadin.terminal.gwt.client.ui.VButtonState_Serializer;\r
-\r
-public class SerializerMapImpl implements SerializerMap {\r
-\r
-    public VaadinSerializer getSerializer(String type) {\r
-        // TODO This should be in a separate class and constructed by a\r
-        // generator\r
-        if (type.equals("com.vaadin.terminal.gwt.client.ui.VButtonState")) {\r
-            return GWT.create(VButtonState_Serializer.class);\r
-        }\r
-\r
-        return GWT.create(ComponentState_Serializer.class);\r
-    }\r
-\r
-}\r
diff --git a/src/com/vaadin/terminal/gwt/widgetsetutils/SerializerMapGenerator.java b/src/com/vaadin/terminal/gwt/widgetsetutils/SerializerMapGenerator.java
new file mode 100644 (file)
index 0000000..651ae24
--- /dev/null
@@ -0,0 +1,106 @@
+package com.vaadin.terminal.gwt.widgetsetutils;\r
+\r
+import java.io.PrintWriter;\r
+import java.util.Date;\r
+\r
+import com.google.gwt.core.ext.Generator;\r
+import com.google.gwt.core.ext.GeneratorContext;\r
+import com.google.gwt.core.ext.TreeLogger;\r
+import com.google.gwt.core.ext.TreeLogger.Type;\r
+import com.google.gwt.core.ext.UnableToCompleteException;\r
+import com.google.gwt.core.ext.typeinfo.JClassType;\r
+import com.google.gwt.core.ext.typeinfo.TypeOracle;\r
+import com.google.gwt.user.rebind.ClassSourceFileComposerFactory;\r
+import com.google.gwt.user.rebind.SourceWriter;\r
+import com.vaadin.terminal.gwt.client.communication.SerializerMap;\r
+import com.vaadin.terminal.gwt.client.communication.SharedState;\r
+import com.vaadin.terminal.gwt.client.communication.VaadinSerializer;\r
+\r
+public class SerializerMapGenerator extends Generator {\r
+\r
+    private String packageName;\r
+    private String className;\r
+\r
+    @Override\r
+    public String generate(TreeLogger logger, GeneratorContext context,\r
+            String typeName) throws UnableToCompleteException {\r
+\r
+        try {\r
+            TypeOracle typeOracle = context.getTypeOracle();\r
+\r
+            // get classType and save instance variables\r
+            JClassType classType = typeOracle.getType(typeName);\r
+            packageName = classType.getPackage().getName();\r
+            className = classType.getSimpleSourceName() + "Impl";\r
+            // Generate class source code\r
+            generateClass(logger, context);\r
+        } catch (Exception e) {\r
+            logger.log(TreeLogger.ERROR, "WidgetMap creation failed", e);\r
+        }\r
+        // return the fully qualifed name of the class generated\r
+        return packageName + "." + className;\r
+    }\r
+\r
+    /**\r
+     * Generate source code for WidgetMapImpl\r
+     * \r
+     * @param logger\r
+     *            Logger object\r
+     * @param context\r
+     *            Generator context\r
+     */\r
+    private void generateClass(TreeLogger logger, GeneratorContext context) {\r
+        // get print writer that receives the source code\r
+        PrintWriter printWriter = null;\r
+        printWriter = context.tryCreate(logger, packageName, className);\r
+        // print writer if null, source code has ALREADY been generated,\r
+        // return (WidgetMap is equal to all permutations atm)\r
+        if (printWriter == null) {\r
+            return;\r
+        }\r
+        logger.log(Type.INFO, "Detecting serializable data types...");\r
+        Date date = new Date();\r
+        TypeOracle typeOracle = context.getTypeOracle();\r
+        JClassType serializerType = typeOracle.findType(SharedState.class\r
+                .getName());\r
+        JClassType[] serializerSubtypes = serializerType.getSubtypes();\r
+\r
+        // init composer, set class properties, create source writer\r
+        ClassSourceFileComposerFactory composer = null;\r
+        composer = new ClassSourceFileComposerFactory(packageName, className);\r
+        composer.addImport("com.google.gwt.core.client.GWT");\r
+        composer.addImplementedInterface(SerializerMap.class.getName());\r
+        SourceWriter sourceWriter = composer.createSourceWriter(context,\r
+                printWriter);\r
+        sourceWriter.indent();\r
+\r
+        sourceWriter.println("public " + VaadinSerializer.class.getName()\r
+                + " getSerializer(String type) {");\r
+        sourceWriter.indent();\r
+\r
+        for (JClassType type : serializerSubtypes) {\r
+            sourceWriter.println("if (type.equals(\""\r
+                    + type.getQualifiedSourceName() + "\")) {");\r
+            sourceWriter.indent();\r
+            sourceWriter.println("return GWT.create("\r
+                    + type.getQualifiedSourceName() + "_Serializer.class);");\r
+            sourceWriter.outdent();\r
+            sourceWriter.println("}");\r
+            logger.log(Type.INFO, "Configured serializer for " + type.getName());\r
+        }\r
+        sourceWriter\r
+                .println("throw new RuntimeException(\"No serializer found for class \"+type);");\r
+        sourceWriter.outdent();\r
+        sourceWriter.println("}");\r
+\r
+        // close generated class\r
+        sourceWriter.outdent();\r
+        sourceWriter.println("}");\r
+        // commit generated class\r
+        context.commit(logger, printWriter);\r
+        logger.log(Type.INFO,\r
+                "Done. (" + (new Date().getTime() - date.getTime()) / 1000\r
+                        + "seconds)");\r
+\r
+    }\r
+}\r