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

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