AspectJ writing 2 separate pointcuts one for onCre

2019-08-13 00:22发布

问题:

I am using AspectJ for Android and I am having a requirement of writing 2 pointcuts one which does pointcut execution for onCreate() and other for rest of the methods in the Android app that excludes onCreate().

Currently my pointcut for methods is also injecting onCreate() which I dont want to happen. It is like this -

pointcut methodCalls():
          execution(* com.hello..*(..)) && !within(com.retro.Tester);

Tester.aj is my Aspect file

回答1:

Assuming you have 2 pointcuts, one for onCreate() method and methodcalls(), you should rely on pointcuts boolean expression:

pointcut yourOnCreatePointcut() : execution(INSERT_YOUR_CONDITIONS);
pointcut methodCalls(): execution(* com.hello..*(..)) && !within(com.retro.Tester);
pointcut GIVE_IT_A_PROPER_NAME() : methodCalls() && ! yourOnCreatePointcut();

Then, add before() and/or after() only for the yourOnCreatePointcut() and GIVE_IT_A_PROPER_NAME() pointcuts, not for methodCalls().