GWT 动态加载使用 GWT.create() 和字符串文字而不是类文字 [英] GWT Dynamic loading using GWT.create() with String literals instead of Class literals
问题描述
GWT.create() 是 GWT 中的反射等价物,但它只需要类文字,而不是类名称的完全限定字符串.我如何使用 GWT.create() 动态创建带有字符串的类?
GWT.create() is the reflection equivalent in GWT, But it take only class literals, not fully qualified String for the Class name. How do i dynamically create classes with Strings using GWT.create()?
根据许多 GWT 论坛帖子,这是不可能的,但它是如何在 Rocket-GWT 等框架中完成的(http://code.google.com/p/rocket-gwt/wiki/Ioc)和 Gwittir(http://code.google.com/p/gwittir/wiki/Introspection)
Its not possible according to many GWT forum posts but how is it being done in frameworks like Rocket-GWT (http://code.google.com/p/rocket-gwt/wiki/Ioc) and Gwittir (http://code.google.com/p/gwittir/wiki/Introspection)
推荐答案
这是可能的,尽管很棘手.以下是血腥细节:
It is possible, albeit tricky. Here are the gory details:
如果你只认为 GWT 是直接从 Java 到 JS,那是行不通的.但是,如果您考虑 Generators - Special classes with your GWT compiler Compiles and Executes 在编译期间,这是可能的.这样,你甚至可以一边编译一边生成java源代码.
If you only think as GWT as a straight Java to JS, it would not work. However, if you consider Generators - Special classes with your GWT compiler Compiles and Executes during compilation, it is possible. Thus, you can generate java source while even compiling.
我今天有这个需求 - 我们的系统处理来自服务的动态资源,以字符串结尾并需要一个类.这是我想出的解决方案 - 顺便说一句,它在托管、IE 和 Firefox 下工作.
I had this need today - Our system deals with Dynamic resources off a Service, ending into a String and a need for a class. Here is the solutuion I've came up with - btw, it works under hosted, IE and Firefox.
- 创建一个 GWT 模块声明:
- 源路径
- 一个生成器(应该保持OUTSIDE GWT模块源路径的包)
- 接口替换(它将注入 Generated 类而不是接口)
- Create a GWT Module declaring:
- A source path
- A Generator (which should be kept OUTSIDE the package of the GWT Module source path)
- An interface replacement (it will inject the Generated class instead of the interface)
一些注意事项:
- 理解的关键在于生成器的概念;
- 为了方便,Abstract 基类派上了用场.
- 另外,请理解在生成的 .js 源代码甚至生成的 Java 源代码中存在名称修饰
- 记住生成器输出 java 文件
- GWT.create 需要一些对 .class 文件的引用.您的生成器输出可能会这样做,只要它以某种方式从您的应用程序中引用(检查 Application.gwt.xml 继承了您的模块,它还用您的 Application.gwt.xml 声明的生成器替换了一个接口)
- 将 GWT.create 调用包装在工厂方法/单例中,也包装在 GWT.isClient() 下
- 将您的代码类加载调用也包裹在 GWT.runAsync 周围是一个非常好的主意,因为它可能需要触发模块加载.这非常重要.
我希望尽快发布源代码.交叉你的手指.:)
I hope to post the source code soon. Cross your fingers. :)
这篇关于GWT 动态加载使用 GWT.create() 和字符串文字而不是类文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!