-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathProxyHook.java
More file actions
53 lines (44 loc) · 1.58 KB
/
ProxyHook.java
File metadata and controls
53 lines (44 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.sample.battery;
import android.util.Log;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* 代理Hook的属性,并在方法调用前回调 beforeInvoke
*/
public class ProxyHook implements InvocationHandler {
private Object mHookTarget;
private InvokeBeforeListener mListener;
public ProxyHook(Object hookRef, String field, InvokeBeforeListener listener) {
if (null == hookRef) {
Log.e("Hoook", "hookRef is not exist");
return;
}
try {
Field fieldRef = hookRef.getClass().getDeclaredField(field);
fieldRef.setAccessible(true);
mHookTarget = fieldRef.get(hookRef);
Object newObj = Proxy.newProxyInstance(this.getClass().getClassLoader(), mHookTarget.getClass().getInterfaces(), this);
fieldRef.set(hookRef, newObj);
this.mListener = listener;
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public void setListener(InvokeBeforeListener listener) {
mListener = listener;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (null != mListener) {
mListener.beforeInvoke(method, args);
}
return method.invoke(mHookTarget, args);
}
public interface InvokeBeforeListener {
void beforeInvoke(Method method, Object[] args);
}
}