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.

RpcProxyCreatorGenerator.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.TypeOracle;
  27. import com.google.gwt.user.rebind.ClassSourceFileComposerFactory;
  28. import com.google.gwt.user.rebind.SourceWriter;
  29. import com.vaadin.shared.communication.ServerRpc;
  30. import com.vaadin.terminal.gwt.client.ServerConnector;
  31. import com.vaadin.terminal.gwt.client.communication.InitializableServerRpc;
  32. import com.vaadin.terminal.gwt.client.communication.RpcProxy.RpcProxyCreator;
  33. public class RpcProxyCreatorGenerator extends Generator {
  34. @Override
  35. public String generate(TreeLogger logger, GeneratorContext ctx,
  36. String requestedClassName) throws UnableToCompleteException {
  37. logger.log(TreeLogger.DEBUG, "Running RpcProxyCreatorGenerator");
  38. TypeOracle typeOracle = ctx.getTypeOracle();
  39. assert (typeOracle != null);
  40. JClassType requestedType = typeOracle.findType(requestedClassName);
  41. if (requestedType == null) {
  42. logger.log(TreeLogger.ERROR, "Unable to find metadata for type '"
  43. + requestedClassName + "'", null);
  44. throw new UnableToCompleteException();
  45. }
  46. String packageName = requestedType.getPackage().getName();
  47. String className = requestedType.getSimpleSourceName() + "Impl";
  48. createType(logger, ctx, packageName, className);
  49. return packageName + "." + className;
  50. }
  51. private void createType(TreeLogger logger, GeneratorContext context,
  52. String packageName, String className) {
  53. ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(
  54. packageName, className);
  55. PrintWriter printWriter = context.tryCreate(logger,
  56. composer.getCreatedPackage(),
  57. composer.getCreatedClassShortName());
  58. if (printWriter == null) {
  59. // print writer is null if source code has already been generated
  60. return;
  61. }
  62. Date date = new Date();
  63. TypeOracle typeOracle = context.getTypeOracle();
  64. // init composer, set class properties, create source writer
  65. composer.addImport(GWT.class.getCanonicalName());
  66. composer.addImport(ServerRpc.class.getCanonicalName());
  67. composer.addImport(ServerConnector.class.getCanonicalName());
  68. composer.addImport(InitializableServerRpc.class.getCanonicalName());
  69. composer.addImport(IllegalArgumentException.class.getCanonicalName());
  70. composer.addImplementedInterface(RpcProxyCreator.class
  71. .getCanonicalName());
  72. SourceWriter sourceWriter = composer.createSourceWriter(context,
  73. printWriter);
  74. sourceWriter.indent();
  75. sourceWriter
  76. .println("public <T extends ServerRpc> T create(Class<T> rpcInterface, ServerConnector connector) {");
  77. sourceWriter.indent();
  78. sourceWriter
  79. .println("if (rpcInterface == null || connector == null) {");
  80. sourceWriter.indent();
  81. sourceWriter
  82. .println("throw new IllegalArgumentException(\"RpcInterface and/or connector cannot be null\");");
  83. sourceWriter.outdent();
  84. JClassType initializableInterface = typeOracle.findType(ServerRpc.class
  85. .getCanonicalName());
  86. for (JClassType rpcType : initializableInterface.getSubtypes()) {
  87. String rpcClassName = rpcType.getQualifiedSourceName();
  88. if (InitializableServerRpc.class.getCanonicalName().equals(
  89. rpcClassName)) {
  90. // InitializableClientToServerRpc is a special marker interface
  91. // that should not get a generated class
  92. continue;
  93. }
  94. sourceWriter.println("} else if (rpcInterface == " + rpcClassName
  95. + ".class) {");
  96. sourceWriter.indent();
  97. sourceWriter.println(rpcClassName + " rpc = GWT.create("
  98. + rpcClassName + ".class);");
  99. sourceWriter.println("((" + InitializableServerRpc.class.getName()
  100. + ") rpc).initRpc(connector);");
  101. sourceWriter.println("return (T) rpc;");
  102. sourceWriter.outdent();
  103. }
  104. sourceWriter.println("} else {");
  105. sourceWriter.indent();
  106. sourceWriter
  107. .println("throw new IllegalArgumentException(\"No RpcInterface of type \"+ rpcInterface.getName() + \" was found.\");");
  108. sourceWriter.outdent();
  109. // End of if
  110. sourceWriter.println("}");
  111. // End of method
  112. sourceWriter.println("}");
  113. // close generated class
  114. sourceWriter.outdent();
  115. sourceWriter.println("}");
  116. // commit generated class
  117. context.commit(logger, printWriter);
  118. logger.log(Type.INFO, composer.getCreatedClassName() + " created in "
  119. + (new Date().getTime() - date.getTime()) / 1000 + "seconds");
  120. }
  121. }