Skip to content

Commit 0c61142

Browse files
author
Vens Chen
committed
update
1 parent f4cf014 commit 0c61142

File tree

18 files changed

+605
-382
lines changed

18 files changed

+605
-382
lines changed

README.md

Lines changed: 174 additions & 125 deletions
Large diffs are not rendered by default.

app/build.gradle

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -28,73 +28,73 @@ 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-
['ClassName' : 'com/threetree/ttreeplugin/MainActivity',
61-
'MethodName': 'testBoolean', 'MethodDes': '(Z)V'],
62-
//根据关键字匹配
63-
['ContainName' : 'Activity',
64-
'MethodName': 'testContainName', 'MethodDes': '(Ljava/lang/String;)V'],
65-
//根据接口名匹配
66-
['InterfaceName': 'android/view/View$OnClickListener',
67-
'MethodName': 'onClick', 'MethodDes': '(Landroid/view/View;)V'],
68-
//根据类型匹配,外部配置覆盖
69-
['ClassName' : 'com/threetree/ttreeplugin/MainActivity',
70-
'MethodName': 'testOverride', 'MethodDes': '()V', 'Override' : true],
71-
//根据类型匹配,拦截方法
72-
['ClassName' : 'com/threetree/ttreeplugin/MainActivity',
73-
'MethodName': 'testInterceptForClass', 'MethodDes': '()V', 'Intercept' : true]
74-
],
75-
//插入的字节码,方法的执行顺序visitAnnotation->onMethodEnter->onMethodExit
76-
'MethodVisitor': {
77-
MethodVisitor mv, int access, String name, String desc, String className ->
78-
AutoMethodVisitor adapter = new AutoMethodVisitor(mv, access, name, desc, className) {
79-
@Override
80-
protected void onMethodEnter() {
81-
super.onMethodEnter();
82-
if("testOverride".equals(name))
83-
{
84-
85-
mv.visitLdcInsn(name)
86-
mv.visitLdcInsn("========start=========")
87-
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "android/util/Log", "e", "(Ljava/lang/String;Ljava/lang/String;)I", false)
88-
}
89-
}
90-
91-
@Override
92-
protected void onMethodExit(int opcode) {
93-
super.onMethodExit(opcode)
94-
}
95-
}
96-
return adapter
97-
}
98-
]
99-
}
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+
// ['ClassName' : 'com/threetree/ttreeplugin/MainActivity',
61+
// 'MethodName': 'testBoolean', 'MethodDes': '(Z)V'],
62+
// //根据关键字匹配
63+
// ['ContainName' : 'Activity',
64+
// 'MethodName': 'testContainName', 'MethodDes': '(Ljava/lang/String;)V'],
65+
// //根据接口名匹配
66+
// ['InterfaceName': 'android/view/View$OnClickListener',
67+
// 'MethodName': 'onClick', 'MethodDes': '(Landroid/view/View;)V'],
68+
// //根据类型匹配,外部配置覆盖
69+
// ['ClassName' : 'com/threetree/ttreeplugin/MainActivity',
70+
// 'MethodName': 'testOverride', 'MethodDes': '()V', 'Override' : true],
71+
// //根据类型匹配,拦截方法
72+
// ['ClassName' : 'com/threetree/ttreeplugin/MainActivity',
73+
// 'MethodName': 'testInterceptForClass', 'MethodDes': '()I']
74+
// ],
75+
// //插入的字节码,方法的执行顺序visitAnnotation->onMethodEnter->onMethodExit
76+
// 'MethodVisitor': {
77+
// MethodVisitor mv, int access, String name, String desc, String className ->
78+
// AutoMethodVisitor adapter = new AutoMethodVisitor(mv, access, name, desc, className) {
79+
// @Override
80+
// protected void onMethodEnter() {
81+
// super.onMethodEnter();
82+
// if("testOverride".equals(name))
83+
// {
84+
//
85+
// mv.visitLdcInsn(name)
86+
// mv.visitLdcInsn("========start=========")
87+
// mv.visitMethodInsn(Opcodes.INVOKESTATIC, "android/util/Log", "e", "(Ljava/lang/String;Ljava/lang/String;)I", false)
88+
// }
89+
// }
90+
//
91+
// @Override
92+
// protected void onMethodExit(int opcode) {
93+
// super.onMethodExit(opcode)
94+
// }
95+
// }
96+
// return adapter
97+
// }
98+
// ]
99+
//}
100100

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.app.Application;
44
import android.util.Log;
55
import android.view.View;
6+
import android.widget.Toast;
67

78
import com.threetree.pluginutil.TtreePlugin;
89
import com.threetree.pluginutil.permission.PermissionUtil;
@@ -40,9 +41,9 @@ public void onCutExit(int type)
4041
}
4142
});
4243

