KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > MyAspect


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 import org.jboss.aop.joinpoint.Invocation;
23 import org.jboss.aop.joinpoint.MethodInvocation;
24 import org.jboss.aop.joinpoint.ConstructorInvocation;
25 import org.jboss.aop.joinpoint.FieldInvocation;
26 import org.jboss.aop.joinpoint.FieldReadInvocation;
27 import org.jboss.aop.joinpoint.FieldWriteInvocation;
28 import org.jboss.aop.advice.Interceptor;
29 import org.jboss.aop.advice.Scope;
30 import org.jboss.aop.Bind;
31 import org.jboss.aop.Aspect;
32 import org.jboss.aop.PointcutDef;
33 import org.jboss.aop.pointcut.Pointcut;
34
35 /**
36  *
37  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
38  * @version $Revision: 37406 $
39  */

40 @Aspect (scope=Scope.PER_VM)
41 public class MyAspect
42 {
43    @PointcutDef("execution(POJO->new(..))")
44    public static Pointcut pojoConstructors;
45
46    @PointcutDef("get(* POJO->*)")
47    public static Pointcut pojoFieldReads;
48
49    @PointcutDef("set(* POJO->*)")
50    public static Pointcut pojoFieldWrites;
51
52    @PointcutDef("execution(* POJO->*(..))")
53    public static Pointcut pojoMethods;
54
55    @PointcutDef("MyAspect.pojoFieldReads OR MyAspect.pojoFieldWrites")
56    public static Pointcut pojoFields;
57
58    @Bind(pointcut = "MyAspect.pojoFields OR MyAspect.pojoMethods OR MyAspect.pojoConstructors")
59    public Object JavaDoc anotherPOJOAdvice(Invocation invocation) throws Throwable JavaDoc
60    {
61       try
62       {
63          if (invocation instanceof ConstructorInvocation)
64          {
65             System.out.println("<<< MyAspect.anotherPOJOAdvice - calling constructor");
66          }
67          else if (invocation instanceof MethodInvocation)
68          {
69             System.out.println("<<< MyAspect.anotherPOJOAdvice - calling method");
70          }
71          else if (invocation instanceof FieldReadInvocation)
72          {
73             System.out.println("<<< MyAspect.anotherPOJOAdvice - reading field");
74          }
75          else if (invocation instanceof FieldWriteInvocation)
76          {
77             System.out.println("<<< MyAspect.anotherPOJOAdvice - writing field");
78          }
79          return invocation.invokeNext();
80       }
81       finally
82       {
83          System.out.println(">>> Leaving MyAspect.anotherPOJOAdvice");
84       }
85    }
86
87 }
88
Popular Tags