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

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

问题描述


我在我的Andr​​oid项目使用AspectJ和我想写一个切入点映入所有调用的onCreate ()的onDestroy()我的活动。我是很新的AspectJ的,所以可能我失去了一些东西,但为什么这样的:
切入点createActivity(活动一):目标(一)及和放大器;执行(* Activity.onCreate(..))及和放大器;内(com.test.activities。*);
作品和这样的:
目标(一)及和放大器;电话(* 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 :-)

在使用AspectJ的android您仅限于编译时织入,这基本上意味着你只能拦截code你自己。

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

第一个示例有效,因为使用的执行()切入点的code时,被编织的内部的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()));
    }

}

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

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