KickJava   Java API By Example, From Geeks To Geeks.

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


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.Constructor JavaDoc;
25 import java.lang.reflect.Field JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.lang.reflect.Modifier JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 import javassist.ClassPool;
34 import javassist.CtClass;
35 import javassist.CtConstructor;
36 import javassist.CtField;
37 import javassist.CtMethod;
38 import javassist.NotFoundException;
39
40 import org.jboss.aop.Advisor;
41 import org.jboss.aop.AspectManager;
42 import org.jboss.aop.annotation.AnnotationElement;
43 import org.jboss.aop.annotation.PortableAnnotationElement;
44 import org.jboss.aop.introduction.InterfaceIntroduction;
45 import org.jboss.aop.pointcut.ast.ASTAttribute;
46 import org.jboss.aop.pointcut.ast.ASTConstructor;
47 import org.jboss.aop.pointcut.ast.ASTException;
48 import org.jboss.aop.pointcut.ast.ASTField;
49 import org.jboss.aop.pointcut.ast.ASTMethod;
50 import org.jboss.aop.pointcut.ast.ASTParameter;
51 import org.jboss.aop.pointcut.ast.ClassExpression;
52 import org.jboss.aop.util.JavassistMethodHashing;
53 import org.jboss.aop.util.MethodHashing;
54
55 /**
56  * Comment
57  *
58  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
59  * @version $Revision: 45039 $
60  */

