KickJava   Java API By Example, From Geeks To Geeks.

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


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
26 import org.jboss.aop.Advisor;
27 import org.jboss.aop.AspectManager;
28 import org.jboss.aop.annotation.AnnotationElement;
29 import org.jboss.aop.pointcut.ast.ClassExpression;
30 import org.jboss.aop.proxy.container.ClassProxyContainer;
31
32 /**
33  * Strategy to allow for different handling for the pointcut matchers for different types of advisor
34  *
35  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
36  * @version $Revision: 55910 $
37  */

38 public abstract class MatcherStrategy
39 {
40    private final static MatcherStrategy ADVISOR_MATCHER_STRATEGY = new AdvisorMatcherStrategy();
41    private final static MatcherStrategy PROXY_MATCHER_STRATEGY = new ProxyMatcherStrategy();
42    
43    public static MatcherStrategy getMatcher(Advisor advisor)
44    {
45       if (advisor instanceof ClassProxyContainer)
46       {
47          return PROXY_MATCHER_STRATEGY;
48       }
49       
50       return ADVISOR_MATCHER_STRATEGY;
51    }
52
53    public boolean subtypeOf(Class JavaDoc clazz, ClassExpression instanceOf, Advisor advisor)
54    {
55       
56       if (clazz == null) return false;
57       if (instanceOf.isInstanceOfAnnotated())
58       {
59          String JavaDoc sub = instanceOf.getInstanceOfAnnotation().substring(1);
60          try
61          {
62             Class JavaDoc annotation = Thread.currentThread().getContextClassLoader().loadClass(sub);
63             if (AnnotationElement.getAnyAnnotation(clazz, annotation) != null)
64             {
65                return true;
66             }
67          }
68          catch (ClassNotFoundException JavaDoc e)
69          {
70             if (AspectManager.verbose)
71             {
72                System.out.println("[warn] The annotation @" + sub + " referenced in one of your pointcut expressions can not be found");
73             }
74             return false;
75          }
76       }
77       else if (instanceOf.matches(clazz.getName()))
78       {
79          return true;
80       }
81
82       Class JavaDoc[] interfaces = clazz.getInterfaces();
83       for (int i = 0; i < interfaces.length; i++)
84       {
85          if (subtypeOf(interfaces[i], instanceOf, advisor)) return true;
86       }
87       if (clazz.isInterface()) return false; // we are done
88

89       if (checkIntroductions(clazz, instanceOf, advisor))
90       {
91          return true;
92       }
93       
94       return subtypeOf(clazz.getSuperclass(), instanceOf, advisor);
95    }
96
97    protected abstract boolean checkIntroductions(Class JavaDoc clazz, ClassExpression instanceOf, Advisor advisor);
98    
99    public abstract Class JavaDoc getDeclaringClass(Advisor advisor, Method JavaDoc m);
100 }
101
Popular Tags