我有自定义类生成器。在这个生成器中我创建了两个类
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");
。但我不会刷新项目,它的库。
由于
答案 0 :(得分: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
答案 1 :(得分:0)
我找到了解决方案,并通过原始gwt ClassSourceFileComposerFactory启发自己的编译器
生成器source
使用
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");