61 public class Util
62 {
63    public static boolean matchesClassExpr(ClassExpression classExpr, CtClass clazz, Advisor advisor)
64    {
65       try
66       {
67          if (classExpr.isAnnotation())
68          {
69             String JavaDoc sub = classExpr.getOriginal().substring(1);
70             if (advisor != null)
71             {
72                if (advisor.getClassMetaData().hasTag(sub)) return true;
73                return advisor.hasAnnotation(clazz, sub);
74             }
75             else
76             {
77                return AnnotationElement.isAnyAnnotationPresent(clazz, sub);
78             }
79          }
80          else if (classExpr.isInstanceOf())
81          {
82             return Util.subtypeOf(clazz, classExpr, advisor);
83          }
84          else if (classExpr.isTypedef())
85          {
86             return Util.matchesTypedef(clazz, classExpr, advisor);
87          }
88          else
89          {
90             return classExpr.matches(clazz.getName());
91          }
92       }
93       catch (Exception JavaDoc e)
94       {
95          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
96
}
97
98    }
99
100    public static boolean matchesClassExpr(ClassExpression classExpr, Class JavaDoc clazz)
101    {
102       return matchesClassExpr(classExpr, clazz, null);
103    }
104    
105    public static boolean matchesClassExpr(ClassExpression classExpr, Class JavaDoc clazz, Advisor advisor)
106    {
107       try
108       {
109          if (classExpr.isAnnotation())
110          {
111             String JavaDoc sub = classExpr.getOriginal().substring(1);
112             if (advisor != null)
113             {
114                if (advisor.getClassMetaData().hasTag(sub)) return true;
115                return advisor.hasAnnotation(clazz, sub);
116             }
117             else
118             {
119                return AnnotationElement.isAnyAnnotationPresent(clazz, sub);
120             }
121          }
122          else if (classExpr.isInstanceOf())
123          {
124             return Util.subtypeOf(clazz, classExpr, advisor);
125          }
126          else if (classExpr.isTypedef())
127          {
128             return Util.matchesTypedef(clazz, classExpr, advisor);
129          }
130          else
131          {
132             return(classExpr.matches(clazz.getName()));
133          }
134       }
135       catch (Exception JavaDoc e)
136       {
137          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
138
}
139
140    }
141
142    /**
143     * @param method Method we are looking for
144     * @param target ClassExpression with the class/interface we are looking for the method in
145     */

146    public static boolean methodExistsInSuperClassOrInterface(Method JavaDoc method, ClassExpression target, boolean exactSuper, Advisor advisor) throws Exception JavaDoc
147    {
148       long hash = MethodHashing.calculateHash(method);
149       boolean exists = methodExistsInSuperClassOrInterface(hash, target, method.getDeclaringClass(), exactSuper);
150       if (!exists)
151       {
152          exists = checkMethodExistsInIntroductions(hash, target, exactSuper, advisor);
153       }
154       return exists;
155    }
156    
157    private static boolean methodExistsInSuperClassOrInterface(long hash, ClassExpression expr, Class JavaDoc clazz, boolean exactSuper) throws Exception JavaDoc
158    {
159       if (clazz == null) return false;
160       
161       if (expr.isAnnotation())
162       {
163          String JavaDoc sub = expr.getOriginal().substring(1);
164           if (AnnotationElement.isAnyAnnotationPresent(clazz, sub))
165           {
166              if (classHasMethod(clazz, hash, exactSuper)) return true;
167           }
168        }
169        else if (expr.matches(clazz.getName()))
170        {
171           if (classHasMethod(clazz, hash, exactSuper)) return true;
172        }
173
174        Class JavaDoc[] interfaces = clazz.getInterfaces();
175        for (int i = 0; i < interfaces.length; i++)
176        {
177           if (methodExistsInSuperClassOrInterface(hash, expr, interfaces[i], exactSuper)) return true;
178        }
179        
180        if (clazz.isInterface()) return false; // we are done
181

182        return methodExistsInSuperClassOrInterface(hash, expr, clazz.getSuperclass(), exactSuper);
183    }
184    
185    /**
186     * When using the SystemClassLoader, trying to load up classes when using loadtime weaving gives ClassCircularityErrors,
187     * so do this with javassist
188     */

189    private static boolean checkMethodExistsInIntroductions(long hash, ClassExpression target, boolean exactSuper, Advisor advisor) throws Exception JavaDoc
190    {
191       if (advisor != null)
192       {
193          ArrayList JavaDoc intros = advisor.getInterfaceIntroductions();
194          if (intros.size() > 0)
195          {
196             ClassLoader JavaDoc tcl = Thread.currentThread().getContextClassLoader();
197             ClassPool pool = advisor.getManager().findClassPool(tcl);
198             HashSet JavaDoc doneClasses = new HashSet JavaDoc();
199             for (Iterator JavaDoc it = intros.iterator() ; it.hasNext() ; )
200             {
201                InterfaceIntroduction intro = (InterfaceIntroduction)it.next();
202                String JavaDoc[] ifs = intro.getInterfaces();
203                for (int i = 0 ; ifs != null && i < ifs.length ; i++)
204                {
205                   if (!doneClasses.contains(ifs[i]))
206                   {
207                      doneClasses.add(ifs[i]);
208                      if (methodExistsInSuperClassOrInterface(pool, hash, target, ifs[i], exactSuper)) return true;
209                   }
210                }
211                
212                ArrayList JavaDoc mixins = intro.getMixins();
213                if (mixins != null && mixins.size() > 0)
214                {
215                   for (Iterator JavaDoc mit = mixins.iterator() ; mit.hasNext() ; )
216                   {
217                      InterfaceIntroduction.Mixin mixin = (InterfaceIntroduction.Mixin)mit.next();
218                      String JavaDoc[] mifs = mixin.getInterfaces();
219                      for (int i = 0 ; mifs != null && i < mifs.length ; i++)
220                      {
221                         if (!doneClasses.contains(mifs[i]))
222                         {
223                            doneClasses.add(mifs[i]);
224                            if (methodExistsInSuperClassOrInterface(pool, hash, target, mifs[i], exactSuper)) return true;
225                         }
226                      }
227                   }
228                }
229             }
230          }
231       }
232       return false;
233    }
234    
235    private static boolean classHasMethod(Class JavaDoc clazz, long hash, boolean exactSuper)throws Exception JavaDoc
236    {
237       Method JavaDoc m = MethodHashing.findMethodByHash(clazz, hash);
238       if (m != null)
239       {
240          if (exactSuper)
241          {
242             //MethodHashing will check all super classes so make sure it is on the class itself
243
return clazz == m.getDeclaringClass();
244          }
245          else
246          {
247             return true;
248          }
249       }
250       
251       return false;
252    }
253
254    /**
255     * @param method CtMethod we are looking for
256     * @param target ClassExpression with the class/interface we are looking for the method in
257     */

258    public static boolean methodExistsInSuperClassOrInterface(CtMethod method, ClassExpression target, boolean exactSuper) throws Exception JavaDoc
259    {
260       long hash = JavassistMethodHashing.methodHash(method);
261       return methodExistsInSuperClassOrInterface(hash, target, method.getDeclaringClass(), exactSuper);
262    }
263    
264    private static boolean methodExistsInSuperClassOrInterface(ClassPool pool, long hash, ClassExpression expr, String JavaDoc className, boolean exactSuper) throws Exception JavaDoc
265    {
266       CtClass clazz = pool.get(className);
267       HashMap JavaDoc map = JavassistMethodHashing.getMethodMap(clazz);
268       return methodExistsInSuperClassOrInterface(hash, expr, clazz, exactSuper);
269    }
270    
271    private static boolean methodExistsInSuperClassOrInterface(long hash, ClassExpression expr, CtClass clazz, boolean exactSuper) throws Exception JavaDoc
272    {
273       if (clazz == null) return false;
274       
275       if (expr.isAnnotation())
276       {
277          String JavaDoc sub = expr.getOriginal().substring(1);
278          if (AnnotationElement.isAnyAnnotationPresent(clazz, sub))
279          {
280             if (classHasMethod(clazz, hash, exactSuper)) return true;
281          }
282       }
283       else if (expr.matches(clazz.getName()))
284       {
285          if (classHasMethod(clazz, hash, exactSuper)) return true;
286       }
287       
288       CtClass[] interfaces = clazz.getInterfaces();
289       for (int i = 0; i < interfaces.length; i++)
290       {
291          if (methodExistsInSuperClassOrInterface(hash, expr, interfaces[i], exactSuper)) return true;
292       }
293       if (clazz.isInterface()) return false; // we are done
294

295       return methodExistsInSuperClassOrInterface(hash, expr, clazz.getSuperclass(), exactSuper);
296    }
297    
298    private static boolean classHasMethod(CtClass clazz, long hash, boolean exactSuper)throws Exception JavaDoc
299    {
300       HashMap JavaDoc methods = JavassistMethodHashing.getMethodMap(clazz);
301       CtMethod m = (CtMethod)methods.get(new Long JavaDoc(hash));
302       if (m != null)
303       {
304          if (exactSuper)
305          {
306             //If a class, JavassistMethodHashing will check all super classes so make sure it is on the class itself
307
return clazz == m.getDeclaringClass();
308          }
309          else
310          {
311             return true;
312          }
313       }
314       
315       if (clazz.isInterface() && !exactSuper)
316       {
317          CtClass[] interfaces = clazz.getInterfaces();
318          for (int i = 0 ; i < interfaces.length ; i++)
319          {
320             if (classHasMethod(interfaces[i], hash, exactSuper)) return true;
321          }
322       }
323       return false;
324    }
325    
326    public static boolean subtypeOf(CtClass clazz, ClassExpression instanceOf, Advisor advisor)
327    {
328       try
329       {
330           if (clazz == null) return false;
331           if (instanceOf.isInstanceOfAnnotated())
332           {
333             if (clazz.isPrimitive()) return false;
334             String JavaDoc sub = instanceOf.getInstanceOfAnnotation().substring(1);
335             if (PortableAnnotationElement.isAnyAnnotationPresent(clazz, sub)) return true;
336           }
337           else if (instanceOf.matches(clazz.getName()))
338           {
339              return true;
340           }
341           CtClass[] interfaces = clazz.getInterfaces();
342           for (int i = 0; i < interfaces.length; i++)
343           {
344              if (subtypeOf(interfaces[i], instanceOf, advisor)) return true;
345           }
346           if (clazz.isInterface()) return false; // we are done
347

348          if (checkIntroductions(clazz, instanceOf, advisor))
349          {
350             return true;
351          }
352          
353           return subtypeOf(clazz.getSuperclass(), instanceOf, advisor);
354       }
355       catch (Exception JavaDoc e)
356       {
357          throw new RuntimeException JavaDoc(e);
358       }
359    }
360
361    private static boolean checkIntroductions(CtClass clazz, ClassExpression instanceOf, Advisor advisor)
362    {
363       try
364       {
365          if (advisor != null)
366          {
367             ArrayList JavaDoc intros = advisor.getInterfaceIntroductions();
368             if (intros.size() > 0)
369             {
370                for (Iterator JavaDoc itIntro = intros.iterator() ; itIntro.hasNext() ; )
371                {
372                   InterfaceIntroduction intro = (InterfaceIntroduction)itIntro.next();
373                   String JavaDoc[] introductions = intro.getInterfaces();
374                   if (introductions != null)
375                   {
376                      for (int i = 0 ; i < introductions.length ; i++)
377                      {
378                         Class JavaDoc iface = Thread.currentThread().getContextClassLoader().loadClass(introductions[i]);
379                         if (subtypeOf(iface, instanceOf, advisor)) return true;
380                      }
381                   }
382                   ArrayList JavaDoc mixins = intro.getMixins();
383                   if (mixins.size() > 0)
384                   {
385                      for (Iterator JavaDoc itMixin = mixins.iterator() ; itMixin.hasNext() ; )
386                      {
387                         InterfaceIntroduction.Mixin mixin = (InterfaceIntroduction.Mixin)itMixin.next();
388                         String JavaDoc[] mixinInterfaces = mixin.getInterfaces();
389                         if (mixinInterfaces != null)
390                         {
391                            for (int i = 0 ; i < mixinInterfaces.length ; i++)
392                            {
393                               Class JavaDoc iface = Thread.currentThread().getContextClassLoader().loadClass(mixinInterfaces[i]);
394                               if (subtypeOf(iface, instanceOf, advisor)) return true;
395                            }
396                         }
397                      }
398                   }
399                }
400             }
401          }
402       }
403       catch (ClassNotFoundException JavaDoc e)
404       {
405          throw new RuntimeException JavaDoc(e);
406       }
407       
408       return false;
409    }
410    
411    public static boolean subtypeOf(Class JavaDoc clazz, ClassExpression instanceOf, Advisor advisor)
412    {
413       return MatcherStrategy.getMatcher(advisor).subtypeOf(clazz, instanceOf, advisor);
414    }
415    
416    public static boolean has(CtClass target, ASTMethod method, Advisor advisor)
417    {
418       return has(target, method, advisor, true);
419    }
420    
421    public static boolean has(CtClass target, ASTMethod method, Advisor advisor, boolean checkSuper)
422    {
423       try
424       {
425          CtMethod[] methods = target.getDeclaredMethods();
426          for (int i = 0; i < methods.length; i++)
427          {
428             MethodMatcher matcher = new MethodMatcher(advisor, methods[i], null);
429             if (matcher.matches(method).booleanValue()) return true;
430          }
431          
432          if (checkSuper)
433          {
434             CtClass superClass = target.getSuperclass();
435             if (superClass != null) return has(superClass, method, advisor, checkSuper);
436          }
437       }
438       catch (NotFoundException e)
439       {
440          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
441
}
442       return false;
443    }
444
445    public static boolean has(CtClass target, ASTField field, Advisor advisor)
446    {
447       return has(target, field, advisor, true);
448    }
449    
450    public static boolean has(CtClass target, ASTField field, Advisor advisor, boolean checkSuper)
451    {
452       try
453       {
454          CtField[] fields = target.getDeclaredFields();
455          for (int i = 0; i < fields.length; i++)
456          {
457             FieldGetMatcher matcher = new FieldGetMatcher(advisor, fields[i], null);
458             if (((Boolean JavaDoc) field.jjtAccept(matcher, null)).booleanValue()) return true;
459          }
460          
461          if (checkSuper)
462          {
463             CtClass superClass = target.getSuperclass();
464             if (superClass != null) return has(superClass, field, advisor, checkSuper);
465          }
466       }
467       catch (NotFoundException e)
468       {
469          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
470
}
471       return false;
472    }
473
474    public static boolean has(CtClass target, ASTConstructor con, Advisor advisor)
475    {
476       try
477       {
478          CtConstructor[] cons = target.getDeclaredConstructors();
479          for (int i = 0; i < cons.length; i++)
480          {
481             ConstructorMatcher matcher = new ConstructorMatcher(advisor, cons[i], null);
482             if (matcher.matches(con).booleanValue()) return true;
483          }
484       }
485       catch (NotFoundException e)
486       {
487          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
488
}
489       return false;
490    }
491
492    public static boolean has(Class JavaDoc target, ASTMethod method, Advisor advisor)
493    {
494       return has(target, method, advisor, true);
495    }
496    
497    public static boolean has(Class JavaDoc target, ASTMethod method, Advisor advisor, boolean checkSuper)
498    {
499       Method JavaDoc[] methods = advisor.getAllMethods();
500       if (methods == null)
501          methods = target.getDeclaredMethods();
502       for (int i = 0; i < methods.length; i++)
503       {
504          MethodMatcher matcher = new MethodMatcher(advisor, methods[i], null);
505          if (matcher.matches(method).booleanValue()) return true;
506       }
507       
508       if (checkSuper)
509       {
510          Class JavaDoc superClass = target.getSuperclass();
511          if (superClass != null) return has(superClass, method, advisor, checkSuper);
512       }
513       return false;
514    }
515
516    public static boolean has(Class JavaDoc target, ASTField field, Advisor advisor)
517    {
518       return has(target, field, advisor, true);
519    }
520    
521    public static boolean has(Class JavaDoc target, ASTField field, Advisor advisor, boolean checkSuper)
522    {
523       Field JavaDoc[] fields = target.getDeclaredFields();
524       for (int i = 0; i < fields.length; i++)
525       {
526          FieldGetMatcher matcher = new FieldGetMatcher(advisor, fields[i], null);
527          if (((Boolean JavaDoc) field.jjtAccept(matcher, null)).booleanValue()) return true;
528       }
529       
530       if (checkSuper)
531       {
532          Class JavaDoc superClass = target.getSuperclass();
533          if (superClass != null) return has(superClass, field, advisor, checkSuper);
534       }
535       return false;
536    }
537
538    public static boolean has(Class JavaDoc target, ASTConstructor con, Advisor advisor)
539    {
540       Constructor JavaDoc[] cons = target.getDeclaredConstructors();
541       for (int i = 0; i < cons.length; i++)
542       {
543          ConstructorMatcher matcher = new ConstructorMatcher(advisor, cons[i], null);
544          if (matcher.matches(con).booleanValue()) return true;
545       }
546       return false;
547    }
548
549    public static boolean matchesTypedef(CtClass clazz, ClassExpression classExpr, Advisor advisor)
550    {
551       String JavaDoc original = classExpr.getOriginal();
552       String JavaDoc typedefName = original.substring("$typedef{".length(), original.lastIndexOf("}"));
553       AspectManager manager = (advisor != null) ? advisor.getManager() : AspectManager.instance();
554       Typedef typedef = manager.getTypedef(typedefName);
555       if (typedef == null) return false;
556       return typedef.matches(advisor, clazz);
557    }
558
559    public static boolean matchesTypedef(Class JavaDoc clazz, ClassExpression classExpr, Advisor advisor)
560    {
561       String JavaDoc original = classExpr.getOriginal();
562       String JavaDoc typedefName = original.substring("$typedef{".length(), original.lastIndexOf("}"));
563       AspectManager manager = (advisor != null) ? advisor.getManager() : AspectManager.instance();
564       Typedef typedef = manager.getTypedef(typedefName);
565       if (typedef == null) return false;
566       return typedef.matches(advisor, clazz);
567    }
568
569    public static boolean matchModifiers(ASTAttribute need, int have)
570    {
571       if (Modifier.isAbstract(need.attribute) && need.not) return !Modifier.isAbstract(have);
572       if (Modifier.isAbstract(need.attribute) && !need.not) return Modifier.isAbstract(have);
573       if (Modifier.isFinal(need.attribute) && need.not) return !Modifier.isFinal(have);
574       if (Modifier.isFinal(need.attribute) && !need.not) return Modifier.isFinal(have);
575       if (Modifier.isInterface(need.attribute) && need.not) return !Modifier.isInterface(have);
576       if (Modifier.isInterface(need.attribute) && !need.not) return Modifier.isInterface(have);
577       if (Modifier.isNative(need.attribute) && need.not) return !Modifier.isNative(have);
578       if (Modifier.isNative(need.attribute) && !need.not) return Modifier.isNative(have);
579       if (Modifier.isPrivate(need.attribute) && need.not) return !Modifier.isPrivate(have);
580       if (Modifier.isPrivate(need.attribute) && !need.not) return Modifier.isPrivate(have);
581       if (Modifier.isProtected(need.attribute) && need.not) return !Modifier.isProtected(have);
582       if (Modifier.isProtected(need.attribute) && !need.not) return Modifier.isProtected(have);
583       if (Modifier.isPublic(need.attribute) && need.not) return !Modifier.isPublic(have);
584       if (Modifier.isPublic(need.attribute) && !need.not) return Modifier.isPublic(have);
585       if (Modifier.isStatic(need.attribute) && need.not) return !Modifier.isStatic(have);
586       if (Modifier.isStatic(need.attribute) && !need.not) return Modifier.isStatic(have);
587       if (Modifier.isStrict(need.attribute) && need.not) return !Modifier.isStrict(have);
588       if (Modifier.isStrict(need.attribute) && !need.not) return Modifier.isStrict(have);
589       if (Modifier.isSynchronized(need.attribute) && need.not) return !Modifier.isSynchronized(have);
590       if (Modifier.isSynchronized(need.attribute) && !need.not) return Modifier.isSynchronized(have);
591       if (Modifier.isTransient(need.attribute) && need.not) return !Modifier.isTransient(have);
592       if (Modifier.isTransient(need.attribute) && !need.not) return Modifier.isTransient(have);
593       if (Modifier.isVolatile(need.attribute) && need.not) return !Modifier.isVolatile(have);
594       if (Modifier.isVolatile(need.attribute) && !need.not) return Modifier.isVolatile(have);
595
596       return true;
597    }
598
599    /**
600     * @param nodeExceptions ArrayList of ASTException entries for a given ASTMethod or ASTConstructor
601     * @param foundExceptions Array of Exceptions found for a method/constructor
602     */

603    public static boolean matchExceptions(ArrayList JavaDoc nodeExceptions, CtClass[] foundExceptions)
604    {
605       if (nodeExceptions.size() > foundExceptions.length) return false;
606       for (Iterator JavaDoc it = nodeExceptions.iterator(); it.hasNext();)
607       {
608          boolean found = false;
609          ASTException ex = (ASTException) it.next();
610          for (int i = 0; i < foundExceptions.length; i++)
611          {
612             if (ex.getType().matches(foundExceptions[i].getName()))
613             {
614                found = true;
615                break;
616             }
617          }
618
619          if (!found) return false;
620       }
621
622       return true;
623    }
624
625    /**
626     * @param nodeExceptions ArrayList of ASTException entries for a given ASTMethod or ASTConstructor
627     * @param foundExceptions Array of Exceptions found for a method/constructor
628     */

629    public static boolean matchExceptions(ArrayList JavaDoc nodeExceptions, Class JavaDoc[] foundExceptions)
630    {
631       if (nodeExceptions.size() > foundExceptions.length) return false;
632       for (Iterator JavaDoc it = nodeExceptions.iterator(); it.hasNext();)
633       {
634          boolean found = false;
635          ASTException ex = (ASTException) it.next();
636          for (int i = 0; i < foundExceptions.length; i++)
637          {
638             if (ex.getType().matches(foundExceptions[i].getName()))
639             {
640                found = true;
641                break;
642             }
643          }
644
645          if (!found) return false;
646       }
647
648       return true;
649    }
650
651    public static boolean matchesParameters(Advisor advisor, ASTMethod node, CtMethod ctMethod)
652    {
653       if (node.isAnyParameters()) return true;
654       try
655       {
656          return Util.matchesParameters(advisor, node.hasAnyZeroOrMoreParameters(), node.getParameters(), ctMethod.getParameterTypes());
657       }
658       catch (NotFoundException e)
659       {
660          // AutoGenerated
661
throw new RuntimeException JavaDoc(e);
662       }
663    }
664
665    public static boolean matchesParameters(Advisor advisor, ASTConstructor node, CtConstructor ctConstructor)
666    {
667       int i = 0;
668       if (node.isAnyParameters()) return true;
669       try
670       {
671          CtClass[] params = ctConstructor.getParameterTypes();
672          return Util.matchesParameters(advisor, node.hasAnyZeroOrMoreParameters(), node.getParameters(), params);
673       }
674       catch (NotFoundException e)
675       {
676          // AutoGenerated
677
throw new RuntimeException JavaDoc(e);
678       }
679    }
680
681    public static boolean matchesParameters(Advisor advisor, ASTMethod node, Method JavaDoc method)
682    {
683       if (node.isAnyParameters()) return true;
684       return matchesParameters(advisor, node.hasAnyZeroOrMoreParameters(), node.getParameters(), method.getParameterTypes());
685    }
686
687    public static boolean matchesParameters(Advisor advisor, ASTConstructor node, Constructor JavaDoc con)
688    {
689       if (node.isAnyParameters()) return true;
690       
691       Class JavaDoc[] params = con.getParameterTypes();
692       
693       return matchesParameters(advisor, node.hasAnyZeroOrMoreParameters(), node.getParameters(), con.getParameterTypes());
694    }
695
696    private static boolean matchesParameters(Advisor advisor, boolean hasAnyZeroOrMoreParameters, ArrayList JavaDoc parameters, Class JavaDoc[] params)
697    {
698       RefParameterMatcher matcher = new RefParameterMatcher(advisor, parameters, params);
699       return matcher.matches();
700    }
701
702    private static boolean matchesParameters(Advisor advisor, boolean hasAnyZeroOrMoreParameters, ArrayList JavaDoc parameters, CtClass[] params)
703    {
704       CtParameterMatcher matcher = new CtParameterMatcher(advisor, parameters, params);
705       return matcher.matches();
706    }
707    
708    private static class ParameterMatcher
709    {
710       Advisor advisor;
711       ArrayList JavaDoc astParameters;
712       final long paramsLength;
713       int asti;
714       int actuali;
715       
716       ParameterMatcher(Advisor advisor, ArrayList JavaDoc parameters, Object JavaDoc[] params)
717       {
718          this.advisor = advisor;
719          this.astParameters = parameters;
720          paramsLength = params.length;
721       }
722
723       boolean matches()
724       {
725          return matches(0, 0);
726       }
727       
728       private boolean matches(int ast, int actual)
729       {
730          boolean match = true;
731          for ( ; match && ast < astParameters.size() && actual < paramsLength ; ast++)
732          {
733             if (isAnyZeroOrMoreParameters(ast))
734             {
735                asti = ast;
736                actuali = actual;
737                match = wildcard();//ast, cls);
738
ast = asti;
739                actual = actuali;
740                ast--;
741             }
742             else
743             {
744                match = doMatch(ast, actual);
745                actual++;
746             }
747          }
748          
749          while (match && ast < astParameters.size() && isAnyZeroOrMoreParameters(ast))
750          {
751             ast++;
752          }
753          return (match && ast == astParameters.size() && paramsLength == actual);
754       }
755       
756       private boolean isAnyZeroOrMoreParameters(int index)
757       {
758          return ((ASTParameter)astParameters.get(index)).isAnyZeroOrMoreParameters();
759       }
760       
761       boolean doMatch(int astIndex, int actualIndex)
762       {
763          //Overridden by sub classes
764
return false;
765       }
766       
767       private boolean wildcard()
768       {
769          boolean match = true;
770          asti++;
771          
772          while (actuali < paramsLength && asti < astParameters.size() && isAnyZeroOrMoreParameters(asti))
773          {
774             asti++;
775          }
776
777          while (asti < astParameters.size() && isAnyZeroOrMoreParameters(asti))
778          {
779             asti++;
780          }
781
782          if (actuali == paramsLength && asti < astParameters.size())
783             return false;
784          if (actuali == paramsLength && asti == astParameters.size())
785             return true;
786          else
787          {
788             if (!matches(asti, actuali))
789             {
790                do
791                {
792                   actuali++;
793                   while (actuali < paramsLength && asti < astParameters.size() && !doMatch(asti, actuali))
794                   {
795                      actuali++;
796                   }
797                   
798                }while ( (actuali < paramsLength) ? !(match = matches(asti, actuali)) : false);
799                
800             }
801             if (actuali == paramsLength && asti == astParameters.size())
802             {
803                match = true;
804             }
805             return match;
806          }
807       }
808    }
809    
810    private static class RefParameterMatcher extends ParameterMatcher
811    {
812       Class JavaDoc[] params;
813       public RefParameterMatcher(Advisor advisor, ArrayList JavaDoc parameters, Class JavaDoc[] params)
814       {
815          super(advisor, parameters, params);
816          this.params = params;
817       }
818       
819       boolean doMatch(int astIndex, int actualIndex)
820       {
821          ASTParameter ast = (ASTParameter) astParameters.get(astIndex);
822          ClassExpression exp = ast.getType();
823
824          if (exp.isSimple())
825          {
826             String JavaDoc asString = ClassExpression.simpleType(params[actualIndex]);
827             if (!exp.matches(asString)) return false;
828          }
829          else
830          {
831             if (!Util.matchesClassExpr(exp, params[actualIndex], advisor)) return false;
832          }
833          
834          return true;
835       }
836    }
837
838    private static class CtParameterMatcher extends ParameterMatcher
839    {
840       CtClass[] params;
841       public CtParameterMatcher(Advisor advisor, ArrayList JavaDoc parameters, CtClass[] params)
842       {
843          super(advisor, parameters, params);
844          this.params = params;
845       }
846       
847       boolean doMatch(int astIndex, int actualIndex)
848       {
849          ASTParameter ast = (ASTParameter) astParameters.get(astIndex);
850          ClassExpression exp = ast.getType();
851          if (!matchesClassExpr(exp, params[actualIndex], advisor)) return false;
852          return true;
853       }
854    }
855 }
856
Popular Tags