Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Learning AspectJ: Beginner's questions (return statements in advice; reading pointcuts)

I would take a look at the developers notebook we wrote for AspectJ 5: https://eclipse.org/aspectj/doc/next/adk15notebook/

That has a good front section on join points/pointcuts.

As discussed there, there are three kind of pointcut designator: kinded, scoping and context:

• Kinded designators are those which select a particular kind of join point. For example: execution, get, set, call, handler
• Scoping designators are those which select a group of join points of interest (of probably many kinds). For example: within, withincode
• Contextual designators are those that match (and optionally bind) based on context. For example: this, target, @annotation

A well written pointcut should try and include at least the first two types (minded and scoping), whilst the contextual designators may be included if wishing to match based on join point context, or bind the context for use in the advice. Supplying either just a kinder or just a contextual designator will work but could affect weaving performance (time/memory used) due to all the extra processing and analysis. Scoping designators are very fast to match so it can be very useful to include one if you can.


pointcut services(Server s): target(s) && call(public * *(..))

call to public methods where the target of the call is a Server, could also be written:

pointcut services(): target(Server) && call(public * *(..))

but in this latter case I’m not binding the Server instance so I can’t use it in my advice.

At the point the call is made in the byte code, we can see (on the stack) the target instance for the method call, and so we can check if it is a Server.


The question basically boils down to: do you read/define pointcuts
outside-in/left to right and execute them inside-out/right to left?

Write them however you feel makes the most sense to you, AspectJ will rewrite them *anyway* into a more optimal form so that matching/weaving is as fast as possible. It doesn’t expect you to know the optimal order.

I would always write it this way round:

pointcut services(Server s): call(public * *(..)) && target(s);

But that says exactly the same thing. Don’t think that the ordering you specify in the pointcut components is relevant. At all the candidate join points we check everything in the pointcut.

If I were further adding scoping, I might do:

pointcut services(Server s): call(public * *(..)) && target(s) && within(SomePackage);

AspectJ is going to rewrite that at match time so the within check is done first (it is the fastest check).


Based on that, how do you build them up? (I have illustrated that bit
of the question on StackOverflow with examples, to illustrate where my
confusion comes from.)

As above, choose a kind:

pointcut mypointcut(): get(int *); // get of int fields

Add some context if you want:

pointcut mypointcut(MyType t): get(int *) && target(t);

Add some scoping:

pointcut mypointcut(MyType t): get(int *) && target(t) && within(com.foo.Bar);

Add more conditions if you like, you aren’t limited to one of each. They just have to *all* be satisfied at any joinpoint in order for it to match.

That return you picked up on looks like a copy-paste oversight, there is no need for the return because there is no code after the if clause that would get run if it didn’t return.

cheers,
Andy

On Mar 11, 2015, at 4:30 AM, Christian Balzer <christian.for.eclipse@xxxxxxxxx> wrote:

Hi all,

I'm trying to learn AspectJ at the moment, am going through the
documentation and have two questions, which I posted on StackOverflow
initially. I'd appreciate if someone could help me.

How do I read pointcuts? ===
http://stackoverflow.com/q/28969333/2018047

This is basically about the example from
https://eclipse.org/aspectj/doc/released/progguide/language-anatomy.html

pointcut services(Server s): target(s) && call(public * *(..))

"This pointcut, named services, picks out those points in the
execution of the program when Server objects have their public methods
called. It also allows anyone using the services pointcut to access
the Server object whose method is being called."

I'm a bit confused how to read (and build up) pointcuts: the parameter
list specifies a parameter s of type Server, yet the "source" for s
seems to be the primitive pointcut target(s) - which hasn't executed
yet, has it?

The question basically boils down to: do you read/define pointcuts
outside-in/left to right and execute them inside-out/right to left?

Based on that, how do you build them up? (I have illustrated that bit
of the question on StackOverflow with examples, to illustrate where my
confusion comes from.)


What does a return statement do in an AspectJ advice? ===
http://stackoverflow.com/q/28984838/2018047

I don't quite understand why the following line from a before advice
requires a return statement - or what it does to begin with:

System.out.println("Illegal value for x"); return;

Will it basically "return form setX()" (the "intercepted" method)
before the method body is executed?

The full example is here:
https://eclipse.org/aspectj/doc/released/progguide/language-interType.html#example-pointassertions

Kind regards,

Christian
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/aspectj-users


Back to the top