You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WidgetMapGenerator.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.widgetsetutils;
  5. import java.io.PrintWriter;
  6. import java.util.Collection;
  7. import java.util.Date;
  8. import java.util.Iterator;
  9. import java.util.TreeSet;
  10. import com.google.gwt.core.ext.Generator;
  11. import com.google.gwt.core.ext.GeneratorContext;
  12. import com.google.gwt.core.ext.TreeLogger;
  13. import com.google.gwt.core.ext.UnableToCompleteException;
  14. import com.google.gwt.core.ext.TreeLogger.Type;
  15. import com.google.gwt.core.ext.typeinfo.JClassType;
  16. import com.google.gwt.core.ext.typeinfo.TypeOracle;
  17. import com.google.gwt.user.rebind.ClassSourceFileComposerFactory;
  18. import com.google.gwt.user.rebind.SourceWriter;
  19. import com.vaadin.terminal.Paintable;
  20. import com.vaadin.terminal.gwt.client.ui.VView;
  21. import com.vaadin.ui.ClientWidget;
  22. /**
  23. * GWT generator to build WidgetMapImpl dynamically based on
  24. * {@link ClientWidget} annotations available in workspace.
  25. *
  26. */
  27. public class WidgetMapGenerator extends Generator {
  28. private String packageName;
  29. private String className;
  30. @Override
  31. public String generate(TreeLogger logger, GeneratorContext context,
  32. String typeName) throws UnableToCompleteException {
  33. try {
  34. TypeOracle typeOracle = context.getTypeOracle();
  35. // get classType and save instance variables
  36. JClassType classType = typeOracle.getType(typeName);
  37. packageName = classType.getPackage().getName();
  38. className = classType.getSimpleSourceName() + "Impl";
  39. // Generate class source code
  40. generateClass(logger, context);
  41. } catch (Exception e) {
  42. logger.log(TreeLogger.ERROR, "WidgetMap creation failed", e);
  43. }
  44. // return the fully qualifed name of the class generated
  45. return packageName + "." + className;
  46. }
  47. /**
  48. * Generate source code for WidgetMapImpl
  49. *
  50. * @param logger
  51. * Logger object
  52. * @param context
  53. * Generator context
  54. */
  55. private void generateClass(TreeLogger logger, GeneratorContext context) {
  56. // get print writer that receives the source code
  57. PrintWriter printWriter = null;
  58. printWriter = context.tryCreate(logger, packageName, className);
  59. // print writer if null, source code has ALREADY been generated,
  60. // return (WidgetMap is equal to all permutations atm)
  61. if (printWriter == null) {
  62. return;
  63. }
  64. logger
  65. .log(Type.INFO,
  66. "Detecting Vaadin components in classpath to generate WidgetMapImpl.java ...");
  67. Date date = new Date();
  68. // init composer, set class properties, create source writer
  69. ClassSourceFileComposerFactory composer = null;
  70. composer = new ClassSourceFileComposerFactory(packageName, className);
  71. composer.addImport("com.google.gwt.core.client.GWT");
  72. composer.addImport("java.util.HashMap");
  73. composer.addImport("com.google.gwt.core.client.RunAsyncCallback");
  74. composer.setSuperclass("com.vaadin.terminal.gwt.client.WidgetMap");
  75. SourceWriter sourceWriter = composer.createSourceWriter(context,
  76. printWriter);
  77. Collection<Class<? extends Paintable>> paintablesHavingWidgetAnnotation = getUsedPaintables();
  78. validatePaintables(logger, context, paintablesHavingWidgetAnnotation);
  79. // generator constructor source code
  80. generateImplementationDetector(sourceWriter,
  81. paintablesHavingWidgetAnnotation);
  82. generateInstantiatorMethod(sourceWriter,
  83. paintablesHavingWidgetAnnotation);
  84. // close generated class
  85. sourceWriter.outdent();
  86. sourceWriter.println("}");
  87. // commit generated class
  88. context.commit(logger, printWriter);
  89. logger.log(Type.INFO, "Done. ("
  90. + (new Date().getTime() - date.getTime()) / 1000 + "seconds)");
  91. }
  92. /**
  93. * Verifies that all client side components are available for client side
  94. * GWT module.
  95. *
  96. * @param logger
  97. * @param context
  98. * @param paintablesHavingWidgetAnnotation
  99. */
  100. private void validatePaintables(
  101. TreeLogger logger,
  102. GeneratorContext context,
  103. Collection<Class<? extends Paintable>> paintablesHavingWidgetAnnotation) {
  104. TypeOracle typeOracle = context.getTypeOracle();
  105. for (Iterator<Class<? extends Paintable>> iterator = paintablesHavingWidgetAnnotation
  106. .iterator(); iterator.hasNext();) {
  107. Class<? extends Paintable> class1 = iterator.next();
  108. ClientWidget annotation = class1.getAnnotation(ClientWidget.class);
  109. if (typeOracle.findType(annotation.value().getName()) == null) {
  110. // GWT widget not inherited
  111. logger.log(Type.WARN, "Widget class "
  112. + annotation.value().getName()
  113. + " was not found. The component " + class1.getName()
  114. + " will not be included in the widgetset.");
  115. iterator.remove();
  116. }
  117. }
  118. logger
  119. .log(Type.INFO,
  120. "Widget set will contain implementations for following components: ");
  121. TreeSet<String> classNames = new TreeSet<String>();
  122. for (Class<? extends Paintable> class1 : paintablesHavingWidgetAnnotation) {
  123. classNames.add(class1.getCanonicalName());
  124. }
  125. for (String string : classNames) {
  126. logger.log(Type.INFO, "\t" + string);
  127. }
  128. }
  129. /**
  130. * This method is protected to allow creation of optimized widgetsets. The
  131. * Widgetset will contain only implementation returned by this function. If
  132. * one knows which widgets are needed for the application, returning only
  133. * them here will significantly optimize the size of the produced JS.
  134. *
  135. * @return a collections of Vaadin components that will be added to
  136. * widgetset
  137. */
  138. protected Collection<Class<? extends Paintable>> getUsedPaintables() {
  139. return ClassPathExplorer.getPaintablesHavingWidgetAnnotation();
  140. }
  141. /**
  142. * Returns true if the widget for given component will be lazy loaded by the
  143. * client. The default implementation reads the information from the
  144. * {@link ClientWidget} annotation.
  145. * <p>
  146. * The method can be overridden to optimize the widget loading mechanism. If
  147. * the Widgetset is wanted to be optimized for a network with a high latency
  148. * or for a one with a very fast throughput, it may be good to return false
  149. * for every component.
  150. *
  151. * @param paintableType
  152. * @return true iff the widget for given component should be lazy loaded by
  153. * the client side engine
  154. */
  155. protected boolean isLazyLoaded(Class<? extends Paintable> paintableType) {
  156. ClientWidget annotation = paintableType
  157. .getAnnotation(ClientWidget.class);
  158. return annotation.lazyLoad();
  159. }
  160. private void generateInstantiatorMethod(
  161. SourceWriter sourceWriter,
  162. Collection<Class<? extends Paintable>> paintablesHavingWidgetAnnotation) {
  163. sourceWriter
  164. .println("public interface Instantiator { public Paintable get();};");
  165. // TODO detect if it would be noticably faster to instantiate with a
  166. // lookup with index than with the hashmap
  167. sourceWriter
  168. .println("private HashMap<Class,Instantiator> instmap = new HashMap<Class,Instantiator>();");
  169. sourceWriter
  170. .println("public void ensureInstantiator(Class<? extends Paintable> classType, final ApplicationConfiguration c) {");
  171. sourceWriter.println("if(!instmap.containsKey(classType)){");
  172. for (Class<? extends Paintable> class1 : paintablesHavingWidgetAnnotation) {
  173. ClientWidget annotation = class1.getAnnotation(ClientWidget.class);
  174. Class<? extends com.vaadin.terminal.gwt.client.Paintable> clientClass = annotation
  175. .value();
  176. if (clientClass == VView.class) {
  177. // VView's are not instantiated by widgetset
  178. continue;
  179. }
  180. sourceWriter.print("if( classType == " + clientClass.getName()
  181. + ".class) {");
  182. String instantiator = "new Instantiator() { public Paintable get(){ return GWT.create("
  183. + clientClass.getName() + ".class );}}";
  184. if (isLazyLoaded(class1)) {
  185. sourceWriter
  186. .print("c.widgetLoadStart();GWT.runAsync(new RunAsyncCallback() {\n"
  187. + " public void onSuccess() {");
  188. sourceWriter.print("instmap.put(");
  189. sourceWriter.print(clientClass.getName());
  190. sourceWriter.print(".class, ");
  191. sourceWriter.print(instantiator);
  192. sourceWriter.println("); c.widgetLoaded();");
  193. sourceWriter
  194. .print(" }\n"
  195. + "\n"
  196. + " public void onFailure(Throwable reason) {c.widgetLoaded();\n"
  197. + "\n" + " }\n" + " });\n");
  198. } else {
  199. // widget implementation in initially loaded js script
  200. sourceWriter.print("instmap.put(");
  201. sourceWriter.print(clientClass.getName());
  202. sourceWriter.print(".class, ");
  203. sourceWriter.print(instantiator);
  204. sourceWriter.print(");");
  205. }
  206. sourceWriter.print("}");
  207. }
  208. sourceWriter.println("}");
  209. sourceWriter.println("}");
  210. sourceWriter
  211. .println("public Paintable instantiate(Class<? extends Paintable> classType) {");
  212. sourceWriter.indent();
  213. sourceWriter
  214. .println("Paintable p = super.instantiate(classType); if(p!= null) return p;");
  215. sourceWriter.println("return instmap.get(classType).get();");
  216. sourceWriter.outdent();
  217. sourceWriter.println("}");
  218. }
  219. /**
  220. *
  221. * @param sourceWriter
  222. * Source writer to output source code
  223. * @param paintablesHavingWidgetAnnotation
  224. */
  225. private void generateImplementationDetector(
  226. SourceWriter sourceWriter,
  227. Collection<Class<? extends Paintable>> paintablesHavingWidgetAnnotation) {
  228. sourceWriter
  229. .println("public Class<? extends Paintable> "
  230. + "getImplementationByServerSideClassName(String fullyQualifiedName, ApplicationConfiguration c) {");
  231. sourceWriter.indent();
  232. sourceWriter
  233. .println("fullyQualifiedName = fullyQualifiedName.intern();");
  234. for (Class<? extends Paintable> class1 : paintablesHavingWidgetAnnotation) {
  235. ClientWidget annotation = class1.getAnnotation(ClientWidget.class);
  236. Class<? extends com.vaadin.terminal.gwt.client.Paintable> clientClass = annotation
  237. .value();
  238. sourceWriter.print("if ( fullyQualifiedName == \"");
  239. sourceWriter.print(class1.getName());
  240. sourceWriter.print("\" ) { ensureInstantiator("
  241. + clientClass.getName() + ".class, c); return ");
  242. sourceWriter.print(clientClass.getName());
  243. sourceWriter.println(".class;}");
  244. sourceWriter.print("else ");
  245. }
  246. sourceWriter
  247. .println("return com.vaadin.terminal.gwt.client.ui.VUnknownComponent.class;");
  248. sourceWriter.outdent();
  249. sourceWriter.println("}");
  250. }
  251. }