Skip to content

Commit 9378fd3

Browse files
author
Vens Chen
committed
update
1 parent 739c800 commit 9378fd3

File tree

15 files changed

+121
-115
lines changed

15 files changed

+121
-115
lines changed

README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ ttree {
3636
],
3737
//插入的字节码,方法的执行顺序visitAnnotation->onMethodEnter->onMethodExit
3838
'MethodVisitor': {
39-
MethodVisitor methodVisitor, int access, String name, String desc ->
40-
AutoMethodVisitor adapter = new AutoMethodVisitor(methodVisitor, access, name, desc) {
39+
MethodVisitor methodVisitor, int access, String name, String desc, String className ->
40+
AutoMethodVisitor adapter = new AutoMethodVisitor(methodVisitor, access, name, desc, className) {
4141
@Override
4242
protected void onMethodEnter() {
4343
super.onMethodEnter();
@@ -68,11 +68,11 @@ ClassFilter,ClassName,MethodName,ContainName,InterfaceName,MethodDes
6868

6969
**AnnotationReceiver:** String类型,使用自定义注解时,处理注解的类,必须使用全路径,
7070
处理的方法必须是:`public static void onMethodExitForAnnotation``public static void onMethodExitForAnnotation`
71-
参数必须是`(String annotationName,String methodName, String jsonValue)`,jsonValue的结构是Hashmap<String,Object>,对应注解的key和value。
71+
参数类型必须是`(String annotationName,String methodName, String jsonValue)`,jsonValue的结构是Hashmap<String,Object>,对应注解的key和value。
7272

7373
**ClassReceiver:** String类型,使用匹配规则时的处理类,必须使用全路径,可以与AnnotationReceiver同名,
7474
处理的方法必须是:`public static void onMethodEnterForClass``public static void onMethodExitForClass`
75-
参数必须是`(String methodName,Object[] objects)`,objects存放了methodName这个方法的参数值,
75+
参数类型必须是`(String className,String methodName,Object[] objects)`,objects存放了methodName这个方法的参数值,
7676
自定义ClassReceiver之后,拓展包里面的ClassReceiver将不再接收。
7777

7878
**ClassName:** String类型,类名,全路径
@@ -86,6 +86,7 @@ ClassFilter,ClassName,MethodName,ContainName,InterfaceName,MethodDes
8686
**MethodDes:** String类型,方法的描述符
8787

8888
**Override:** boolean类型,是否重载MethodVisitor(高级用法,需要对asm有一定的了解)
89+
8990
**MethodVisitor:** 在Override为true的时候使用(参考demo)
9091

9192
**匹配规则优先级:** ClassName > InterfaceName > ContainName
@@ -102,7 +103,7 @@ buildscript {
102103
}
103104
}
104105
dependencies {
105-
classpath 'com.github.chenthreetrees:ttreeplugin:2.0.1'
106+
classpath 'com.github.chenthreetrees:ttreeplugin:2.0.2'
106107
}
107108
}
108109
```
@@ -125,7 +126,7 @@ allprojects {
125126

126127
在app的gradle文件引用依赖:
127128
```
128-
compile 'com.github.chenthreetrees:ttreepluginext:1.0.1'
129+
compile 'com.github.chenthreetrees:ttreepluginext:1.0.2'
129130
```
130131

131132
在application初始化:
@@ -160,7 +161,8 @@ TtreePlugin.setOnCutListener(new TtreePlugin.IOnCutListener() {
160161

161162
**动态权限申请**
162163

163-
注意:动态申请的权限需要在manifest注册
164+
**注意**:动态申请的权限需要在manifest注册
165+
164166
在需要动态申请权限的方法添加注解
165167
`@Permission`,权限使用`PermissionConsts`里面的值
166168
例如:
@@ -201,16 +203,17 @@ ttree {
201203
```
202204
TtreePlugin.setOnTrackListener(new TtreePlugin.IOnTrackListener() {
203205
@Override
204-
public void onTrackEnter(String methodName, Object[] objects)
206+
public void onTrackEnter(String className,String methodName, Object[] objects)
205207
{
206208
}
207209
208210
@Override
209-
public void onTrackExit(String methodName, Object[] objects)
211+
public void onTrackExit(String className,String methodName, Object[] objects)
210212
{
211213
}
212214
});
213215
```
216+
**编译完成后,可以在app项目路径build\intermediates\transforms\AutoTransform查看最终注入的代码**
214217

215218
**其他使用场景,在平时开发中有遇到,再进行拓展**
216219

app/build.gradle

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -28,66 +28,66 @@ dependencies {
2828
}
2929

3030

31-
buildscript {
32-
repositories {
33-
maven {
34-
url uri('../repo')
35-
}
36-
}
37-
dependencies {
38-
classpath 'com.threetrees.plugin:plugin:1.0.0'
39-
}
40-
}
41-
42-
apply plugin: 'ttreeplugin'
43-
44-
import com.threetrees.plugin.asm.AutoMethodVisitor
45-
import org.objectweb.asm.MethodVisitor
46-
import org.objectweb.asm.Opcodes
47-
48-
ttree {
49-
isDebug = true
50-
//具体配置
51-
matchData = [
52-
'AnnotationPath' : 'com.threetree.ttreeplugin.annotation',
53-
'AnnotationReceiver' : 'com.threetree.ttreeplugin.MyReceiver',
54-
'ClassReceiver' : 'com.threetree.ttreeplugin.MyReceiver',
55-
'ClassFilter' : [
56-
//根据类型匹配
57-
['ClassName' : 'com/threetree/ttreeplugin/MainActivity',
58-
'MethodName': 'testClassName', 'MethodDes': '()V'],
59-
//根据关键字匹配
60-
['ContainName' : 'Activity',
61-
'MethodName': 'testContainName', 'MethodDes': '(Ljava/lang/String;)V'],
62-
//根据接口名匹配
63-
['InterfaceName': 'android/view/View$OnClickListener',
64-
'MethodName': 'onClick', 'MethodDes': '(Landroid/view/View;)V'],
65-
//根据类型匹配
66-
['ClassName' : 'com/threetree/ttreeplugin/MainActivity',
67-
'MethodName': 'testOverride', 'MethodDes': '()V', 'Override' : true]
68-
],
69-
//插入的字节码,方法的执行顺序visitAnnotation->onMethodEnter->onMethodExit
70-
'MethodVisitor': {
71-
MethodVisitor methodVisitor, int access, String name, String desc ->
72-
AutoMethodVisitor adapter = new AutoMethodVisitor(methodVisitor, access, name, desc) {
73-
@Override
74-
protected void onMethodEnter() {
75-
super.onMethodEnter();
76-
if("testOverride".equals(name))
77-
{
78-
methodVisitor.visitLdcInsn(name)
79-
methodVisitor.visitLdcInsn("========start=========")
80-
methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, "android/util/Log", "e", "(Ljava/lang/String;Ljava/lang/String;)I", false)
81-
}
82-
}
83-
84-
@Override
85-
protected void onMethodExit(int opcode) {
86-
super.onMethodExit(opcode)
87-
}
88-
}
89-
return adapter
90-
}
91-
]
92-
}
31+
//buildscript {
32+
// repositories {
33+
// maven {
34+
// url uri('../repo')
35+
// }
36+
// }
37+
// dependencies {
38+
// classpath 'com.threetrees.plugin:plugin:1.0.0'
39+
// }
40+
//}
41+
//
42+
//apply plugin: 'ttreeplugin'
43+
//
44+
//import com.threetrees.plugin.asm.AutoMethodVisitor
45+
//import org.objectweb.asm.MethodVisitor
46+
//import org.objectweb.asm.Opcodes
47+
//
48+
//ttree {
49+
// isDebug = true
50+
// //具体配置
51+
// matchData = [
52+
// 'AnnotationPath' : 'com.threetree.ttreeplugin.annotation',
53+
// 'AnnotationReceiver' : 'com.threetree.ttreeplugin.MyReceiver',
54+
// 'ClassReceiver' : 'com.threetree.ttreeplugin.MyReceiver',
55+
// 'ClassFilter' : [
56+
// //根据类型匹配
57+
// ['ClassName' : 'com/threetree/ttreeplugin/MainActivity',
58+
// 'MethodName': 'testClassName', 'MethodDes': '()V'],
59+
// //根据关键字匹配
60+
// ['ContainName' : 'Activity',
61+
// 'MethodName': 'testContainName', 'MethodDes': '(Ljava/lang/String;)V'],
62+
// //根据接口名匹配
63+
// ['InterfaceName': 'android/view/View$OnClickListener',
64+
// 'MethodName': 'onClick', 'MethodDes': '(Landroid/view/View;)V'],
65+
// //根据类型匹配
66+
// ['ClassName' : 'com/threetree/ttreeplugin/MainActivity',
67+
// 'MethodName': 'testOverride', 'MethodDes': '()V', 'Override' : true]
68+
// ],
69+
// //插入的字节码,方法的执行顺序visitAnnotation->onMethodEnter->onMethodExit
70+
// 'MethodVisitor': {
71+
// MethodVisitor methodVisitor, int access, String name, String desc, String className ->
72+
// AutoMethodVisitor adapter = new AutoMethodVisitor(methodVisitor, access, name, desc, className) {
73+
// @Override
74+
// protected void onMethodEnter() {
75+
// super.onMethodEnter();
76+
// if("testOverride".equals(name))
77+
// {
78+
// methodVisitor.visitLdcInsn(name)
79+
// methodVisitor.visitLdcInsn("========start=========")
80+
// methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, "android/util/Log", "e", "(Ljava/lang/String;Ljava/lang/String;)I", false)
81+
// }
82+
// }
83+
//
84+
// @Override
85+
// protected void onMethodExit(int opcode) {
86+
// super.onMethodExit(opcode)
87+
// }
88+
// }
89+
// return adapter
90+
// }
91+
// ]
92+
//}
9393

app/src/main/java/com/threetree/ttreeplugin/BaseApplication.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void onCutExit(int type)
4242

4343
TtreePlugin.setOnTrackListener(new TtreePlugin.IOnTrackListener() {
4444
@Override
45-
public void onTrackEnter(String methodName, Object[] objects)
45+
public void onTrackEnter(String className,String methodName, Object[] objects)
4646
{
4747
if("onClick".equals(methodName))
4848
{
@@ -55,7 +55,7 @@ public void onTrackEnter(String methodName, Object[] objects)
5555
}
5656

5757
@Override
58-
public void onTrackExit(String methodName, Object[] objects)
58+
public void onTrackExit(String className,String methodName, Object[] objects)
5959
{
6060
if("onClick".equals(methodName))
6161
{

app/src/main/java/com/threetree/ttreeplugin/MyReceiver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class MyReceiver {
2222
* @param methodName
2323
* @param objects
2424
*/
25-
public static void onMethodEnterForClass(String methodName,Object[] objects)
25+
public static void onMethodEnterForClass(String className,String methodName,Object[] objects)
2626
{
2727
Log.e("onMethodEnterForClass",methodName);
2828
if("onClick".equals(methodName))
@@ -40,7 +40,7 @@ public static void onMethodEnterForClass(String methodName,Object[] objects)
4040
* @param methodName
4141
* @param objects
4242
*/
43-
public static void onMethodExitForClass(String methodName,Object[] objects)
43+
public static void onMethodExitForClass(String className,String methodName,Object[] objects)
4444
{
4545
Log.e("onMethodExitForClass",methodName);
4646
if("onClick".equals(methodName))

plugin/build.gradle

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
apply plugin: 'groovy'
22
apply plugin: 'java'//导入java插件用于,编译打包我们的插件
3-
apply plugin: 'maven'//maven插件,用于上传插件到仓库
3+
//apply plugin: 'maven'//maven插件,用于上传插件到仓库
44

5-
//// JitPack Maven
6-
//apply plugin: 'com.github.dcendents.android-maven'
7-
//// Your Group
8-
//group='com.github.chenthreetrees'
5+
// JitPack Maven
6+
apply plugin: 'com.github.dcendents.android-maven'
7+
// Your Group
8+
group='com.github.chenthreetrees'
99

1010
compileGroovy {
1111
sourceCompatibility = 1.7
@@ -26,16 +26,16 @@ repositories {
2626
mavenCentral()
2727
}
2828

29-
group='com.threetrees.plugin'
30-
version='1.0.0'
31-
32-
//uploadArchives 类型是upload,这个task不是'maven'创建的
33-
//而是'maven'定义了一个rule,而后由我们自己创建的
34-
uploadArchives{
35-
//本地仓库的一种
36-
repositories{
37-
mavenDeployer {
38-
repository(url: uri('../repo'))
39-
}
40-
}
41-
}
29+
//group='com.threetrees.plugin'
30+
//version='1.0.0'
31+
//
32+
////uploadArchives 类型是upload,这个task不是'maven'创建的
33+
////而是'maven'定义了一个rule,而后由我们自己创建的
34+
//uploadArchives{
35+
// //本地仓库的一种
36+
// repositories{
37+
// mavenDeployer {
38+
// repository(url: uri('../repo'))
39+
// }
40+
// }
41+
//}

plugin/src/main/groovy/com/threetrees/plugin/asm/AutoClassVisitor.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,15 @@ public class AutoClassVisitor extends ClassVisitor {
145145
if (vivi != null && result==4) {
146146
Logger.info("||-----------------外部配置匹配${name}--------------------------")
147147
try {
148-
adapter = vivi(methodVisitor, access, name, desc)
148+
adapter = vivi(methodVisitor, access, name, desc, mClassName)
149149
} catch (Exception e) {
150150
e.printStackTrace()
151151
}
152152
}
153153

154154
if(adapter == null)
155155
{
156-
adapter = new AutoMethodVisitor(methodVisitor,access,name,desc,result)
156+
adapter = new AutoMethodVisitor(methodVisitor,access,name,desc,mClassName,result)
157157
}
158158
return adapter
159159
}

plugin/src/main/groovy/com/threetrees/plugin/asm/AutoMethodVisitor.groovy

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.objectweb.asm.commons.AdviceAdapter
88

99
public class AutoMethodVisitor extends AdviceAdapter {
1010

11+
String className
1112
String methodName
1213
List<AutoAnnotationVisitor> annotations
1314

@@ -20,14 +21,15 @@ public class AutoMethodVisitor extends AdviceAdapter {
2021
//注解的路径
2122
String annotationPath
2223

23-
public AutoMethodVisitor(MethodVisitor mv, int access, String name, String desc) {
24-
this(mv,access,name,desc,0)
24+
public AutoMethodVisitor(MethodVisitor mv, int access, String name, String desc, String clsName) {
25+
this(mv,access,name,desc,clsName,0)
2526
}
2627

27-
public AutoMethodVisitor(MethodVisitor mv, int access, String name, String desc,int matchType) {
28+
public AutoMethodVisitor(MethodVisitor mv, int access, String name, String desc,String clsName,int matchType) {
2829
super(Opcodes.ASM5, mv, access, name, desc)
30+
className = TextUtil.changeClassNameDot(clsName)
31+
Logger.info("className${className}")
2932
methodName = name
30-
methodDesc = desc
3133
annotations = new ArrayList<AutoAnnotationVisitor>()
3234
map = new HashMap<>()
3335
annotationReceive = "com/threetree/pluginutil/TtreeReceiver"
@@ -225,6 +227,7 @@ public class AutoMethodVisitor extends AdviceAdapter {
225227
return
226228
}
227229

230+
mv.visitLdcInsn(className)
228231
mv.visitLdcInsn(methodName)
229232

230233
boolean isStatic = false
@@ -248,11 +251,11 @@ public class AutoMethodVisitor extends AdviceAdapter {
248251
if(!TextUtil.isEmpty(receiver))
249252
{
250253
mv.visitMethodInsn(INVOKESTATIC, receiver,
251-
action, "(Ljava/lang/String;[Ljava/lang/Object;)V", false)
254+
action, "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V", false)
252255
}else
253256
{
254257
mv.visitMethodInsn(INVOKESTATIC, classReceiver,
255-
action, "(Ljava/lang/String;[Ljava/lang/Object;)V", false)
258+
action, "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V", false)
256259
}
257260

258261
}

pluginutil/src/main/java/com/threetree/pluginutil/TtreePlugin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ public interface IOnCutListener
4141
*/
4242
public interface IOnTrackListener
4343
{
44-
void onTrackEnter(String methodName,Object[] objects);
44+
void onTrackEnter(String className,String methodName,Object[] objects);
4545

46-
void onTrackExit(String methodName,Object[] objects);
46+
void onTrackExit(String className,String methodName,Object[] objects);
4747
}
4848

4949
/**

0 commit comments

Comments
 (0)