Skip to content

Commit

Permalink
fix generics for invokeConstructor
Browse files Browse the repository at this point in the history
  • Loading branch information
rushiiMachine committed Aug 28, 2024
1 parent 7baa88c commit b9101f5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
7 changes: 7 additions & 0 deletions core/src/androidTest/java/com/aliucord/hook/Dummy.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ public Dummy(Object a, int b, Integer c, String d) {
}

public Dummy(Object... varargs) {}

public static class Dummy2 extends Dummy {
public Dummy2() {
super();
d = "dummy2";
}
}
}
25 changes: 24 additions & 1 deletion core/src/androidTest/java/com/aliucord/hook/UnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import android.os.Build;
Expand Down Expand Up @@ -120,20 +121,42 @@ public void shouldNotHookField() throws Throwable {
@Test
public void shouldAllocateInstance() {
var instance = XposedBridge.allocateInstance(Dummy.class);
assertNotNull("failed to alloc", instance);
assertFalse(instance.initialized);
}

@Test
public void shouldInvokeConstructor() throws Throwable {
var instance = XposedBridge.allocateInstance(Dummy.class);
assertNotNull("failed to alloc", instance);
assertFalse("constructor not supposed to be called", instance.initialized);

var success = XposedBridge.invokeConstructor(instance, Dummy.class.getDeclaredConstructor());
assertTrue("invokeConstructor failed", success);
assertTrue("constructor not called", instance.initialized);
}

@Test
public void shouldInvokeSuperConstructor() throws Throwable {
var instance = XposedBridge.allocateInstance(Dummy.Dummy2.class);
assertFalse("constructor not supposed to be called", instance.initialized);

var success = XposedBridge.invokeConstructor(instance, Dummy.class.getDeclaredConstructor());
assertTrue("invokeConstructor failed", success);
assertTrue("supertype ctor not called", instance.initialized);
assertNull("subtype ctor should not be called", instance.d);
}

@Test
public void shouldInvokeSubConstructor() throws Throwable {
var instance = XposedBridge.allocateInstance(Dummy.Dummy2.class);
assertFalse("constructor not supposed to be called", instance.initialized);

var success = XposedBridge.invokeConstructor(instance, Dummy.Dummy2.class.getDeclaredConstructor());
assertTrue("invokeConstructor failed", success);
assertTrue("supertype ctor not called", instance.initialized);
assertEquals("subtype ctor not called", "dummy2", instance.d);
}

@Test(expected = IllegalArgumentException.class)
public void shouldNotInvokeVarargsConstructor() throws Throwable {
XposedBridge.invokeConstructor(new Dummy(), Dummy.class.getDeclaredConstructor(Object[].class));
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/de/robv/android/xposed/XposedBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,11 @@ public static <T> T allocateInstance(Class<T> clazz) {
* {@link XposedBridge#allocateInstance(Class)} in order to control when the constructor gets called.
*
* @param instance A class instance.
* @param constructor Constructor located on the instance's class.
* @param constructor Constructor located on the instance's class or one of its supertypes.
* @param args Args matching the constructor, if any. Can be null.
* @return True if operation was successful
*/
public static <T> boolean invokeConstructor(T instance, Constructor<T> constructor, Object... args) {
public static <S, T extends S> boolean invokeConstructor(T instance, Constructor<S> constructor, Object... args) {
Objects.requireNonNull(instance);
Objects.requireNonNull(constructor);
if (constructor.isVarArgs()) throw new IllegalArgumentException("varargs parameters are not supported");
Expand Down

0 comments on commit b9101f5

Please sign in to comment.