Recipe 12.5. Declaring Anonymous PointcutsProblemYou want to declare a simple pointcut anonymously within a named pointcut declaration, or attached directly to an advice. SolutionAnonymous pointcuts are the building blocks of pointcut declarations. They have been used throughout all the pointcut-based chapters, but this recipe gives anonymous pointcuts more detailed attention. Example 12-6 shows an example of anonymous pointcuts being used as the foundation for more complexly named pointcuts, as well as directly on an advice declaration. Example 12-6. Using anonymous pointcutspublic aspect AnonymousPointcutRecipe { /* A pointcut declaration that is built up from one anonymous pointcut: Anonymous Pointcuts: call(void MyClass.foo(int,String) */ pointcut singleAnonymousPointcut( ) : call(void MyClass.foo(int, String)); /* A pointcut declaration that is built up from two anonymous pointcuts: Anonymous Pointcuts: call(void MyClass.foo(int,String) call(void MyClass.foo(int,String) !within(AnonymousPointcutRecipe +) */ pointcut multipleAnonymousPointcut( ) : ( call(void MyClass.bar( )) || call(void MyClass.foo(int, String)) && !within(AnonymosPointcutRecipe +)); /* A pointcut declaration attached to the advice it will invoke, built up from anonymous pointcuts: Anonymous Pointcuts: within(LogicalOrRecipe +) */ before( ) : singleAnonymousPointcut( ) && !within(AnonymousPointcutRecipe +) { System.out.println( "------------------- Aspect Advice Logic --------------------"); System.out.println( "In the advice picked by singleAnonymousPointcut and"); System.out.println("!within(AnonymousPointcutRecipe +"); System.out.println( "Signature: " + thisJoinPoint.getSignature( )); System.out.println( "Source Line: " + thisJoinPoint.getSourceLocation( )); System.out.println( "------------------------------------------------------------"); } /* A pointcut declaration attached to the advice it will invoke, built up from anonymous pointcuts: Anonymous Pointcuts: None */ before( ) : multipleAnonymousPointcut( ) { System.out.println( "------------------- Aspect Advice Logic --------------------"); System.out.println( "In the advice picked by multipleAnonymousPointcut( )"); System.out.println( "Signature: " + thisJoinPoint.getSignature( )); System.out.println( "Source Line: " + thisJoinPoint.getSourceLocation( )); System.out.println( "------------------------------------------------------------"); } } DiscussionThis recipe differs from the other pointcut-based recipes in this book because it deals with one of two particular mechanisms for declaring pointcuts rather than examining a specific pointcut type. Pointcuts can be declared anonymously within named pointcut declarations or by being directly attached to the advice they will invoke. See AlsoRecipes 12.2 and 12.3 cover techniques for combining pointcut declarations; the within(TypePattern) pointcut is described in Recipe 9.1; the NOT(!) operator is described in Recipe 12.4; Chapter 13 describes the different types of advice available in AspectJ. |