KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > pointcut > ProxyMatcherStrategy


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.aop.pointcut;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import org.jboss.aop.Advisor;
29 import org.jboss.aop.introduction.InterfaceIntroduction;
30 import org.jboss.aop.pointcut.ast.ClassExpression;
31
32 /**
33  * Strategy to allow for different handling for the pointcut matchers for different types of advisor
34  * This one uses the "default" strategy, used for proxy advisors. The main problem for these is that
35  * methods from interface introductions will have the class of the interface rather than the class of
36  * the advised class.
37  *
38  *
39  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
40  * @version $Revision: 44750 $
41  */

42 public class ProxyMatcherStrategy extends MatcherStrategy
43 {
44    public ProxyMatcherStrategy()
45    {
46    }
47    
48    protected boolean checkIntroductions(Class JavaDoc clazz, ClassExpression instanceOf, Advisor advisor)
49    {
50       try
51       {
52          if (advisor != null)
53          {
54             ArrayList JavaDoc intros = advisor.getInterfaceIntroductions();
55             if (intros.size() > 0)
56             {
57                for (Iterator JavaDoc itIntro = intros.iterator() ; itIntro.hasNext() ; )
58                {
59                   InterfaceIntroduction intro = (InterfaceIntroduction)itIntro.next();
60                   String JavaDoc[] introductions = intro.getInterfaces();
61                   if (introductions != null)
62                   {
63                      for (int i = 0 ; i < introductions.length ; i++)
64                      {
65                         Class JavaDoc iface = Thread.currentThread().getContextClassLoader().loadClass(introductions[i]);
66                         if (subtypeOf(iface, instanceOf, advisor)) return true;
67                      }
68                   }
69                   ArrayList JavaDoc mixins = intro.getMixins();
70                   if (mixins.size() > 0)
71                   {
72                      for (Iterator JavaDoc itMixin = mixins.iterator() ; itMixin.hasNext() ; )
73                      {
74                         InterfaceIntroduction.Mixin mixin = (InterfaceIntroduction.Mixin)itMixin.next();
75                         String JavaDoc[] mixinInterfaces = mixin.getInterfaces();
76                         if (mixinInterfaces != null)
77                         {
78                            for (int i = 0 ; i < mixinInterfaces.length ; i++)
79                            {
80                               Class JavaDoc iface = Thread.currentThread().getContextClassLoader().loadClass(mixinInterfaces[i]);
81                               if (subtypeOf(iface, instanceOf, advisor)) return true;
82                            }
83                         }
84                      }
85                   }
86                }
87             }
88          }
89       }
90       catch (ClassNotFoundException JavaDoc e)
91       {
92          throw new RuntimeException JavaDoc(e);
93       }
94       
95       return false;
96    }
97
98    /**
99     * Interface Introduced methods on the proxy will have the wrong declaring class for the matcher,
100     * use the advisor class if it is an interface
101     */

102    public Class JavaDoc getDeclaringClass(Advisor advisor, Method JavaDoc m)
103    {
104       final Class JavaDoc methodClass = m.getDeclaringClass();
105       final Class JavaDoc advisorClass = advisor.getClazz();
106       
107       if (advisorClass != null && methodClass.isInterface())
108       {
109          return advisorClass;
110       }
111       return methodClass;
112    }
113    
114    
115 }
116
Popular Tags