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.

AbstractConnectorClassBasedFactoryGenerator.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright 2011 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.terminal.gwt.widgetsetutils;
  17. import java.io.PrintWriter;
  18. import java.util.Date;
  19. import com.google.gwt.core.client.GWT;
  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.JMethod;
  27. import com.google.gwt.core.ext.typeinfo.JType;
  28. import com.google.gwt.core.ext.typeinfo.NotFoundException;
  29. import com.google.gwt.core.ext.typeinfo.TypeOracle;
  30. import com.google.gwt.user.rebind.ClassSourceFileComposerFactory;
  31. import com.google.gwt.user.rebind.SourceWriter;
  32. import com.vaadin.terminal.gwt.client.ServerConnector;
  33. import com.vaadin.terminal.gwt.client.ui.ConnectorClassBasedFactory;
  34. import com.vaadin.terminal.gwt.client.ui.ConnectorClassBasedFactory.Creator;
  35. /**
  36. * GWT generator that creates a lookup method for
  37. * {@link ConnectorClassBasedFactory} instances.
  38. *
  39. * @since 7.0
  40. */
  41. public abstract class AbstractConnectorClassBasedFactoryGenerator extends
  42. Generator {
  43. @Override
  44. public String generate(TreeLogger logger, GeneratorContext context,
  45. String typeName) throws UnableToCompleteException {
  46. try {
  47. // get classType and save instance variables
  48. return generateConnectorClassBasedFactory(typeName, logger, context);
  49. } catch (Exception e) {
  50. logger.log(TreeLogger.ERROR, typeName + " creation failed", e);
  51. throw new UnableToCompleteException();
  52. }
  53. }
  54. private String generateConnectorClassBasedFactory(String typeName,
  55. TreeLogger logger, GeneratorContext context)
  56. throws NotFoundException {
  57. TypeOracle typeOracle = context.getTypeOracle();
  58. JClassType classType = typeOracle.getType(typeName);
  59. String superName = classType.getSimpleSourceName();
  60. String packageName = classType.getPackage().getName();
  61. String className = superName + "Impl";
  62. // get print writer that receives the source code
  63. PrintWriter printWriter = null;
  64. printWriter = context.tryCreate(logger, packageName, className);
  65. // print writer if null, source code has ALREADY been generated
  66. if (printWriter == null) {
  67. return packageName + "." + className;
  68. }
  69. Date date = new Date();
  70. // init composer, set class properties, create source writer
  71. ClassSourceFileComposerFactory composer = null;
  72. composer = new ClassSourceFileComposerFactory(packageName, className);
  73. composer.addImport(GWT.class.getName());
  74. composer.addImport(Creator.class.getCanonicalName());
  75. composer.setSuperclass(superName);
  76. SourceWriter sourceWriter = composer.createSourceWriter(context,
  77. printWriter);
  78. sourceWriter.indent();
  79. // public ConnectorStateFactoryImpl() {
  80. sourceWriter.println("public " + className + "() {");
  81. sourceWriter.indent();
  82. JClassType serverConnectorType = typeOracle.getType(getConnectorType()
  83. .getCanonicalName());
  84. for (JClassType connector : serverConnectorType.getSubtypes()) {
  85. // addCreator(TextAreaConnector.class, new Creator<SharedState>() {
  86. if (connector.isInterface() != null || connector.isAbstract()) {
  87. continue;
  88. }
  89. JClassType targetType = getTargetType(connector);
  90. if (targetType.isAbstract()) {
  91. continue;
  92. }
  93. sourceWriter.println("addCreator("
  94. + connector.getQualifiedSourceName()
  95. + ".class, new Creator<"
  96. + targetType.getQualifiedSourceName() + ">() {");
  97. // public SharedState create() {
  98. sourceWriter.println("public "
  99. + targetType.getQualifiedSourceName() + " create() {");
  100. // return GWT.create(TextAreaState.class);
  101. sourceWriter.println("return GWT.create("
  102. + targetType.getQualifiedSourceName() + ".class);");
  103. // }
  104. sourceWriter.println("}");
  105. // });
  106. sourceWriter.println("});");
  107. }
  108. // End of constructor
  109. sourceWriter.outdent();
  110. sourceWriter.println("}");
  111. // close generated class
  112. sourceWriter.outdent();
  113. sourceWriter.println("}");
  114. // commit generated class
  115. context.commit(logger, printWriter);
  116. logger.log(Type.INFO,
  117. "Done. (" + (new Date().getTime() - date.getTime()) / 1000
  118. + "seconds)");
  119. return packageName + "." + className;
  120. }
  121. protected abstract Class<? extends ServerConnector> getConnectorType();
  122. protected abstract JClassType getTargetType(JClassType connectorType);
  123. protected JClassType getGetterReturnType(JClassType connector,
  124. String getterName) {
  125. try {
  126. JMethod getMethod = connector.getMethod(getterName, new JType[] {});
  127. return (JClassType) getMethod.getReturnType();
  128. } catch (NotFoundException e) {
  129. return getGetterReturnType(connector.getSuperclass(), getterName);
  130. }
  131. }
  132. }