KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > aop > dynamicgenadvisor > MyInterceptor


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 package org.jboss.test.aop.dynamicgenadvisor;
23
24 import java.lang.reflect.AccessibleObject JavaDoc;
25 import java.lang.reflect.Constructor JavaDoc;
26 import java.lang.reflect.Field JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28
29 import org.jboss.aop.advice.Interceptor;
30 import org.jboss.aop.joinpoint.ConstructionInvocation;
31 import org.jboss.aop.joinpoint.ConstructorInvocation;
32 import org.jboss.aop.joinpoint.FieldInvocation;
33 import org.jboss.aop.joinpoint.FieldReadInvocation;
34 import org.jboss.aop.joinpoint.FieldWriteInvocation;
35 import org.jboss.aop.joinpoint.Invocation;
36 import org.jboss.aop.joinpoint.MethodInvocation;
37
38 public class MyInterceptor implements Interceptor
39 {
40
41    public String JavaDoc getName()
42    {
43       return "MyInterceptor";
44    }
45
46    public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc
47    {
48       System.out.println("MyInterceptor.invoke()");
49       if (invocation instanceof ConstructorInvocation)
50       {
51          ConstructorInvocation ci = (ConstructorInvocation)invocation;
52          Interceptions.addConstructorInterception(this.getClass(), ci.getConstructor());
53       }
54       if (invocation instanceof ConstructionInvocation)
55       {
56          ConstructionInvocation ci = (ConstructionInvocation)invocation;
57          Interceptions.addConstructionInterception(this.getClass(), ci.getConstructor());
58       }
59       else if (invocation instanceof MethodInvocation)
60       {
61          MethodInvocation mi = (MethodInvocation)invocation;
62          Interceptions.addMethodInterception(this.getClass(), mi.getMethod());
63       }
64       else if (invocation instanceof FieldReadInvocation)
65       {
66          FieldInvocation fi = (FieldReadInvocation)invocation;
67          Interceptions.addFieldReadInterception(this.getClass(), fi.getField());
68       }
69       else if (invocation instanceof FieldWriteInvocation)
70       {
71          FieldWriteInvocation fi = (FieldWriteInvocation)invocation;
72          Interceptions.addFieldWriteInterception(this.getClass(), fi.getField());
73       }
74       
75       return invocation.invokeNext();
76    }
77    
78    private String JavaDoc getClassName(Constructor JavaDoc c)
79    {
80       return getClassName(c.getDeclaringClass().getName());
81    }
82    
83    private String JavaDoc getClassName(Method JavaDoc m)
84    {
85       return getClassName(m.getDeclaringClass().getName());
86    }
87    
88    private String JavaDoc getClassName(Field JavaDoc f)
89    {
90       return getClassName(f.getDeclaringClass().getName());
91    }
92    
93    private String JavaDoc getClassName(String JavaDoc s)
94    {
95       return s.substring(s.lastIndexOf('.') + 1);
96    }
97
98 }
99
Popular Tags