|
| 1 | +/* |
| 2 | + * Tencent is pleased to support the open source community by making Tencent Shadow available. |
| 3 | + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | + * |
| 5 | + * Licensed under the BSD 3-Clause License (the "License"); you may not use |
| 6 | + * this file except in compliance with the License. You may obtain a copy of |
| 7 | + * the License at |
| 8 | + * |
| 9 | + * https://opensource.org/licenses/BSD-3-Clause |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package com.tencent.shadow.core.transform.specific |
| 20 | + |
| 21 | +import com.tencent.shadow.core.transform_kit.SpecificTransform |
| 22 | +import com.tencent.shadow.core.transform_kit.TransformStep |
| 23 | +import javassist.* |
| 24 | +import javassist.compiler.Javac.CtFieldWithInit |
| 25 | + |
| 26 | +/** |
| 27 | + * 系统回调BroadcastReceiver的onReceive(Context context, Intent intent)方法时: |
| 28 | + * 1. 传回的context是宿主的,需要修改为插件的。 |
| 29 | + * 2. intent的ExtrasClassLoader是宿主的,需要改为插件的。 |
| 30 | + * |
| 31 | + * 如果是系统类的BroadcastReceiver,它也不会和插件的context或classloader有什么联系, |
| 32 | + * 所以我们只需要修改插件代码中的BroadcastReceiver。 |
| 33 | + * |
| 34 | + * 把原本插件的onReceive方法改个名字,再统一添加一个onReceive方法。 |
| 35 | + * 在新增的onReceive方法中修改收到的系统回调参数,再转调被改名了的原本插件的onReceive方法。 |
| 36 | + */ |
| 37 | +class ReceiverSupportTransform : SpecificTransform() { |
| 38 | + |
| 39 | + companion object { |
| 40 | + const val AndroidBroadcastReceiverClassname = "android.content.BroadcastReceiver" |
| 41 | + const val AndroidContextClassname = "android.content.Context" |
| 42 | + const val AndroidIntentClassname = "android.content.Intent" |
| 43 | + } |
| 44 | + |
| 45 | + private fun CtClass.isReceiver(): Boolean = isClassOf(AndroidBroadcastReceiverClassname) |
| 46 | + |
| 47 | + override fun setup(allInputClass: Set<CtClass>) { |
| 48 | + mClassPool.importPackage("android.content") |
| 49 | + mClassPool.importPackage("com.tencent.shadow.core.runtime") |
| 50 | + |
| 51 | + val androidContext = mClassPool[AndroidContextClassname] |
| 52 | + val androidIntent = mClassPool[AndroidIntentClassname] |
| 53 | + |
| 54 | + /** |
| 55 | + * 收集覆盖了onReceive方法的Receiver作为修改目标 |
| 56 | + */ |
| 57 | + val targetReceivers = mutableSetOf<CtClass>() |
| 58 | + newStep(object : TransformStep { |
| 59 | + override fun filter(allInputClass: Set<CtClass>) = allInputClass |
| 60 | + .filter { it.isReceiver() } |
| 61 | + .toSet() |
| 62 | + |
| 63 | + override fun transform(ctClass: CtClass) { |
| 64 | + val onReceiveMethod: CtMethod? = |
| 65 | + try { |
| 66 | + ctClass.getDeclaredMethod( |
| 67 | + "onReceive", |
| 68 | + arrayOf(androidContext, androidIntent) |
| 69 | + ) |
| 70 | + } catch (e: NotFoundException) { |
| 71 | + null |
| 72 | + } |
| 73 | + if (onReceiveMethod != null) { |
| 74 | + targetReceivers.add(ctClass) |
| 75 | + } |
| 76 | + } |
| 77 | + }) |
| 78 | + |
| 79 | + /** |
| 80 | + * 对原本的onReceive方法改名,并添加新的onReceive方法。 |
| 81 | + */ |
| 82 | + newStep(object : TransformStep { |
| 83 | + override fun filter(allInputClass: Set<CtClass>) = targetReceivers |
| 84 | + |
| 85 | + override fun transform(ctClass: CtClass) { |
| 86 | + ctClass.defrost() |
| 87 | + |
| 88 | + // 改名 |
| 89 | + val originalOnReceiveMethod: CtMethod = |
| 90 | + ctClass.getDeclaredMethod( |
| 91 | + "onReceive", |
| 92 | + arrayOf(androidContext, androidIntent) |
| 93 | + ) |
| 94 | + originalOnReceiveMethod.name = "onReceiveShadowContext" |
| 95 | + originalOnReceiveMethod.modifiers = |
| 96 | + Modifier.setPrivate(originalOnReceiveMethod.modifiers) |
| 97 | + |
| 98 | + // 声明两个域变量保存onReceive收到的原始参数,供调用super方法时使用。 |
| 99 | + // the compiler embedded in Javassist does not support generics |
| 100 | + arrayOf( |
| 101 | + CtFieldWithInit.make( |
| 102 | + "ThreadLocal originalOnReceiveContext = new ThreadLocal();", |
| 103 | + ctClass |
| 104 | + ), |
| 105 | + CtFieldWithInit.make( |
| 106 | + "ThreadLocal originalOnReceiveIntent = new ThreadLocal();", |
| 107 | + ctClass |
| 108 | + ), |
| 109 | + ).forEach { |
| 110 | + ctClass.addField(it) |
| 111 | + } |
| 112 | + |
| 113 | + // 添加新onReceive方法 |
| 114 | + val newOnReceiveMethod = CtMethod.make( |
| 115 | + """ |
| 116 | + public void onReceive(Context context, Intent intent) { |
| 117 | + try{ |
| 118 | + //保存收到的参数 |
| 119 | + originalOnReceiveContext.set(context); |
| 120 | + originalOnReceiveIntent.set(intent); |
| 121 | + |
| 122 | + //通过当前插件类ClassLoader找到相关的插件Application |
| 123 | + ClassLoader cl = this.getClass().getClassLoader(); |
| 124 | + PluginPartInfo info = PluginPartInfoManager.getPluginInfo(cl); |
| 125 | + Context shadowContext = info.application; |
| 126 | + Intent intentCopy = new Intent(intent);//不修改原本的intent |
| 127 | + intentCopy.setExtrasClassLoader(cl); |
| 128 | + |
| 129 | + //调用原本的onReceive方法 |
| 130 | + onReceiveShadowContext(shadowContext, intentCopy); |
| 131 | + }finally { |
| 132 | + originalOnReceiveContext.remove(); |
| 133 | + originalOnReceiveIntent.remove(); |
| 134 | + } |
| 135 | + } |
| 136 | + """.trimIndent(), ctClass |
| 137 | + ) |
| 138 | + ctClass.addMethod(newOnReceiveMethod) |
| 139 | + |
| 140 | + // 定义superOnReceiveMethod方法 |
| 141 | + val newSuperMethod = CtMethod.make( |
| 142 | + """ |
| 143 | + private void superOnReceive(Context context, Intent intent) { |
| 144 | + context = (Context)originalOnReceiveContext.get(); |
| 145 | + intent = (Intent)originalOnReceiveIntent.get(); |
| 146 | + super.onReceive(context, intent); |
| 147 | + } |
| 148 | + """.trimIndent(), ctClass |
| 149 | + ) |
| 150 | + |
| 151 | + //转调super.onReceive方法 |
| 152 | + val superMethod: CtMethod = |
| 153 | + ctClass.superclass.getMethod( |
| 154 | + "onReceive", |
| 155 | + "(Landroid/content/Context;Landroid/content/Intent;)V" |
| 156 | + ) |
| 157 | + |
| 158 | + if (Modifier.isAbstract(superMethod.modifiers).not()) { |
| 159 | + val codeConverter = CodeConverter() |
| 160 | + codeConverter.redirectMethodCall(superMethod, newSuperMethod) |
| 161 | + try { |
| 162 | + ctClass.instrument(codeConverter) |
| 163 | + } catch (e: Exception) { |
| 164 | + System.err.println("处理" + ctClass.name + "时出错:" + e) |
| 165 | + throw e |
| 166 | + } |
| 167 | + ctClass.addMethod(newSuperMethod) |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + }) |
| 172 | + } |
| 173 | +} |
0 commit comments