javaandroidxposed

Hooking method with ArrayList<MyObject> parameter


I have a class MyObject and an app which main activity implements a Method test(ArrayList<MyObject> x)

    public class MyObject {

    private String x;

    MyObject(String x) {
        this.x = x;
    }

    public String getX() {
        return x;
    }
}

Now i need to hook the method test with xposed to get the parameter x. How can i access and iterate through the ArrayList and call the getX method? My first approach was to pass this as argument for the findAndHookMethod method:

final Class<?> myObject = XposedHelpers.findClass(
    "com.xposed.packagehook.MyObject", lpparam.classLoader);`

But i don't know how to wrap this into an ArrayList.


Solution

  • When developing hooks it is not a good idea to use the application source code as base. It is recommended to use the compiled APK file and decompile it using a tool like apktool. The reason for this is that the compiled code sometimes looks a bit different to what you expect:

    The method parameter definition ArrayList<MyObject> comprises of two sections:

    1. The object type ArrayList
    2. A (generics) type parameter MyObject

    Generics were not part of the original Java definition and when this concept was later added it was restricted to the compiler. Therefore the generics type parameter only exists at compiler time in the method signature. In the app dex byte code and at rune-time you will only see the method test this way:

    void test(ArrayList x)
    

    Or the same in Smali code as it it output by apktool:

    method public test(Ljava/util/ArrayList;)V
    

    Some decompiler may also show the generic type parameter, but that information comes from an annotation that is not part of the method signature you need for hooking the method.

    Hence if you want to hook this method the MyObject is totally irrelevant. Just hook the test method that has the java.util.ArrayList parameter and you are done.