Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyJingFish committed Aug 7, 2024
2 parents 522a7aa + 387be55 commit c1128a4
Show file tree
Hide file tree
Showing 19 changed files with 500 additions and 232 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
plugins {
//必须项 👇 apply 设置为 true 自动为所有module“预”配置debugMode,false则按下边步骤五的方式二
id "io.github.FlyJingFish.AndroidAop.android-aop" version "2.0.4" apply true
id "io.github.FlyJingFish.AndroidAop.android-aop" version "2.0.5" apply true
}
```

Expand All @@ -83,7 +83,7 @@ plugins {
buildscript {
dependencies {
//必须项 👇
classpath 'io.github.FlyJingFish.AndroidAop:android-aop-plugin:2.0.4'
classpath 'io.github.FlyJingFish.AndroidAop:android-aop-plugin:2.0.5'
}
}
// 👇加上这句自动为所有module“预”配置debugMode,不加则按下边步骤五的方式二
Expand Down Expand Up @@ -122,7 +122,7 @@ apply plugin: 'android.aop' //最好放在最后一行
//必须项 👇
plugins {
...
id "io.github.FlyJingFish.AndroidAop.android-aop" version "2.0.4"//最好放在最后一行
id "io.github.FlyJingFish.AndroidAop.android-aop" version "2.0.5"//最好放在最后一行
}
```

