KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > reflect > plugins > introspection > IntrospectionTypeInfoFactoryImpl


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.introspection;
23
24 import java.lang.annotation.Annotation JavaDoc;
25 import java.lang.reflect.AccessibleObject JavaDoc;
26 import java.lang.reflect.Constructor JavaDoc;
27 import java.lang.reflect.Field JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import java.security.AccessController JavaDoc;
30 import java.security.PrivilegedAction JavaDoc;
31
32 import org.jboss.reflect.plugins.AnnotationAttributeImpl;
33 import org.jboss.reflect.plugins.AnnotationHelper;
34 import org.jboss.reflect.plugins.AnnotationInfoImpl;
35 import org.jboss.reflect.plugins.AnnotationValueFactory;
36 import org.jboss.reflect.plugins.ArrayInfoImpl;
37 import org.jboss.reflect.plugins.ClassInfoHelper;
38 import org.jboss.reflect.plugins.ClassInfoImpl;
39 import org.jboss.reflect.plugins.ConstructorInfoImpl;
40 import org.jboss.reflect.plugins.EnumInfoImpl;
41 import org.jboss.reflect.plugins.FieldInfoImpl;
42 import org.jboss.reflect.plugins.MethodInfoImpl;
43 import org.jboss.reflect.spi.AnnotationInfo;
44 import org.jboss.reflect.spi.AnnotationValue;
45 import org.jboss.reflect.spi.ClassInfo;
46 import org.jboss.reflect.spi.InterfaceInfo;
47 import org.jboss.reflect.spi.PrimitiveInfo;
48 import org.jboss.reflect.spi.TypeInfo;
49 import org.jboss.reflect.spi.TypeInfoFactory;
50 import org.jboss.util.collection.WeakClassCache;
51
52 /**
53  * An introspection type factory.
54  *
55  * FIXME: use lazy loading to avoid reading the entire class model
56  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
57  */

58 public class IntrospectionTypeInfoFactoryImpl extends WeakClassCache implements TypeInfoFactory, AnnotationHelper, ClassInfoHelper
59 {
60    final static AnnotationValue[] NO_ANNOTATIONS = new AnnotationValue[0];
61
62    /**
63     * Generate the type info for a class
64     *
65     * @param clazz the class
66     * @param info the class info
67     */

68    public void generateTypeInfo(Class JavaDoc clazz, ClassInfoImpl info)
69    {
70       // Everything is done lazily
71
}
72
73    public ClassInfoImpl getSuperClass(ClassInfoImpl classInfo)
74    {
75       Class JavaDoc clazz = classInfo.getType();
76       ClassInfoImpl superType = null;
77       if (clazz.isInterface() == false)
78       {
79          Class JavaDoc superClazz = clazz.getSuperclass();
80          if (superClazz != null)
81             superType = (ClassInfoImpl) getTypeInfo(superClazz);
82       }
83       return superType;
84    }
85
86    public AnnotationValue[] getAnnotations(Object JavaDoc obj)
87    {
88       Annotation JavaDoc[] annotations;
89       if (obj instanceof AccessibleObject JavaDoc)
90       {
91          annotations = readAnnotations((AccessibleObject JavaDoc)obj);
92       }
93       else if (obj instanceof Class JavaDoc)
94       {
95          annotations = readAnnotations((Class JavaDoc)obj);
96       }
97       else
98       {
99          throw new RuntimeException JavaDoc("Attempt was made to read annotations from unsupported type " + obj.getClass().getName() + ": " + obj);
100       }
101
102       if (annotations.length == 0)
103       {
104          return NO_ANNOTATIONS;
105       }
106
107       AnnotationValue[] annotationValues = new AnnotationValue[annotations.length];
108       for (int i = 0 ; i < annotations.length ; i++)
109       {
110          AnnotationInfo info = (AnnotationInfo)getTypeInfo(annotations[i].annotationType());
111          annotationValues[i] = createAnnotationValue(info, annotations[i]);
112       }
113       return annotationValues;
114    }
115
116    public AnnotationValue createAnnotationValue(AnnotationInfo info, Object JavaDoc ann)
117    {
118       return AnnotationValueFactory.createAnnotationValue(this, this, info, ann);
119    }
120
121    public ConstructorInfoImpl[] getConstructors(ClassInfoImpl classInfo)
122    {
123       Class JavaDoc clazz = classInfo.getType();
124       ReflectConstructorInfoImpl[] infos = null;
125       if (clazz.isInterface() == false)
126       {
127          Constructor JavaDoc[] constructors = getDeclaredConstructors(clazz);
128          if (constructors != null && constructors.length > 0)
129          {
130             infos = new ReflectConstructorInfoImpl[constructors.length];
131             for (int i = 0; i < constructors.length; ++i)
132             {
133                AnnotationValue[] annotations = getAnnotations(constructors[i]);
134                infos[i] = new ReflectConstructorInfoImpl(annotations, getTypeInfos(constructors[i].getParameterTypes()), getParameterAnnotations(constructors[i].getParameterAnnotations()), getClassInfos(constructors[i].getExceptionTypes()), constructors[i].getModifiers(), (ClassInfo) getTypeInfo(constructors[i].getDeclaringClass()));
135                infos[i].setConstructor(constructors[i]);
136             }
137          }
138       }
139       return infos;
140    }
141
142    public FieldInfoImpl[] getFields(ClassInfoImpl classInfo)
143    {
144       Class JavaDoc clazz = classInfo.getType();
145       Field JavaDoc[] fields = getDeclaredFields(clazz);
146       if (fields == null || fields.length == 0)
147          return null;
148
149       ReflectFieldInfoImpl[] infos = new ReflectFieldInfoImpl[fields.length];
150       for (int i = 0; i < fields.length; ++i)
151       {
152          AnnotationValue[] annotations = getAnnotations(fields[i]);
153          infos[i] = new ReflectFieldInfoImpl(annotations, fields[i].getName(), getTypeInfo(fields[i].getType()), fields[i].getModifiers(), (ClassInfo) getTypeInfo(fields[i].getDeclaringClass()));
154          infos[i].setField(fields[i]);
155       }
156       return infos;
157    }
158
159    public MethodInfoImpl[] getMethods(ClassInfoImpl classInfo)
160    {
161       Class JavaDoc clazz = classInfo.getType();
162       Method JavaDoc[] methods = getDeclaredMethods(clazz);
163       if (methods == null || methods.length == 0)
164          return null;
165
166       ReflectMethodInfoImpl[] infos = new ReflectMethodInfoImpl[methods.length];
167       for (int i = 0; i < methods.length; ++i)
168       {
169          AnnotationValue[] annotations = getAnnotations(methods[i]);
170          infos[i] = new ReflectMethodInfoImpl(annotations, methods[i].getName(), getTypeInfo(methods[i].getReturnType()), getTypeInfos(methods[i].getParameterTypes()), getParameterAnnotations(methods[i].getParameterAnnotations()), getClassInfos(methods[i].getExceptionTypes()), methods[i].getModifiers(), (ClassInfo) getTypeInfo(methods[i].getDeclaringClass()));
171          infos[i].setMethod(methods[i]);
172       }
173       return infos;
174    }
175
176    public InterfaceInfo[] getInterfaces(ClassInfoImpl classInfo)
177    {
178       Class JavaDoc clazz = classInfo.getType();
179       Class JavaDoc[] interfaces = clazz.getInterfaces();
180       if (interfaces == null || interfaces.length == 0)
181          return null;
182
183       InterfaceInfo[] infos = new InterfaceInfo[interfaces.length];
184       for (int i = 0; i < interfaces.length; ++i)
185          infos[i] = (InterfaceInfo) getTypeInfo(interfaces[i]);
186
187       return infos;
188    }
189
190    /**
191     * Get the type infos for some classes
192     *
193     * @param classes the classes
194     * @return the type infos
195     */

196    public TypeInfo[] getTypeInfos(Class JavaDoc[] classes)
197    {
198       if (classes == null || classes.length == 0)
199          return null;
200
201       TypeInfo[] result = new TypeInfo[classes.length];
202       for (int i = 0; i < classes.length; ++i)
203          result[i] = getTypeInfo(classes[i]);
204       return result;
205    }
206
207    /**
208     * Get the class infos for some classes
209     *
210     * @param classes the classes
211     * @return the class infos
212     */

213    public ClassInfo[] getClassInfos(Class JavaDoc[] classes)
214    {
215       if (classes == null || classes.length == 0)
216          return null;
217
218       ClassInfo[] result = new ClassInfo[classes.length];
219       for (int i = 0; i < classes.length; ++i)
220          result[i] = (ClassInfo) getTypeInfo(classes[i]);
221       return result;
222    }
223
224    public TypeInfo getTypeInfo(Class JavaDoc clazz)
225    {
226       if (clazz == null)
227          throw new IllegalArgumentException JavaDoc("Null class");
228
229       TypeInfo primitive = PrimitiveInfo.valueOf(clazz.getName());
230       if (primitive != null)
231          return primitive;
232
233       return (TypeInfo) get(clazz);
234    }
235
236    public TypeInfo getTypeInfo(String JavaDoc name, ClassLoader JavaDoc cl) throws ClassNotFoundException JavaDoc
237    {
238       if (name == null)
239          throw new IllegalArgumentException JavaDoc("Null class name");
240       if (cl == null)
241          cl = Thread.currentThread().getContextClassLoader();
242
243       Class JavaDoc clazz = cl.loadClass(name);
244       return getTypeInfo(clazz);
245    }
246
247    protected Object JavaDoc instantiate(Class JavaDoc clazz)
248    {
249       ClassInfoImpl result;
250       if (clazz.isArray())
251       {
252          TypeInfo componentType = getTypeInfo(clazz.getComponentType());
253          result = new ArrayInfoImpl(componentType);
254       }
255       else if (clazz.isEnum())
256       {
257          result = new EnumInfoImpl(clazz.getName(), clazz.getModifiers());
258       }
259       else if (clazz.isAnnotation())
260       {
261          result = new AnnotationInfoImpl(clazz.getName(), clazz.getModifiers());
262          Method JavaDoc[] methods = getDeclaredMethods(clazz);
263          AnnotationAttributeImpl[] atttributes = new AnnotationAttributeImpl[methods.length];
264          for (int i = 0 ; i < methods.length ; i++)
265          {
266             atttributes[i] = new AnnotationAttributeImpl(methods[i].getName(), getTypeInfo(methods[i].getReturnType()), null);
267          }
268          ((AnnotationInfoImpl)result).setAttributes(atttributes);
269       }
270       else if (clazz.isInterface())
271       {
272          result = new ReflectClassInfoImpl(clazz.getName());
273       }
274       else
275       {
276          result = new ReflectClassInfoImpl(clazz.getName());
277       }
278       result.setType(clazz);
279       result.setClassInfoHelper(this);
280       result.setAnnotationHelper(this);
281       return result;
282    }
283
284    protected void generate(Class JavaDoc clazz, Object JavaDoc result)
285    {
286       generateTypeInfo(clazz, (ClassInfoImpl) result);
287    }
288
289    protected Constructor JavaDoc[] getDeclaredConstructors(final Class JavaDoc clazz)
290    {
291       if (System.getSecurityManager() == null)
292          return clazz.getDeclaredConstructors();
293       else
294       {
295          PrivilegedAction JavaDoc<Constructor JavaDoc[]> action = new PrivilegedAction JavaDoc<Constructor JavaDoc[]>()
296          {
297             public Constructor JavaDoc[] run()
298             {
299                return clazz.getDeclaredConstructors();
300             }
301          };
302          return AccessController.doPrivileged(action);
303       }
304    }
305
306    protected Field JavaDoc[] getDeclaredFields(final Class JavaDoc clazz)
307    {
308       if (System.getSecurityManager() == null)
309          return clazz.getDeclaredFields();
310       else
311       {
312          PrivilegedAction JavaDoc<Field JavaDoc[]> action = new PrivilegedAction JavaDoc<Field JavaDoc[]>()
313          {
314             public Field JavaDoc[] run()
315             {
316                return clazz.getDeclaredFields();
317             }
318          };
319          return AccessController.doPrivileged(action);
320       }
321    }
322
323    protected Method JavaDoc[] getDeclaredMethods(final Class JavaDoc clazz)
324    {
325       if (System.getSecurityManager() == null)
326          return clazz.getDeclaredMethods();
327       else
328       {
329          PrivilegedAction JavaDoc<Method JavaDoc[]> action = new PrivilegedAction JavaDoc<Method JavaDoc[]>()
330          {
331             public Method JavaDoc[] run()
332             {
333                return clazz.getDeclaredMethods();
334             }
335          };
336          return AccessController.doPrivileged(action);
337       }
338    }
339
340    private Annotation JavaDoc[] readAnnotations(final AccessibleObject JavaDoc ao)
341    {
342       if (System.getSecurityManager() == null)
343          return ao.getAnnotations();
344       else
345       {
346          PrivilegedAction JavaDoc<Annotation JavaDoc[]> action = new PrivilegedAction JavaDoc<Annotation JavaDoc[]>()
347          {
348             public Annotation JavaDoc[] run()
349             {
350                return ao.getAnnotations();
351             }
352          };
353          
354          return AccessController.doPrivileged(action);
355       }
356    }
357    
358    private Annotation JavaDoc[] readAnnotations(final Class JavaDoc clazz)
359    {
360       if (System.getSecurityManager() == null)
361          return clazz.getAnnotations();
362       else
363       {
364          PrivilegedAction JavaDoc<Annotation JavaDoc[]> action = new PrivilegedAction JavaDoc<Annotation JavaDoc[]>()
365          {
366             public Annotation JavaDoc[] run()
367             {
368                return clazz.getAnnotations();
369             }
370          };
371          return AccessController.doPrivileged(action);
372       }
373    }
374    
375
376    protected AnnotationValue[][] getParameterAnnotations(Annotation JavaDoc[][] annotations)
377    {
378       AnnotationValue[][] annotationValues = new AnnotationValue[annotations.length][];
379       for (int param = 0 ; param < annotations.length ; param++)
380       {
381          annotationValues[param] = new AnnotationValue[annotations[param].length];
382          for (int ann = 0 ; ann < annotations[param].length ; ann++)
383          {
384             AnnotationInfo info = (AnnotationInfo)getTypeInfo(annotations[param][ann].annotationType());
385             annotationValues[param][ann] = createAnnotationValue(info, annotations[param][ann]);
386          }
387       }
388       return annotationValues;
389    }
390
391 }
392
Popular Tags