Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to listen to another class' method execution?

Tags:

java

oop

Is it possible to listen to a method execution of an instance or all instances of a class, without modifying their code ? Something like:

someInstance.addMethodExecutionListener('methodName', handler);
SomeClass.addMethodExecutionListener('methodName', handler);

It would be for logging purposes...

like image 245
ldiqual Avatar asked Nov 25 '11 11:11

ldiqual


People also ask

How to call a method in another class Java?

In Java, a method can be invoked from another class based on its access modifier. For example, a method created with a public modifier can be called from inside as well as outside of a class/package. The protected method can be invoked from another class using inheritance.


1 Answers

What you're asking about is a small subset of what you can do with Aspect Oriented Programming. It's not supported in plain Java and its implementations, but it's the central reason for AspectJ.

like image 177
outis Avatar answered Oct 23 '22 05:10

outis