Skip to content

Comments

Implement hookClassInitializer API for static initializers#535

Merged
JingMatrix merged 2 commits intomasterfrom
api
Feb 13, 2026
Merged

Implement hookClassInitializer API for static initializers#535
JingMatrix merged 2 commits intomasterfrom
api

Conversation

@JingMatrix
Copy link
Owner

Since standard Java Reflection cannot access the <clinit> method (class's static initializer), we add a new native function, HookBridge.getStaticInitializer, which uses JNI GetStaticMethodID to retrieve a Method handle for it. The LSPosedContext API then passes this handle to the existing doHook machinery.

There are two possible exceptions thrown by the API:

  • IllegalArgumentException: Thrown for user-level errors, such as attempting to hook a class that has no static initializer block (static { ... }). This is a predictable failure based on the provided class.

  • HookFailedError: Thrown when the underlying native hooking engine (lsplant) fails to install the hook. This indicates a framework or environment-level issue (e.g., ART incompatibility, method inlining by the JIT/AOT compiler) and is not a fault in the module's logic.

Since standard Java Reflection cannot access the `<clinit>` method (class's static initializer), we add a new native function, `HookBridge.getStaticInitializer`, which uses JNI `GetStaticMethodID` to retrieve a `Method` handle for it. The `LSPosedContext` API then passes this handle to the existing `doHook` machinery.

There are two possible exceptions thrown by the API:

- `IllegalArgumentException`: Thrown for user-level errors, such as attempting to hook a class that has no static initializer block (`static { ... }`). This is a predictable failure based on the provided class.

- `HookFailedError`: Thrown when the underlying native hooking engine (`lsplant`) fails to install the hook. This indicates a framework or environment-level issue (e.g., ART incompatibility, method inlining by the JIT/AOT compiler) and is not a fault in the module's logic.
Name `clazz` was used in marco
@JingMatrix JingMatrix merged commit cef2ffb into master Feb 13, 2026
1 check passed
@JingMatrix JingMatrix deleted the api branch February 13, 2026 18:38
JingMatrix added a commit that referenced this pull request Feb 14, 2026
Since standard Java Reflection cannot access the `<clinit>` method (class's static initializer), we add a new native function, `HookBridge.getStaticInitializer`, which uses JNI `GetStaticMethodID` to retrieve a `Method` handle for it. The `LSPosedContext` API then passes this handle to the existing `doHook` machinery.

There are two possible exceptions thrown by the API:

- `IllegalArgumentException`: Thrown for user-level errors, such as attempting to hook a class that has no static initializer block (`static { ... }`). This is a predictable failure based on the provided class.

- `HookFailedError`: Thrown when the underlying native hooking engine (`lsplant`) fails to install the hook. This indicates a framework or environment-level issue (e.g., ART incompatibility, method inlining by the JIT/AOT compiler) and is not a fault in the module's logic.
TheSillyOk pushed a commit to TheSillyOk/ReLSP that referenced this pull request Feb 22, 2026
…x#535)

Since standard Java Reflection cannot access the `<clinit>` method (class's static initializer), we add a new native function, `HookBridge.getStaticInitializer`, which uses JNI `GetStaticMethodID` to retrieve a `Method` handle for it. The `LSPosedContext` API then passes this handle to the existing `doHook` machinery.

There are two possible exceptions thrown by the API:

- `IllegalArgumentException`: Thrown for user-level errors, such as attempting to hook a class that has no static initializer block (`static { ... }`). This is a predictable failure based on the provided class.

- `HookFailedError`: Thrown when the underlying native hooking engine (`lsplant`) fails to install the hook. This indicates a framework or environment-level issue (e.g., ART incompatibility, method inlining by the JIT/AOT compiler) and is not a fault in the module's logic.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant