Skip to content

Commit 1948371

Browse files
committed
default method hook example
1 parent 89b22a5 commit 1948371

File tree

6 files changed

+38
-4
lines changed

6 files changed

+38
-4
lines changed

examples/android-gradle-kts/build.gradle.kts

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ androidMethodHook {
4646
configs {
4747
create("debug") {
4848
addConfig("./methodhook/descriptor.conf")
49-
addConfig("./methodhook/okhttp.conf")
49+
addConfig("./methodhook/default.conf")
5050
addConfig("./methodhook/auto_trace.conf")
51+
52+
addConfig("./methodhook/okhttp.conf")
5153
addConfig("./methodhook/frame_counter.conf")
5254
}
5355
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
descriptorFoo {
2+
type = "default"
3+
package = "io.github.aleksrychkov.example.sandbox"
4+
superClass = "*"
5+
interfaces = []
6+
class = "io.github.aleksrychkov.example.sandbox.DefaultPlayground"
7+
methods = [ "test" ]
8+
enter = "io.github.aleksrychkov.methodhook.DefaultHook.enter"
9+
exit = "io.github.aleksrychkov.methodhook.DefaultHook.exit"
10+
}

examples/android-gradle-kts/src/main/kotlin/io/github/aleksrychkov/example/MainActivity.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.aleksrychkov.example
22

33
import android.os.Bundle
4+
import io.github.aleksrychkov.example.sandbox.DefaultPlayground
45
import io.github.aleksrychkov.example.sandbox.DescriptorPlayground
56
import io.github.aleksrychkov.example.sandbox.OkhttpPlayground
67
import io.github.aleksrychkov.example.sandbox.Playground
@@ -10,7 +11,8 @@ class MainActivity : AbstractActivity(), AutoTrace {
1011

1112
private val sandbox: List<Playground> = listOf(
1213
DescriptorPlayground(),
13-
OkhttpPlayground()
14+
OkhttpPlayground(),
15+
DefaultPlayground(),
1416
)
1517

1618
override fun onCreate(savedInstanceState: Bundle?) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.github.aleksrychkov.example.sandbox
2+
3+
class DefaultPlayground : Playground {
4+
override fun test() {}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.github.aleksrychkov.methodhook
2+
3+
@Suppress("unused")
4+
object DefaultHook {
5+
6+
@JvmStatic
7+
fun enter(clazz: String, method: String, descriptor: String) {
8+
println("DefaultHook::enter::$clazz.$method.$descriptor")
9+
}
10+
11+
@JvmStatic
12+
fun exit(clazz: String, method: String, descriptor: String) {
13+
println("DefaultHook::exit::$clazz.$method.$descriptor")
14+
}
15+
}

methodhook/src/main/kotlin/io/github/aleksrychkov/methodhook/injects/injectors/DefaultInjector.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal class DefaultInjector(
2727
override fun onEnter(context: InjectorContext, mv: MethodVisitor) = with(mv) {
2828
if (enterInjectMethod == null) return@with
2929

30-
visitLdcInsn(context.className)
30+
visitLdcInsn(context.className.replace("/", "."))
3131
visitLdcInsn(context.methodName)
3232
visitLdcInsn(context.methodDescriptor)
3333

@@ -48,7 +48,7 @@ internal class DefaultInjector(
4848
) = with(mv) {
4949
if (exitInjectMethod == null) return@with
5050

51-
visitLdcInsn(context.className)
51+
visitLdcInsn(context.className.replace("/", "."))
5252
visitLdcInsn(context.methodName)
5353
visitLdcInsn(context.methodDescriptor)
5454

0 commit comments

Comments
 (0)