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.

RpcManagerGenerator.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.widgetsetutils;
  5. import java.io.PrintWriter;
  6. import java.util.ArrayList;
  7. import java.util.Date;
  8. import java.util.List;
  9. import com.google.gwt.core.ext.Generator;
  10. import com.google.gwt.core.ext.GeneratorContext;
  11. import com.google.gwt.core.ext.TreeLogger;
  12. import com.google.gwt.core.ext.TreeLogger.Type;
  13. import com.google.gwt.core.ext.UnableToCompleteException;
  14. import com.google.gwt.core.ext.typeinfo.JClassType;
  15. import com.google.gwt.core.ext.typeinfo.JMethod;
  16. import com.google.gwt.core.ext.typeinfo.JType;
  17. import com.google.gwt.core.ext.typeinfo.TypeOracle;
  18. import com.google.gwt.user.rebind.ClassSourceFileComposerFactory;
  19. import com.google.gwt.user.rebind.SourceWriter;
  20. import com.vaadin.terminal.gwt.client.ServerConnector;
  21. import com.vaadin.terminal.gwt.client.ConnectorMap;
  22. import com.vaadin.terminal.gwt.client.communication.ClientRpc;
  23. import com.vaadin.terminal.gwt.client.communication.MethodInvocation;
  24. import com.vaadin.terminal.gwt.client.communication.RpcManager;
  25. /**
  26. * GWT generator that creates an implementation for {@link RpcManager} on the
  27. * client side classes for executing RPC calls received from the the server.
  28. *
  29. * @since 7.0
  30. */
  31. public class RpcManagerGenerator extends Generator {
  32. @Override
  33. public String generate(TreeLogger logger, GeneratorContext context,
  34. String typeName) throws UnableToCompleteException {
  35. String packageName = null;
  36. String className = null;
  37. try {
  38. TypeOracle typeOracle = context.getTypeOracle();
  39. // get classType and save instance variables
  40. JClassType classType = typeOracle.getType(typeName);
  41. packageName = classType.getPackage().getName();
  42. className = classType.getSimpleSourceName() + "Impl";
  43. // Generate class source code for SerializerMapImpl
  44. generateClass(logger, context, packageName, className);
  45. } catch (Exception e) {
  46. logger.log(TreeLogger.ERROR,
  47. "SerializerMapGenerator creation failed", e);
  48. }
  49. // return the fully qualifed name of the class generated
  50. return packageName + "." + className;
  51. }
  52. /**
  53. * Generate source code for RpcManagerImpl
  54. *
  55. * @param logger
  56. * Logger object
  57. * @param context
  58. * Generator context
  59. * @param packageName
  60. * package name for the class to generate
  61. * @param className
  62. * class name for the class to generate
  63. */
  64. private void generateClass(TreeLogger logger, GeneratorContext context,
  65. String packageName, String className) {
  66. // get print writer that receives the source code
  67. PrintWriter printWriter = null;
  68. printWriter = context.tryCreate(logger, packageName, className);
  69. // print writer if null, source code has ALREADY been generated
  70. if (printWriter == null) {
  71. return;
  72. }
  73. logger.log(Type.INFO,
  74. "Detecting server to client RPC interface types...");
  75. Date date = new Date();
  76. TypeOracle typeOracle = context.getTypeOracle();
  77. JClassType serverToClientRpcType = typeOracle.findType(ClientRpc.class
  78. .getName());
  79. JClassType[] rpcInterfaceSubtypes = serverToClientRpcType.getSubtypes();
  80. // init composer, set class properties, create source writer
  81. ClassSourceFileComposerFactory composer = null;
  82. composer = new ClassSourceFileComposerFactory(packageName, className);
  83. composer.addImport("com.google.gwt.core.client.GWT");
  84. composer.addImplementedInterface(RpcManager.class.getName());
  85. SourceWriter sourceWriter = composer.createSourceWriter(context,
  86. printWriter);
  87. sourceWriter.indent();
  88. List<JClassType> rpcInterfaces = new ArrayList<JClassType>();
  89. // iterate over RPC interfaces and create helper methods for each
  90. // interface
  91. for (JClassType type : rpcInterfaceSubtypes) {
  92. if (null == type.isInterface()) {
  93. // only interested in interfaces here, not implementations
  94. continue;
  95. }
  96. rpcInterfaces.add(type);
  97. // generate method to call methods of an RPC interface
  98. sourceWriter.println("private void " + getInvokeMethodName(type)
  99. + "(" + MethodInvocation.class.getName() + " invocation, "
  100. + ConnectorMap.class.getName() + " connectorMap) {");
  101. sourceWriter.indent();
  102. // loop over the methods of the interface and its superinterfaces
  103. // methods
  104. for (JClassType currentType : type.getFlattenedSupertypeHierarchy()) {
  105. for (JMethod method : currentType.getMethods()) {
  106. sourceWriter.println("if (\"" + method.getName()
  107. + "\".equals(invocation.getMethodName())) {");
  108. sourceWriter.indent();
  109. // construct parameter string with appropriate casts
  110. String paramString = "";
  111. JType[] parameterTypes = method.getParameterTypes();
  112. for (int i = 0; i < parameterTypes.length; ++i) {
  113. paramString = paramString + "("
  114. + parameterTypes[i].getQualifiedSourceName()
  115. + ") invocation.getParameters()[" + i + "]";
  116. if (i < parameterTypes.length - 1) {
  117. paramString = paramString + ", ";
  118. }
  119. }
  120. sourceWriter
  121. .println(ServerConnector.class.getName()
  122. + " connector = connectorMap.getConnector(invocation.getConnectorId());");
  123. sourceWriter
  124. .println("for ("
  125. + ClientRpc.class.getName()
  126. + " rpcImplementation : connector.getRpcImplementations(\""
  127. + type.getQualifiedSourceName() + "\")) {");
  128. sourceWriter.indent();
  129. sourceWriter.println("((" + type.getQualifiedSourceName()
  130. + ") rpcImplementation)." + method.getName() + "("
  131. + paramString + ");");
  132. sourceWriter.outdent();
  133. sourceWriter.println("}");
  134. sourceWriter.println("return;");
  135. sourceWriter.outdent();
  136. sourceWriter.println("}");
  137. }
  138. }
  139. sourceWriter.outdent();
  140. sourceWriter.println("}");
  141. logger.log(Type.DEBUG,
  142. "Constructed helper method for server to client RPC for "
  143. + type.getName());
  144. }
  145. // generate top-level "switch-case" method to select the correct
  146. // previously generated method based on the RPC interface
  147. sourceWriter.println("public void applyInvocation("
  148. + MethodInvocation.class.getName() + " invocation, "
  149. + ConnectorMap.class.getName() + " connectorMap) {");
  150. sourceWriter.indent();
  151. for (JClassType type : rpcInterfaces) {
  152. sourceWriter.println("if (\"" + type.getQualifiedSourceName()
  153. + "\".equals(invocation.getInterfaceName())) {");
  154. sourceWriter.indent();
  155. sourceWriter.println(getInvokeMethodName(type)
  156. + "(invocation, connectorMap);");
  157. sourceWriter.println("return;");
  158. sourceWriter.outdent();
  159. sourceWriter.println("}");
  160. logger.log(Type.INFO,
  161. "Configured server to client RPC for " + type.getName());
  162. }
  163. sourceWriter.outdent();
  164. sourceWriter.println("}");
  165. // close generated class
  166. sourceWriter.outdent();
  167. sourceWriter.println("}");
  168. // commit generated class
  169. context.commit(logger, printWriter);
  170. logger.log(Type.INFO,
  171. "Done. (" + (new Date().getTime() - date.getTime()) / 1000
  172. + "seconds)");
  173. }
  174. private String getInvokeMethodName(JClassType type) {
  175. return "invoke" + type.getQualifiedSourceName().replaceAll("\\.", "_");
  176. }
  177. }