KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > teatools > TypeDescription


1 /* ====================================================================
2  * TeaTools - Copyright (c) 1997-2000 GO.com
3  * ====================================================================
4  * The Tea Software License, Version 1.0
5  *
6  * Copyright (c) 2000 GO.com. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by GO.com
23  * (http://opensource.go.com/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "GO.com" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact opensource@go.com.
31  *
32  * 5. Products derived from this software may not be called "Tea",
33  * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
34  * "Kettle" or "Trove" appear in their name, without prior written
35  * permission of GO.com.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL GO.COM OR ITS CONTRIBUTORS BE LIABLE
41  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
44  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
45  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
46  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
47  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * For more information about Tea, please see http://opensource.go.com/.
51  */

52
53 package com.go.teatools;
54
55 import com.go.trove.classfile.AccessFlags;
56
57 import java.beans.*;
58 import java.lang.reflect.*;
59
60 /******************************************************************************
61  * Object wrapper for TeaToolsUtils functions that operate on Class objects.
62  * This class offers an O-O interface alternative to the TeaToolsUtils
63  * procedural design.
64  *
65  * @author Mark Masse
66  * @version
67  * <!--$$Revision:--> 3 <!-- $-->, <!--$$JustDate:--> 11/13/00 <!-- $-->
68  */

69 public class TypeDescription extends FeatureDescription {
70     
71     /** The type to wrap and describe */
72     private Class JavaDoc mType;
73     
74     /** The BeanInfo for the type */
75     private BeanInfo mBeanInfo;
76
77     /**
78      * Create a new TypeDescription to wrap the specified type.
79      * The TeaToolsUtils object provides the implementation of
80      * this class's methods.
81      */

82     public TypeDescription(Class JavaDoc type, TeaToolsUtils utils) {
83         super(utils);
84         mType = type;
85     }
86
87     /**
88      * Returns the type.
89      */

90     public Class JavaDoc getType() {
91         return mType;
92     }
93
94
95     /**
96      * Returns a AccessFlags instance that can be used to check the type's
97      * modifiers.
98      */

99     public AccessFlags getAccessFlags() {
100         return getTeaToolsUtils().getAccessFlags(mType.getModifiers());
101     }
102
103     /**
104      * Returns the full name of the type. This method
105      * provides special formatting for array and inner classes.
106      */

107     public String JavaDoc getFullName() {
108         return getTeaToolsUtils().getFullClassName(mType);
109     }
110
111     /**
112      * Returns the class name of the type. The class name returned
113      * does not include the package. This method provides special formatting
114      * for array and inner classes.
115      */

116     public String JavaDoc getName() {
117         return getTeaToolsUtils().getClassName(mType);
118     }
119
120     /**
121      * Returns the package name of the class. Returns "" if the
122      * class has no package.
123      */

124     public String JavaDoc getPackage() {
125         return getTeaToolsUtils().getClassPackage(mType);
126     }
127
128     /**
129      * Returns the name of the type of the Class described by this
130      * TypeDescription.
131      * <p>
132      * <UL>
133      * <LI>A Class returns "class"
134      * <LI>An Interface returns "interface"
135      * <LI>An array returns null
136      * <LI>A primitive returns null
137      * </UL>
138      */

139     public String JavaDoc getTypeName() {
140         return getTeaToolsUtils().getClassTypeName(mType);
141     }
142
143     /**
144      * Create a version information string based on what the build process
145      * provided. The string is of the form "M.m.r" or
146      * "M.m.r.bbbb" (i.e. 1.1.0.0004) if the build number can be retrieved.
147      * Returns <code>null</code> if the version string cannot be retrieved.
148      */

149     public String JavaDoc getVersion() {
150         return getTeaToolsUtils().getPackageVersion(getPackage());
151     }
152     
153     /**
154      * Returns the array type. Returns this if it is not an
155      * array type.
156      */

157     public TypeDescription getArrayType() {
158
159         Class JavaDoc c = getTeaToolsUtils().getArrayType(mType);
160         if (mType == c) {
161             return this;
162         }
163
164         return getTeaToolsUtils().createTypeDescription(c);
165     }
166
167
168     /**
169      * Returns the array dimensions.
170      * Returns 0 if the type is not an array.
171      */

172     public int getArrayDimensions() {
173         return getTeaToolsUtils().getArrayDimensions(mType);
174     }
175
176     /**
177      * Returns the array dimensions String (i.e. "[][][]").
178      * Returns "" (empty string) if the type is not an array.
179      */

180     public String JavaDoc getArrayDimensionsString() {
181         return getTeaToolsUtils().getArrayDimensionsString(mType);
182     }
183
184     /**
185      * Introspects a Java bean to learn about all its properties,
186      * exposed methods, and events. Returns null if the BeanInfo
187      * could not be created.
188      */

189     public BeanInfo getBeanInfo() {
190         if (mBeanInfo == null) {
191             try {
192                 mBeanInfo = getTeaToolsUtils().getBeanInfo(mType);
193             }
194             catch (Exception JavaDoc e) {
195                 return null;
196             }
197         }
198
199         return mBeanInfo;
200     }
201
202     /**
203      * Introspects a Java bean to learn all about its properties, exposed
204      * methods, below a given "stop" point.
205      *
206      * @param stopClass the base class at which to stop the analysis.
207      * Any methods/properties/events in the stopClass or in its baseclasses
208      * will be ignored in the analysis
209      */

210     public BeanInfo getBeanInfo(Class JavaDoc stopClass)
211     throws IntrospectionException {
212         return getTeaToolsUtils().getBeanInfo(mType, stopClass);
213     }
214
215
216     /**
217      * Returns the type's PropertyDescriptors.
218      */

219     public PropertyDescriptor[] getPropertyDescriptors() {
220         BeanInfo info = getBeanInfo();
221         if (info == null) {
222             return null;
223         }
224
225         PropertyDescriptor[] pds = info.getPropertyDescriptors();
226         getTeaToolsUtils().sortPropertyDescriptors(pds);
227         return pds;
228     }
229
230     /**
231      * Returns the type's PropertyDescriptions.
232      */

233     public PropertyDescription[] getPropertyDescriptions() {
234         return getTeaToolsUtils().createPropertyDescriptions(
235                                           getPropertyDescriptors());
236
237     }
238
239     /**
240      * Returns an array of all the available properties on the class.
241      */

242     public PropertyDescriptor[] getTeaBeanPropertyDescriptors() {
243         return getTeaToolsUtils().getTeaBeanPropertyDescriptors(mType);
244     }
245
246     /**
247      * Returns an array of all the available properties on the class.
248      */

249     public PropertyDescription[] getTeaBeanPropertyDescriptions() {
250         return getTeaToolsUtils().createPropertyDescriptions(
251                                           getTeaBeanPropertyDescriptors());
252     }
253
254     /**
255      * Returns the type's MethodDescriptors.
256      */

257     public MethodDescriptor[] getMethodDescriptors() {
258         BeanInfo info = getBeanInfo();
259         if (info == null) {
260             return null;
261         }
262
263         MethodDescriptor[] mds = info.getMethodDescriptors();
264         getTeaToolsUtils().sortMethodDescriptors(mds);
265         return mds;
266     }
267
268     /**
269      * Returns the type's MethodDescriptions.
270      */

271     public MethodDescription[] getMethodDescriptions() {
272         return getTeaToolsUtils().createMethodDescriptions(
273                                                      getMethodDescriptors());
274     }
275
276
277     /**
278      * Gets the MethodDescriptors of the context class including
279      * all of the MethodDescriptors for methods declared in the class's
280      * superclass and interfaces
281      */

282     public MethodDescriptor[] getTeaContextMethodDescriptors() {
283         return getTeaToolsUtils().getTeaContextMethodDescriptors(mType);
284     }
285
286
287     /**
288      * Gets the MethodDescriptions of the context class including
289      * all of the MethodDescriptions for methods declared in the class's
290      * superclass and interfaces
291      */

292     public MethodDescription[] getTeaContextMethodDescriptions() {
293         return getTeaToolsUtils().createMethodDescriptions(
294                                           getTeaContextMethodDescriptors());
295     }
296
297
298     /**
299      * Gets the MethodDescriptors of the context class
300      *
301      * @param thisClassOnly true indicates that this function should
302      * only return MethodDescriptors declared by the wrapped Class.
303      */

304     public MethodDescriptor[] getTeaContextMethodDescriptors(
305                                                  boolean thisClassOnly) {
306
307         return getTeaToolsUtils().getTeaContextMethodDescriptors(
308                                                               mType,
309                                                               thisClassOnly);
310     }
311     
312     /**
313      * Returns the full class name of the class. This method
314      * provides special formatting for array and inner classes. If the
315      * specified class is implicitly imported by Tea, then its package is
316      * omitted in the returned name.
317      */

318     public String JavaDoc getTeaFullName() {
319         return getTeaToolsUtils().getTeaFullClassName(mType);
320     }
321
322     /**
323      * Returns true if the class is
324      * implicitly imported by Tea.
325      * <p>
326      * Returns true if the specified class represents a primitive type or
327      * a class or interface defined in one of the IMPLICIT_TEA_IMPORTS
328      * packages. This method also works for array types.
329      */

330     public boolean isImplicitTeaImport() {
331         return getTeaToolsUtils().isImplicitTeaImport(mType);
332     }
333
334     /**
335      * Returns true if the class is compatible with Tea's
336      * <code>foreach</code> statement. Compatibility implies that the
337      * class can be iterated on by the <code>foreach</code>.
338      */

339     public boolean isForeachCompatible() {
340         return getTeaToolsUtils().isForeachCompatible(mType);
341     }
342
343     /**
344      * Returns true if the class is compatible with Tea's <code>if
345      * </code> statement. Only Boolean.class and boolean.class qualify.
346      */

347     public boolean isIfCompatible() {
348         return getTeaToolsUtils().isIfCompatible(mType);
349     }
350
351     /**
352      * Returns true if it is likely that the class serves as
353      * a Tea runtime context class.
354      */

355     public boolean isLikelyContextClass() {
356         return getTeaToolsUtils().isLikelyContextClass(mType);
357     }
358
359
360     //
361
// FeatureDescription methods
362
//
363

364     public FeatureDescriptor getFeatureDescriptor() {
365         BeanInfo info = getBeanInfo();
366         if (info == null) {
367             return null;
368         }
369
370         return info.getBeanDescriptor();
371     }
372
373
374     public String JavaDoc getShortFormat() {
375         return getName();
376     }
377
378     public String JavaDoc getLongFormat() {
379         return getFullName();
380     }
381
382
383
384 }
385
386
387
388
389
Popular Tags