Skip to content

Commit f6c4cf8

Browse files
committed
优化反射方法中的变量命名
1 parent 96044bf commit f6c4cf8

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

library/src/main/java/com/hjq/toast/NotificationToast.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,26 @@ private static void hookNotificationService() {
3838
sHookService = true;
3939
try {
4040
// 获取到 Toast 中的 getService 静态方法
41-
Method getService = Toast.class.getDeclaredMethod("getService");
42-
getService.setAccessible(true);
41+
Method getServiceMethod = Toast.class.getDeclaredMethod("getService");
42+
getServiceMethod.setAccessible(true);
4343
// 执行方法,会返回一个 INotificationManager$Stub$Proxy 类型的对象
44-
final Object iNotificationManager = getService.invoke(null);
45-
if (iNotificationManager == null) {
44+
final Object notificationManagerSourceObject = getServiceMethod.invoke(null);
45+
if (notificationManagerSourceObject == null) {
4646
return;
4747
}
4848
// 如果这个对象已经被动态代理过了,并且已经 Hook 过了,则不需要重复 Hook
49-
if (Proxy.isProxyClass(iNotificationManager.getClass()) &&
50-
Proxy.getInvocationHandler(iNotificationManager) instanceof NotificationServiceProxy) {
49+
if (Proxy.isProxyClass(notificationManagerSourceObject.getClass()) &&
50+
Proxy.getInvocationHandler(notificationManagerSourceObject) instanceof NotificationServiceProxy) {
5151
return;
5252
}
53-
Object iNotificationManagerProxy = Proxy.newProxyInstance(
53+
Object notificationManagerProxyObject = Proxy.newProxyInstance(
5454
Thread.currentThread().getContextClassLoader(),
5555
new Class[]{Class.forName("android.app.INotificationManager")},
56-
new NotificationServiceProxy(iNotificationManager));
56+
new NotificationServiceProxy(notificationManagerSourceObject));
5757
// 将原来的 INotificationManager$Stub$Proxy 替换掉
58-
Field sService = Toast.class.getDeclaredField("sService");
59-
sService.setAccessible(true);
60-
sService.set(null, iNotificationManagerProxy);
58+
Field serviceField = Toast.class.getDeclaredField("sService");
59+
serviceField.setAccessible(true);
60+
serviceField.set(null, notificationManagerProxyObject);
6161
} catch (Exception e) {
6262
e.printStackTrace();
6363
}

library/src/main/java/com/hjq/toast/SafeToast.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,22 @@ private void hookToastTN() {
3939

4040
try {
4141
// 获取 Toast.mTN 字段对象
42-
Field mTNField = Toast.class.getDeclaredField("mTN");
43-
mTNField.setAccessible(true);
44-
Object mTN = mTNField.get(this);
42+
Field tnField = Toast.class.getDeclaredField("mTN");
43+
tnField.setAccessible(true);
44+
Object tnObject = tnField.get(this);
4545

4646
// 获取 mTN 中的 mHandler 字段对象
47-
Field mHandlerField = mTNField.getType().getDeclaredField("mHandler");
48-
mHandlerField.setAccessible(true);
49-
Handler mHandler = (Handler) mHandlerField.get(mTN);
47+
Field handlerField = tnField.getType().getDeclaredField("mHandler");
48+
handlerField.setAccessible(true);
49+
Handler handlerObject = (Handler) handlerField.get(tnObject);
5050

5151
// 如果这个对象已经被反射替换过了
52-
if (mHandler instanceof SafeHandler) {
52+
if (handlerObject instanceof SafeHandler) {
5353
return;
5454
}
5555

5656
// 偷梁换柱
57-
mHandlerField.set(mTN, new SafeHandler(mHandler));
57+
handlerField.set(tnObject, new SafeHandler(handlerObject));
5858

5959
} catch (IllegalAccessException | NoSuchFieldException e) {
6060
// Android 9.0 上反射会出现报错

0 commit comments

Comments
 (0)