KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > util > JavassistUtils


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.util;
23
24 import java.util.ArrayList JavaDoc;
25
26 import javassist.CtClass;
27 import javassist.CtMethod;
28 import javassist.NotFoundException;
29
30 /**
31  *
32  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
33  * @version $Revision: 40295 $
34  */

35 public class JavassistUtils
36 {
37    public static CtClass[] EMPTY_CTCLASS_ARRAY = new CtClass[0];
38    
39    public static CtMethod[] getMethodsWithName(CtClass clazz, String JavaDoc name)
40    {
41       CtMethod[] methods = clazz.getMethods();
42       return getMethodsWithName(methods, name);
43    }
44    
45    public static CtMethod[] getDeclaredMethodsWithName(CtClass clazz, String JavaDoc name)
46    {
47       CtMethod[] methods = clazz.getDeclaredMethods();
48       return getMethodsWithName(methods, name);
49    }
50    
51    private static CtMethod[] getMethodsWithName(CtMethod[] methods, String JavaDoc name)
52    {
53       ArrayList JavaDoc foundMethods = new ArrayList JavaDoc();
54       for (int i = 0 ; i < methods.length ; i++)
55       {
56          if (methods[i].getName().equals(name))
57          {
58             foundMethods.add(methods[i]);
59          }
60       }
61       return (CtMethod[])foundMethods.toArray(new CtMethod[foundMethods.size()]);
62    }
63    
64    public static boolean isSubclassOrImplements(CtClass clazz, CtClass lookingFor) throws NotFoundException
65    {
66       if (clazz == null) return false;
67       if (clazz.equals(lookingFor)) return true;
68       if (clazz.getName().equals("java.lang.Object")) return false;
69       
70       if (clazz.isPrimitive()) return false;
71       
72       CtClass[] interfaces = clazz.getInterfaces();
73       for (int i = 0 ; i < interfaces.length ; i++)
74       {
75          if (isSubclassOrImplements(interfaces[i], lookingFor)) return true;
76       }
77       
78       if (isSubclassOrImplements(clazz.getSuperclass(), lookingFor)) return true;
79       
80       return false;
81    }
82    
83    public static String JavaDoc getNullOrZeroInitialiser(CtClass clazz)
84    {
85       if (clazz.isPrimitive())
86       {
87          if (clazz.equals(CtClass.booleanType)) return "false";
88          else if (clazz.equals(CtClass.charType)) return "'\\0'";
89          else if (clazz.equals(CtClass.byteType)) return "(byte)0";
90          else if (clazz.equals(CtClass.shortType)) return "(short)0";
91          else if (clazz.equals(CtClass.intType)) return "(int)0";
92          else if (clazz.equals(CtClass.longType)) return "0L";
93          else if (clazz.equals(CtClass.floatType)) return "0.0f";
94          else if (clazz.equals(CtClass.doubleType)) return "0.0d";
95       }
96       else
97       {
98          return "null";
99       }
100       return null;
101    }
102 }
103
Popular Tags