43-
TtreePlugin.setOnTrackListener(new TtreePlugin.IOnTrackListener() {
44+
TtreePlugin.setOnMethodListener(new TtreePlugin.IOnMethodListener() {
4445
@Override
45-
public void onTrackEnter(Object object, String className,String methodName, Object[] objects)
46+
public boolean onMethodEnter(Object object, String className, String methodName, Object[] objects)
4647
{
4748
if("onClick".equals(methodName))
4849
{
@@ -51,11 +52,15 @@ public void onTrackEnter(Object object, String className,String methodName, Obje
5152
View view = (View)objects[0];
5253
Log.e("onClickEnter","view=" + view.getId());
5354
}
55+
}else if("testInterceptForClass".equals(methodName))
56+
{
57+
return true;
5458
}
59+
return false;
5560
}
5661

5762
@Override
58-
public void onTrackExit(Object object, String className,String methodName, Object[] objects)
63+
public void onMethodExit(Object object, String className, String methodName, Object[] objects)
5964
{
6065
if("onClick".equals(methodName))
6166
{
@@ -67,5 +72,18 @@ public void onTrackExit(Object object, String className,String methodName, Objec
6772
}
6873
}
6974
});
75+
76+
TtreePlugin.setOnInterceptListener(new TtreePlugin.IOnInterceptListener() {
77+
@Override
78+
public Object onIntercept(Object object, String className, String methodName, String annotationName, Object[] objects, String jsonValue, String returnType)
79+
{
80+
if("testInterceptForClass".equals(methodName))
81+
{
82+
Toast.makeText(getApplicationContext(),"testInterceptForClass is intercepted",Toast.LENGTH_SHORT).show();
83+
return 0;
84+
}
85+
return TtreePlugin.getReturnType(returnType);
86+
}
87+
});
7088
}
7189
}

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.os.Bundle;
44
import android.support.v7.app.AppCompatActivity;
55
import android.view.View;
6+
import android.widget.Toast;
67

78
import com.threetree.pluginutil.annotation.Cut;
89
import com.threetree.pluginutil.annotation.Debounce;
@@ -13,18 +14,20 @@
1314

1415
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
1516

17+
private int mCount;
1618

1719
@Override
1820
protected void onCreate(Bundle savedInstanceState)
1921
{
2022
super.onCreate(savedInstanceState);
2123
setContentView(R.layout.activity_main);
22-
24+
testPermission();
2325
findViewById(R.id.text_tv).setOnClickListener(new View.OnClickListener() {
2426
@Override
2527
public void onClick(View v)
2628
{
27-
29+
testInterceptForClass();
30+
// testIntercept();
2831
}
2932
});
3033
}
@@ -65,7 +68,7 @@ public int testCut()
6568
@Permission({PermissionConsts.STORAGE, PermissionConsts.CAMERA})
6669
public void testPermission()
6770
{
68-
71+
int id = 2;
6972
}
7073

7174
//测试重写MethodVisitor
@@ -81,15 +84,18 @@ public void onClick(View v)
8184
int id = v.getId();
8285
}
8386

84-
@Debounce(time = 1000,onIntercept = true)
87+
@Debounce(time = 1000)
8588
public int testIntercept()
8689
{
87-
int id = 234;
88-
return id;
90+
mCount++;
91+
Toast.makeText(MainActivity.this,"test:"+mCount,Toast.LENGTH_SHORT).show();
92+
return 0;
8993
}
9094

91-
public void testInterceptForClass()
95+
public int testInterceptForClass()
9296
{
9397
int id = 234;
98+
Toast.makeText(MainActivity.this,"test:"+id,Toast.LENGTH_SHORT).show();
99+
return id;
94100
}
95101
}

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/AutoClassFilter.groovy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ class AutoClassFilter {
1010
String MethodName
1111
String MethodDes
1212
boolean Override
13-
boolean Intercept
1413
}

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

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public class AutoClassVisitor extends ClassVisitor {
7272
return isMatch
7373
}
7474

75-
//0-不匹配 1-类名匹配 2-接口名匹配 3-关键字匹配 4-外部配置匹配 5-拦截方法
75+
//0-不匹配 1-类名匹配 2-接口名匹配 3-关键字匹配 4-外部配置匹配
7676
//这个关键的方法,匹配的规则
7777
int matchingType(String name, String desc)
7878
{
@@ -87,8 +87,6 @@ public class AutoClassVisitor extends ClassVisitor {
8787
String appMethodDes = filter.getMethodDes()
8888
//是否会被外部的配置重写
8989
boolean override = filter.getOverride()
90-
//是否拦截方法
91-
boolean intercept = filter.getIntercept()
9290

9391
//方法匹配
9492
if(name == appMethodName && desc == appMethodDes)
@@ -102,10 +100,6 @@ public class AutoClassVisitor extends ClassVisitor {
102100
{
103101
return 4
104102
}
105-
if(intercept)
106-
{
107-
return 5;
108-
}
109103
Logger.info("||-----------------类名匹配${mClassName}--------------------------")
110104
return 1
111105
}
@@ -117,10 +111,6 @@ public class AutoClassVisitor extends ClassVisitor {
117111
{
118112
return 4
119113
}
120-
if(intercept)
121-
{
122-
return 5;
123-
}
124114
Logger.info("||-----------------接口名匹配${appInterfaceName}--------------------------")
125115
return 2
126116
}
@@ -132,10 +122,6 @@ public class AutoClassVisitor extends ClassVisitor {
132122
{
133123
return 4
134124
}
135-
if(intercept)
136-
{
137-
return 5;
138-
}
139125
Logger.info("||-----------------关键字匹配${appContainName}--------------------------")
140126
return 3
141127
}

0 commit comments

Comments
 (0)