Java advanced annotation and reflection



This article is from http://blog.csdn.net/liuxian13183/  , and the reference must be indicated!


The generation of Java's annotation, reflection and other mechanisms makes dynamic proxy possible. Generally, a class can be found through the fully qualified name + class name, its constructor and other methods can be invoked, and its parameter (Field) name and value can be obtained.

Annotations are generally used in code comments, code review (whether written in accordance with standards, such as inspect), code injection (hook, asbectj), and what needs to be considered is when to inject (compile time and run time)

Reflection is generally used to dynamically convert json and Object to each other and execute related underlying code, such as setting the Accessible of a class to false to prevent others from modifying hooks

Example: Ali's FastJson parsing:

 
 
@Override public < T > T json2Object (String json , Class< T > clazz) {
return JSON. parseObject (json , clazz) ;
}
@Override public String object2Json (Object instance) {
return JSON. toJSONString (instance) ;
}

Example Java's default annotation strategy:

public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
	默认,编译时被抛弃
     */
SOURCE,
/**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.    
    
	Interpreted by the compiler by default, but discarded at runtime
      */
 CLASS ,
 /**
      * Annotations are to be recorded in the class file by the compiler and
      * retained by the VM at run time, so they may be read reflectively.
      *    
    
       被编译时解释,运行时仍保存,可以直接被使用
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}
 
 
例Hook:

hook一事看似神秘,其实并不是那么难,希望各位看官看过本文之后能有所收获。

本次是hook Android的点击事件,也就是OnClickListener,hook的意义在于你能在调用setOnClickListener后做些其他的事,其他一些你想和所有点击事件一起处理的事,那么在这里,我就以埋点为例吧。

先来展示下效果:

public void onClick(View view) {
    Map map = new HashMap();
    switch (view.getId()) {
      case R.id.btn_hook1:
        map.put("巴", "掌");
        map.put("菜", "比");
        break;
      case R.id.btn_hook2:
        map.put("TF-Boys", "嘿嘿嘿");
        map.put("id", "111");
        break;
    }
    view.setTag(R.id.id_hook, map);
}

我在onClick内干了三件事:

1、new HashMap

2、map塞你想埋点的数据

3、把数据传到对应的view里

然后点击按钮会弹出一个Toast,如下图:

hook

那么有意思的地方来了,我们并没有在点击事件里弹Toast,那这个Toast哪来的呢?嘿嘿嘿,当然是hook的啦。

Hook

下面开始hook过程:

整个过程浓缩下来就是四个字--移花接木!

分析源代码

static class

首先来看看android.view.View中的这块代码,mOnClickListener变量静静的在这里(这里还有别的事件哦,比如OnLongClickListener等,大家学完之后可以试着hook下别的),我们需要做的就是移花接木,把自己的花替换掉这个木,mOnClickListener是ListenerInfo这个类的成员变量,那继续看看ListenerInfo在View的哪里被初始化了,因为我们最开始拿到的只有View这一个对象。

ListenerInfo

没错,找到了,getListenerInfo()干了这件事,我们从这个方法入手先把ListenerInfo拿下,然后再移花接木。

技术方案已经有了,那么就开始着手撸码。

实现

hook的过程就是充分利用java反射机制的过程,几行代码搞定,我们来看看:

//先拿下View的Class对象
Class clazzView = Class.forName("android.view.View");
//再把getListenerInfo拿到
Method method = clazzView.getDeclaredMethod("getListenerInfo");
//由于getListenerInfo并不是pulic方法,所以需要修改为可访问
method.setAccessible(true);
//继续拿下ListenerInfo内部类的Class对象
Class clazzInfo = Class.forName("android.view.View$ListenerInfo");
//拿到主角mOnClickListener成员变量
Field field = clazzInfo.getDeclaredField("mOnClickListener");
//截止到这,我们已经完成了百分之95了,只剩最后一步,那就是把我们的木接进来
//那么这里先暂时停留下,我们把木给创建好。
//挖个坑 --> 待会填

由于移花接木有个本质不能忘,那就是尊重原有类型,因此,我们的木也得实现View.OnClickListener接口:

public static class HookListener implements View.OnClickListener {

    private View.OnClickListener mOriginalListener;

    //直接在构造函数中传进来原来的OnClickListener
    public HookListener(View.OnClickListener originalListener) {
      mOriginalListener = originalListener;
    }

    @Override public void onClick(View v) {
      if (mOriginalListener != null) {
        mOriginalListener.onClick(v);
      }
      StringBuilder sb = new StringBuilder();
      sb.append("hook succeed.\n");
      //拿到之前传递的参数
      Object obj = v.getTag(R.id.id_hook);
      //下面的操作可以猥琐欲为了
      if (obj != null && obj instanceof HashMap && !((Map) obj).isEmpty()) {
        for (Map.Entry<String, String> entry : ((Map<String, String>) obj).entrySet()) {
          sb.append("key => ")
              .append(entry.getKey())
              .append(" ")
              .append("value => ")
              .append(entry.getValue())
              .append("\n");
        }
      } else {
        sb.append("params => null\n");
      }

      Toast.makeText(v.getContext(), sb.toString(), Toast.LENGTH_LONG).show();
    }
}

以上代码就是我们的木,为了看起来更简单,我直接通过构造函数把原来的花(OnClickListener)给传过来了,然后在新的HookListener的onClick()里把原来的事件继续完成,并加上自己想猥琐欲为的一些事情。

那么继续填上之前埋的坑:

field.set(listenerInfo, new HookListener((View.OnClickListener) field.get(listenerInfo)));

接木的过程干了两件事,一个是把原有的OnClickListener传给HookListener,二是把新的HookListener替换进ListenerInfo,perfect。

至此,移花接木就完成了,简单吧。

合适的调用hook

我们把hook方法都写好了,最后就是调用你需要hook的View了,在大多数情况下,你可以把hook这件事交给Base去做,遍历当前rootView所有的View,然后每个都调用hook,本文的重点不是这,我就不赘述了。

小结

本文仅仅以埋点为例,= = 其实我觉得埋点这个栗子并不太好,妹的都传了这么多参数过来了,还在乎在这里调用一下自己的tracker?不管了,没有栗子会让本次hook感觉很无力,希望各位同学看过后能对hook不再懵逼,其实和自定义View一样简单的啦。

Sample代码已同步到github上,有问题可以提issue => https://github.com/JeasonWong/ClickTracker


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325959492&siteId=291194637