Of course you can reuse an agent's implementation. A Java agent, represented by some method
public static void premain(String agentArgs, Instrumentation inst);
is treated like any Java class. The only thing is that this agent is invoked before the main entry point of the normal Java application. Additionally, the agent offers a possibility to easily add Instrumentation
. You could refactor your agent to come within its own bundle and add the agent to any program by just adding the agent's bundle to the program's class path. The agent is basically just a normal Java program that gets invoked before the Java program that was declared in a manifest file or as the explicit program entry point in the command line.
Just be aware that any classes that must be loaded in order to load the agent can no longer be intercepted.