Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
504 views
in Technique[技术] by (71.8m points)

aop - 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. 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

The first example works because when using the execution() pointcut the code gets weaved "inside" your 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. 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()));
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...