Android AspectJ @Around方法参数不起作用



在我当前的Android应用程序中,我正在研究@AspectJ的使用

我试图将所有执行"捕获"到签名类似于以下方法的方法:

public void onMethodClicked(com.example.CustomType customType) {}

我有以下POINTCUTS

1(忽略我的方面类:

@Pointcut("!within(com.example.aspect)")
public void notAspect() { }

2(使用customType参数选择所有"单击"方法

@Pointcut("execution(* com.example..*.*Clicked(com.example.CustomType)) && args(custom)";)
public void customClicked(CustomType custom) { }

3( 我的@Around:-

@Around("notAspect() && customClicked()")
public Object selectedClicked(final ProceedingJoinPoint joinPoint, CustomType custom) throws Throwable {
Log.d(TAG, "Found a clicked method " + custom);
Object result = joinPoint.proceed();
return result;
}

当我构建我的Android应用程序时,我收到这些消息

no match for this type name: CustomType [Xlint:invalidAbsoluteTypeName]
bad parameter to pointcut reference
formal unbound in pointcut 
no match for this type name: com.example.aspect [Xlint:invalidAbsoluteTypeName]
the parameter custom is not bound in [all branches of] pointcut
use of ProceedingJoinPoint is allowed only on around advice (arg 1 in (before(extraFlags: 2): (((!within(com.example.aspect+) && execution(* com.example..*.*Clicked(com.example.CustomType)) && args(custom)) && persingleton(com.example.aspect.TraceAspect))->void com.example.aspect.TraceAspect.selectedClicked(org.aspectj.lang.JoinPoint, com.example.CustomType)))

我做错了什么?

更新

我已经通过更正!within()来修复其中一个错误/警告消息,如下所示:-

1(忽略我的方面类:

@Pointcut("!within(com.example.aspect.TraceAspect)")
public void notAspect() { }

我不确定您的问题,但您可以尝试像这样更改POINTCUT

@Pointcut("!within(com.example.aspect.TraceAspect)")
public void notAspect() { }
@Pointcut("execution(* com.example..*.*Clicked(com.example.CustomType)))
public void customClicked() { }

看,我已经删除了@Around注释内的args(custom)部分。是的,当然,我已经删除了customClicked函数的函数参数参数和语句末尾的分号。

现在通过从这里传递参数来像这样编写selectedClicked函数。

@Around("notAspect() && customClicked() && args(custom)")
public Object selectedClicked(final ProceedingJoinPoint joinPoint, CustomType custom) throws Throwable {
Log.d(TAG, "Found a clicked method " + custom);
Object result = joinPoint.proceed();
return result;
}

它应该不会失败。

最新更新