GWT动态加载使用GWT.create()与字符串文字而不是类文字 [英] GWT Dynamic loading using GWT.create() with String literals instead of Class literals

查看:172
本文介绍了GWT动态加载使用GWT.create()与字符串文字而不是类文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GWT.create()是GWT中等价的反射,
但是它只接受类文字,而不是完全合格的String作为类名。
如何使用GWT.create()动态创建带有字符串的类?根据许多GWT论坛帖子,它是不可能的,但是它是如何在Rocket-GWT等框架中完成的( http://code.google.com/p/rocket-gwt/wiki/Ioc )和Gwittir( http://code.google.com/p/gwittir/wiki/Introspection

解决方案

尽管有些棘手,但这是可能的。这里是血淋淋的细节:



如果你只是认为GWT是一个直接的Java到JS,它不会工作。但是,如果考虑Generators - 使用GWT编译器的特殊类在编译期间编译和执行,则可能。因此,您甚至可以在编译时生成Java源代码。



今天我有这种需求 - 我们的系统处理动态资源关闭服务,结束为字符串,一类。这是我想出的solutuion - 顺便说一句,它在托管,IE和Firefox下工作。


  • 创建一个GWT模块声明:


    • 源路径

    • 一个生成器(它应该保存在GWT模块源路径的包中)

    • 一个接口替换将注入Generated类而不是接口)

    • 在这个包里面,创建一个Marker接口(我称之为Constructable)。生成器将查找该标记
    • 创建一个基本抽象类来保存该工厂。我这样做是为了简化生成的源代码。

    • 声明该模块继承于您的Application.gwt.xml



    一些注意事项:


    • 理解的关键在于发生器的概念;
    • 为了简化,抽象基类派上用场。
    • li>
    • GWT.create需要对.class文件进行一些引用。您的生成器输出可能会这样做,只要它以某种方式从您的应用程序中引用(请检查Application.gwt.xml继承您的模块,该模块也将您的Application.gwt.xml声明的生成器替换为一个接口)
    • 将GWT.create调用包装在工厂方法/单例中,也包含在GWT.isClient()中 这也是一个非常好的主意,可以包装您的代码类负责围绕GWT.runAsync调用,因为它可能需要触发模块加载。这是非常重要的。



    我希望很快发布源代码。横过你的手指。 :)

    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()?

    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:

    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.

    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.

    • 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)
    • Inside that package, create a Marker interface (i call that Constructable). The Generator will lookup for that Marker
    • Create a base abstract class to hold that factory. I do this in order to ease on the generated source code
    • Declare that module inheriting on your Application.gwt.xml

    Some notes:

    • Key to understanding is around the concept of generators;
    • In order to ease, the Abstract base class came in handy.
    • Also, understand that there is name mandling into the generated .js source and even the generated Java source
    • Remember the Generator outputs java files
    • GWT.create needs some reference to the .class file. Your generator output might do that, as long as it is referenced somehow from your application (check Application.gwt.xml inherits your module, which also replaces an interface with the generator your Application.gwt.xml declares)
    • Wrap the GWT.create call inside a factory method/singleton, and also under GWT.isClient()
    • It is a very good idea to also wrap your code-class-loading-calls around a GWT.runAsync, as it might need to trigger a module load. This is VERY important.

    I hope to post the source code soon. Cross your fingers. :)

    这篇关于GWT动态加载使用GWT.create()与字符串文字而不是类文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