KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jboss.aop.AspectManager;
25
26 import javassist.CtBehavior;
27 import javassist.CtClass;
28 import javassist.CtMethod;
29 import javassist.NotFoundException;
30
31 /**
32  * Comment
33  *
34  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
35  * @version $Revision$
36  */

37 public class JavassistToReflect
38 {
39    public static String JavaDoc getParameterClassArrayString(String JavaDoc arrayName, CtBehavior behavior)throws NotFoundException
40    {
41       CtClass[] params = behavior.getParameterTypes();
42       return getParameterClassArrayString(params, arrayName);
43    }
44    
45    public static String JavaDoc getParameterClassArrayString(CtClass[] params, String JavaDoc arrayName) throws NotFoundException
46    {
47       StringBuffer JavaDoc arr = new StringBuffer JavaDoc(" new java.lang.Class[" + params.length + "];");
48       
49       for (int i = 0 ; i < params.length ; i++)
50       {
51          arr.append(arrayName + "["+ i + "] = ");
52          
53          if (params[i].isArray())
54          {
55             arr.append(getClassObjectString(params[i], true));
56          }
57          else
58          {
59             arr.append(getClassObjectString(params[i]));
60          }
61          arr.append(";");
62       }
63       return arr.toString();
64    }
65    
66    public static String JavaDoc getClassObjectString(CtClass clazz)
67    {
68       //Cavaj crashes when trying to use bytecode using stuff like
69
// POJO.class
70
//so get hold of the class from its name here
71
return getClassObjectString(clazz, false);
72    }
73    
74    public static String JavaDoc getClassObjectString(CtClass clazz, boolean isArray)
75    {
76       //Cavaj crashes when trying to use bytecode using stuff like
77
// POJO.class
78
//so get hold of the class from its name here if we are in debug mode
79

80       if (!isArray)
81       {
82          if (clazz.isPrimitive())
83          {
84             return getClassName(clazz);
85          }
86          else
87          {
88             return "java.lang.Class.forName(\"" + getClassName(clazz) + "\")";
89          }
90       }
91       else
92       {
93          //Array initialisers are not supported
94
if (AspectManager.debugClasses)
95          {
96             return "new " + getArrayClassName(clazz) + "[0].getClass()";
97          }
98          else
99          {
100             return getArrayClassName(clazz) + "[].class";
101          }
102       }
103    }
104    
105    public static String JavaDoc getArrayClassName(CtClass clazz)
106    {
107          String JavaDoc name = clazz.getName();
108          int ind = name.indexOf("[");
109          return name.substring(0, ind);
110    }
111
112    public static String JavaDoc getClassName(CtClass clazz)
113    {
114       if (!clazz.isPrimitive())
115       {
116          return clazz.getName();
117       }
118       else
119       {
120          if (clazz == CtClass.booleanType) return "java.lang.Boolean.TYPE";
121          else if (clazz == CtClass.byteType) return "java.lang.Byte.TYPE";
122          else if (clazz == CtClass.charType) return "java.lang.Character.TYPE";
123          else if (clazz == CtClass.doubleType)return "java.lang.Double.TYPE";
124          else if (clazz == CtClass.floatType) return "java.lang.Float.TYPE";
125          else if (clazz == CtClass.intType) return "java.lang.Integer.TYPE";
126          else if (clazz == CtClass.longType) return "java.lang.Long.TYPE";
127          else if (clazz == CtClass.shortType) return "java.lang.Short.TYPE";
128          else throw new RuntimeException JavaDoc("Unsupported type " + clazz.getName());
129       }
130    }
131    
132    public static String JavaDoc castInvocationValueToTypeString(CtClass type)
133    {
134       return castInvocationValueToTypeString(type, "value");
135    }
136
137    public static String JavaDoc castInvocationValueToTypeString(CtClass type, String JavaDoc valueName)
138    {
139       String JavaDoc cast = null;
140       if (type.isPrimitive())
141       {
142          if (type.equals(CtClass.booleanType))
143          {
144             cast = "((Boolean)" + valueName +").booleanValue();";
145          }
146          else if (type.equals(CtClass.byteType))
147          {
148             cast = "((Byte)" + valueName +").byteValue();";
149          }
150          else if (type.equals(CtClass.charType))
151          {
152             cast = "((Character)" + valueName +").charValue();";
153          }
154          else if (type.equals(CtClass.doubleType))
155          {
156             cast = "((Double)" + valueName +").doubleValue();";
157          }
158          else if (type.equals(CtClass.floatType))
159          {
160             cast = "((Float)" + valueName +").floatValue();";
161          }
162          else if (type.equals(CtClass.intType))
163          {
164             cast = "((Integer)" + valueName +").intValue();";
165          }
166          else if (type.equals(CtClass.longType))
167          {
168             cast = "((Long)" + valueName +").longValue();";
169          }
170          else if (type.equals(CtClass.shortType))
171          {
172             cast = "((Short)" + valueName +").shortValue();";
173          }
174       }
175       else if (type.isArray())
176       {
177          cast = "(" + type.getName() + ")" + valueName +";";
178       }
179       else
180       {
181          cast = "(" + type.getName() + ")" + valueName +";";
182       }
183
184       return cast;
185    }
186    
187 }
188
Popular Tags