Multiple types in pointcut

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Multiple types in pointcut

Mansour Al Akeel
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
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/aspectj-users
Reply | Threaded
Open this post in threaded view
|

Re: Multiple types in pointcut

Andy Clement
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 <[hidden email]> 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
> [hidden email]
> 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
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/aspectj-users
Reply | Threaded
Open this post in threaded view
|

Re: Multiple types in pointcut

Mansour Al Akeel

Thank you Andy.

On Apr 25, 2016 1:24 PM, "Andy Clement" <[hidden email]> 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 <[hidden email]> 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
> [hidden email]
> 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
[hidden email]
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
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/aspectj-users