Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Multiple types in pointcut

Thank you Andy.

On Apr 25, 2016 1:24 PM, "Andy Clement" <andrew.clement@xxxxxxxxx> wrote:
If the types have a common super type, you could use that (or you could introduce one as a marker interface via declare parents perhaps).

Worst case, if you don’t want to duplicate the code, you can use ‘Object’ as the type and then do the necessary analysis in your advice to work out precisely what you need to do (i.e. which exact subclass it is).

pointcut contextAccessed(Object thiz): get(private com.example.api.IContext *.ctx) && this(this);

before(Object thiz): contextAccessed(thiz) {
    if (a instanceof Action) {
      ((Action)thiz).field = new Bar();
    } else if (thiz instanceof _expression_) {
      ((_expression_)thiz).field = new Bar();
    }
}

Andy

> On Apr 24, 2016, at 7:06 PM, Mansour Al Akeel <mansour.alakeel@xxxxxxxxx> wrote:
>
> I have a aspect to inject a value for a field:
>
>    pointcut contextAccessed(Action action ):
>        get( private com.example.api.IContext *.ctx ) && this(action) ;
>
>    before(Action action ): contextAccessed(action) {
> ...
>
> This is working fine so  far, and doing what I am expecting. However,
> I need to inject IContext in more that one class. For example, I need
> to define the pointcut to handle multiple classes. Currently, it's
> processing "Action" class, but I want to add "_expression_" class. I can
> duplicate the code (copy/paste).
>
> For example I am looking for something like (this wont work):
>
>    pointcut contextAccessed(Action action   || _expression_<?> _expression_):
>        get( private com.example.api.IContext *.ctx ) && this(action) ;
>
>    before(Action action   || _expression_<?> _expression_ ):
> contextAccessed(action,_expression_) {
> ....
>
> Is there a way to handle this case?
> I am new to AspectJ, so an example would be highly appreciated.
>
> Thank you in advance
> _______________________________________________
> 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

_______________________________________________
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