--- /dev/null
+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