Skip to content

Commit ea71745

Browse files
JingMatrixfrknkrc44
andcommitted
Implement constructor invocation APIs in LSPosedContext (#533)
Unlike the existing `newInstance` variants which allocate and return a new object, these new APIs execute constructor logic on an existing, pre-allocated instance (`thisObject`). This separation of allocation and initialization allows for invoking original or super constructors within hook callbacks where the object reference is already established. The implementation leverages the existing JNI `HookBridge` methods, as `invokeOriginalMethod` and `invokeSpecialMethod` already support void-return signatures required for constructor execution. Co-authored-by: frknkrc44 <krc440002@gmail.com>
1 parent aed7030 commit ea71745

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

core/src/main/java/org/lsposed/lspd/impl/LSPosedContext.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,16 @@ public <T> boolean deoptimize(@NonNull Constructor<T> constructor) {
210210

211211
@Nullable
212212
@Override
213-
public Object invokeOrigin(@NonNull Method method, @Nullable Object thisObject, Object[] args) throws InvocationTargetException, IllegalArgumentException, IllegalAccessException {
213+
public Object invokeOrigin(@NonNull Method method, @Nullable Object thisObject, Object... args) throws InvocationTargetException, IllegalArgumentException, IllegalAccessException {
214214
return HookBridge.invokeOriginalMethod(method, thisObject, args);
215215
}
216216

217+
@Override
218+
public <T> void invokeOrigin(@NonNull Constructor<T> constructor, @NonNull T thisObject, Object... args) throws InvocationTargetException, IllegalArgumentException, IllegalAccessException {
219+
// The bridge returns an Object (null for void/constructors), which we discard.
220+
HookBridge.invokeOriginalMethod(constructor, thisObject, args);
221+
}
222+
217223
private static char getTypeShorty(Class<?> type) {
218224
if (type == int.class) {
219225
return 'I';
@@ -257,6 +263,11 @@ public Object invokeSpecial(@NonNull Method method, @NonNull Object thisObject,
257263
return HookBridge.invokeSpecialMethod(method, getExecutableShorty(method), method.getDeclaringClass(), thisObject, args);
258264
}
259265

266+
@Override
267+
public <T> void invokeSpecial(@NonNull Constructor<T> constructor, @NonNull T thisObject, Object... args) throws InvocationTargetException, IllegalArgumentException, IllegalAccessException {
268+
HookBridge.invokeSpecialMethod(constructor, getExecutableShorty(constructor), constructor.getDeclaringClass(), thisObject, args);
269+
}
270+
260271
@NonNull
261272
@Override
262273
public <T> T newInstanceOrigin(@NonNull Constructor<T> constructor, Object... args) throws InvocationTargetException, IllegalAccessException, InstantiationException {

0 commit comments

Comments
 (0)