The code I'm writing needs to intercept calls to object #2 (part of an external library) that is instantiated/retrieved on the fly by object #1 (also in the external library).
Therefore, I'm intercepting object #1 and then adding an aspect to object #2; this way I can get the number of calls, exceptions thrown, response times, etc.
// My aspect intercepts object #1 that retrieves object #2 and adds the aspect
Object obj2 = joinPoint.proceed();
AspectJProxyFactory proxyFactory = new AspectJProxyFactory(obj2);
proxyFactory.addAspect(this);
Now, since obj2
will be retrieved millions of times will proxyFactory.addAspect(this);
degrade the app over time when called (adding the aspect) over and over again, or is this call idempotent and has little side effects?
Note: I'm guessing obj2 will be instantiated once of just a few times, and then it's going to be the same object over and over again, but I can't be positive about this, since it's an external, closed source library.
Like I said in my comment, simply use native AspectJ, ideally via load-time weaving (LTW), because then you can also intercept third-party classes without tedious post-compile binary weaving and repackaging of existing JARs. That should be quite easy and super efficient.
FWIW, your Spring AOP question was interesting enough to think about it. If you are for some reason unhappy about the performance when repeatedly and redundantly creating proxy factories, proxies and linking them to aspects, simply cache the target instances in a weak hash map, using the original instances as keys and the proxies as values. It would basically look like this:
As you can see, the component always returns the same POJO instance, i.e. it should be cacheable.
The console log shows how it works: