KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > reflect > plugins > javassist > JavassistTypeInfoFactoryImpl


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.reflect.plugins.javassist;
23
24 import java.lang.annotation.Annotation JavaDoc;
25
26 import javassist.ClassPool;
27 import javassist.CtClass;
28 import javassist.CtMember;
29 import javassist.CtMethod;
30 import javassist.CtPrimitiveType;
31 import javassist.NotFoundException;
32
33 import org.jboss.reflect.plugins.AnnotationAttributeImpl;
34 import org.jboss.reflect.plugins.AnnotationHelper;
35 import org.jboss.reflect.plugins.AnnotationValueFactory;
36 import org.jboss.reflect.plugins.AnnotationValueImpl;
37 import org.jboss.reflect.spi.AnnotationInfo;
38 import org.jboss.reflect.spi.AnnotationValue;
39 import org.jboss.reflect.spi.PrimitiveInfo;
40 import org.jboss.reflect.spi.TypeInfo;
41 import org.jboss.reflect.spi.TypeInfoFactory;
42 import org.jboss.util.JBossStringBuilder;
43 import org.jboss.util.collection.WeakClassCache;
44
45 /**
46  * A javassist type factory.
47  *
48  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
49  */

50 public class JavassistTypeInfoFactoryImpl extends WeakClassCache implements TypeInfoFactory, AnnotationHelper
51 {
52    static final ClassPool pool = ClassPool.getDefault();
53
54    static final AnnotationValue[] NO_ANNOTATIONS = new AnnotationValue[0];
55    /**
56     * Raise NoClassDefFoundError for javassist not found
57     *
58     * @param name the name
59     * @param e the not found error
60     * @return never
61     * @throws NoClassDefFoundError always
62     */

63    public static NoClassDefFoundError JavaDoc raiseClassNotFound(String JavaDoc name, NotFoundException e) throws NoClassDefFoundError JavaDoc
64    {
65       NoClassDefFoundError JavaDoc ex = new NoClassDefFoundError JavaDoc("Unable to find class");
66       if (e.getCause() != null)
67          ex.initCause(e.getCause()); // Hide the javassist error
68
throw ex;
69    }
70
71    /**
72     * Raise NoClassDefFoundError for javassist not found
73     *
74     * @param name the name
75     * @param e the not found error
76     * @return never
77     * @throws NoClassDefFoundError always
78     */

79    public static NoClassDefFoundError JavaDoc raiseClassNotFound(String JavaDoc name, ClassNotFoundException JavaDoc e) throws NoClassDefFoundError JavaDoc
80    {
81       NoClassDefFoundError JavaDoc ex = new NoClassDefFoundError JavaDoc("Unable to find class");
82       ex.initCause(e);
83       throw ex;
84    }
85
86    /**
87     * Raise NoClassDefFoundError for javassist not found
88     *
89     * @param name the name
90     * @param e the not found error
91     * @return never
92     * @throws NoClassDefFoundError always
93     */

94    public static NoClassDefFoundError JavaDoc raiseMethodNotFound(String JavaDoc name, NotFoundException e) throws NoClassDefFoundError JavaDoc
95    {
96       NoSuchMethodError JavaDoc ex = new NoSuchMethodError JavaDoc("Unable to find method " + name);
97       if (e.getCause() != null)
98          ex.initCause(e.getCause()); // Hide the javassist error
99
throw ex;
100    }
101
102    /**
103     * Raise NoClassDefFoundError for javassist not found
104     *
105     * @param name the name
106     * @param e the not found error
107     * @return never
108     * @throws NoClassDefFoundError always
109     */

110    public static NoClassDefFoundError JavaDoc raiseFieldNotFound(String JavaDoc name, NotFoundException e) throws NoClassDefFoundError JavaDoc
111    {
112       NoSuchFieldError JavaDoc ex = new NoSuchFieldError JavaDoc("Unable to find field");
113       if (e.getCause() != null)
114          ex.initCause(e.getCause()); // Hide the javassist error
115
throw ex;
116    }
117
118    @SuppressWarnings JavaDoc("unchecked")
119    protected Object JavaDoc instantiate(Class JavaDoc clazz)
120    {
121       try
122       {
123          CtClass ctClass = getCtClass(clazz.getName());
124
125          if (clazz.isArray())
126          {
127             TypeInfo componentType = getTypeInfo(clazz.getComponentType());
128             return new JavassistArrayInfoImpl(componentType);
129          }
130
131          if (ctClass.isAnnotation())
132          {
133             JavassistAnnotationInfo result = new JavassistAnnotationInfo(this, ctClass, clazz);
134             CtMethod[] methods = ctClass.getDeclaredMethods();
135             AnnotationAttributeImpl[] atttributes = new AnnotationAttributeImpl[methods.length];
136             for (int i = 0 ; i < methods.length ; i++)
137             {
138                atttributes[i] = new AnnotationAttributeImpl(methods[i].getName(), getTypeInfo(methods[i].getReturnType()), null);
139             }
140             result.setAttributes(atttributes);
141             return result;
142
143          }
144          else if (ctClass.isEnum())
145          {
146             return new JavassistEnumInfo(this, ctClass, clazz);
147          }
148
149          
150          return new JavassistTypeInfo(this, ctClass, clazz);
151       }
152       catch (NotFoundException e)
153       {
154          throw new RuntimeException JavaDoc(e);
155       }
156    }
157
158    /**
159     * Get the type info
160     *
161     * @param ctClass the ctClass
162     * @return the typeinfo
163     */

164    protected TypeInfo getTypeInfo(CtClass ctClass)
165    {
166       try
167       {
168          String JavaDoc name = convertName(ctClass);
169          return getTypeInfo(name, null);
170       }
171       catch (ClassNotFoundException JavaDoc e)
172       {
173          throw raiseClassNotFound(ctClass.getName(), e);
174       }
175    }
176
177    /**
178     * Convert name
179     *
180     * @param clazz the class
181     * @return the converted name
182     */

183    protected String JavaDoc convertName(CtClass clazz)
184    {
185       CtClass temp = clazz;
186       if (temp.isArray())
187       {
188          JBossStringBuilder buffer = new JBossStringBuilder();
189          try
190          {
191             while (temp.isArray())
192             {
193                buffer.append('[');
194                temp = temp.getComponentType();
195             }
196             if (temp.isPrimitive())
197             {
198                CtPrimitiveType primitive = (CtPrimitiveType) temp;
199                buffer.append(Character.toString(primitive.getDescriptor()));
200             }
201             else
202             {
203                buffer.append('L');
204                buffer.append(temp.getName());
205                buffer.append(';');
206             }
207             return buffer.toString();
208          }
209          catch (NotFoundException e)
210          {
211             throw raiseClassNotFound(clazz.getName(), e);
212          }
213       }
214       return clazz.getName();
215    }
216
217    /**
218     * Get the CtClass
219     *
220     * @param name the name
221     * @return the CtClass
222     */

223    protected CtClass getCtClass(String JavaDoc name)
224    {
225       try
226       {
227          return pool.get(name);
228       }
229       catch (NotFoundException e)
230       {
231          throw raiseClassNotFound(name, e);
232       }
233    }
234
235    protected void generate(Class JavaDoc clazz, Object JavaDoc result)
236    {
237       // Everything is done lazily
238
}
239
240    public TypeInfo getTypeInfo(Class JavaDoc clazz)
241    {
242       if (clazz == null)
243          throw new IllegalArgumentException JavaDoc("Null class");
244
245       TypeInfo primitive = PrimitiveInfo.valueOf(clazz.getName());
246       if (primitive != null)
247          return primitive;
248
249       return (TypeInfo) get(clazz);
250    }
251
252    public TypeInfo getTypeInfo(String JavaDoc name, ClassLoader JavaDoc cl) throws ClassNotFoundException JavaDoc
253    {
254       if (name == null)
255          throw new IllegalArgumentException JavaDoc("Null class name");
256       if (cl == null)
257          cl = Thread.currentThread().getContextClassLoader();
258
259       TypeInfo primitive = PrimitiveInfo.valueOf(name);
260       if (primitive != null)
261          return primitive;
262
263       Class JavaDoc clazz = cl.loadClass(name);
264       return getTypeInfo(clazz);
265    }
266    
267    public AnnotationValue[] getAnnotations(Object JavaDoc obj)
268    {
269       try
270       {
271          Object JavaDoc[] annotations;
272          if (obj instanceof CtMember)
273          {
274             annotations = ((CtMember)obj).getAvailableAnnotations();
275          }
276          else if (obj instanceof CtClass)
277          {
278             annotations = ((CtClass)obj).getAvailableAnnotations();
279          }
280          else
281          {
282             throw new RuntimeException JavaDoc("Attempt was made to read annotations from unsupported type " + obj.getClass().getName() + ": " + obj);
283          }
284
285          if (annotations.length == 0)
286          {
287             return NO_ANNOTATIONS;
288          }
289
290          AnnotationValue[] annotationValues = new AnnotationValueImpl[annotations.length];
291          for (int i = 0 ; i < annotations.length ; i++)
292          {
293             Class JavaDoc clazz = ((Annotation JavaDoc)annotations[i]).annotationType();
294             
295             AnnotationInfo info = (AnnotationInfo)getTypeInfo(clazz);
296             annotationValues[i] = AnnotationValueFactory.createAnnotationValue(this, this, info, annotations[i]);
297          }
298          return annotationValues;
299       }
300       catch (ClassNotFoundException JavaDoc e)
301       {
302          throw new RuntimeException JavaDoc(e);
303       }
304       catch (Throwable JavaDoc t)
305       {
306          throw new RuntimeException JavaDoc(t);
307       }
308    }
309
310    public AnnotationValue createAnnotationValue(AnnotationInfo info, Object JavaDoc ann)
311    {
312       return AnnotationValueFactory.createAnnotationValue(this, this, info, ann);
313    }
314    
315 }
316
Popular Tags