KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > reflect > plugins > ClassInfoImpl


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;
23
24 import java.lang.reflect.Array JavaDoc;
25 import java.lang.reflect.Modifier JavaDoc;
26 import java.util.HashMap JavaDoc;
27
28 import org.jboss.reflect.spi.AnnotationValue;
29 import org.jboss.reflect.spi.ClassInfo;
30 import org.jboss.reflect.spi.ConstructorInfo;
31 import org.jboss.reflect.spi.FieldInfo;
32 import org.jboss.reflect.spi.InterfaceInfo;
33 import org.jboss.reflect.spi.MethodInfo;
34 import org.jboss.reflect.spi.TypeInfo;
35 import org.jboss.util.JBossStringBuilder;
36 import org.jboss.util.UnreachableStatementException;
37
38 /**
39  * Class info
40  *
41  * @todo fix the introspection assumption
42  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
43  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
44  */

45 public class ClassInfoImpl extends InheritableAnnotationHolder implements ClassInfo
46 {
47    /** serialVersionUID */
48    private static final long serialVersionUID = 3545798779904340792L;
49
50    /** Marker for generation */
51    static final ClassInfo UNKNOWN_CLASS = new UnknownClassInfo();
52
53    /** Marker for generation */
54    static final InterfaceInfo[] UNKNOWN_INTERFACES = new InterfaceInfo[0];
55
56    /** Marker for generation */
57    static final ConstructorInfo[] UNKNOWN_CONSTRUCTORS = new ConstructorInfo[0];
58
59    /** Marker for generation */
60    static final MethodInfo[] UNKNOWN_METHODS = new MethodInfo[0];
61
62    /** Marker for generation */
63    private static final FieldInfo[] UNKNOWN_FIELDS = new FieldInfo[0];
64    
65    /** The class name */
66    protected String JavaDoc name;
67    
68    /** The class modifiers */
69    protected int modifiers;
70    
71    /** The interfaces */
72    protected InterfaceInfo[] interfaces = UNKNOWN_INTERFACES;
73    
74    /** The methods */
75    protected MethodInfo[] methods = UNKNOWN_METHODS;
76    
77    /** The fields */
78    protected FieldInfo[] fields = UNKNOWN_FIELDS;
79    
80    /** Field map Map<String, FieldInfo> */
81    protected HashMap JavaDoc fieldMap;
82
83    /** The super class */
84    protected ClassInfo superclass = UNKNOWN_CLASS;
85
86    /** The constructor info */
87    protected ConstructorInfo[] constructors = UNKNOWN_CONSTRUCTORS;
88
89    /** The class info helper */
90    protected ClassInfoHelper classInfoHelper;
91    
92    /**
93     * Find a method
94     *
95     * @param methods the methods
96     * @param name the name
97     * @param parameters the parameters
98     * @return the method info
99     */

100    public static MethodInfo findMethod(MethodInfo[] methods, String JavaDoc name, TypeInfo[] parameters)
101    {
102       if (methods == null) return null;
103       for (int i = 0; i < methods.length; i++)
104       {
105          if (methods[i].getName().equals(name))
106          {
107             final int length = (parameters != null) ? parameters.length : 0;
108             if (methods[i].getParameterTypes().length == length)
109             {
110                boolean ok = true;
111                for (int j = 0; j < length; j++)
112                {
113                   if (!parameters[j].equals(methods[i].getParameterTypes()[j]))
114                   {
115                      ok = false;
116                      break;
117                   }
118                }
119                if (ok) return methods[i];
120             }
121          }
122       }
123       return null;
124    }
125
126    /**
127     * Get an array class
128     *
129     * @todo there must be a better way to do this!
130     * @param clazz the class
131     * @param depth the depth
132     * @return the array class
133     */

134    public static Class JavaDoc getArrayClass(Class JavaDoc clazz, int depth)
135    {
136       return Array.newInstance(clazz, depth).getClass();
137    }
138    
139    /**
140     * Create a new abstract ClassInfo.
141     */

142    public ClassInfoImpl()
143    {
144    }
145
146    /**
147     * Create a new class info
148     *
149     * @param name the class name
150     */

151    public ClassInfoImpl(String JavaDoc name)
152    {
153       this.name = name;
154    }
155
156    /**
157     * Create a new abstract ClassInfo.
158     *
159     * @param name the class name
160     * @param modifiers the class modifiers
161     * @param interfaces the interfaces
162     * @param superclass the super class
163     */

164    public ClassInfoImpl(String JavaDoc name, int modifiers, InterfaceInfo[] interfaces,
165                         ClassInfoImpl superclass)
166    {
167       this.name = name;
168       this.modifiers = modifiers;
169       this.interfaces = interfaces;
170       this.superclass = superclass;
171    }
172
173    /**
174     * Set the class info helper
175     *
176     * @param helper the helper
177     */

178    public void setClassInfoHelper(ClassInfoHelper helper)
179    {
180       this.classInfoHelper = helper;
181    }
182    
183    /**
184     * Set the type
185     *
186     * @param type the class
187     */

188    public void setType(Class JavaDoc type)
189    {
190       setAnnotatedElement(type);
191    }
192    
193    /**
194     * Set the interfaces
195     *
196     * @param interfaces the interfaces
197     */

198    public void setInterfaces(InterfaceInfo[] interfaces)
199    {
200       this.interfaces = interfaces;
201    }
202
203    /**
204     * Set the declared methods
205     *
206     * @param methods the methods
207     */

208    public void setDeclaredMethods(MethodInfoImpl[] methods)
209    {
210       this.methods = methods;
211       if (methods != null)
212       {
213          for (int i = 0; i < methods.length; i++)
214             methods[i].declaringClass = this;
215       }
216    }
217
218    /**
219     * Set the declared fields
220     *
221     * @param fields the fields
222     */

223    public void setDeclaredFields(FieldInfoImpl[] fields)
224    {
225       this.fields = fields;
226       if (fields != null)
227       {
228          for (int i = 0; i < fields.length; i++)
229             fields[i].declaringClass = this;
230       }
231    }
232
233    /**
234     * Set the declared constructors
235     *
236     * @param constructors the constructors
237     */

238    public void setDeclaredConstructors(ConstructorInfoImpl[] constructors)
239    {
240       this.constructors = constructors;
241       if (constructors != null)
242       {
243          for (int i = 0; i < constructors.length; i++)
244             constructors[i].declaringClass = this;
245       }
246    }
247
248    /**
249     * Set the super class
250     *
251     * @param superInfo the super class
252     */

253    public void setSuperclass(ClassInfoImpl superInfo)
254    {
255       this.superclass = superInfo;
256    }
257
258
259    public boolean isInterface()
260    {
261       return false;
262    }
263
264    public InterfaceInfo[] getInterfaces()
265    {
266       if (interfaces == UNKNOWN_INTERFACES)
267          setInterfaces(classInfoHelper.getInterfaces(this));
268       return interfaces;
269    }
270    
271    public MethodInfo getDeclaredMethod(String JavaDoc name, TypeInfo[] parameters)
272    {
273       if (methods == UNKNOWN_METHODS)
274          setDeclaredMethods(classInfoHelper.getMethods(this));
275       return findMethod(methods, name, parameters);
276    }
277
278    public MethodInfo[] getDeclaredMethods()
279    {
280       if (methods == UNKNOWN_METHODS)
281          setDeclaredMethods(classInfoHelper.getMethods(this));
282       return methods;
283    }
284
285    public FieldInfo getDeclaredField(String JavaDoc name)
286    {
287       if (fields == UNKNOWN_FIELDS)
288          setDeclaredFields(classInfoHelper.getFields(this));
289       return (FieldInfo) fieldMap.get(name);
290    }
291
292    public FieldInfo[] getDeclaredFields()
293    {
294       if (fields == UNKNOWN_FIELDS)
295          setDeclaredFields(classInfoHelper.getFields(this));
296       return fields;
297    }
298
299    public ConstructorInfo[] getDeclaredConstructors()
300    {
301       if (constructors == UNKNOWN_CONSTRUCTORS)
302          setDeclaredConstructors(classInfoHelper.getConstructors(this));
303       return constructors;
304    }
305
306    public ClassInfo getSuperclass()
307    {
308       if (superclass == UNKNOWN_CLASS)
309          setSuperclass(classInfoHelper.getSuperClass(this));
310       return superclass;
311    }
312    
313    public int getModifiers()
314    {
315       return modifiers;
316    }
317    
318    public boolean isStatic()
319    {
320       return Modifier.isStatic(modifiers);
321    }
322    
323    public boolean isPublic()
324    {
325       return Modifier.isPublic(modifiers);
326    }
327
328    public String JavaDoc getName()
329    {
330       return name;
331    }
332
333    public Class JavaDoc<? extends Object JavaDoc> getType()
334    {
335       return (Class JavaDoc<? extends Object JavaDoc>) annotatedElement;
336    }
337    
338    public Object JavaDoc convertValue(Object JavaDoc value) throws Throwable JavaDoc
339    {
340       return ValueConvertor.convertValue(getType(), value);
341    }
342
343    public boolean isArray()
344    {
345       return getType().isArray();
346    }
347
348    public TypeInfo getArrayType(int depth)
349    {
350       Class JavaDoc arrayClass = getArrayClass(getType(), depth);
351       return classInfoHelper.getTypeInfo(arrayClass);
352    }
353
354    public Object JavaDoc[] newArrayInstance(int size) throws Throwable JavaDoc
355    {
356       Class JavaDoc clazz = getType();
357       if (clazz.isArray() == false)
358          throw new ClassCastException JavaDoc(clazz + " is not an array.");
359       return (Object JavaDoc[]) Array.newInstance(clazz.getComponentType(), size);
360    }
361
362    protected InheritableAnnotationHolder getSuperHolder()
363    {
364       return (ClassInfoImpl) getSuperclass();
365    }
366    
367    protected void toString(JBossStringBuilder buffer)
368    {
369       buffer.append("name=").append(getName());
370    }
371
372    public boolean equals(Object JavaDoc obj)
373    {
374       if (this == obj)
375          return true;
376       if (obj == null || obj instanceof ClassInfo == false)
377          return false;
378
379       final ClassInfo other = (ClassInfo) obj;
380
381       if (name != null ? name.equals(other.getName()) == false : other.getName() != null)
382          return false;
383       return true;
384    }
385
386    public int hashCode()
387    {
388       return (name != null ? name.hashCode() : 0);
389    }
390    
391    static class UnknownClassInfo implements ClassInfo
392    {
393       public ConstructorInfo[] getDeclaredConstructors()
394       {
395          throw new UnreachableStatementException();
396       }
397
398       public FieldInfo getDeclaredField(String JavaDoc name)
399       {
400          throw new UnreachableStatementException();
401       }
402
403       public FieldInfo[] getDeclaredFields()
404       {
405          throw new UnreachableStatementException();
406       }
407
408       public MethodInfo getDeclaredMethod(String JavaDoc name, TypeInfo[] parameters)
409       {
410          throw new UnreachableStatementException();
411       }
412
413       public MethodInfo[] getDeclaredMethods()
414       {
415          throw new UnreachableStatementException();
416       }
417
418       public InterfaceInfo[] getInterfaces()
419       {
420          throw new UnreachableStatementException();
421       }
422
423       public String JavaDoc getName()
424       {
425          throw new UnreachableStatementException();
426       }
427
428       public ClassInfo getSuperclass()
429       {
430          throw new UnreachableStatementException();
431       }
432
433       public boolean isInterface()
434       {
435          throw new UnreachableStatementException();
436       }
437
438       public AnnotationValue getAnnotation(String JavaDoc name)
439       {
440          throw new UnreachableStatementException();
441       }
442
443       public AnnotationValue[] getAnnotations()
444       {
445          throw new UnreachableStatementException();
446       }
447
448       public boolean isAnnotationPresent(String JavaDoc name)
449       {
450          throw new UnreachableStatementException();
451       }
452
453       public String JavaDoc toShortString()
454       {
455          throw new UnreachableStatementException();
456       }
457
458       public void toShortString(JBossStringBuilder buffer)
459       {
460          throw new UnreachableStatementException();
461       }
462
463       public Class JavaDoc getType()
464       {
465          throw new UnreachableStatementException();
466       }
467
468       public Object JavaDoc convertValue(Object JavaDoc value) throws Throwable JavaDoc
469       {
470          throw new UnreachableStatementException();
471       }
472
473       public TypeInfo getArrayType(int depth)
474       {
475          throw new UnreachableStatementException();
476       }
477
478       public boolean isArray()
479       {
480          throw new UnreachableStatementException();
481       }
482
483       public Object JavaDoc[] newArrayInstance(int size) throws Throwable JavaDoc
484       {
485          throw new UnreachableStatementException();
486       }
487
488       public int getModifiers()
489       {
490          throw new UnreachableStatementException();
491       }
492
493       public boolean isPublic()
494       {
495          throw new UnreachableStatementException();
496       }
497
498       public boolean isStatic()
499       {
500          throw new UnreachableStatementException();
501       }
502       
503       public Object JavaDoc clone()
504       {
505          throw new UnreachableStatementException();
506       }
507    }
508 }
509
Popular Tags