Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use two stars in point cut expression to match return type?

In book Spring in Action, I found following AspectJ point cut expression:

@Pointcut("execution(** concert.Performance.perform(..))")
void performance();

This will designate point cut performance to include methods whose name is "perform" and whose return type can be any. But notice it uses two stars (**) to match the return type, as I've experimented, one star (*) can perfectly do the job, this means the following line can do the same thing:

 @Pointcut("execution(* concert.Performance.perform(..))")
 void performance();

And I noticed that many AspectJ demos use two stars (**) to match "any return type", so is there some reason to do so? What is the problem of using one star to match "any return type"?

like image 452
wangshuaijie Avatar asked Apr 19 '16 11:04

wangshuaijie


People also ask

What is the difference between Joinpoint and pointcut?

JoinPoint: Joinpoint are points in your program execution where flow of execution got changed like Exception catching, Calling other method. PointCut: PointCut are basically those Joinpoints where you can put your advice(or call aspect). So basically PointCuts are the subset of JoinPoints.

Which of the following option will match the given pointcut?

Explanation: Union means the methods that either pointcut matches. Intersection means the methods that both pointcuts match. Union is usually more useful. Explanation: Using the static methods in the org.

What is pointcut in AspectJ?

AspectJ provides primitive pointcuts that capture join points at these times. These pointcuts use the dynamic types of their objects to pick out join points. They may also be used to expose the objects used for discrimination. this(Type or Id) target(Type or Id)

What methods does this pointcut expression reference @target?

@target – pointcut expression for matching to join points where the class of the executing object has an annotation of the given type. @args – pointcut expression for matching to join points where the runtime type of the actual arguments passed have annotations of the given type.

Which type of advice allows to access the return value of a Joinpoint?

After (finally) advice: Advice to be executed regardless of the means by which a join point exits (normal or exceptional return). Around advice: Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice.


1 Answers

Spring documentation helps understand this better. It says

Spring AOP users are likely to use the execution pointcut designator the most often. The format of an execution expression is:

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern) throws-pattern?)

The first part modifiers-pattern is optional as you can see it has suffixed with ?. This is to designate method's access type.

So in your case, pointcut expression execution(** concert.Performance.perform(..)) advises execution of methods with any access modifier and any return type on the type concert.Performance with method name perform that has any argument type.

execution(* concert.Performance.perform(..)) means the same where the first * is optional that denotes perform method on the type concert.Performance that accepts any type of arguments and method may have any return type(with optional access modifier meaning that access modifier can be anything).

like image 77
Bunti Avatar answered Sep 28 '22 12:09

Bunti