Fully featured low overhead profiler for Java EE and Java SE platforms.
Monitoring and profiling solution for Gradle, Maven, Ant, JUnit and TestNG.
Easy to use performance and memory profiler for .NET framework.

Callback onEnter()

To execute a code on method entry, define callback method onEnter() in probe class.

Only one method named onEnter() can exist in a probe class. You can omit onEnter() method if it is not needed for your probe.

Parameters

The callback can have any number of parameters, including zero.

Each of the parameters must be annotated with one of the following annotations:


Return type

onEnter() can be void or return a value.

If onEnter() is not void, the returned value will be accessible in onReturn(), onUncaughtException() and onUncaughtExceptionExt() callbacks of the same probe class with the help of a parameter annotated with @OnEnterResult. This approach enables effective way of data transfer between method enter and exit callbacks (the value is passed via stack).

Example 1:

import com.yourkit.probes.*;

// Instrument calls of method 'findPerson' in class 'com.foo.Person', which has 2 parameters
@MethodPattern("com.foo.Person:findPerson(String, int)")
public class MyProbe {
  public static void onEnter(
    @Param(1) Object param1,
    @Param(2) int param2
  ) {
    //...
  }
}

Example 2:

import com.yourkit.probes.*;

// Instrument calls of method(s) 'findPerson' in class 'com.foo.Person', with any signature,
// and print method execution times
@MethodPattern("com.foo.Person:findPerson(*)")
public class MyProbe {
  public static long onEnter() {
    return System.currentTimeMillis();
  }

  public static void onReturn(
    @OnEnterResult long enterTime
  ) {
    long exitTime = System.currentTimeMillis();
    System.out.println("method execution time: " + (exitTime - enterTime));
  }
} 

Special notes on instrumenting a constructor

Using callback parameter annotated with @This in a probe applied to a constructor affects the point where the call to onEnter() is injected. Please find detail here.