KRISTIJAN TOMASINI
KRISTIJAN TOMASINI

Reputation: 459

How to call a method as a response to another method call in Java?

Suppose you have a Library L.jar which contains class C.class and a method m.

Now, you have a project which imports this library and uses the m method.

In the project there is class X with method mX.

What I want to do is to somehow monitor all invocations of the method m and invoke the method mX whenever method m gets invoked.

If I can somehow read the parameters which get passed to method m when invoking it, that would be awesome.

Is this possible in Java and if so, how to do it?

Upvotes: 1

Views: 74

Answers (1)

Em Ae
Em Ae

Reputation: 8704

There are two approaches. You can either use AspectJ to intercept your program calls. OR You should be wrapping L.jar method m into a class of yours and expose that class to end user. end user should be using this class to invoke m. now you will have full control on who is calling m with which parameters. The downside is that user can always call m directly and bypass your code.

Upvotes: 1

Related Questions