Pointcut method return value
Ordinary function¶
For @AndroidAopPointCut and @AndroidAopMatchClassMethod, both aspects have their aspect callback processing classes respectively
- @AndroidAopPointCut corresponds to BasePointCut
- @AndroidAopMatchClassMethod corresponds to MatchClassMethod
Note
You can see that both invoke methods have a return value, which will replace the return value of the entry point method and will be automatically converted to the return type of the original method, but the following two types have no return value
What is the return value¶
- If the section method has a return value, the return value of invoke is the section method return value
- In addition, if the section method has a return value, the return value type of invoke must be consistent with the section method return type
- If the section method does not have a return value, it doesn’t matter what is returned
public class MyAnnoCut implements BasePointCut<MyAnno> {
@Nullable
@Override
public Object invoke(@NonNull ProceedJoinPoint joinPoint, @NonNull MyAnno anno) {
int value1 = (int) joinPoint.args[0];
int value2 = (int) joinPoint.args[1];
int result = value1 * value2;
return result;
}
}
suspend function¶
- BasePointCutSuspend inherits from BasePointCut
- MatchClassMethodSuspend inherits from MatchClassMethod
Note
For the suspend function of the cut point function, it is better to use the above two types. If you continue to use BasePointCut
and MatchClassMethod
, its return value must be joinPoint.proceed()
The return value of the onReturn function. If you need to modify the return value, please see the following code:
class MyAnnoCut5 : BasePointCutSuspend<MyAnno5> {
override suspend fun invokeSuspend(joinPoint: ProceedJoinPointSuspend, anno: MyAnno5) {
withContext(Dispatchers.IO) {
//Modify the return value by setting OnSuspendReturnListener The return value of onReturn is the return value of the suspend point function
joinPoint.proceed(object : OnSuspendReturnListener {
override fun onReturn(proceedReturn: ProceedReturn): Any? {
return (proceedReturn.proceed() as Int) + 100
}
})
}
}
}
Note
Here onReturn The explanation is the same as What is the return value here