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.

AcceptCriteriaFactoryGenerator.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright 2000-2022 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. @SuppressWarnings("deprecation")
  40. public class AcceptCriteriaFactoryGenerator extends Generator {
  41. private String packageName;
  42. private String className;
  43. @Override
  44. public String generate(TreeLogger logger, GeneratorContext context,
  45. String typeName) throws UnableToCompleteException {
  46. try {
  47. TypeOracle typeOracle = context.getTypeOracle();
  48. // get classType and save instance variables
  49. JClassType classType = typeOracle.getType(typeName);
  50. packageName = classType.getPackage().getName();
  51. className = classType.getSimpleSourceName() + "Impl";
  52. // Generate class source code
  53. generateClass(logger, context);
  54. } catch (Exception e) {
  55. logger.log(TreeLogger.ERROR,
  56. "Accept criterion factory creation failed", e);
  57. }
  58. // return the fully qualifed name of the class generated
  59. return packageName + "." + className;
  60. }
  61. /**
  62. * Generate source code for WidgetMapImpl
  63. *
  64. * @param logger
  65. * Logger object
  66. * @param context
  67. * Generator context
  68. */
  69. private void generateClass(TreeLogger logger, GeneratorContext context) {
  70. // get print writer that receives the source code
  71. PrintWriter printWriter = context.tryCreate(logger, packageName,
  72. className);
  73. // print writer if null, source code has ALREADY been generated,
  74. // return (WidgetMap is equal to all permutations atm)
  75. if (printWriter == null) {
  76. return;
  77. }
  78. logger.log(Type.INFO, "Detecting available criteria ...");
  79. Date date = new Date();
  80. // init composer, set class properties, create source writer
  81. ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(
  82. packageName, className);
  83. composer.addImport("com.google.gwt.core.client.GWT");
  84. composer.setSuperclass(
  85. "com.vaadin.client.ui.dd.VAcceptCriterionFactory");
  86. SourceWriter sourceWriter = composer.createSourceWriter(context,
  87. printWriter);
  88. // generator constructor source code
  89. generateInstantiatorMethod(sourceWriter, context, logger);
  90. // close generated class
  91. sourceWriter.outdent();
  92. sourceWriter.println("}");
  93. // commit generated class
  94. context.commit(logger, printWriter);
  95. logger.log(Type.INFO, "Done. ("
  96. + (new Date().getTime() - date.getTime()) / 1000 + "seconds)");
  97. }
  98. private void generateInstantiatorMethod(SourceWriter sourceWriter,
  99. GeneratorContext context, TreeLogger logger) {
  100. sourceWriter.println("public VAcceptCriterion get(String name) {");
  101. sourceWriter.indent();
  102. sourceWriter.println("name = name.intern();");
  103. JClassType criteriaType = context.getTypeOracle()
  104. .findType(VAcceptCriterion.class.getName());
  105. JClassType[] subtypes = criteriaType.getSubtypes();
  106. Arrays.sort(subtypes, ConnectorBundle.jClassComparator);
  107. for (JClassType clientClass : subtypes) {
  108. AcceptCriterion annotation = clientClass
  109. .getAnnotation(AcceptCriterion.class);
  110. if (annotation != null) {
  111. String clientClassName = clientClass.getQualifiedSourceName();
  112. Class<?> serverClass = clientClass
  113. .getAnnotation(AcceptCriterion.class).value();
  114. String serverClassName = serverClass.getCanonicalName();
  115. logger.log(Type.INFO,
  116. "creating mapping for " + serverClassName);
  117. sourceWriter.print("if (\"");
  118. sourceWriter.print(serverClassName);
  119. sourceWriter.print("\" == name) return GWT.create(");
  120. sourceWriter.print(clientClassName);
  121. sourceWriter.println(".class );");
  122. sourceWriter.print("else ");
  123. }
  124. }
  125. sourceWriter.println("return null;");
  126. sourceWriter.outdent();
  127. sourceWriter.println("}");
  128. }
  129. }