Reflectionhooks
Java reflection hooking library
Install / Use
/learn @xxDark/ReflectionhooksREADME
reflectionhooks
WARNING: java.lang.invoke hooks can only be used if jar is appended into bootstrap class path
public class Test {
private static int val = 1;
private Test(String str) {
System.err.println(str);
}
public static void main(String[] a) throws Throwable {
HooksFactory factory = new DefaultHooksFactory();
Hook hook = factory
.createMethodHook(Void.class, Test.class.getDeclaredMethod("hi", String.class),
(parent, handle, args) -> {
System.out
.println("Hello, World!, arg0 is: "
+ args[0] + " and my instance is " + handle);
parent.invoke(null, handle, args);
return null;
});
hook.hook();
Test.class.getDeclaredMethod("hi", String.class)
.invoke(null, "Hi!");
Hook hook1 = factory.createFieldHook(Test.class.getDeclaredField("val"),
(parent, handle) -> {
System.out.println("get called from " + handle);
return parent.get(null, handle);
}, (parent, handle, value) -> {
System.out.println("set called from " + handle + ", to " + value);
parent.set(null, handle, value);
});
hook1.hook();
Field field = Test.class.getDeclaredField("val");
field.setInt(null, 5);
Hook hook2 = factory
.createConstructorHook(Test.class, Test.class.getDeclaredConstructor(String.class),
(parent, handle, args) -> {
System.err.println("Constructor called!");
args[0] = "World!";
return parent.invoke(null, handle, args);
});
hook2.hook();
Test.class.getDeclaredConstructor(String.class).newInstance("Hello, ");
}
public static void hi(String str) {
System.out.println(str);
}
}
public class Test {
public static void main(String[] args) throws Throwable {
JavaInvokeInjector.inject();
HooksFactory factory = new DefaultHooksFactory();
factory.createMethodInvokeHook((type, classRef, nameRef, typeRef) -> {
System.out.println("Call: " + classRef.get() + ' ' + nameRef.get() + ' ' + typeRef.get());
nameRef.set("hooked");
});
MethodHandles.publicLookup().findStatic(Test.class, "first", MethodType.methodType(void.class))
.invokeExact();
}
public static void first() {
System.out.println("Hello, ");
}
public static void hooked() {
System.out.println("World!");
}
}
Related Skills
node-connect
343.3kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
92.1kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
343.3kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
343.3kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
