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.4KB

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