Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

AcceptCriteriaFactoryGenerator.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.server.widgetsetutils;
  17. import java.io.PrintWriter;
  18. import java.util.Arrays;
  19. import java.util.Date;
  20. import com.google.gwt.core.ext.Generator;
  21. import com.google.gwt.core.ext.GeneratorContext;
  22. import com.google.gwt.core.ext.TreeLogger;
  23. import com.google.gwt.core.ext.TreeLogger.Type;
  24. import com.google.gwt.core.ext.UnableToCompleteException;
  25. import com.google.gwt.core.ext.typeinfo.JClassType;
  26. import com.google.gwt.core.ext.typeinfo.TypeOracle;
  27. import com.google.gwt.user.rebind.ClassSourceFileComposerFactory;
  28. import com.google.gwt.user.rebind.SourceWriter;
  29. import com.vaadin.client.ui.dd.VAcceptCriterion;
  30. import com.vaadin.client.ui.dd.VAcceptCriterionFactory;
  31. import com.vaadin.server.widgetsetutils.metadata.ConnectorBundle;
  32. import com.vaadin.shared.ui.dd.AcceptCriterion;
  33. /**
  34. * GWT generator to build {@link VAcceptCriterionFactory} implementation
  35. * dynamically based on {@link AcceptCriterion} annotations available in
  36. * classpath.
  37. *
  38. */
  39. public class AcceptCriteriaFactoryGenerator extends Generator {
  40. private String packageName;
  41. private String className;
  42. @Override
  43. public String generate(TreeLogger logger, GeneratorContext context,
  44. String typeName) throws UnableToCompleteException {
  45. try {
  46. TypeOracle typeOracle = context.getTypeOracle();
  47. // get classType and save instance variables
  48. JClassType classType = typeOracle.getType(typeName);
  49. packageName = classType.getPackage().getName();
  50. className = classType.getSimpleSourceName() + "Impl";
  51. // Generate class source code
  52. generateClass(logger, context);
  53. } catch (Exception e) {
  54. logger.log(TreeLogger.ERROR,
  55. "Accept criterion factory creation failed", e);
  56. }
  57. // return the fully qualifed name of the class generated
  58. return packageName + "." + className;
  59. }
  60. /**
  61. * Generate source code for WidgetMapImpl
  62. *
  63. * @param logger
  64. * Logger object
  65. * @param context
  66. * Generator context
  67. */
  68. private void generateClass(TreeLogger logger, GeneratorContext context) {
  69. // get print writer that receives the source code
  70. PrintWriter printWriter = null;
  71. printWriter = context.tryCreate(logger, packageName, className);
  72. // print writer if null, source code has ALREADY been generated,
  73. // return (WidgetMap is equal to all permutations atm)
  74. if (printWriter == null) {
  75. return;
  76. }
  77. logger.log(Type.INFO, "Detecting available criteria ...");
  78. Date date = new Date();
  79. // init composer, set class properties, create source writer
  80. ClassSourceFileComposerFactory composer = null;
  81. composer = new ClassSourceFileComposerFactory(packageName, className);
  82. composer.addImport("com.google.gwt.core.client.GWT");
  83. composer.setSuperclass(
  84. "com.vaadin.client.ui.dd.VAcceptCriterionFactory");
  85. SourceWriter sourceWriter = composer.createSourceWriter(context,
  86. printWriter);
  87. // generator constructor source code
  88. generateInstantiatorMethod(sourceWriter, context, logger);
  89. // close generated class
  90. sourceWriter.outdent();
  91. sourceWriter.println("}");
  92. // commit generated class
  93. context.commit(logger, printWriter);
  94. logger.log(Type.INFO, "Done. ("
  95. + (new Date().getTime() - date.getTime()) / 1000 + "seconds)");
  96. }
  97. private void generateInstantiatorMethod(SourceWriter sourceWriter,
  98. GeneratorContext context, TreeLogger logger) {
  99. sourceWriter.println("public VAcceptCriterion get(String name) {");
  100. sourceWriter.indent();
  101. sourceWriter.println("name = name.intern();");
  102. JClassType criteriaType = context.getTypeOracle()
  103. .findType(VAcceptCriterion.class.getName());
  104. JClassType[] subtypes = criteriaType.getSubtypes();
  105. Arrays.sort(subtypes, ConnectorBundle.jClassComparator);
  106. for (JClassType clientClass : subtypes) {
  107. AcceptCriterion annotation = clientClass
  108. .getAnnotation(AcceptCriterion.class);
  109. if (annotation != null) {
  110. String clientClassName = clientClass.getQualifiedSourceName();
  111. Class<?> serverClass = clientClass
  112. .getAnnotation(AcceptCriterion.class).value();
  113. String serverClassName = serverClass.getCanonicalName();
  114. logger.log(Type.INFO,
  115. "creating mapping for " + serverClassName);
  116. sourceWriter.print("if (\"");
  117. sourceWriter.print(serverClassName);
  118. sourceWriter.print("\" == name) return GWT.create(");
  119. sourceWriter.print(clientClassName);
  120. sourceWriter.println(".class );");
  121. sourceWriter.print("else ");
  122. }
  123. }
  124. sourceWriter.println("return null;");
  125. sourceWriter.outdent();
  126. sourceWriter.println("}");
  127. }
  128. }