From 29236e0fc7bdea3a22374c52559a175a3bc90d24 Mon Sep 17 00:00:00 2001 From: Henri Sara Date: Wed, 22 Feb 2012 11:45:29 +0200 Subject: [PATCH] Convert CRLF to LF. --- .../client/communication/SerializerMap.java | 68 ++-- .../communication/VaadinSerializer.java | 66 ++-- .../terminal/gwt/client/ui/VButtonState.java | 130 +++---- .../widgetsetutils/SerializerGenerator.java | 354 +++++++++--------- .../SerializerMapGenerator.java | 256 ++++++------- 5 files changed, 437 insertions(+), 437 deletions(-) diff --git a/src/com/vaadin/terminal/gwt/client/communication/SerializerMap.java b/src/com/vaadin/terminal/gwt/client/communication/SerializerMap.java index 07b4918b67..12983d3579 100644 --- a/src/com/vaadin/terminal/gwt/client/communication/SerializerMap.java +++ b/src/com/vaadin/terminal/gwt/client/communication/SerializerMap.java @@ -1,34 +1,34 @@ -/* -@VaadinApache2LicenseForJavaFiles@ - */ - -package com.vaadin.terminal.gwt.client.communication; - -import com.vaadin.terminal.gwt.widgetsetutils.SerializerMapGenerator; - -/** - * Provide a mapping from a type (communicated between the server and the - * client) and a {@link VaadinSerializer} instance. - * - * An implementation of this class is created at GWT compilation time by - * {@link SerializerMapGenerator}, so this interface can be instantiated with - * GWT.create(). - * - * @since 7.0 - */ -public interface SerializerMap { - - /** - * Returns a serializer instance for a given type. - * - * @param type - * type communicated on between the server and the client - * (currently fully qualified class name) - * @return serializer instance, not null - * @throws RuntimeException - * if no serializer is found - */ - // TODO better error handling in javadoc and in generator - public VaadinSerializer getSerializer(String type); - -} +/* +@VaadinApache2LicenseForJavaFiles@ + */ + +package com.vaadin.terminal.gwt.client.communication; + +import com.vaadin.terminal.gwt.widgetsetutils.SerializerMapGenerator; + +/** + * Provide a mapping from a type (communicated between the server and the + * client) and a {@link VaadinSerializer} instance. + * + * An implementation of this class is created at GWT compilation time by + * {@link SerializerMapGenerator}, so this interface can be instantiated with + * GWT.create(). + * + * @since 7.0 + */ +public interface SerializerMap { + + /** + * Returns a serializer instance for a given type. + * + * @param type + * type communicated on between the server and the client + * (currently fully qualified class name) + * @return serializer instance, not null + * @throws RuntimeException + * if no serializer is found + */ + // TODO better error handling in javadoc and in generator + public VaadinSerializer getSerializer(String type); + +} diff --git a/src/com/vaadin/terminal/gwt/client/communication/VaadinSerializer.java b/src/com/vaadin/terminal/gwt/client/communication/VaadinSerializer.java index 5d9be6fbf6..02484b1e74 100644 --- a/src/com/vaadin/terminal/gwt/client/communication/VaadinSerializer.java +++ b/src/com/vaadin/terminal/gwt/client/communication/VaadinSerializer.java @@ -1,33 +1,33 @@ -/* -@VaadinApache2LicenseForJavaFiles@ - */ - -package com.vaadin.terminal.gwt.client.communication; - -import com.google.gwt.json.client.JSONObject; -import com.vaadin.terminal.gwt.client.VPaintableMap; - -/** - * Serializer that can deserialize custom objects received from the server. - * - * Each serializer can handle objects of a single type - see - * {@link SerializerMap}. - * - * @since 7.0 - */ -public interface VaadinSerializer { - - /** - * Creates and deserializes an object received from the server. - * - * @param jsonValue - * JSON map from property name to property value - * @param idMapper - * mapper from paintable id to paintable, used to decode - * references to paintables - * @return deserialized object - */ - // TODO Object -> something - Object deserialize(JSONObject jsonValue, VPaintableMap idMapper); - -} +/* +@VaadinApache2LicenseForJavaFiles@ + */ + +package com.vaadin.terminal.gwt.client.communication; + +import com.google.gwt.json.client.JSONObject; +import com.vaadin.terminal.gwt.client.VPaintableMap; + +/** + * Serializer that can deserialize custom objects received from the server. + * + * Each serializer can handle objects of a single type - see + * {@link SerializerMap}. + * + * @since 7.0 + */ +public interface VaadinSerializer { + + /** + * Creates and deserializes an object received from the server. + * + * @param jsonValue + * JSON map from property name to property value + * @param idMapper + * mapper from paintable id to paintable, used to decode + * references to paintables + * @return deserialized object + */ + // TODO Object -> something + Object deserialize(JSONObject jsonValue, VPaintableMap idMapper); + +} diff --git a/src/com/vaadin/terminal/gwt/client/ui/VButtonState.java b/src/com/vaadin/terminal/gwt/client/ui/VButtonState.java index dd915106c5..e452ba6910 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VButtonState.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VButtonState.java @@ -1,65 +1,65 @@ -/* -@VaadinApache2LicenseForJavaFiles@ - */ - -package com.vaadin.terminal.gwt.client.ui; - -import com.vaadin.terminal.gwt.client.ComponentState; -import com.vaadin.ui.Button; - -/** - * Shared state for Button and NativeButton. - * - * @see ComponentState - * - * @since 7.0 - */ -public class VButtonState extends ComponentState { - private boolean disableOnClick = false; - private int clickShortcutKeyCode = 0; - - /** - * Checks whether the button should be disabled on the client side on next - * click. - * - * @return true if the button should be disabled on click - */ - public boolean isDisableOnClick() { - return disableOnClick; - } - - /** - * Sets whether the button should be disabled on the client side on next - * click. - * - * @param disableOnClick - * true if the button should be disabled on click - */ - public void setDisableOnClick(boolean disableOnClick) { - this.disableOnClick = disableOnClick; - } - - /** - * Returns the key code for activating the button via a keyboard shortcut. - * - * See {@link Button#setClickShortcut(int, int...)} for more information. - * - * @return key code or 0 for none - */ - public int getClickShortcutKeyCode() { - return clickShortcutKeyCode; - } - - /** - * Sets the key code for activating the button via a keyboard shortcut. - * - * See {@link Button#setClickShortcut(int, int...)} for more information. - * - * @param clickShortcutKeyCode - * key code or 0 for none - */ - public void setClickShortcutKeyCode(int clickShortcutKeyCode) { - this.clickShortcutKeyCode = clickShortcutKeyCode; - } - -} +/* +@VaadinApache2LicenseForJavaFiles@ + */ + +package com.vaadin.terminal.gwt.client.ui; + +import com.vaadin.terminal.gwt.client.ComponentState; +import com.vaadin.ui.Button; + +/** + * Shared state for Button and NativeButton. + * + * @see ComponentState + * + * @since 7.0 + */ +public class VButtonState extends ComponentState { + private boolean disableOnClick = false; + private int clickShortcutKeyCode = 0; + + /** + * Checks whether the button should be disabled on the client side on next + * click. + * + * @return true if the button should be disabled on click + */ + public boolean isDisableOnClick() { + return disableOnClick; + } + + /** + * Sets whether the button should be disabled on the client side on next + * click. + * + * @param disableOnClick + * true if the button should be disabled on click + */ + public void setDisableOnClick(boolean disableOnClick) { + this.disableOnClick = disableOnClick; + } + + /** + * Returns the key code for activating the button via a keyboard shortcut. + * + * See {@link Button#setClickShortcut(int, int...)} for more information. + * + * @return key code or 0 for none + */ + public int getClickShortcutKeyCode() { + return clickShortcutKeyCode; + } + + /** + * Sets the key code for activating the button via a keyboard shortcut. + * + * See {@link Button#setClickShortcut(int, int...)} for more information. + * + * @param clickShortcutKeyCode + * key code or 0 for none + */ + public void setClickShortcutKeyCode(int clickShortcutKeyCode) { + this.clickShortcutKeyCode = clickShortcutKeyCode; + } + +} diff --git a/src/com/vaadin/terminal/gwt/widgetsetutils/SerializerGenerator.java b/src/com/vaadin/terminal/gwt/widgetsetutils/SerializerGenerator.java index cd7fdf888f..a5be3be84d 100644 --- a/src/com/vaadin/terminal/gwt/widgetsetutils/SerializerGenerator.java +++ b/src/com/vaadin/terminal/gwt/widgetsetutils/SerializerGenerator.java @@ -1,177 +1,177 @@ -/* -@VaadinApache2LicenseForJavaFiles@ - */ - -package com.vaadin.terminal.gwt.widgetsetutils; - -import java.io.PrintWriter; -import java.util.Date; - -import com.google.gwt.core.client.GWT; -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.JMethod; -import com.google.gwt.core.ext.typeinfo.JPrimitiveType; -import com.google.gwt.core.ext.typeinfo.JType; -import com.google.gwt.core.ext.typeinfo.TypeOracle; -import com.google.gwt.json.client.JSONArray; -import com.google.gwt.json.client.JSONObject; -import com.google.gwt.user.rebind.ClassSourceFileComposerFactory; -import com.google.gwt.user.rebind.SourceWriter; -import com.vaadin.terminal.gwt.client.VPaintableMap; -import com.vaadin.terminal.gwt.client.communication.JsonDecoder; -import com.vaadin.terminal.gwt.client.communication.VaadinSerializer; - -/** - * GWT generator for creating serializer classes for custom classes sent from - * server to client. - * - * Only fields with a correspondingly named setter are deserialized. - * - * @since 7.0 - */ -public class SerializerGenerator extends Generator { - - private String packageName; - private String beanSerializerClassName; - - @Override - public String generate(TreeLogger logger, GeneratorContext context, - String beanTypeName) throws UnableToCompleteException { - String beanSerializerTypeName = beanTypeName + "_Serializer"; - try { - TypeOracle typeOracle = context.getTypeOracle(); - - // get classType and save instance variables - JClassType classType = typeOracle.getType(beanTypeName); - packageName = classType.getPackage().getName(); - beanSerializerClassName = classType.getSimpleSourceName() - + "_Serializer"; - // Generate class source code - generateClass(logger, context, beanTypeName, beanSerializerTypeName); - } catch (Exception e) { - logger.log(TreeLogger.ERROR, "SerializerGenerator failed for " - + beanTypeName, e); - } - // return the fully qualifed name of the class generated - return packageName + "." + beanSerializerClassName; - } - - /** - * Generate source code for a VaadinSerializer implementation. - * - * @param logger - * Logger object - * @param context - * Generator context - * @param beanTypeName - * bean type for which the serializer is to be generated - * @param beanSerializerTypeName - * name of the serializer class to generate - */ - private void generateClass(TreeLogger logger, GeneratorContext context, - String beanTypeName, String beanSerializerTypeName) { - // get print writer that receives the source code - PrintWriter printWriter = null; - printWriter = context.tryCreate(logger, packageName, - beanSerializerClassName); - - // print writer if null, source code has ALREADY been generated - if (printWriter == null) { - return; - } - Date date = new Date(); - TypeOracle typeOracle = context.getTypeOracle(); - logger.log(Type.DEBUG, "Processing serializable type " + beanTypeName - + "..."); - - // init composer, set class properties, create source writer - ClassSourceFileComposerFactory composer = null; - composer = new ClassSourceFileComposerFactory(packageName, - beanSerializerClassName); - composer.addImport(GWT.class.getName()); - composer.addImport(JSONArray.class.getName()); - // composer.addImport(JSONObject.class.getName()); - // composer.addImport(VPaintableMap.class.getName()); - composer.addImport(JsonDecoder.class.getName()); - // composer.addImport(VaadinSerializer.class.getName()); - - composer.addImplementedInterface(VaadinSerializer.class.getName()); - - SourceWriter sourceWriter = composer.createSourceWriter(context, - printWriter); - sourceWriter.indent(); - - sourceWriter.println("public " + beanTypeName + " deserialize(" - + JSONObject.class.getName() + " jsonValue, " - + VPaintableMap.class.getName() + " idMapper) {"); - sourceWriter.indent(); - - // VButtonState state = GWT.create(VButtonState.class); - sourceWriter.println(beanTypeName + " state = GWT.create(" - + beanTypeName + ".class);"); - JClassType beanType = typeOracle.findType(beanTypeName); - for (JMethod method : beanType.getMethods()) { - // Process all setters that have corresponding fields - if (!method.isPublic() || method.isStatic() - || !method.getName().startsWith("set") - || method.getParameterTypes().length != 1) { - // Not setter, skip to next method - continue; - - } - String setterName = method.getName(); - String capitalizedFieldName = setterName.substring(3); - String fieldName = decapitalize(capitalizedFieldName); - JType setterParameterType = method.getParameterTypes()[0]; - - logger.log(Type.DEBUG, "* Processing field " + fieldName + " in " - + beanTypeName); - - String jsonFieldName = "json" + capitalizedFieldName; - // JSONArray jsonHeight = (JSONArray) jsonValue.get("height"); - sourceWriter.println("JSONArray " + jsonFieldName - + " = (JSONArray) jsonValue.get(\"" + fieldName + "\");"); - - // state.setHeight((String) - // JsonDecoder.convertValue(jsonFieldValue,idMapper)); - - String fieldType; - JPrimitiveType primitiveType = setterParameterType.isPrimitive(); - if (primitiveType != null) { - // This is a primitive type -> must used the boxed type - fieldType = primitiveType.getQualifiedBoxedSourceName(); - } else { - fieldType = setterParameterType.getQualifiedSourceName(); - } - - sourceWriter.println("state." + setterName + "((" + fieldType - + ") JsonDecoder.convertValue(" + jsonFieldName - + ", idMapper));"); - } - - // return state; - sourceWriter.println("return state;"); - sourceWriter.println("}"); - sourceWriter.outdent(); - - // End of class - sourceWriter.println("}"); - sourceWriter.outdent(); - - // commit generated class - context.commit(logger, printWriter); - logger.log(Type.INFO, - "Done. (" + (new Date().getTime() - date.getTime()) / 1000 - + "seconds)"); - - } - - private String decapitalize(String name) { - return name.substring(0, 1).toLowerCase() + name.substring(1); - } -} +/* +@VaadinApache2LicenseForJavaFiles@ + */ + +package com.vaadin.terminal.gwt.widgetsetutils; + +import java.io.PrintWriter; +import java.util.Date; + +import com.google.gwt.core.client.GWT; +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.JMethod; +import com.google.gwt.core.ext.typeinfo.JPrimitiveType; +import com.google.gwt.core.ext.typeinfo.JType; +import com.google.gwt.core.ext.typeinfo.TypeOracle; +import com.google.gwt.json.client.JSONArray; +import com.google.gwt.json.client.JSONObject; +import com.google.gwt.user.rebind.ClassSourceFileComposerFactory; +import com.google.gwt.user.rebind.SourceWriter; +import com.vaadin.terminal.gwt.client.VPaintableMap; +import com.vaadin.terminal.gwt.client.communication.JsonDecoder; +import com.vaadin.terminal.gwt.client.communication.VaadinSerializer; + +/** + * GWT generator for creating serializer classes for custom classes sent from + * server to client. + * + * Only fields with a correspondingly named setter are deserialized. + * + * @since 7.0 + */ +public class SerializerGenerator extends Generator { + + private String packageName; + private String beanSerializerClassName; + + @Override + public String generate(TreeLogger logger, GeneratorContext context, + String beanTypeName) throws UnableToCompleteException { + String beanSerializerTypeName = beanTypeName + "_Serializer"; + try { + TypeOracle typeOracle = context.getTypeOracle(); + + // get classType and save instance variables + JClassType classType = typeOracle.getType(beanTypeName); + packageName = classType.getPackage().getName(); + beanSerializerClassName = classType.getSimpleSourceName() + + "_Serializer"; + // Generate class source code + generateClass(logger, context, beanTypeName, beanSerializerTypeName); + } catch (Exception e) { + logger.log(TreeLogger.ERROR, "SerializerGenerator failed for " + + beanTypeName, e); + } + // return the fully qualifed name of the class generated + return packageName + "." + beanSerializerClassName; + } + + /** + * Generate source code for a VaadinSerializer implementation. + * + * @param logger + * Logger object + * @param context + * Generator context + * @param beanTypeName + * bean type for which the serializer is to be generated + * @param beanSerializerTypeName + * name of the serializer class to generate + */ + private void generateClass(TreeLogger logger, GeneratorContext context, + String beanTypeName, String beanSerializerTypeName) { + // get print writer that receives the source code + PrintWriter printWriter = null; + printWriter = context.tryCreate(logger, packageName, + beanSerializerClassName); + + // print writer if null, source code has ALREADY been generated + if (printWriter == null) { + return; + } + Date date = new Date(); + TypeOracle typeOracle = context.getTypeOracle(); + logger.log(Type.DEBUG, "Processing serializable type " + beanTypeName + + "..."); + + // init composer, set class properties, create source writer + ClassSourceFileComposerFactory composer = null; + composer = new ClassSourceFileComposerFactory(packageName, + beanSerializerClassName); + composer.addImport(GWT.class.getName()); + composer.addImport(JSONArray.class.getName()); + // composer.addImport(JSONObject.class.getName()); + // composer.addImport(VPaintableMap.class.getName()); + composer.addImport(JsonDecoder.class.getName()); + // composer.addImport(VaadinSerializer.class.getName()); + + composer.addImplementedInterface(VaadinSerializer.class.getName()); + + SourceWriter sourceWriter = composer.createSourceWriter(context, + printWriter); + sourceWriter.indent(); + + sourceWriter.println("public " + beanTypeName + " deserialize(" + + JSONObject.class.getName() + " jsonValue, " + + VPaintableMap.class.getName() + " idMapper) {"); + sourceWriter.indent(); + + // VButtonState state = GWT.create(VButtonState.class); + sourceWriter.println(beanTypeName + " state = GWT.create(" + + beanTypeName + ".class);"); + JClassType beanType = typeOracle.findType(beanTypeName); + for (JMethod method : beanType.getMethods()) { + // Process all setters that have corresponding fields + if (!method.isPublic() || method.isStatic() + || !method.getName().startsWith("set") + || method.getParameterTypes().length != 1) { + // Not setter, skip to next method + continue; + + } + String setterName = method.getName(); + String capitalizedFieldName = setterName.substring(3); + String fieldName = decapitalize(capitalizedFieldName); + JType setterParameterType = method.getParameterTypes()[0]; + + logger.log(Type.DEBUG, "* Processing field " + fieldName + " in " + + beanTypeName); + + String jsonFieldName = "json" + capitalizedFieldName; + // JSONArray jsonHeight = (JSONArray) jsonValue.get("height"); + sourceWriter.println("JSONArray " + jsonFieldName + + " = (JSONArray) jsonValue.get(\"" + fieldName + "\");"); + + // state.setHeight((String) + // JsonDecoder.convertValue(jsonFieldValue,idMapper)); + + String fieldType; + JPrimitiveType primitiveType = setterParameterType.isPrimitive(); + if (primitiveType != null) { + // This is a primitive type -> must used the boxed type + fieldType = primitiveType.getQualifiedBoxedSourceName(); + } else { + fieldType = setterParameterType.getQualifiedSourceName(); + } + + sourceWriter.println("state." + setterName + "((" + fieldType + + ") JsonDecoder.convertValue(" + jsonFieldName + + ", idMapper));"); + } + + // return state; + sourceWriter.println("return state;"); + sourceWriter.println("}"); + sourceWriter.outdent(); + + // End of class + sourceWriter.println("}"); + sourceWriter.outdent(); + + // commit generated class + context.commit(logger, printWriter); + logger.log(Type.INFO, + "Done. (" + (new Date().getTime() - date.getTime()) / 1000 + + "seconds)"); + + } + + private String decapitalize(String name) { + return name.substring(0, 1).toLowerCase() + name.substring(1); + } +} diff --git a/src/com/vaadin/terminal/gwt/widgetsetutils/SerializerMapGenerator.java b/src/com/vaadin/terminal/gwt/widgetsetutils/SerializerMapGenerator.java index 2255dbdd2a..3536cbd117 100644 --- a/src/com/vaadin/terminal/gwt/widgetsetutils/SerializerMapGenerator.java +++ b/src/com/vaadin/terminal/gwt/widgetsetutils/SerializerMapGenerator.java @@ -1,128 +1,128 @@ -/* -@VaadinApache2LicenseForJavaFiles@ - */ - -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; - -/** - * GWT generator that creates a {@link SerializerMap} implementation (mapper - * from type string to serializer instance) and serializer classes for all - * subclasses of {@link SharedState}. - * - * @since 7.0 - */ -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 for SerializerMapImpl - generateClass(logger, context); - - // Generate serializer classes for each subclass of SharedState - JClassType serializerType = typeOracle.findType(SharedState.class - .getName()); - JClassType[] serializerSubtypes = serializerType.getSubtypes(); - SerializerGenerator sg = new SerializerGenerator(); - for (JClassType type : serializerSubtypes) { - sg.generate(logger, context, type.getQualifiedSourceName()); - } - } catch (Exception e) { - logger.log(TreeLogger.ERROR, - "SerializerMapGenerator 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(); - - // TODO cache serializer instances in a map - 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)"); - - } -} +/* +@VaadinApache2LicenseForJavaFiles@ + */ + +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; + +/** + * GWT generator that creates a {@link SerializerMap} implementation (mapper + * from type string to serializer instance) and serializer classes for all + * subclasses of {@link SharedState}. + * + * @since 7.0 + */ +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 for SerializerMapImpl + generateClass(logger, context); + + // Generate serializer classes for each subclass of SharedState + JClassType serializerType = typeOracle.findType(SharedState.class + .getName()); + JClassType[] serializerSubtypes = serializerType.getSubtypes(); + SerializerGenerator sg = new SerializerGenerator(); + for (JClassType type : serializerSubtypes) { + sg.generate(logger, context, type.getQualifiedSourceName()); + } + } catch (Exception e) { + logger.log(TreeLogger.ERROR, + "SerializerMapGenerator 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(); + + // TODO cache serializer instances in a map + 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