Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Matching all methods on an annotated type?

Hi Andy,

Thank you for the quick response, and the clear explanation of the pointcut. I see where I went wrong in using @annotation instead of @within to pick out the methods in the type. I like the way you've split it in two, makes it easier to understand.

Thanks!

Steve

On Sat, Aug 15, 2009 at 2:13 AM, Andy Clement <andrew.clement@xxxxxxxxx> wrote:
Hi Steve,

Let's take the pointcut apart:


pointcut test1(Object target, ApplyMethodTiming applyMethodTiming) :
        execution(@ApplyMethodTiming * *(..)) // execution of any method that has the @ApplyMethodTiming annotation
        && @annotation(applyMethodTiming) // where the subject of the join point has @ApplyMethodTiming (subject of method execution is the method)
        && target(target); // and the target is not null (ie. non-static methods)

Effectively the 'execution' is already limiting you to annotated methods only - so there isn't a part that is specifying an annotated type.

What you'd like to write is this:

//    pointcut test1(Object target, ApplyMethodTiming applyMethodTiming) :
//        ((execution(@ApplyMethodTiming * *(..)) && @annotation(applyMethodTiming)) || // annotated methods
//        (@within(applyMethodTiming) && execution(* *(..)) )) // methods in an annotated type
//        && target(target);

but that won't work because of an AspectJ bug that I never seem to find time to fix... instead (and perhaps it is easier to understand), it can be broken into two:

pointcut annotatedMethods(Object target, ApplyMethodTiming applyMethodTiming) :
        execution(* *(..)) && @annotation(applyMethodTiming) && target(target);

pointcut methodsInAnAnnotatedType(Object target, ApplyMethodTiming applyMethodTiming) :
        execution(* *(..)) && @within(applyMethodTiming) && target(target);

attach some small bit of advice to each that forwards to your timing code.

Andy


2009/8/14 Stephen Houston <stephenjhouston@xxxxxxxxx>
Hi All,

I've been having some difficulty defining a pointcut that matches all the methods in an annotated type. For example, my understanding of

pointcut test1(Object target, ApplyMethodTiming applyMethodTiming) :
        execution(@ApplyMethodTiming * *(..))
        && @annotation(applyMethodTiming)
        && target(target);

is that it should match all methods that are annotated by @ApplyMethodTiming or are members of a type annotated by @ApplyMethodTiming, but this doesn't appear to be the case. Only those methods annotated with @ApplyMethodTiming are picked out.

I've attached an archive of a maven project containing a few simple tests for this. The only one that works for me is the one that uses reflection to test if the class has the annotation applied to it. I'd rather not do this as it will be quite slow.

The output of the test project is:

00:21:10,377 FATAL [           main] DOC methodTimingTest.ApplyMethodTimingAspect - A - Applied to execution(void test.methodTimingTest.ApplyMethodTimingAspectTest.takesNoTime())
00:21:10,382 FATAL [           main] DOC methodTimingTest.ApplyMethodTimingAspect - A - Applied to execution(void test.methodTimingTest.TimedObject.takesNoTime())
00:21:10,385 FATAL [           main] DOC methodTimingTest.ApplyMethodTimingAspect - R - Applied to execution(void test.methodTimingTest.TimedObject.takesNoTime())
00:21:10,385 FATAL [           main] DOC methodTimingTest.TimedObject - takesNoTime
00:21:10,391 FATAL [           main] DOC methodTimingTest.ApplyMethodTimingAspect - A - Applied to execution(void test.methodTimingTest.ApplyMethodTimingAspectTest.worksIfAnnotated())
00:21:10,394 FATAL [           main] DOC methodTimingTest.ApplyMethodTimingAspect - 1 - Applied to execution(void test.methodTimingTest.TimedObject.worksIfAnnotated())
00:21:10,394 FATAL [           main] DOC methodTimingTest.ApplyMethodTimingAspect - 2 - Applied to execution(void test.methodTimingTest.TimedObject.worksIfAnnotated())
00:21:10,394 FATAL [           main] DOC methodTimingTest.ApplyMethodTimingAspect - 3 - Applied to execution(void test.methodTimingTest.TimedObject.worksIfAnnotated())
00:21:10,395 FATAL [           main] DOC methodTimingTest.ApplyMethodTimingAspect - A - Applied to execution(void test.methodTimingTest.TimedObject.worksIfAnnotated())
00:21:10,395 FATAL [           main] DOC methodTimingTest.ApplyMethodTimingAspect - R - Applied to execution(void test.methodTimingTest.TimedObject.worksIfAnnotated())
00:21:10,395 FATAL [           main] DOC methodTimingTest.TimedObject - annotated

Where
- A - indicates a match on all methods (execution * *(..))
- R - indicates a match on all methods, then use reflection to determine if the class has the @ApplyMethodTiming aspect
- 1 - indicates a match on execution(@ApplyMethodTiming * *(..)) && @annotation(applyMethodTiming)
- 2 - indicates a match on execution(@ApplyMethodTiming * *(..))
- 3 - indicates a match on execution(* *(..)) && @annotation(applyMethodTiming)

I'm using Apsectj 1.6.5. The strange thing is that I'm sure I seen this working earlier today, but it doesn't appear to want to work now.

Thanks

Steve

_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users



_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users



Back to the top