Skip to content

Commit

Permalink
动态添加
Browse files Browse the repository at this point in the history
  • Loading branch information
xishuang committed Jan 9, 2018
1 parent 231d4b2 commit 284aaa4
Show file tree
Hide file tree
Showing 61 changed files with 436 additions and 166 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions annotation/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
29 changes: 29 additions & 0 deletions annotation/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"


defaultConfig {
minSdkVersion 10
targetSdkVersion 23
versionCode 1
versionName "1.0"

}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.0'

}
21 changes: 21 additions & 0 deletions annotation/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.xishuang.annotation;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();

assertEquals("com.xishuang.annotation.test", appContext.getPackageName());
}
}
6 changes: 6 additions & 0 deletions annotation/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xishuang.annotation"
xmlns:tools="http://schemas.android.com/tools"
>
<uses-sdk tools:overrideLibrary="com.xishuang.annotation"/>
</manifest>
11 changes: 11 additions & 0 deletions annotation/src/main/java/com/xishuang/annotation/AutoCount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.xishuang.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

/**
* Created by xishuang on 2018/1/9.
*/
@Target(ElementType.METHOD)
public @interface AutoCount {
}
3 changes: 3 additions & 0 deletions annotation/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">Annotation</string>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.xishuang.annotation;

import org.junit.Test;

import static org.junit.Assert.*;

/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}
59 changes: 41 additions & 18 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.MethodVisitor
import com.xixi.plugin.MethodLogAdapter
import com.xixi.plugin.tracking.AutoMethodVisitor
import org.objectweb.asm.Opcodes

apply plugin: 'com.android.application'
Expand Down Expand Up @@ -31,27 +31,50 @@ dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.0.0'
compile files('libs/MiPush_SDK_Client_3_2_2.jar')
compile project(':annotation')
}

xiaoqingwa{
name = "小傻逼"
//this is the most important part
modifyMatchMaps = [
'*MainActivity': [
['methodName': 'on**', 'methodDesc': null, 'adapter': {
ClassVisitor cv, int access, String name, String desc, String signature, String[] exceptions ->
MethodVisitor methodVisitor = cv.visitMethod(access, name, desc, signature, exceptions);
MethodVisitor adapter = new MethodLogAdapter(methodVisitor) {
@Override
void visitCode() {
super.visitCode();
methodVisitor.visitLdcInsn(desc);
methodVisitor.visitLdcInsn(name);
methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, "com/xishuang/plugintest/MainActivity", "hookXM", "(Ljava/lang/Object;Ljava/lang/Object;)V");
}
}
return adapter;
}]
]
matchData = [
'ClassFilter': [
['ClassName': 'MainActivity', 'InnerClassName': null, 'InterfaceName': 'OnClickListener', 'MethodName':'countTime']
],
'MethodVisitor':{
ClassVisitor cv, int access, String name, String desc, String signature, String[] exceptions ->
MethodVisitor methodVisitor = cv.visitMethod(access, name, desc, signature, exceptions)
MethodVisitor adapter = new AutoMethodVisitor(methodVisitor) {
@Override
protected void onMethodEnter() {
super.onMethodEnter()
methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, "com/xishuang/plugintest/MainActivity", "hookXM", "()V")
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;")
mv.visitLdcInsn("========start=========")
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false)
mv.visitLdcInsn(name);
mv.visitMethodInsn(INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J", false)
mv.visitMethodInsn(INVOKESTATIC, "com/xishuang/plugintest/TimeCache", "setStartTime", "(Ljava/lang/String;J)V", false)
}

@Override
protected void onMethodExit(int opcode) {
super.onMethodExit(opcode)
mv.visitLdcInsn(name)
mv.visitMethodInsn(INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J", false)
mv.visitMethodInsn(INVOKESTATIC, "com/xishuang/plugintest/TimeCache", "setEndTime", "(Ljava/lang/String;J)V", false)

mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;")
mv.visitLdcInsn(name)
mv.visitMethodInsn(INVOKESTATIC, "com/xishuang/plugintest/TimeCache", "getCostTime", "(Ljava/lang/String;)Ljava/lang/String;", false)
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false)

mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;")
mv.visitLdcInsn("========end=========")
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false)
}
}
return adapter
}
]
}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
Expand Down
21 changes: 19 additions & 2 deletions app/src/main/java/com/xishuang/plugintest/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package com.xishuang.plugintest;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.xishuang.annotation.AutoCount;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private static Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -18,9 +23,20 @@ protected void onCreate(Bundle savedInstanceState) {
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "文本", Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, "我是文本", Toast.LENGTH_SHORT).show();
}
});
context = this.getApplicationContext();

countTime();
}
@AutoCount
private void countTime() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

@Override
Expand All @@ -34,14 +50,15 @@ protected void onStart() {
}

public static void hookXM() {
Toast.makeText(context, "哈哈", Toast.LENGTH_SHORT).show();
Log.i("hookXM", "");
Log.i("hookXM", "");
}

@Override
public void onClick(View v) {
if (v.getId() == R.id.button) {
Toast.makeText(this, "按钮", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "我是按钮", Toast.LENGTH_SHORT).show();
}
}
}
28 changes: 28 additions & 0 deletions app/src/main/java/com/xishuang/plugintest/TimeCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.xishuang.plugintest;

import java.util.HashMap;
import java.util.Map;

/**
* Created by xishuang on 2018/1/9.
*/

public class TimeCache {
public static Map<String, Long> sStartTime = new HashMap<>();
public static Map<String, Long> sEndTime = new HashMap<>();

public static void setStartTime(String methodName, long time) {
sStartTime.put(methodName, time);
}

public static void setEndTime(String methodName, long time) {
sEndTime.put(methodName, time);
}

public static String getCostTime(String methodName) {
long start = sStartTime.get(methodName);
long end = sEndTime.get(methodName);
long dex = end - start;
return "method: " + methodName + " cost " + dex + " ns";
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed plugin/build/classes/main/com/xixi/plugin/Const.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed plugin/build/classes/main/com/xixi/plugin/Log.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed plugin/build/classes/main/com/xixi/plugin/Util.class
Binary file not shown.
24 changes: 0 additions & 24 deletions plugin/build/ivy.xml

This file was deleted.

Binary file removed plugin/build/libs/plugin-1.0.1-SNAPSHOT.jar
Binary file not shown.
16 changes: 0 additions & 16 deletions plugin/build/poms/pom-default.xml

This file was deleted.

This file was deleted.

2 changes: 0 additions & 2 deletions plugin/build/tmp/jar/MANIFEST.MF

This file was deleted.

Loading

0 comments on commit 284aaa4

Please sign in to comment.