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.

RpcProxyGenerator.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 com.google.gwt.core.ext.Generator;
  19. import com.google.gwt.core.ext.GeneratorContext;
  20. import com.google.gwt.core.ext.TreeLogger;
  21. import com.google.gwt.core.ext.TreeLogger.Type;
  22. import com.google.gwt.core.ext.UnableToCompleteException;
  23. import com.google.gwt.core.ext.typeinfo.JClassType;
  24. import com.google.gwt.core.ext.typeinfo.JMethod;
  25. import com.google.gwt.core.ext.typeinfo.JParameter;
  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.shared.communication.MethodInvocation;
  30. import com.vaadin.shared.communication.ServerRpc;
  31. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  32. import com.vaadin.terminal.gwt.client.ServerConnector;
  33. import com.vaadin.terminal.gwt.client.communication.InitializableServerRpc;
  34. /**
  35. * GWT generator that creates client side proxy classes for making RPC calls
  36. * from the client to the server.
  37. *
  38. * GWT.create() calls for interfaces extending {@link ServerRpc} are affected,
  39. * and a proxy implementation is created. Note that the init(...) method of the
  40. * proxy must be called before the proxy is used.
  41. *
  42. * @since 7.0
  43. */
  44. public class RpcProxyGenerator extends Generator {
  45. @Override
  46. public String generate(TreeLogger logger, GeneratorContext ctx,
  47. String requestedClassName) throws UnableToCompleteException {
  48. logger.log(TreeLogger.DEBUG, "Running RpcProxyGenerator", null);
  49. TypeOracle typeOracle = ctx.getTypeOracle();
  50. assert (typeOracle != null);
  51. JClassType requestedType = typeOracle.findType(requestedClassName);
  52. if (requestedType == null) {
  53. logger.log(TreeLogger.ERROR, "Unable to find metadata for type '"
  54. + requestedClassName + "'", null);
  55. throw new UnableToCompleteException();
  56. }
  57. String generatedClassName = "ServerRpc_"
  58. + requestedType.getName().replaceAll("[$.]", "_");
  59. JClassType initializableInterface = typeOracle
  60. .findType(InitializableServerRpc.class.getCanonicalName());
  61. ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(
  62. requestedType.getPackage().getName(), generatedClassName);
  63. composer.addImplementedInterface(requestedType.getQualifiedSourceName());
  64. composer.addImplementedInterface(initializableInterface
  65. .getQualifiedSourceName());
  66. composer.addImport(MethodInvocation.class.getCanonicalName());
  67. PrintWriter printWriter = ctx.tryCreate(logger,
  68. composer.getCreatedPackage(),
  69. composer.getCreatedClassShortName());
  70. if (printWriter != null) {
  71. logger.log(Type.INFO, "Generating client proxy for RPC interface '"
  72. + requestedType.getQualifiedSourceName() + "'");
  73. SourceWriter writer = composer.createSourceWriter(ctx, printWriter);
  74. // constructor
  75. writer.println("public " + generatedClassName + "() {}");
  76. // initialization etc.
  77. writeCommonFieldsAndMethods(logger, writer, typeOracle);
  78. // actual proxy methods forwarding calls to the server
  79. writeRemoteProxyMethods(logger, writer, typeOracle, requestedType,
  80. requestedType.isClassOrInterface().getInheritableMethods());
  81. // End of class
  82. writer.outdent();
  83. writer.println("}");
  84. ctx.commit(logger, printWriter);
  85. }
  86. return composer.getCreatedClassName();
  87. }
  88. private void writeCommonFieldsAndMethods(TreeLogger logger,
  89. SourceWriter writer, TypeOracle typeOracle) {
  90. JClassType applicationConnectionClass = typeOracle
  91. .findType(ApplicationConnection.class.getCanonicalName());
  92. // fields
  93. writer.println("private " + ServerConnector.class.getName()
  94. + " connector;");
  95. // init method from the RPC interface
  96. writer.println("public void initRpc(" + ServerConnector.class.getName()
  97. + " connector) {");
  98. writer.indent();
  99. writer.println("this.connector = connector;");
  100. writer.outdent();
  101. writer.println("}");
  102. }
  103. private static void writeRemoteProxyMethods(TreeLogger logger,
  104. SourceWriter writer, TypeOracle typeOracle,
  105. JClassType requestedType, JMethod[] methods) {
  106. for (JMethod m : methods) {
  107. writer.print(m.getReadableDeclaration(false, false, false, false,
  108. true));
  109. writer.println(" {");
  110. writer.indent();
  111. writer.print("this.connector.getConnection().addMethodInvocationToQueue(new MethodInvocation(this.connector.getConnectorId(), \""
  112. + requestedType.getQualifiedBinaryName() + "\", \"");
  113. writer.print(m.getName());
  114. writer.print("\", new Object[] {");
  115. // new Object[] { ... } for parameters - autoboxing etc. by the
  116. // compiler
  117. JParameter[] parameters = m.getParameters();
  118. boolean first = true;
  119. for (JParameter p : parameters) {
  120. if (!first) {
  121. writer.print(", ");
  122. }
  123. first = false;
  124. writer.print(p.getName());
  125. }
  126. writer.println("}), true);");
  127. writer.outdent();
  128. writer.println("}");
  129. }
  130. }
  131. }