mercredi 23 septembre 2015

Dynamic proxy with custom methods

I am trying to write a generic wrapper for javax.sql.DataSource instance. My problem is that this class is configured by calling setters on the implementation - in configuration I get some properties with foo=bar and then setFoo("bar") is called on the data source. These setters are not invoked using interface calls but using reflection.

Now, when I want to wrap an implementation, I cannot know until runtime what setters it has. I would need a java.lang.reflect.Proxy that would, in its getClass().getMethods() return my own set of methods, not just those that are defined in the interfaces.

One way I can see is use Javassist and generate a custom interface whose methods would match setters on the instance of DataSource I got, and then provide this interface to the proxy factory method. Is there any pure Java rt.jar way to achieve the above, without additional dependencies (to Javassist or CGLib or ...)?





Aucun commentaire:

Enregistrer un commentaire