JAHA (JAva Hooking Agent) is a Java library for building Java agents that hook methods at runtime while preserving access to original method behavior.
- Java 8 (project targets Java 8 for compatibility)
- Zig installed (required for cross-platform building)
Below is an example of hooking Integer.toString().
import java.lang.instrument.Instrumentation;
import me.exeos.jaha.Jaha;
public final class AgentMain {
public static void premain(String args, Instrumentation inst) {
// Register hook classes
Jaha.register(ToStringHook.class);
// Apply hooks
Jaha.load(inst);
}
}import me.exeos.jaha.Jaha;
import me.exeos.jaha.annotations.Apply;
import me.exeos.jaha.annotations.Hook;
@Hook(target = "java/lang/Integer")
public class ToStringHook {
@Apply
public String toString() {
String original = (String) Jaha.callOriginalObjectMethod();
return "This has been hooked: " + original;
}
}Let's say you attach the example to this Program:
import java.util.Random;
public class Main {
public static void main(String[] args) {
System.out.println(new Integer(new Random().nextInt()));
}
}The result would be:
This has been hooked: -1604796949
JAHA hooks methods by rewriting bytecode using Instrumentation.
You register classes annotated with @Hook(target = "...") using Jaha.register(...).
Each hook class provides replacement methods annotated with @Apply
Jaha.applyHooks(inst) installs a ClassFileTransformer.
When a target class is transformed, JAHA:
- parses the target bytecode
- finds matching
@Applymethods in the hook source - replaces target method bodies with hook method bodies
Before replacing a method, JAHA clones the original implementation into an internal runtime container Class.
This copy is what “original method” calls are routed to.
Inside hook methods, calls like:
Jaha.callOriginalObjectMethod(...)Jaha.callOriginalIntMethod(...)- etc.
are placeholders. During transformation, JAHA rewrites these calls to invoke the cloned original implementation.
-
Jaha.applyHooks(Instrumentation inst)
Applies all registered hooks -
register(Class<?> hookSource)
Registers a hook class -
callOriginalVoidMethod(Object... params)
Calls original method when return type isvoid. -
callOriginalObjectMethod(Object... params)
Calls original method when return type is an object. (cast the result) -
callOriginalByteMethod(Object... params)
Calls original method when return type isbyte. -
callOriginalShortMethod(Object... params)
Calls original method when return type isshort. -
callOriginalIntMethod(Object... params)
Calls original method when return type isint. -
callOriginalLongMethod(Object... params)
Calls original method when return type islong. -
callOriginalFloatMethod(Object... params)
Calls original method when return type isfloat. -
callOriginalDoubleMethod(Object... params)
Calls original method when return type isdouble. -
callOriginalBooleanMethod(Object... params)
Calls original method when return type isboolean. -
callOriginalCharMethod(Object... params)
Calls original method when return type ischar.
This project is LICENSED under the GPL-3.0.
