@@ -59,42 +59,33 @@ class BytecodePatchContext internal constructor(private val config: PatcherConfi
59
59
internal val lookupMaps by lazy { LookupMaps (classes) }
60
60
61
61
/* *
62
- * Merge the extensions for this set of patches .
62
+ * Merge the extension of this patch .
63
63
*/
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 \" " )
69
69
70
- forEachRecursively { patch ->
71
- if (patch !is BytecodePatch ) return @forEachRecursively
70
+ classes + = classDef
71
+ lookupMaps.classesByType[classDef.type] = classDef
72
72
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
+ }
77
75
78
- classes + = classDef
79
- classesByType[classDef.type] = classDef
76
+ logger.fine(" Class \" $classDef \" exists already. Adding missing methods and fields." )
80
77
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
82
82
}
83
83
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
95
86
}
96
87
}
97
- }
88
+ } ? : return logger.fine( " No extension to merge " )
98
89
}
99
90
100
91
/* *
@@ -185,6 +176,11 @@ class BytecodePatchContext internal constructor(private val config: PatcherConfi
185
176
*/
186
177
internal val methodsByStrings = MethodClassPairsLookupMap ()
187
178
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
+
188
184
init {
189
185
classes.forEach { classDef ->
190
186
classDef.methods.forEach { method ->
@@ -231,6 +227,7 @@ class BytecodePatchContext internal constructor(private val config: PatcherConfi
231
227
232
228
override fun close () {
233
229
methodsByStrings.clear()
230
+ classesByType.clear()
234
231
}
235
232
}
236
233
0 commit comments