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

48 views Asked by At

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(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

0

There are 0 answers