I have this code
Foo foo = new Foo();
foo.callTheMethod();
Is there any way I can intercept the Foo.callTheMethod()
call without subclassing or modifying Foo
class, and without having a Foo
factory?
EDIT: sorry forgot to mention this is on Android platform.
(OOP) domain, is a technique of intercepting function calls at program runtime. Without directly modifying. the original code, FCI enables to undertake certain operations before and/or after the called function, or even. to replace the intercepted call.
Defining Interceptors The preferred way to define in Java code is by using meta-data annotations. They can be defined in the application descriptor as well, but, in that case they are not portable across Java EE servers. Some of the meta-data annotations found in the javax.
Interception. Fody provides basic interceptors to intercept methods, properties and constructors and its feature-set is aimed towards eliminating boilerplate code.
Use Java's Proxy
class. It creates dynamic implementations of interfaces and intercepts methods, all reflectively.
Here's a tutorial.
Have you considered aspect-oriented-programming and perhaps AspectJ ? See here and here for AspectJ/Android info.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With