Using xposed to hook a method with a custom class array as an argument

倾然丶 夕夏残阳落幕 提交于 2021-01-28 01:07:02

问题


How can I hook a method that contains an array of a custom class?

[Lcom/samsung/android/uniform/widget/notification/NotificationItem;

This is the smali argument. I can get the class with XposedHelpers.findClass() but I can't create an array of it..


回答1:


just try this.

Class<?> notificationItemClass = Class.forName("...NotificationItem");
Class<?> notificationItemClassArray = java.lang.reflect.Array.newInstance(notificationItemClass, 2).getClass();

here is a demo:

package com.aegis.log.controller.open;

import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by gallonyin on 2019/1/25.
 */
public class TestReflection {

  public static void main(String[] args) throws ClassNotFoundException,
    InstantiationException, IllegalAccessException,
    IllegalArgumentException, SecurityException,
    InvocationTargetException, NoSuchMethodException {

    Class<?> relativeClass = Class.forName("com.aegis.log.controller.judgetool.Relative");
    Object relativeFather = relativeClass.newInstance();
    Object relativeMother = relativeClass.newInstance();
    relativeClass.getMethod("setName", String.class).invoke(relativeFather,
      "father");
    relativeClass.getMethod("setName", String.class).invoke(relativeMother,
      "mother");

    Class<?> personClass = Class.forName("com.aegis.log.controller.judgetool.Person");
    Object personObject = personClass.newInstance();

    Object[] temp = (Object[]) Array.newInstance(relativeClass, 2);
    temp[0] = relativeFather;
    temp[1] = relativeMother;

    Class<?> relativeArrayClass = Array.newInstance(relativeClass, 2).getClass();
    Method setParents = personClass.getMethod("setParents", relativeArrayClass);
    setParents.invoke(personObject, relativeArrayClass.cast(temp));
    personClass.getMethod("getParents").invoke(personObject);
  }
}

public class Person {
  private Relative[] parents;

  public void getParents() {
    for (Relative r : parents)
      r.getName();
  }

  public void setParents(Relative[] parents) {
    this.parents = parents;
  }
}

public class Relative {
  private String name;

  public void getName() {
    System.out.println(name);
  }

  public void setName(String name) {
    this.name = name;
  }
}



回答2:


Try this:

XposedHelpers.findAndHookMethod(class, "xxxMethod", "com.samsung.android.uniform.widget.notification.NotificationItem[]", new XC_MethodHook() {
            ...
        });


来源:https://stackoverflow.com/questions/51815136/using-xposed-to-hook-a-method-with-a-custom-class-array-as-an-argument

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!