GWT Reflection

200 views
Skip to first unread message

Luis Costa

unread,
Jan 11, 2013, 9:38:14 AM1/11/13
to google-we...@googlegroups.com

Hi all,

What’s the best way to invoke a method by reflection using GWT, converting something like this:

 

GreetingServiceAsync service = GWT.create(GreetingService.class);

AsyncCallback callBack = new AsyncCallback< Void>() {

                                                @Override

                                                public void onFailure(Throwable caught) {                                                           

                                                }

                                                @Override

                                                public void onSuccess(Void result) {                                       

                                                }

                                };                            

service.doSomething(callBack);

 

in:

GreetingServiceAsync greetingService = GWT.create(GreetingService.class);

 

String methodName = “doSomething”;

Object service;

AsyncCallback callBack = new AsyncCallback< Void>() {

                                                @Override

                                                public void onFailure(Throwable caught) {                                                           

                                                }

                                                @Override

                                                public void onSuccess(Void result) {                                       

                                                }

                                };

/*somehow invoke by reflection*/

Class<?> c = Class.forName(GreetingServiceAsync.class.getName());

Method  method = c.getMethod(methodName, AsyncCallback.class);

method.invoke (service, callBack);

 

 

Many thanks,

Luis.

 

Jens

unread,
Jan 11, 2013, 10:31:58 AM1/11/13
to google-we...@googlegroups.com
To mimic method invocation in GWT you need to generate code that handles all possible methods for a given class. A typical example that exists in GWT is ClientBundleWithLookup which will be generated by GWT and has a method getResource(String name). The generated code of getResource(String name) may look like:

ResourcePrototype getResource(String functionName) {
  if("existingFunction1".equals(functionName)) {
    return this.functionName();
  }
  if("existingFunction2".equals(functionName)) {
     return this.functionName2();
  }
}

So basically it dispatches the method name and calls the method. I think there are some libraries that provide limited reflection support for GWT, but always keep in mind that they probably will generate lots of code that GWT can not optimize away in most cases (like the function above can't be optimized if the input is dynamically calculated).


-- J.

Andy Stevko

unread,
Jan 11, 2013, 10:46:34 AM1/11/13
to google-web-toolkit

I create a Command instance and pass that into the event handler

> --
> You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/R8nkNmJ_F4IJ.
>
> To post to this group, send email to google-we...@googlegroups.com.
> To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

Vagner Araujo

unread,
Jan 14, 2013, 9:24:39 AM1/14/13
to google-we...@googlegroups.com
Some time ago I toyed with it and created a mini library of reflection. The documentation on how to use it can be accessed by clicking here. Below are links to sample application and mini library.

Sample Application Link.

Mini Api Link.


Note 1:
I created it just for fun, so do not recommend their use in a production environment.

Note 2: The documentation was written in pt_BR.

Note 3: To use it just the following statement in your module:

<inherits name="org.puregwt.reflection.Reflection" />


--
Vagner Araujo

Vagner Araujo

unread,
Jan 12, 2013, 3:48:42 PM1/12/13
to google-we...@googlegroups.com

Some time ago I toyed with it and created a mini library of reflection. The documentation on how to use it can be accessed by clicking here. Below are links to sample application and mini library.

Sample Application Link.

Mini Api Link.


Note 1: I created it just as a joke, so do not recommend their use in a production environment.


Note 2: The documentation was written in pt_BR.

Note 3: To use it just the following statement in your module:

<inherits name="org.puregwt.reflection.Reflection" />


--
Vagner Araujo

Reply all
Reply to author
Forward
0 new messages