Skip to content

Commit

Permalink
feat: add extensions for cloning methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Sculas authored and oSumAtrIX committed Jun 5, 2022
1 parent 6c97975 commit 01bfbd6
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/main/kotlin/app/revanced/patcher/extensions/Extensions.kt
@@ -1,8 +1,13 @@
package app.revanced.patcher.extensions

import app.revanced.patcher.proxy.mutableTypes.MutableMethod
import app.revanced.patcher.proxy.mutableTypes.MutableMethod.Companion.toMutable
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.builder.BuilderInstruction
import org.jf.dexlib2.builder.MutableMethodImplementation
import org.jf.dexlib2.iface.Method
import org.jf.dexlib2.immutable.ImmutableMethod
import org.jf.dexlib2.immutable.ImmutableMethodImplementation

infix fun AccessFlags.or(other: AccessFlags) = this.value or other.value
infix fun Int.or(other: AccessFlags) = this or other.value
Expand All @@ -11,4 +16,43 @@ fun MutableMethodImplementation.addInstructions(index: Int, instructions: List<B
for (i in instructions.lastIndex downTo 0) {
this.addInstruction(index, instructions[i])
}
}
}

/**
* Clones the method.
* @param registerCount This parameter allows you to change the register count of the method.
* This may be a positive or negative number.
* @return The **immutable** cloned method. Call [toMutable] or [cloneMutable] to get a **mutable** copy.
*/
fun Method.clone(
registerCount: Int = 0,
): ImmutableMethod {
val clonedImplementation = implementation?.let {
ImmutableMethodImplementation(
it.registerCount + registerCount,
it.instructions,
it.tryBlocks,
it.debugItems,
)
}
return ImmutableMethod(
returnType,
name,
parameters,
returnType,
accessFlags,
annotations,
hiddenApiRestrictions,
clonedImplementation
)
}

/**
* Clones the method.
* @param registerCount This parameter allows you to change the register count of the method.
* This may be a positive or negative number.
* @return The **mutable** cloned method. Call [clone] to get an **immutable** copy.
*/
fun Method.cloneMutable(
registerCount: Int = 0,
) = clone(registerCount).toMutable()

0 comments on commit 01bfbd6

Please sign in to comment.