Results 1 to 5 of 5

Thread: GWT Reflection? Can it be done?

  1. #1
    Ext JS Premium Member firejack's Avatar
    Join Date
    Apr 2007
    Location
    Baltimore, MD
    Posts
    22

    Lightbulb GWT Reflection? Can it be done?

    Anyone -

    I am looking at trying to extend your MVC framework, and I was wondering if you know of a way to get a reference to the Javascript instance of a given object via JSNI. I am attempting to build a sort of hobbled version of reflection this way - specifically the ability to invoke certain methods on my object
    dynamically at run time.

    The thought is this:

    public Object native invokeMethod(String name, Object[] params) /*{

    var target = // somehow get a reference to the actual JS instance???
    var method = target[name];

    // invoke the method
    return method.apply(target, params);
    )*/

    If any of you are familiar with the GWT internals, I thought you might know how to traverse the instances between Java and JavaScript. I am also making an assumption that there is a 1-to-1 mapping between the java method and the js method names in the example above - which I am not sure is true.

    I can see there might be a need to map the Java name to the JS name somehow as well. Any thoughts on how to attack a problem like this?

    I will update this thread with progress - my goal is to simulate reflection in GWT so you can leverage it for neat stuff. Native JS is normally better at dynamic invocation like this than Java - but we lose that flexibility all of a sudden in GWT. Seems silly.

  2. #2
    Ext JS Premium Member firejack's Avatar
    Join Date
    Apr 2007
    Location
    Baltimore, MD
    Posts
    22

    Default Another approach

    There is an open source attempt to do this here:
    http://gwtreflection.sourceforge.net

    I am evaluating - but I am not sure this is the approach I want to take.

  3. #3

    Default

    Quote Originally Posted by firejack View Post
    I am looking at trying to extend your MVC framework, and I was wondering if you know of a way to get a reference to the Javascript instance of a given object via JSNI.
    Why don't you simply use generators in this case? The only constraint would be to have all your invoked classes visible at compile time, but it's the case for the majority of cases.

    At generation time, you can pre-build an indexed hashtable of alll the methods that can be invoked in your namespace and then do a simple switch that will be executed at runtime. So: you take the object (getClass().getName() exists in GWT 1.5), the method name, you take the index from the map and you call the method corresponding to the index.

    Of course it's better to add an annotation allowing to list all the concerned classes (or at least superclasses), otherwise the full list from the TypeOracle risks to be a bit long...

  4. #4
    Ext JS Premium Member firejack's Avatar
    Join Date
    Apr 2007
    Location
    Baltimore, MD
    Posts
    22

    Default Contraints

    So what I really want to be able to do is write a controller class with handler methods on it (one class) and eliminate the need to tediously bind listeners from the UI. I want to just have a naming convention for the event, the name of the widget, and a rule for how to process the event and have the controller generate the appropriate listener code and bind to the controller methods.

    i.e.

    public Object handleMyWidgetDoubleClick(EventContext context)
    {
    // unpack the event context and perform an action, update the UI, etc.
    }

    EventContext would know the MVC context, the application context, etc and give you all sorts of neat access to the application in general so you could perform pretty much any operation.

    I had very good luck with a framework like this for Swing, but GWT is presenting some issues doing the same. I am looking at Generators, but I am not sure they will give me what I want.

  5. #5

    Default

    Quote Originally Posted by firejack View Post
    So what I really want to be able to do is write a controller class with handler methods on it (one class) and eliminate the need to tediously bind listeners from the UI. I want to just have a naming convention for the event, the name of the widget, and a rule for how to process the event and have the controller generate the appropriate listener code and bind to the controller methods.
    Personally, I always prefer annotations to naming conventions, at least you have a kind of declaration, and when the system grows it becomes easier to maintain a consistent domain.

    As far as I understand your problem, generators would be a good solution even without the need to simulate Reflection behaviour. You simply make your UI elements implement a marker interface, you attach a generator to that interface and you generate all the specific event logic on the fly at compile time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •