From 73012c1b4f438731f99ae5d170cccb695f7986ac Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Tue, 21 Feb 2012 16:48:53 +0200 Subject: [PATCH] #8304 Generator for Serializer mappings --- .../terminal/gwt/DefaultWidgetSet.gwt.xml | 6 + .../communication/SerializerMapImpl.java | 19 ---- .../SerializerMapGenerator.java | 106 ++++++++++++++++++ 3 files changed, 112 insertions(+), 19 deletions(-) delete mode 100644 src/com/vaadin/terminal/gwt/client/communication/SerializerMapImpl.java create mode 100644 src/com/vaadin/terminal/gwt/widgetsetutils/SerializerMapGenerator.java diff --git a/src/com/vaadin/terminal/gwt/DefaultWidgetSet.gwt.xml b/src/com/vaadin/terminal/gwt/DefaultWidgetSet.gwt.xml index cd9bdfad93..2b8a7610c9 100644 --- a/src/com/vaadin/terminal/gwt/DefaultWidgetSet.gwt.xml +++ b/src/com/vaadin/terminal/gwt/DefaultWidgetSet.gwt.xml @@ -20,6 +20,12 @@ + + + + + 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 index eb37db3eb0..0000000000 --- a/src/com/vaadin/terminal/gwt/client/communication/SerializerMapImpl.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.vaadin.terminal.gwt.client.communication; - -import com.google.gwt.core.client.GWT; -import com.vaadin.terminal.gwt.client.ComponentState_Serializer; -import com.vaadin.terminal.gwt.client.ui.VButtonState_Serializer; - -public class SerializerMapImpl implements SerializerMap { - - public VaadinSerializer getSerializer(String type) { - // TODO This should be in a separate class and constructed by a - // generator - if (type.equals("com.vaadin.terminal.gwt.client.ui.VButtonState")) { - return GWT.create(VButtonState_Serializer.class); - } - - return GWT.create(ComponentState_Serializer.class); - } - -} diff --git a/src/com/vaadin/terminal/gwt/widgetsetutils/SerializerMapGenerator.java b/src/com/vaadin/terminal/gwt/widgetsetutils/SerializerMapGenerator.java new file mode 100644 index 0000000000..651ae24b2d --- /dev/null +++ b/src/com/vaadin/terminal/gwt/widgetsetutils/SerializerMapGenerator.java @@ -0,0 +1,106 @@ +package com.vaadin.terminal.gwt.widgetsetutils; + +import java.io.PrintWriter; +import java.util.Date; + +import com.google.gwt.core.ext.Generator; +import com.google.gwt.core.ext.GeneratorContext; +import com.google.gwt.core.ext.TreeLogger; +import com.google.gwt.core.ext.TreeLogger.Type; +import com.google.gwt.core.ext.UnableToCompleteException; +import com.google.gwt.core.ext.typeinfo.JClassType; +import com.google.gwt.core.ext.typeinfo.TypeOracle; +import com.google.gwt.user.rebind.ClassSourceFileComposerFactory; +import com.google.gwt.user.rebind.SourceWriter; +import com.vaadin.terminal.gwt.client.communication.SerializerMap; +import com.vaadin.terminal.gwt.client.communication.SharedState; +import com.vaadin.terminal.gwt.client.communication.VaadinSerializer; + +public class SerializerMapGenerator extends Generator { + + private String packageName; + private String className; + + @Override + public String generate(TreeLogger logger, GeneratorContext context, + String typeName) throws UnableToCompleteException { + + try { + TypeOracle typeOracle = context.getTypeOracle(); + + // get classType and save instance variables + JClassType classType = typeOracle.getType(typeName); + packageName = classType.getPackage().getName(); + className = classType.getSimpleSourceName() + "Impl"; + // Generate class source code + generateClass(logger, context); + } catch (Exception e) { + logger.log(TreeLogger.ERROR, "WidgetMap creation failed", e); + } + // return the fully qualifed name of the class generated + return packageName + "." + className; + } + + /** + * Generate source code for WidgetMapImpl + * + * @param logger + * Logger object + * @param context + * Generator context + */ + private void generateClass(TreeLogger logger, GeneratorContext context) { + // get print writer that receives the source code + PrintWriter printWriter = null; + printWriter = context.tryCreate(logger, packageName, className); + // print writer if null, source code has ALREADY been generated, + // return (WidgetMap is equal to all permutations atm) + if (printWriter == null) { + return; + } + logger.log(Type.INFO, "Detecting serializable data types..."); + Date date = new Date(); + TypeOracle typeOracle = context.getTypeOracle(); + JClassType serializerType = typeOracle.findType(SharedState.class + .getName()); + JClassType[] serializerSubtypes = serializerType.getSubtypes(); + + // init composer, set class properties, create source writer + ClassSourceFileComposerFactory composer = null; + composer = new ClassSourceFileComposerFactory(packageName, className); + composer.addImport("com.google.gwt.core.client.GWT"); + composer.addImplementedInterface(SerializerMap.class.getName()); + SourceWriter sourceWriter = composer.createSourceWriter(context, + printWriter); + sourceWriter.indent(); + + sourceWriter.println("public " + VaadinSerializer.class.getName() + + " getSerializer(String type) {"); + sourceWriter.indent(); + + for (JClassType type : serializerSubtypes) { + sourceWriter.println("if (type.equals(\"" + + type.getQualifiedSourceName() + "\")) {"); + sourceWriter.indent(); + sourceWriter.println("return GWT.create(" + + type.getQualifiedSourceName() + "_Serializer.class);"); + sourceWriter.outdent(); + sourceWriter.println("}"); + logger.log(Type.INFO, "Configured serializer for " + type.getName()); + } + sourceWriter + .println("throw new RuntimeException(\"No serializer found for class \"+type);"); + sourceWriter.outdent(); + sourceWriter.println("}"); + + // close generated class + sourceWriter.outdent(); + sourceWriter.println("}"); + // commit generated class + context.commit(logger, printWriter); + logger.log(Type.INFO, + "Done. (" + (new Date().getTime() - date.getTime()) / 1000 + + "seconds)"); + + } +} -- 2.39.5