首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android AspectJ @ with method args不起作用

Android AspectJ @ with method args不起作用
EN

Stack Overflow用户
提问于 2017-08-09 17:33:04
回答 1查看 2.1K关注 0票数 1

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

我试图“捕获”所有对其签名类似的方法的执行:

代码语言:javascript
复制
public void onMethodClicked(com.example.CustomType customType) {}

我有以下POINTCUTS

1)忽略我的方面类:

代码语言:javascript
复制
@Pointcut("!within(com.example.aspect)")
public void notAspect() { }

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

代码语言:javascript
复制
@Pointcut("execution(* com.example..*.*Clicked(com.example.CustomType)) && args(custom)";)
public void customClicked(CustomType custom) { }

3)我的@Around:-

代码语言:javascript
复制
@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应用程序时,我收到以下消息

代码语言:javascript
复制
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)忽略我的方面类:

代码语言:javascript
复制
@Pointcut("!within(com.example.aspect.TraceAspect)")
public void notAspect() { }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-09 21:07:37

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

代码语言:javascript
复制
@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函数。

代码语言:javascript
复制
@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;
}

它应该可以正常工作,不会失败。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45586836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档