Android 中的 AspectJ:切入点调用(* Activity.onCreate(..)) 不挑出 Activity.onCreate() 调用 [英] AspectJ in Android: pointcut call(* Activity.onCreate(..)) doesn't pick out Activity.onCreate() calls

查看:33
本文介绍了Android 中的 AspectJ:切入点调用(* Activity.onCreate(..)) 不挑出 Activity.onCreate() 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在我的 Android 项目中使用 AspectJ,我想编写一个 pointcut 来捕获对 onCreate() 的所有调用>onDestroy() 我的活动.我对 AspectJ 很陌生,所以可能我在这里遗漏了一些东西,但为什么会这样:
pointcut createActivity(Activity a) : target(a) &&执行(* Activity.onCreate(..)) &&内(com.test.activities..*);
工作原理:
target(a) &&调用(* Activity.onCreate(..)) &&内(com.test.activities..*);
不起作用?


I am using AspectJ in my Android project and I'd like to write a pointcut that catches all the calls to onCreate() and onDestroy() of my activities. I am quite new to AspectJ, so probably I am missing something here but why this:
pointcut createActivity(Activity a) : target(a) && execution(* Activity.onCreate(..)) && within(com.test.activities..*);
works and this:
target(a) && call(* Activity.onCreate(..)) && within(com.test.activities..*);
doesn't work?

推荐答案

很高兴看到其他人尝试使用 aspectJ 和 Android :-)

Nice to see other people adventuring into aspectJ and Android :-)

当在android中使用aspectJ时,你仅限于编译时编织,这基本上意味着你只能拦截你拥有的代码.

When using aspectJ with android you are limited to compile-time weaving, which basically means that you can only intercept code you own.

第一个例子是有效的,因为当使用 execution() 切入点时,代码被编织在你的 Activitiy.onCreate() 的内部".

The first example works because when using the execution() pointcut the code gets weaved "inside" your Activitiy.onCreate().

第二个示例不起作用,因为建议必须被编入调用您的活动的 onCreate 的方法中.这可能类似于您无法修改的 ActivityManager.

The second example does not work, because the advice would have to get weaved into the methods that call your activity's onCreate. That's probably something like the ActivityManager that you cannot modify.

作为参考,以下是我在开发中使用的:

As a reference, here's what I use in development:

public aspect LogAspect {

    public String ATAG = "LogAspect";

    pointcut tolog1() : execution(* Activity+.*(..)) ;
    before() : tolog1() {
        String method = thisJoinPoint.getSignature().toShortString();

        Log.d(ATAG, "=========== entering " + method+", parms="+Arrays.toString(thisJoinPoint.getArgs()));
    }

}

这篇关于Android 中的 AspectJ:切入点调用(* Activity.onCreate(..)) 不挑出 Activity.onCreate() 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