Skip to content

Commit

Permalink
release 1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
tangxiaolv committed Jun 1, 2017
1 parent 216d5b1 commit e371289
Show file tree
Hide file tree
Showing 34 changed files with 231 additions and 163 deletions.
60 changes: 41 additions & 19 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

|Lib|surgeon-plugin|surgeon-compile|
|:---:|:---|:---|
|最新版本|1.0.1|1.0.0|
|最新版本|1.0.2|1.0.1|

Surgeon是Android上一个简单,灵活,高性能的方法热替换框架。

Expand All @@ -18,7 +18,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.tangxiaolv.surgeon:surgeon-plugin:1.0.1'//version参照上表
classpath 'com.tangxiaolv.surgeon:surgeon-plugin:1.0.2'//version参照上表
}
}
Expand All @@ -31,7 +31,7 @@ apply plugin: 'com.tangxiaolv.surgeon'
//添加注解解析器
dependencies {
annotationProcessor 'com.tangxiaolv.surgeon:surgeon-compile:1.0.0'//version参照上表
annotationProcessor 'com.tangxiaolv.surgeon:surgeon-compile:1.0.1'//version参照上表
}
```

Expand All @@ -41,7 +41,10 @@ dependencies {
```java
package com.tangxiaolv.sdk;
public class SDKActivity extends AppCompatActivity {
@ReplaceAble
//namespace 命名空间,通常为packageName + className,也可以自定义
//function 方法名称,通常为当前的方法名字,也可自定义
//namespace + function必须唯一,否则无法正确被替换
@ReplaceAble(namespace = "com.tangxiaolv.sdk.SDKActivity", function = "getTwo")
private String getTwo() {
return "TWO";
}
Expand All @@ -52,12 +55,15 @@ public class SDKActivity extends AppCompatActivity {
```java
//创建新类实现ISurgeon
public class HotReplace implements ISurgeon {
/**
* ref为目标方法的packageName+className+methodName
* @param target 默认传递:目标方法所在对象,如果目标方法为静态,target为null
*/
@Replace(ref = "com.tangxiaolv.sdk.SDKActivity.getTwo")
public String getTwo(Object target) {

//当前类是否为单例
@Override
public boolean singleton() {
return false;
}

@Replace(namespace = "com.tangxiaolv.sdk.SDKActivity", function = "getTwo")
public String getTwo(TargetHandle handle) {
return "getTwo from HotReplace2";
}
}
Expand All @@ -69,17 +75,22 @@ public class HotReplace implements ISurgeon {
```java
package com.tangxiaolv.sdk;
public class SDKActivity extends AppCompatActivity {
@ReplaceAble

@Override
public boolean singleton() {
return false;
}

@ReplaceAble(namespace = "com.tangxiaolv.sdk.SDKActivity", function = "getTwo")
private String getTwo() {
return "TWO";
}

@ReplaceAble(extra = "text")
@ReplaceAble(namespace = "com.tangxiaolv.sdk.SDKActivity", function = "getTwo.text")
private String getTwo(String text) {
return text;
}

@ReplaceAble(extra = "text")
@ReplaceAble(namespace = "com.tangxiaolv.sdk.SDKActivity", function = "getThree")
private String getThree(String text) {
return "getThree_"+text;
}
Expand All @@ -89,29 +100,40 @@ public class SDKActivity extends AppCompatActivity {
**一:静态替换**
```java
public class HotReplace implements ISurgeon {

@Override
public boolean singleton() {
return false;
}

//调用目标方法前调用
@ReplaceBefore(ref = "com.tangxiaolv.sdk.SDKActivity.getTwo")
//target 目标方法所在对象
@ReplaceBefore(namespace = "com.tangxiaolv.sdk.SDKActivity", function = "getTwo")
public void getTwoBefore(Object target) {
}

//替换目标方法
@Replace(ref = "com.tangxiaolv.sdk.SDKActivity.getTwo")
//handle 目标方法的处理类,可通过它调用目标方法和获取目标方法所在对象
@Replace(namespace = "com.tangxiaolv.sdk.SDKActivity", function = "getTwo")
public String getTwo(TargetHandle handle) {
return "getTwo from remote";
}

//目标重载方法替换
@Replace(ref = "com.tangxiaolv.sdk.SDKActivity.getTwo",extra = "text")
//handle 目标方法的处理类,可通过它调用目标方法和获取目标方法所在对象
@Replace(namespace = "com.tangxiaolv.sdk.SDKActivity", function = "getTwo.text")
public String getTwo(TargetHandle handle,String text/**目标方法参数*/) {
return "getTwo from remote";
}

//目标方法调用之后调用
@ReplaceAfter(ref = "com.tangxiaolv.sdk.SDKActivity.getTwo")
//target 目标方法所在对象
@ReplaceAfter(namespace = "com.tangxiaolv.sdk.SDKActivity", function = "getTwo")
public void getTwoAfter(Object target) {
}

@Replace(ref = "com.tangxiaolv.sdk.SDKActivity.getThree", extra = "text")
//替换目标方法
@Replace(namespace = "com.tangxiaolv.sdk.SDKActivity", function = "getThree")
public String getThree(TargetHandle handle, String text) throws Throwable {
String newText = text + "_hack!";
//使用新参数调用原始方法
Expand Down
59 changes: 39 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ English | [中文](https://github.com/TangXiaoLv/Surgeon/blob/master/README-CN.m

|Lib|surgeon-plugin|surgeon-compile|
|:---:|:---|:---|
|latest|1.0.1|1.0.0|
|latest|1.0.2|1.0.1|

Surgeon is a hot function replace framework for Android which was simple to use,flexible,high-performance.

Expand All @@ -18,7 +18,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.tangxiaolv.surgeon:surgeon-plugin:1.0.1'
classpath 'com.tangxiaolv.surgeon:surgeon-plugin:1.0.2'
}
}
Expand All @@ -33,7 +33,7 @@ apply plugin: 'com.tangxiaolv.surgeon'
//add annotationProcessor
dependencies {
annotationProcessor 'com.tangxiaolv.surgeon:surgeon-compile:1.0.0'
annotationProcessor 'com.tangxiaolv.surgeon:surgeon-compile:1.0.1'
}
```

Expand All @@ -43,7 +43,10 @@ Getting Started
```java
package com.tangxiaolv.sdk;
public class SDKActivity extends AppCompatActivity {
@ReplaceAble
//namespace usually is packageName + className,Also can define any string if you want.
//function function name,usually is current function name,Also can define any string if you want.
//namespace + function must unique
@ReplaceAble(namespace = "com.tangxiaolv.sdk.SDKActivity", function = "getTwo")
private String getTwo() {
return "TWO";
}
Expand All @@ -54,12 +57,14 @@ public class SDKActivity extends AppCompatActivity {
```java
//Create ISurgeon subclass.
public class HotReplace implements ISurgeon {
/**
* ref = target(packageName + className + methodName)
* @param target Defualt passed,The target function owner object,If target function is static then it equal null.
*/
@Replace(ref = "com.tangxiaolv.sdk.SDKActivity.getTwo")
public String getTwo(Object target) {

@Override
public boolean singleton() {
return false;
}

@Replace(namespace = "com.tangxiaolv.sdk.SDKActivity", function = "getTwo")
public String getTwo(TargetHandle handle) {
return "getTwo from HotReplace2";
}
}
Expand All @@ -71,17 +76,21 @@ Advance
```java
package com.tangxiaolv.sdk;
public class SDKActivity extends AppCompatActivity {
@ReplaceAble
@Override
public boolean singleton() {
return false;
}

@ReplaceAble(namespace = "com.tangxiaolv.sdk.SDKActivity", function = "getTwo")
private String getTwo() {
return "TWO";
}

@ReplaceAble(extra = "text")
@ReplaceAble(namespace = "com.tangxiaolv.sdk.SDKActivity", function = "getTwo.text")
private String getTwo(String text) {
return text;
}

@ReplaceAble(extra = "text")
@ReplaceAble(namespace = "com.tangxiaolv.sdk.SDKActivity", function = "getThree")
private String getThree(String text) {
return "getThree_"+text;
}
Expand All @@ -91,29 +100,39 @@ public class SDKActivity extends AppCompatActivity {
**1.Static Replace**
```java
public class HotReplace implements ISurgeon {
@Override
public boolean singleton() {
return false;
}

//called before target function call
@ReplaceBefore(ref = "com.tangxiaolv.sdk.SDKActivity.getTwo")
//target : target function owner object
@ReplaceBefore(namespace = "com.tangxiaolv.sdk.SDKActivity", function = "getTwo")
public void getTwoBefore(Object target) {
}

//replace target function
@Replace(ref = "com.tangxiaolv.sdk.SDKActivity.getTwo")
//handle :You can invoke target function or get target function owner by handler.
@Replace(namespace = "com.tangxiaolv.sdk.SDKActivity", function = "getTwo")
public String getTwo(TargetHandle handle) {
return "getTwo from remote";
}

//replace target override function
@Replace(ref = "com.tangxiaolv.sdk.SDKActivity.getTwo",extra = "text")
public String getTwo(TargetHandle handle,String text/**origin params*/) {
//handle :You can invoke target function or get target function owner by handler.
@Replace(namespace = "com.tangxiaolv.sdk.SDKActivity", function = "getTwo.text")
public String getTwo(TargetHandle handle,String text/**目标方法参数*/) {
return "getTwo from remote";
}

//called after target function call
@ReplaceAfter(ref = "com.tangxiaolv.sdk.SDKActivity.getTwo")
//target : target function owner object
@ReplaceAfter(namespace = "com.tangxiaolv.sdk.SDKActivity", function = "getTwo")
public void getTwoAfter(Object target) {
}

@Replace(ref = "com.tangxiaolv.sdk.SDKActivity.getThree", extra = "text")
//replace target function
@Replace(namespace = "com.tangxiaolv.sdk.SDKActivity", function = "getThree")
public String getThree(TargetHandle handle, String text) throws Throwable {
String newText = text + "_hack!";
//invoke origin method with new params
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'com.tangxiaolv.surgeon:surgeon-plugin:1.0.1'
classpath 'com.tangxiaolv.surgeon:surgeon-plugin:1.0.2'

//jcenter push plugin
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4'
Expand Down
1 change: 1 addition & 0 deletions example/sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ dependencies {
})
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
//compile project(':surgeon-core')
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import android.content.Context;
import android.graphics.Canvas;
import android.text.Layout;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.surgeon.weaving.annotations.ReplaceAble;

public class MyLinearLayout extends LinearLayout {
public MyLinearLayout(Context context) {
super(context);
Expand All @@ -20,6 +20,7 @@ public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@ReplaceAble(namespace = "com.tangxiaolv.sdk.MyLinearLayout", function = "onMeasure")
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Expand Down
10 changes: 5 additions & 5 deletions example/sdk/src/main/java/com/tangxiaolv/sdk/SDKActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void after(Object[] params) {
});
}

@ReplaceAble
@ReplaceAble(namespace = "com.tangxiaolv.sdk.SDKActivity", function = "oneClick")
public void oneClick(View view) {
content.setText(getOne());
}
Expand All @@ -61,22 +61,22 @@ public void threeClick(View view) {
content.setText(getThree("Text"));
}

@ReplaceAble
@ReplaceAble(namespace = "com.tangxiaolv.sdk.SDKActivity", function = "getOne")
private String getOne() {
return "ONE";
}

@ReplaceAble
@ReplaceAble(namespace = "com.tangxiaolv.sdk.SDKActivity", function = "getTwo")
private final static String getTwo() {
return "TWO";
}

@ReplaceAble(extra = "text")
@ReplaceAble(namespace = "com.tangxiaolv.sdk.SDKActivity", function = "getTwo.text")
private String getTwo(String text) {
return text;
}

@ReplaceAble(extra = "text")
@ReplaceAble(namespace = "com.tangxiaolv.sdk.SDKActivity", function = "getThree.text")
private String getThree(String text) {
return "getThree_" + text;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class StringUtils {

@ReplaceAble
@ReplaceAble(namespace = "com.tangxiaolv.sdk.StringUtils", function = "getThree")
public static String getThree() {
return "THREE";
}
Expand Down
3 changes: 2 additions & 1 deletion example/simple/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies {
testCompile 'junit:junit:4.12'

compile project(':example:sdk')
annotationProcessor 'com.tangxiaolv.surgeon:surgeon-compile:1.0.1'
//annotationProcessor project(':surgeon-compile')
//compile project(':surgeon-core')
annotationProcessor project(':surgeon-compile')
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,22 @@ public class HotReplace implements ISurgeon {
* @param handle {@link TargetHandle}
* @param view origin params
*/
@Replace(ref = "com.tangxiaolv.sdk.SDKActivity.oneClick")
@Replace(namespace = "com.tangxiaolv.sdk.SDKActivity", function = "oneClick")
public void showToast(TargetHandle handle, View view) {
Toast.makeText(view.getContext(), "Hack!!!", Toast.LENGTH_SHORT).show();
}

@Replace(namespace = "com.tangxiaolv.sdk.MyLinearLayout", function = "onMeasure")
public void onMeasure(TargetHandle handle, int widthMeasureSpec, int heightMeasureSpec) throws Throwable {
int w_s = View.MeasureSpec.getSize(widthMeasureSpec);
int w_m = View.MeasureSpec.getMode(widthMeasureSpec);
int h_s = View.MeasureSpec.getSize(heightMeasureSpec);
int h_m = View.MeasureSpec.getMode(heightMeasureSpec);
handle.proceed(View.MeasureSpec.makeMeasureSpec(w_s / 2, w_m), heightMeasureSpec);
}

@Override
public boolean singleton() {
return true;
}
}
Loading

0 comments on commit e371289

Please sign in to comment.