'Instruct JVM to call a function before a function return

I am developing a library which will push some meta data to a server regarding the execution of the function and the result. For that purpose simple approach would be my library will expose a method (along with params) which will need to be called from the function which need to push the data. Something like pseudo code:

Implementation code class:

Public class perform
{
    public int addition(int a, int b)
    {
        //some logic
        Metric.record(); // this is the library function which will push metrics
    }
}

I am wondering if there is way rather than asking implementation developer to call Metric.record() in function, we can only ask to annotate the function which needs to call the library function before exit. And rest of the thing to call library function before method returns will be handled by JVM

something like this may be:

Public class perform
{
    @BeforeExitRecordMetric //this annotation will instruct JVM to call a function from library before function exit and return
    public int addition(int a, int b)
    {
        //some logic
    }
}

I already seen Java: Is it possible to always execute a certain function before other functions are called? (Like @Before in JUnit) post but that won't be possible in my case because to make this possible I need to ask to initialize class using proxy class.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source