An extensible java agent framework that instruments (modifies the bytecode at class loading time) programs running on the JVM, with the purpose of capturing method invocation events (start, finish, errors ...) and notifying custom listeners.

Project maintained by brutusin Hosted on GitHub Pages — Theme by mattgraham

org.brutusin:instrumentation Build Status Maven Central Latest Version

An extensible java agent framework that instruments programs running on the JVM (modifying the bytecode at class loading time), with the purpose of capturing method invocation events (start, finish, errors ...) and notifying custom listeners.

Table of Contents

How it works

The java instrumentation package introduced in JDK1.5, provides a simple way to transform java-class definition at loading time, consisting basically in a byte[] to byte[] transformation, by the so called "java agents".

This module provides an agent (org.brutusin.instrumentation.Agent) that creates an execution listener instance (from the name of a concrete class extending org.brutusin.instrumentation.Interceptor passed from the JVM agent arguments) and, making use of the ASM library, introduces a series of instructions in the method definitions of the classes to be loaded (classes and methods can be skipped) to notify these execution events to the listener.

From a simplified point of view, the dynamic transformation turns a method like this:

public Object foo(Object bar){
    return new Object();

into that:

public Object foo(Object bar){
        Object ret = new Object();
        return ret;
    } catch(Throwable th){
        throw th; // at bytecode level this is legal

allowing your custom listener to be notified.

Maven dependency


Click here to see the latest available version released to the Maven Central Repository.

If you are not using maven and need help you can ask here.


See logging-instrumentation for a complete working example.


Create the following listener implementation:

package mypackage;

public class MyInterceptor extends Interceptor {

    public void init(String arg) {
        System.out.println("Interceptor args: " + arg);

    public boolean interceptClass(String className, byte[] byteCode) {
        return true; // all classes can be intrumented

    public boolean interceptMethod(ClassNode cn,MethodNode mn) {
        return true; // all methods are instrumented

    protected void doOnStart(Method m, Object[] arg, String executionId) {
        System.out.println("doOnStart " + m + " " + executionId);

    protected void doOnThrowableThrown(Method m, Throwable throwable, String executionId) {
        System.out.println("doOnThrowableThrown " + m + " " + executionId);

    protected void doOnThrowableUncatched(Method m, Throwable throwable, String executionId) {
        System.out.println("doOnThrowableUncatched " + m + " " + executionId);

    protected void doOnFinish(Method m, Object result, String executionId) {
        System.out.println("doOnFinish " + m + " " + executionId);


Create a fat-jar with the previous class and its dependencies. Add the following attribute to the manifest of the agent JAR:

Premain-Class: org.brutusin.instrumentation.Agent

Suppose this jar to be named myagent.jar

JRE launching

Run (at least JRE 1.5) the desired java application with the following JVM options: (suppossing myagent.jar located in the working directory)


Main stack

This module could not be possible without:

Brutusin dependent modules

Support, bugs and requests


Contributions are always welcome and greatly appreciated!


Apache License, Version 2.0