Search code examples
javajavascriptgwtreflectionjsni

java.lang.reflect.Proxy-like functionality in GWT via JSNI


For some custom client-server-communication code in GWT, I'd like to dynamically implement a Java interface similar to the way it would be done with java.lang.reflect.Proxy. Since GWT doesn't include any reflection support and I am loathe to write a clunky Code Generator, I'm wondering if this could be done with JSNI and Overlay types.

I'm thinking along the following lines:

  1. I have some existing java interface, and somehow a representation of its methods and their signatures, annotations, etc. (since the actual class object won't be of any help in GWT).
  2. I pass that representation into some JSNI method that uses it to construct a new Javascript object and define corresponding methods on it. The actual implementation of the methods is fairly trivial, they just write their arguments into some datastructure and return null.
  3. I get a Javascript object back from JSNI that is somehow castable to the interface type. When methods from the interface are invoked on this instance, the corresponding Javascript method that was defined in step 2 gets invoked and saves the arguments.

Has anybody attempted something similar? Are there reasons why this won't work on principle?

Thanks in advance.


Solution

  • What prevents you from doing the same thing in Java? Have a factory method that you pass some data to, and it returns an implementation of your interface. Since you say implementation is pretty trivial you would have a limited set of concrete classes.