Hi 🙂 , You may have thought of how to hook a java anonymous inner class, since these classes does not have a unique name at a glance. So, I decied to write a post to explain how to specify the class name while hooking java anonymous inner classes.
I take this anonymous inner class as an example.
abstract class Person{ abstract String eat(); public static void methodX(){ Person p=new Person(){ String eat(){ return "nice fruits";} }; p.eat(); test(); } public static void methodY(){ Person p=new Person(){ String eat(){ return "tasty fruits";} }; } }
In this example, we can see two anonymous inner classes. Therefor, to hook eat() method in these two classes you need to differentiate the class name. The compilar has its own way to differentiate these classes. We can follow the same which is to name as “example. Person$i”, where i = {1,2,..,n} and n = the no of anonymous inner clases in the particlar class.
Solution:
Hook the first anonymous inner class
findAndHookMethod("example.Person$1", loadPackageParam.classLoader, "eat",new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
Log.v("xposed1", "example.Person$1 : " + param.getResults());
}
});
Hook the second anonymous inner class
findAndHookMethod("example.Person$2", loadPackageParam.classLoader, "eat",new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
Log.v("xposed1", "example.Person$2 : " + param.getResults());
}
});
Cheers !