lundi 16 août 2010

GWT and reflexion API

As you probably know, GWT does not emulate the reflection API of Java. It is a conscious choice by Google, which is motivated by the performance degradation induced by the introduction of mechanisms for reflection (not mentioned the lack of control of code).
However, it is sometimes useful to use the reflection API. Recently, I was confronted with the establishment of a GWT application that handles plugins. These plugins must be discovered dynamically. In Java you would use a scan packages with the reflection API or with Spring IOC. But in my case it is not possible because there are plugins that are client side, so that must comply with the constraints of GWT.

So how can i do that ?

To overcome this problem, the GWT team has developed the mechanism called "deffered binding" that allows to perform operations before the compilation of Java code in Javascript.
For my problem, I used a Generator. The generator, as its name suggests will allow to dynamically generate Java code before compiling for a given type.


What is interesting is that at this stage, it is possible to use any Java API that is desired because the generator is running in a JVM.
We can therefore use the reflection API. Furthermore, the scanning of packages is possible because the classes corresponding to the project source code is loaded into the classloader.
Once obtained the list of plugins (a plugin implements an interface call "Plugin"), it must generate a list of plugin object with an object through PluginFactory generator.



On this occasion, I also discovered a very nice API to list the packages in the ClassPath: classpath-explorer

So here we are an elegant technique to develop a plugin mechanism with GWT. The only drawback to this method is that for the addition of a plugin, it is necessary to recompile the project, which is not needed with Java plugins (like Eclipse plugins).

Aucun commentaire:

Enregistrer un commentaire