Java – Actions that reflect methods in Java

Actions that reflect methods in Java… here is a solution to the problem.

Actions that reflect methods in Java

I would like to know how, if possible, what method calls are performed inside the method during reflection during execution. I’m particularly interested in external method calls (i.e. methods in other classes) or calling some specific methods (like getDatabaseConnection()).

My intention is to monitor the operation of predefined objects inside the method and execute other code when some specific conditions are met, such as calling certain methods with specific values. The monitor will be entirely an external class or a set of classes, with no direct access to the object to be monitored by means other than reflection.

Solution

Aspect J will solve your problem.

Try defining the entry point like this:

pointcut profilling(): execution(public * *(..)) && (
            within(com.myPackage..*) ||

In this way, you will capture all calls to any public method in the package com.myPackage. Add as many clauses as you want.

Then add code like this:

Object around(): profilling() {

    //Do wherever you need before method call
    proceed();
    //Do wherever you need after method call

}

If you want to know more about aspectJ, follow this guide .

Related Problems and Solutions