aspose file tools*
The moose likes Spring and the fly likes Spring AOP: How can I create pointcut for middle argument ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring AOP: How can I create pointcut for middle argument ?" Watch "Spring AOP: How can I create pointcut for middle argument ?" New topic
Forums: Spring Aspect Oriented Programming
Author

Spring AOP: How can I create pointcut for middle argument ?

Boris Romashov
Ranch Hand

Joined: Feb 23, 2009
Posts: 38
If argument is unique - I can write:

If argument is first:

If argument is last:

But if it is in the middle the following fails:


How can I do it ?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

It might work with a specific type.

So if you have a possible joinpoint like

public void doSomething(String s, Integer i, Order order)

args(anArg)

@Pointcut (value = "args(anArg)")
private void pointcut(Integer anArg) {}

Then Spring would use reflection and see that you are referring to the second argument of doSomething, by examining its type.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Boris Romashov
Ranch Hand

Joined: Feb 23, 2009
Posts: 38
Mark Spritzler wrote:

As I wrote in first message, your version points only to methods with unique argument.
That is not what I need.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

How about

args(*, myObject, ..)

Mark
Boris Romashov
Ranch Hand

Joined: Feb 23, 2009
Posts: 38
Mark Spritzler wrote:How about
args(*, myObject, ..)

This only works when myObject is second argument.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

So then what exactly do you mean by middle parameter? How many parameters?

If ther eis nothing to grab onto, some pattern, that is where I find creating my own Annotation and creating a Pointcut that matches the methods. But I think that might still nto completely solve your problem, which only leaves creating pointcuts that combine pointcuts with &&(and), || (not), ! (or)

Mark
Boris Romashov
Ranch Hand

Joined: Feb 23, 2009
Posts: 38
Mark Spritzler wrote:So then what exactly do you mean by middle parameter?

Middle means anything except of first and last - no matter how many parameters are present.

Creating annotation and marking all methods that fit my requirement is a solution with some limitations.
What if there are a lot of methods? If my pattern changes I need to look through all annotated methods.
What if these methods are not in my classes (that's why I cannot edit them).
So I want to do it in by one pointcut without editing classes.
Besides, this information can be successfully calculated without any marking. When argument is first and last it is very easy.
Why this cannot be easy for middle argument?
ah habibi
Greenhorn

Joined: Dec 02, 2013
Posts: 6
Boris Romashov,

Did you get a solution for this issue ? Please update.

I came up with a workaround for this. Since i know the number of arguments the method (inherited from interface) will take, i used * in place of other arguments and named the parameter of my concern. Then i was able to use it.

like


this worked for me.

Please update if you have found a way to fix this, if the number of arguments is unknown.


Thanks
 
Don't get me started about those stupid light bulbs.
 
subject: Spring AOP: How can I create pointcut for middle argument ?
 
Similar Threads
WebServiceContext and AspectJ
AspectJ LTW using Spring
Getting method name on spring AOP @Before
How to pass information from target object to Advice Object
Not able to understant the error $Proxy7 cannot be cast