1
+ package com.panda912.defensor.plugin.internal.interceptor
2
+
3
+ import com.panda912.defensor.plugin.internal.BytecodeInterceptor
4
+ import com.panda912.defensor.plugin.internal.Output
5
+ import com.panda912.defensor.plugin.internal.visitor.BaseClassReader
6
+ import com.panda912.defensor.plugin.internal.visitor.BaseClassVisitor
7
+ import com.panda912.defensor.plugin.internal.visitor.BaseMethodVisitor
8
+ import com.panda912.defensor.plugin.utils.TOAST_COMPAT
9
+ import com.panda912.defensor.plugin.utils.convertToStaticDescriptor
10
+ import com.panda912.defensor.plugin.utils.toInternalName
11
+ import org.objectweb.asm.ClassWriter
12
+ import org.objectweb.asm.MethodVisitor
13
+ import org.objectweb.asm.Opcodes
14
+
15
+ /* *
16
+ * Created by panda on 2022/5/24 19:00
17
+ */
18
+ class ToastInterceptor : BytecodeInterceptor {
19
+
20
+ override fun intercept (chain : BytecodeInterceptor .Chain ): Output {
21
+ val input = chain.request()
22
+
23
+ val cr = BaseClassReader (input.bytes)
24
+ val cw = ClassWriter (cr, ClassWriter .COMPUTE_MAXS )
25
+ cr.accept(object : BaseClassVisitor (cw) {
26
+ override fun visitMethod (
27
+ access : Int ,
28
+ name : String? ,
29
+ descriptor : String? ,
30
+ signature : String? ,
31
+ exceptions : Array <out String >?
32
+ ): MethodVisitor {
33
+ val mv = super .visitMethod(access, name, descriptor, signature, exceptions)
34
+ return ToastMethodVisitor (mv)
35
+ }
36
+ })
37
+ input.bytes = cw.toByteArray()
38
+ return chain.proceed(input)
39
+ }
40
+ }
41
+
42
+ class ToastMethodVisitor (mv : MethodVisitor ) : BaseMethodVisitor(mv) {
43
+
44
+ override fun visitMethodInsn (
45
+ opcode : Int ,
46
+ owner : String ,
47
+ name : String ,
48
+ descriptor : String ,
49
+ isInterface : Boolean
50
+ ) {
51
+
52
+ if (owner == " android/widget/Toast" && name == " show" && opcode == Opcodes .INVOKEVIRTUAL && descriptor == " ()V" ) {
53
+ super .visitMethodInsn(
54
+ Opcodes .INVOKESTATIC ,
55
+ TOAST_COMPAT .toInternalName(),
56
+ name,
57
+ descriptor.convertToStaticDescriptor(" Landroid/widget/Toast;" ),
58
+ isInterface
59
+ )
60
+ return
61
+ }
62
+
63
+ super .visitMethodInsn(opcode, owner, name, descriptor, isInterface)
64
+ }
65
+ }
0 commit comments