Skip to content

Commit a797fca

Browse files
committed
feat:完善 demo
1 parent 1c59e1f commit a797fca

File tree

3 files changed

+130
-2
lines changed

3 files changed

+130
-2
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.ysj.demo.aspect
2+
3+
import android.util.Log
4+
import com.ysj.lib.bcu.modifier.aspect.api.Aspect
5+
import com.ysj.lib.bcu.modifier.aspect.api.CallingPoint
6+
import com.ysj.lib.bcu.modifier.aspect.api.POSITION_CALL
7+
import com.ysj.lib.bcu.modifier.aspect.api.POSITION_RETURN
8+
import com.ysj.lib.bcu.modifier.aspect.api.POSITION_START
9+
import com.ysj.lib.bcu.modifier.aspect.api.Pointcut
10+
11+
/**
12+
* AOP 功能演示。
13+
*
14+
* @author Ysj
15+
* Create time: 2023/10/15
16+
*/
17+
@Aspect
18+
object AspectDemo {
19+
20+
private const val TAG = "AspectDemo"
21+
22+
/**
23+
* [MainActivity.test1]
24+
*/
25+
@Pointcut(
26+
target = "class:com/ysj/demo/aspect/MainActivity",
27+
funName = "test1",
28+
position = POSITION_START,
29+
)
30+
fun test1onStart() {
31+
Log.i(TAG, "test1onStart.")
32+
}
33+
34+
/**
35+
* [MainActivity.test1]
36+
*/
37+
@Pointcut(
38+
target = "class:.*/MainActivity",
39+
funName = "test1",
40+
position = POSITION_RETURN,
41+
)
42+
fun test1onReturn() {
43+
Log.i(TAG, "test1onReturn.")
44+
}
45+
46+
/**
47+
* [MainActivity.test2] (str: String)
48+
*/
49+
@Pointcut(
50+
target = "class:.*/MainActivity",
51+
funName = "test2",
52+
funDesc = "\\(Ljava/lang/String;\\)V",
53+
position = POSITION_CALL,
54+
)
55+
fun test2Str(cp: CallingPoint) {
56+
// 演示使用自定义参数调用源方法
57+
val arg = cp.args.first() as String
58+
cp.call("$arg ha ha!")
59+
}
60+
61+
/**
62+
* [MainActivity.test2] (num: Int)
63+
*/
64+
@Pointcut(
65+
target = "class:.*/MainActivity",
66+
funName = "test2",
67+
funDesc = "\\(I\\)V",
68+
position = POSITION_CALL,
69+
)
70+
fun test2Num(cp: CallingPoint) {
71+
Log.i(TAG, "test2Num: ${cp.args.contentToString()}")
72+
// 演示不执行 test2(num: Int) 方法
73+
// cp.call()
74+
}
75+
76+
/**
77+
* [MainActivity.test3]
78+
*/
79+
@Pointcut(
80+
target = "class:.*/MainActivity",
81+
funName = "test3",
82+
funDesc = "\\(II\\)I",
83+
position = POSITION_CALL,
84+
)
85+
fun test3(cp: CallingPoint): Int {
86+
// 演示返回自定义的值
87+
val result = cp.call() as Int
88+
return result + 1
89+
}
90+
91+
}

app/src/main/java/com/ysj/demo/aspect/MainActivity.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,38 @@ class MainActivity : AppCompatActivity() {
2525
vb.btnClickInterval.setOnClickListener {
2626
testClickInterval()
2727
}
28+
vb.btnAopDemo.setOnClickListener {
29+
onAopDemoClicked()
30+
}
2831
}
2932

3033
@ClickInterval
3134
private fun testClickInterval() {
3235
Log.i(TAG, "testClickInterval")
3336
}
3437

38+
private fun onAopDemoClicked() {
39+
test1()
40+
test2(12)
41+
test2("hello world")
42+
Log.i(TAG, "onAopDemoClicked: ${test3(1, 2)}")
43+
}
44+
45+
private fun test1() {
46+
Log.i(TAG, "test1.")
47+
}
48+
49+
private fun test2(str: String) {
50+
Log.i(TAG, "test2: $str")
51+
}
52+
53+
54+
private fun test2(num: Int) {
55+
Log.i(TAG, "test2: $num")
56+
}
57+
58+
private fun test3(a: Int, b: Int): Int {
59+
return a + b
60+
}
61+
3562
}

app/src/main/res/layout/activity_main.xml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
4-
xmlns:tools="http://schemas.android.com/tools"
54
android:layout_width="match_parent"
65
android:layout_height="match_parent"
76
android:gravity="center"
@@ -11,7 +10,18 @@
1110
android:id="@+id/btnClickInterval"
1211
android:layout_width="wrap_content"
1312
android:layout_height="wrap_content"
14-
android:text="测试点击间隔切面"
13+
android:text="演示点击间隔切面"
14+
app:layout_constraintBottom_toBottomOf="parent"
15+
app:layout_constraintLeft_toLeftOf="parent"
16+
app:layout_constraintRight_toRightOf="parent"
17+
app:layout_constraintTop_toTopOf="parent" />
18+
19+
<Button
20+
android:id="@+id/btnAopDemo"
21+
android:layout_width="wrap_content"
22+
android:layout_height="wrap_content"
23+
android:layout_marginTop="16dp"
24+
android:text="演示通用功能"
1525
app:layout_constraintBottom_toBottomOf="parent"
1626
app:layout_constraintLeft_toLeftOf="parent"
1727
app:layout_constraintRight_toRightOf="parent"

0 commit comments

Comments
 (0)