mercredi 9 mai 2018

Options for "Decorating" methods in Java Static Class without using AOJ or similar

I have a static class that has a number of methods with different names but otherwise the same signature, e.g.:

protected static MyResponse [validationMethodName] (MyObject target, MyStatus failStatus) throws Exception {...}

I would like these particular methods to execute some code on entry, such as

target.checkCache(target, validationMethodName);
target.doOtherStuff();

without having to duplicate this code in each method.

I am looking into the following techniques and am wondering which if any might be possible/best, or if there's a better way:

  • use a Proxy for the containing static class and add the desired code via the InvocationHandler
  • extend the java.lang.reflect.method class with a special invoke()
  • just duplicate the code in each method with some initial methodInit(args) call

I'm willing to use anything in core available through Java10





Aucun commentaire:

Enregistrer un commentaire