0

我有自定义类生成器。在这个生成器中,我创建了两个类

public class WsRpcServerGenerator extends Generator{
   @Override
   public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException {
      JClassType classType;
      try {
         classType = context.getTypeOracle().getType(typeName);
         SourceWriter src;
         try {
            // generating first file xxxAsync for client
            src = generateMethod( classType, context, logger);
            // generating second class for server side
            SourceWriter src2 = generateMethodArgs( classType, context, logger);
         if (src2!=null)
            src2.commit(logger);
         } catch (Exception e) {}
         // returning first class for client
         if (src == null)return typeName + "__AsyncWsRpcGenerated";
            src.commit(logger);         
         return typeName + "__AsyncWsRpcGenerated";
      } catch (NotFoundException e) {}
   }
}

我用

TestObject obj = GWT.create(TestObject.class);

这是工作。gwt 生成了两个文件。首先加载到客户端。

但我不知道如何在服务器端加载第二个文件。如果我在 Eclipse 中刷新项目以获取可见性生成的类,则类将加载test=Class.forName("com.xxx.TestObject__ArgsGenerated");. 但我不会刷新项目,它的库。

谢谢

4

2 回答 2

0

我在看什么默认 gwtRpc,gwtRpc 保存有关 rpc 序列化策略的信息,我保存到类com.xxx.TestObject__ArgsGenerated的内容,将纯文本文件保存到 web 目录。所以我必须走这条路。在我的生成器中,我必须创建资源文件,并将序列化策略放在那里。

public class WsRpcServerGenerator extends Generator{
   @Override
   public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException {
      JClassType classType;
      try {
         classType = context.getTypeOracle().getType(typeName);
         SourceWriter src;
         try {
            // generating first file xxxAsync for client
            src = generateMethod( classType, context, logger);
            // generating file to war directorz
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(baos, SerializationPolicyLoader.SERIALIZATION_POLICY_FILE_ENCODING);
            TypeOracle oracle = context.getTypeOracle();
            PrintWriter pw = new PrintWriter(osw);
            // generate content here
            pw.close();
            byte[] serializationPolicyFileContents = baos.toByteArray();
            String serializationPolicyName = Util.computeStrongName(serializationPolicyFileContents);
            String serializationPolicyFileName = SerializationPolicyLoader.getSerializationPolicyFileName(serializationPolicyName);
            OutputStream os = context.tryCreateResource(logger, serializationPolicyFileName);
            if (os != null) {
                 os.write(serializationPolicyFileContents);
                 GeneratedResource resource = ctx.commitResource(logger, os);
            }    
         // returning first class for client
         if (src == null)return typeName + "__AsyncWsRpcGenerated";
            src.commit(logger);         
         return typeName + "__AsyncWsRpcGenerated";
      } catch (NotFoundException e | IOException e) {}
   }
}

服务器端的阅读策略

HttpServlet servlet;
String modulename; // sended from client GWT.getModuleBaseURL() reolacing host
Sending serialiyationpolicyid; // from generated xxxAsync
// Open the RPC resource file and read its contents.
InputStream is=servlet.getServletContext().getResourceAsStream(modulename+"/"+serialiyationpolicyid+".rpc");
// read policy
于 2013-07-15T06:05:26.957 回答
0

我找到了解决方案,并在原始 gwt ClassSourceFileComposerFactory 的启发下制作了自己的编译器

发生器

用法

DynamicJavaFileObject composer = new DynamicJavaFileObject("com.xxx","ClassName");

composer.setSuperclass("superclass");
composer.addImport(GWT.class.getCanonicalName());

SourceWriter writer = composer.getSourceWriter();

writer.println("public String test(){return \"test\"}");

writer.commit(logger);

现在我可以在服务器端找到课程

Class.forName("com.xxx.ClassName");
于 2013-07-15T17:42:24.710 回答