Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Passing instance of Aspect into a pointcut

Thanks for the answer Alexander, 

I think what I am looking for should be achievable - at least judging from the code generated for the advice.

A reference to the instance of the Aspect is available in the call site of the isActive() method.

So that means to me that 
1. isActive() can be an instance method ( I admit there are use-cases where it must be static ) 
2. when if() accepts an argument with a type of the Aspect type ( e.g. isActive(FooAspect aspect)) - instance of the Aspect can be resolved. 


Thanks,
Alex


On Wed, Sep 7, 2022 at 8:44 AM Alexander Kriegisch <alexander@xxxxxxxxxxxxxx> wrote:

Hi Alex.

Your question is about Spring AOP rather than native AspectJ. Anyway, AFAIK you cannot inject a Spring bean into a static field, but of course you also cannot access an instance field from a static 'if()' method without some means to fetch a reference to the instance you wish to access. So if you think that an `if()` pointcut looks prettier than a condition at the beginning of an advice method, you are going to have to live with calling 'aspectOf()'.

Kind regards

--
Alexander Kriegisch
https://scrum-master.de
 

Alex Rojkov schrieb am 06.09.2022 05:11 (GMT +02:00):

I have an Aspect that defines @Around advice.
 
@Aspect 
class FooAspect {
  @spring.Autowired Env env; 
 
  @Around(some()) 
  public Object advice(String name) {
     return env.get(name);
  }
}
 
I'd like this Aspect to execute when env is not null and delegate to JoinPoint when it is null. 
 
@Around(some())
public Object advice(String name, ProceedingJointPoint pjp) {
   if (env != null) {
        return env.get(name);
    else 
        return pjp.proceed();
}
 
However, I'd like to avoid cluttering the advice and do something like this instead.
 
@Pointcut("if()") 
public static boolean isActive(FooAspect aspect) {
   return aspect.env != null;
}
 
 @Around(isActive() && some()) 
  public Object advice(String name, ProceedingJointPoint pjp) {
     return env.get(name);
  }
 
Question: is there a way to pass the instance of the advice to the isActive() pointcut? 
 
I'd like to avoid using Aspects.aspectOf() to get hold of the instance. 
 
Thanks,
Alex
 
 
 
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/aspectj-users

Back to the top