【问题标题】:Android AspectJ @Around with method args not workingAndroid AspectJ @Around 方法参数不起作用
【发布时间】:2017-08-09 09:33:03
【问题描述】:

在我当前的 Android 应用程序中,我正在调查 @AspectJ 的使用情况

我正在尝试将所有执行“捕获”到签名类似的方法:-

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

我有以下POINTCUTS

1) 忽略我的 Aspect 类:

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

2) 选择所有带有 customType 参数的“Clicked”方法

@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) 忽略我的 Aspect 类:

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

【问题讨论】:

    标签: android aop aspectj


    【解决方案1】:

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

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

    看,我已经删除了 args(custom) 部分,它位于 @Around 注释中。是的,当然我已经删除了 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;
    }
    

    它应该可以正常工作。

    【讨论】:

    • 干得好!这解决了我的问题,尽管我不得不删除“pointcut =”fronte Around 建议
    • 很高兴知道这有帮助! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 2019-08-26
    • 2014-08-05
    相关资源
    最近更新 更多