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

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