KickJava   Java API By Example, From Geeks To Geeks.

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


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 javassist.ClassPool;
25 import javassist.CtClass;
26 import javassist.CtConstructor;
27 import javassist.CtField;
28 import javassist.CtMethod;
29 import javassist.NotFoundException;
30
31 import org.jboss.aop.AspectManager;
32
33 import java.lang.reflect.Constructor JavaDoc;
34 import java.lang.reflect.Field JavaDoc;
35 import java.lang.reflect.Method JavaDoc;
36
37 /**
38  * Comment
39  *
40  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
41  * @version $Revision: 39449 $
42  *
43  **/

44 public class ReflectToJavassist
45 {
46    public static CtClass classToJavassist(Class JavaDoc clazz) throws NotFoundException
47    {
48       ClassPool pool = AspectManager.instance().findClassPool(clazz.getClassLoader());
49       CtClass ct = pool.get(clazz.getName());
50       return ct;
51    }
52    
53    public static CtField fieldToJavassist(Field field) throws NotFoundException
54    {
55       Class JavaDoc clazz = field.getDeclaringClass();
56       return classToJavassist(field.getDeclaringClass()).getField(field.getName());
57    }
58    public static CtConstructor constructorToJavassist(Constructor con) throws NotFoundException
59    {
60       Class JavaDoc clazz = con.getDeclaringClass();
61       Class JavaDoc[] params = con.getParameterTypes();
62       String JavaDoc[] strParams = new String JavaDoc[params.length];
63       for (int i = 0; i < params.length; i++)
64       {
65          strParams[i] = simpleType(params[i]);
66       }
67       CtClass ct = classToJavassist(clazz);
68
69       CtConstructor[] methods = ct.getDeclaredConstructors();
70       for (int i = 0; i < methods.length; i++)
71       {
72          CtClass[] ctParams = methods[i].getParameterTypes();
73          if (ctParams.length == params.length)
74          {
75             boolean found = true;
76             for (int j = 0; j < ctParams.length; j++)
77             {
78                if (!ctParams[j].getName().equals(strParams[j]))
79                {
80                   found = false;
81                   break;
82                }
83             }
84             if (found) return methods[i];
85          }
86       }
87       return null;
88    }
89
90    protected static String JavaDoc simpleType(Class JavaDoc type)
91    {
92       Class JavaDoc ret = type;
93       if (ret.isArray())
94       {
95          Class JavaDoc arr = ret;
96          String JavaDoc array = "";
97          while (arr.isArray())
98          {
99             array += "[]";
100             arr = arr.getComponentType();
101          }
102          return arr.getName() + array;
103       }
104       return ret.getName();
105    }
106    
107    public static CtMethod methodToJavassist(Method method) throws NotFoundException
108    {
109       Class JavaDoc clazz = method.getDeclaringClass();
110       Class JavaDoc[] params = method.getParameterTypes();
111       String JavaDoc[] strParams = new String JavaDoc[params.length];
112       for (int i = 0; i < params.length; i++)
113       {
114          strParams[i] = simpleType(params[i]);
115       }
116       CtClass ct = classToJavassist(clazz);
117
118       while (!ct.getName().equals(clazz.getName()))
119       {
120          ct = ct.getSuperclass();
121       }
122       CtMethod[] methods = ct.getDeclaredMethods();
123       //String state = "LOOPING THROUGH CtMethods";
124
for (int i = 0; i < methods.length; i++)
125       {
126          if (methods[i].getName().equals(method.getName()))
127          {
128             //state = "FOUND METHOD OF SAME NAME: " + method.toString();
129
CtClass[] ctParams = methods[i].getParameterTypes();
130             if (ctParams.length == params.length)
131             {
132                boolean found = true;
133                for (int j = 0; j < ctParams.length; j++)
134                {
135                   if (!ctParams[j].getName().equals(strParams[j]))
136                   {
137                      //state ="**** method " + method.getName() + " ctParams: '" + ctParams[j].getName() + "' != " + "'" + strParams[j] + "'";
138
found = false;
139                      break;
140                   }
141                }
142                if (found) return methods[i];
143             }
144          }
145       }
146       //throw new Exception("failed to find method: " + method.toString() + " state: " + state);
147
return null;
148    }
149
150 }
151
Popular Tags