Expand All @@ -148,17 +148,17 @@ plugins {
dependencies {
//必须项 👇
implementation 'io.github.FlyJingFish.AndroidAop:android-aop-core:2.0.4'
implementation 'io.github.FlyJingFish.AndroidAop:android-aop-annotation:2.0.4'
implementation 'io.github.FlyJingFish.AndroidAop:android-aop-core:2.0.5'
implementation 'io.github.FlyJingFish.AndroidAop:android-aop-annotation:2.0.5'
//必须项 👇如果您项目内已经有了这项不用加也可以
implementation 'androidx.appcompat:appcompat:1.3.0' // 至少在1.3.0及以上
//非必须项 👇,如果你想自定义切面需要用到,⚠️支持Java和Kotlin代码写的切面
ksp 'io.github.FlyJingFish.AndroidAop:android-aop-ksp:2.0.4'
ksp 'io.github.FlyJingFish.AndroidAop:android-aop-ksp:2.0.5'
//非必须项 👇,如果你想自定义切面需要用到,⚠️只适用于Java代码写的切面
annotationProcessor 'io.github.FlyJingFish.AndroidAop:android-aop-processor:2.0.4'
annotationProcessor 'io.github.FlyJingFish.AndroidAop:android-aop-processor:2.0.5'
//⚠️上边的 android-aop-ksp 和 android-aop-processor 二选一
}
```
Expand Down
14 changes: 7 additions & 7 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Using the **plugins DSL**:
plugins {
//Required item 👇 apply is set to true to automatically apply debugMode to all modules, if false, follow step 5 below.
id "io.github.FlyJingFish.AndroidAop.android-aop" version "2.0.4" apply true
id "io.github.FlyJingFish.AndroidAop.android-aop" version "2.0.5" apply true
}
```

Expand All @@ -80,7 +80,7 @@ plugins {
buildscript {
dependencies {
//Required items 👇
classpath 'io.github.FlyJingFish.AndroidAop:android-aop-plugin:2.0.4'
classpath 'io.github.FlyJingFish.AndroidAop:android-aop-plugin:2.0.5'
}
}
//👇Add this sentence to automatically apply debugMode to all modules. If not, follow step 5 below.
Expand Down Expand Up @@ -119,7 +119,7 @@ Add directly to ```build.gradle``` of **app**
//Required items 👇
plugins {
...
id "io.github.FlyJingFish.AndroidAop.android-aop" version "2.0.4"
id "io.github.FlyJingFish.AndroidAop.android-aop" version "2.0.5"
}
```

Expand Down Expand Up @@ -149,16 +149,16 @@ plugins {
dependencies {
//Required items 👇
implementation 'io.github.FlyJingFish.AndroidAop:android-aop-core:2.0.4'
implementation 'io.github.FlyJingFish.AndroidAop:android-aop-annotation:2.0.4'
implementation 'io.github.FlyJingFish.AndroidAop:android-aop-core:2.0.5'
implementation 'io.github.FlyJingFish.AndroidAop:android-aop-annotation:2.0.5'
//Required item 👇If you already have this item in your project, you don’t need to add it.
implementation 'androidx.appcompat:appcompat:1.3.0' // At least in 1.3.0 and above
//Optional 👇, if you want to customize aspects, you need to use them, ⚠️supports aspects written in Java and Kotlin code
ksp 'io.github.FlyJingFish.AndroidAop:android-aop-ksp:2.0.4'
ksp 'io.github.FlyJingFish.AndroidAop:android-aop-ksp:2.0.5'
//Optional 👇, if you want to customize aspects, you need to use them, ⚠️only applies to aspects written in Java code
annotationProcessor 'io.github.FlyJingFish.AndroidAop:android-aop-processor:2.0.4'
annotationProcessor 'io.github.FlyJingFish.AndroidAop:android-aop-processor:2.0.5'
//⚠️Choose one of the above android-aop-ksp and android-aop-processor
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.flyjingfish.android_aop_annotation.base.MatchClassMethodSuspend;
import com.flyjingfish.android_aop_annotation.base.OnBaseSuspendReturnListener;
import com.flyjingfish.android_aop_annotation.impl.AopMethodImpl;
import com.flyjingfish.android_aop_annotation.impl.JoinPoint;
import com.flyjingfish.android_aop_annotation.impl.ProceedReturnImpl;
import com.flyjingfish.android_aop_annotation.utils.AndroidAopBeanUtils;
import com.flyjingfish.android_aop_annotation.utils.InvokeMethod;
Expand Down Expand Up @@ -119,9 +120,9 @@ public Object joinPointExecute(Continuation continuation) {
ProceedJoinPoint proceedJoinPoint;
AopMethod aopMethod = new AopMethodImpl(originalMethod,isSuspend,continuation,mParamNames,mArgClasses,mReturnClass);
if (isSuspend){
proceedJoinPoint = new ProceedJoinPointSuspend(targetClass, mArgs,target,true,targetMethod,invokeMethod,aopMethod);
proceedJoinPoint = JoinPoint.INSTANCE.getJoinPointSuspend(targetClass, mArgs,target,true,targetMethod,invokeMethod,aopMethod);
}else {
proceedJoinPoint = new ProceedJoinPoint(targetClass, mArgs,target,false,targetMethod,invokeMethod,aopMethod);
proceedJoinPoint = JoinPoint.INSTANCE.getJoinPoint(targetClass, mArgs,target,false,targetMethod,invokeMethod,aopMethod);
}

Annotation[] annotations = originalMethod.getAnnotations();
Expand Down Expand Up @@ -153,11 +154,11 @@ public Object joinPointExecute(Continuation continuation) {


if (basePointCuts.size() > 1) {
proceedJoinPoint.setOnInvokeListener(() -> {
JoinPoint.INSTANCE.setOnInvokeListener(proceedJoinPoint,() -> {
if (iterator.hasNext()) {
PointCutAnnotation nextCutAnnotation = iterator.next();
iterator.remove();
proceedJoinPoint.setHasNext(iterator.hasNext());
JoinPoint.INSTANCE.setHasNext(proceedJoinPoint,iterator.hasNext());
Object value;
if (nextCutAnnotation.basePointCut != null) {
if (isSuspend){
Expand Down Expand Up @@ -208,7 +209,7 @@ public Object joinPointExecute(Continuation continuation) {
});
}

proceedJoinPoint.setHasNext(basePointCuts.size() > 1);
JoinPoint.INSTANCE.setHasNext(proceedJoinPoint,basePointCuts.size() > 1);
PointCutAnnotation cutAnnotation = iterator.next();
iterator.remove();
if (cutAnnotation.basePointCut != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ interface AopMethod {
* @return 返回此方法上指定类型的注解
* @param <T> 具体泛型信息
</T> */
fun <T : Annotation> getAnnotation(annotationClass: Class<T>): T
fun <T : Annotation> getAnnotation(annotationClass: Class<T>): T?

/**
* @return 返回方法的参数信息的数组
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,26 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.flyjingfish.android_aop_annotation.base.OnBaseSuspendReturnListener;
import com.flyjingfish.android_aop_annotation.utils.AndroidAopBeanUtils;
import com.flyjingfish.android_aop_annotation.utils.InvokeMethod;
import com.flyjingfish.android_aop_annotation.utils.Utils;

import java.lang.reflect.Method;

/**
* 切点相关信息类,<a href = "https://github.com/FlyJingFish/AndroidAOP/wiki/ProceedJoinPoint">wiki 文档使用说明</a>
*/
public class ProceedJoinPoint {
public interface ProceedJoinPoint {
/**
*
* <a href = "https://github.com/FlyJingFish/AndroidAOP/wiki/ProceedJoinPoint#args">wiki 文档使用说明</a>
*
* @return 调用切点方法时传入的参数数组
*/
@Nullable
public final Object[] args;
@Nullable
private final Object[] originalArgs;
@Nullable
public final Object target;
@NonNull
public final Class<?> targetClass;
private final Method targetMethod;
private final InvokeMethod targetInvokeMethod;
private final AopMethod targetAopMethod;
private final int argCount;
private final boolean isSuspend;
private final Object suspendContinuation;
private OnInvokeListener onInvokeListener;
private boolean hasNext;

ProceedJoinPoint(@NonNull Class<?> targetClass, Object[] args, @Nullable Object target, boolean isSuspend,
Method targetMethod,InvokeMethod invokeMethod,AopMethod aopMethod) {
this.targetClass = targetClass;
this.target = target;
this.isSuspend = isSuspend;
this.targetMethod = targetMethod;
this.targetInvokeMethod = invokeMethod;
this.targetAopMethod = aopMethod;

Object[] fakeArgs;
if (isSuspend && args != null){
fakeArgs = new Object[args.length - 1];
System.arraycopy(args, 0, fakeArgs, 0, args.length - 1);
suspendContinuation = args[args.length - 1];
}else {
fakeArgs = args;
suspendContinuation = null;
}
this.args = fakeArgs;

if (fakeArgs != null) {
this.originalArgs = fakeArgs.clone();
} else {
this.originalArgs = null;
}
this.argCount = fakeArgs != null ? fakeArgs.length : 0;
}
Object[] getArgs();

/**
* 调用切点方法内代码
*
* @return 返回切点方法返回值 <a href = "https://github.com/FlyJingFish/AndroidAOP/wiki/ProceedJoinPoint#proceed">wiki 文档使用说明</a>
*/
@Nullable
public Object proceed() {
return proceed(args);
}
Object proceed();

/**
* 调用切点方法内代码
Expand All @@ -79,114 +31,33 @@ public Object proceed() {
* @return 返回切点方法返回值 <a href = "https://github.com/FlyJingFish/AndroidAOP/wiki/ProceedJoinPoint#proceed">wiki 文档使用说明</a>
*/
@Nullable
public Object proceed(Object... args) {
return realProceed(null,args);
}
Object proceed(Object... args);

/**
* @return 切点方法相关信息
* @return 切点方法相关信息,例如方法参数类型、返回类型、参数的注解等等
*/
@NonNull
public AopMethod getTargetMethod() {
return targetAopMethod;
}
AopMethod getTargetMethod();

/**
* @return 切点方法所在对象,如果方法为静态的,此值为null
*/
@Nullable
public Object getTarget() {
return target;
}
Object getTarget();

/**
* @return 切点方法所在类 Class
*/
@NonNull
public Class<?> getTargetClass() {
return targetClass;
}
Class<?> getTargetClass();

/**
* 和 {@link ProceedJoinPoint#args} 相比,返回的引用地址不同,但数组里边的对象一致
* 和 {@link ProceedJoinPoint#getArgs()} 相比,返回的引用地址不同,但数组里边的对象一致。多用于多个切面逻辑时在某一个切面中获得最初的传入参数
*
* @return 最开始进入方法时的参数 <a href = "https://github.com/FlyJingFish/AndroidAOP/wiki/ProceedJoinPoint#args">wiki 文档使用说明</a>
*/
@Nullable
public Object[] getOriginalArgs() {
return originalArgs;
}


@Nullable
Object realProceed(OnBaseSuspendReturnListener onSuspendReturnListener, Object... args) {
if (argCount > 0) {
if (args == null || args.length != argCount) {
throw new IllegalArgumentException("proceed 所参数个数不对");
}
}

Object[] realArgs;
if (isSuspend) {
realArgs = new Object[argCount + 1];
if (args != null){
System.arraycopy(args, 0, realArgs, 0, args.length);
}
realArgs[argCount] = suspendContinuation;
} else {
realArgs = args;
}

if (realArgs != null && this.args != null){
System.arraycopy(realArgs, 0, this.args, 0, this.args.length);
}

try {
Object returnValue = null;
setReturnListener(onSuspendReturnListener);
if (!hasNext) {
if (targetInvokeMethod != null) {
returnValue = targetInvokeMethod.invoke(target, realArgs);
} else {
returnValue = targetMethod.invoke(target, realArgs);
}
} else if (onInvokeListener != null) {
returnValue = onInvokeListener.onInvoke();
}

return returnValue;
} catch (Throwable e) {
throw Utils.INSTANCE.getRealRuntimeException(e);
}
}

private void setReturnListener(OnBaseSuspendReturnListener onSuspendReturnListener){
if (isSuspend && onSuspendReturnListener != null && suspendContinuation != null){
Object key1 = suspendContinuation;
AndroidAopBeanUtils.INSTANCE.addSuspendReturnListener(key1,onSuspendReturnListener);
try {
Method method = suspendContinuation.getClass().getMethod("getCompletion");
method.setAccessible(true);
Object key2 = method.invoke(suspendContinuation);
if (key2 != null){
AndroidAopBeanUtils.INSTANCE.addSuspendReturnListener(key2,onSuspendReturnListener);
AndroidAopBeanUtils.INSTANCE.saveReturnKey(key1,key2);
}
} catch (Throwable ignored) {
}

}
}

interface OnInvokeListener {
Object onInvoke();
}
Object[] getOriginalArgs();

void setOnInvokeListener(OnInvokeListener onInvokeListener) {
this.onInvokeListener = onInvokeListener;
}

void setHasNext(boolean hasNext) {
this.hasNext = hasNext;
}
}
Loading

0 comments on commit c1128a4

Please sign in to comment.