@@ -59,42 +59,33 @@ class BytecodePatchContext internal constructor(private val config: PatcherConfi
5959 internal val lookupMaps by lazy { LookupMaps (classes) }
6060
6161 /* *
62- * Merge the extensions for this set of patches .
62+ * Merge the extension of this patch .
6363 */
64- internal fun Set < Patch < * >>. mergeExtensions () {
65- // Lookup map to check if a class exists by its type quickly.
66- val classesByType = mutableMapOf< String , ClassDef >(). apply {
67- classes.forEach { classDef -> put( classDef.type, classDef) }
68- }
64+ internal fun BytecodePatch. mergeExtension () {
65+ extension?.use { extensionStream ->
66+ RawDexIO .readRawDexFile(extensionStream, 0 , null ).classes.forEach { classDef ->
67+ val existingClass = lookupMaps.classesByType[ classDef.type] ? : run {
68+ logger.fine( " Adding class \" $classDef \" " )
6969
70- forEachRecursively { patch ->
71- if (patch !is BytecodePatch ) return @forEachRecursively
70+ classes + = classDef
71+ lookupMaps.classesByType[classDef.type] = classDef
7272
73- patch.extension?.use { extensionStream ->
74- RawDexIO .readRawDexFile(extensionStream, 0 , null ).classes.forEach { classDef ->
75- val existingClass = classesByType[classDef.type] ? : run {
76- logger.fine(" Adding class \" $classDef \" " )
73+ return @forEach
74+ }
7775
78- classes + = classDef
79- classesByType[classDef.type] = classDef
76+ logger.fine(" Class \" $classDef \" exists already. Adding missing methods and fields." )
8077
81- return @forEach
78+ existingClass.merge(classDef, this @BytecodePatchContext).let { mergedClass ->
79+ // If the class was merged, replace the original class with the merged class.
80+ if (mergedClass == = existingClass) {
81+ return @let
8282 }
8383
84- logger.fine(" Class \" $classDef \" exists already. Adding missing methods and fields." )
85-
86- existingClass.merge(classDef, this @BytecodePatchContext).let { mergedClass ->
87- // If the class was merged, replace the original class with the merged class.
88- if (mergedClass == = existingClass) {
89- return @let
90- }
91-
92- classes - = existingClass
93- classes + = mergedClass
94- }
84+ classes - = existingClass
85+ classes + = mergedClass
9586 }
9687 }
97- }
88+ } ? : return logger.fine( " No extension to merge " )
9889 }
9990
10091 /* *
@@ -185,6 +176,11 @@ class BytecodePatchContext internal constructor(private val config: PatcherConfi
185176 */
186177 internal val methodsByStrings = MethodClassPairsLookupMap ()
187178
179+ // Lookup map for fast checking if a class exists by its type.
180+ val classesByType = mutableMapOf<String , ClassDef >().apply {
181+ classes.forEach { classDef -> put(classDef.type, classDef) }
182+ }
183+
188184 init {
189185 classes.forEach { classDef ->
190186 classDef.methods.forEach { method ->
@@ -231,6 +227,7 @@ class BytecodePatchContext internal constructor(private val config: PatcherConfi
231227
232228 override fun close () {
233229 methodsByStrings.clear()
230+ classesByType.clear()
234231 }
235232 }
236233
0 commit comments