简体   繁体   English

Android中的AspectJ:切入点调用(* Activity.onCreate(..))不会选择Activity.onCreate()调用

[英]AspectJ in Android: pointcut call(* Activity.onCreate(..)) doesn't pick out Activity.onCreate() calls


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. 我在我的Android项目中使用AspectJ,我想编写一个pointcut来捕获对我的活动的onCreate()onDestroy()的所有调用。 I am quite new to AspectJ, so probably I am missing something here but why this: 我对AspectJ很新,所以可能我在这里遗漏了一些东西,但为什么:
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? 不起作用?

Nice to see other people adventuring into aspectJ and Android :-) 很高兴看到其他人冒险进入aspectJ和Android :-)

When using aspectJ with android you are limited to compile-time weaving, which basically means that you can only intercept code you own. 在android中使用aspectJ时,你只能使用编译时编织,这基本上意味着你只能拦截你拥有的代码。

The first example works because when using the execution() pointcut the code gets weaved "inside" your Activitiy.onCreate(). 第一个示例有效,因为当使用execution()切入点时,代码会在您的Activitiy.onCreate()内部编织。

The second example does not work, because the advice would have to get weaved into the methods that call your activity's onCreate. 第二个例子不起作用,因为建议必须编织到调用你的活动的onCreate的方法中。 That's probably something like the ActivityManager that you cannot modify. 这可能类似于您无法修改的ActivityManager。

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()));
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM