KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > beandoc > teadoc > ClassDoc


1 /* ====================================================================
2  * BeanDoc - 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", BeanDoc and "GO.com"
28  * must 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", "Trove" or BeanDoc, nor may "Tea", "TeaServlet",
34  * "Kettle", "Trove" or BeanDoc 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.beandoc.teadoc;
54
55 import java.util.Arrays JavaDoc;
56
57 /******************************************************************************
58  *
59  * @author Brian S O'Neill
60  * @version
61  * <!--$$Revision:--> 6 <!-- $-->, <!--$$JustDate:--> 8/23/00 <!-- $-->
62  */

63 public class ClassDoc extends ProgramElementDoc {
64
65     private com.sun.javadoc.ClassDoc mDoc;
66     private String JavaDoc[] mPath;
67
68     public static ClassDoc[] convert(RootDoc root,
69                                      com.sun.javadoc.ClassDoc[] docs) {
70         int length = docs.length;
71         ClassDoc[] newDocs = new ClassDoc[length];
72         for (int i=0; i<length; i++) {
73             newDocs[i] = new ClassDoc(root, docs[i]);
74         }
75         return newDocs;
76     }
77
78     /**
79      * Splits a class name into two strings.
80      * <br>
81      * [0] = package name (or null if the class is unpackaged) <br>
82      * [1] = class name
83      */

84     public static String JavaDoc[] parseClassName(String JavaDoc fullClassName) {
85
86         int dotIndex = fullClassName.lastIndexOf(".");
87         String JavaDoc packageName = null;
88         String JavaDoc className = fullClassName;
89
90         if (dotIndex > 0) {
91             packageName = fullClassName.substring(0, dotIndex);
92             className = fullClassName.substring(dotIndex + 1);
93         }
94
95         return new String JavaDoc[] { packageName, className };
96     }
97
98
99
100     
101     public ClassDoc(RootDoc root, com.sun.javadoc.ClassDoc doc) {
102         super(root, doc);
103         mDoc = doc;
104     }
105
106     public boolean isAbstract() {
107         return mDoc.isAbstract();
108     }
109
110     public boolean isSerializable() {
111         return mDoc.isSerializable();
112     }
113
114     public boolean isExternalizable() {
115         return mDoc.isExternalizable();
116     }
117
118     public MethodDoc[] getSerializationMethods() {
119         return MethodDoc.convert(mRootDoc, mDoc.serializationMethods());
120     }
121
122     public FieldDoc[] getSerializableFields() {
123         return FieldDoc.convert(mRootDoc, mDoc.serializableFields());
124     }
125
126     public boolean getDefinesSerializableFields() {
127         return mDoc.definesSerializableFields();
128     }
129
130     public PackageDoc getContainingPackage() {
131         return new PackageDoc(mRootDoc, mDoc.containingPackage());
132     }
133
134     public String JavaDoc[] getPath() {
135         if (mPath == null) {
136             ClassDoc parent = getContainingClass();
137             if (parent != null) {
138                 String JavaDoc[] parentPath = parent.getPath();
139                 int length = parentPath.length;
140                 mPath = new String JavaDoc[length];
141                 System.arraycopy(parentPath, 0, mPath, 0, length);
142                 mPath[length - 1] = getName();
143             }
144             else {
145                 mPath = parseName(getQualifiedName());
146             }
147         }
148
149         return mPath;
150     }
151
152     public ClassDoc getSuperclass() {
153         com.sun.javadoc.ClassDoc superclass = mDoc.superclass();
154         if (superclass == null) {
155             return null;
156         }
157
158         return new ClassDoc(mRootDoc, superclass);
159     }
160
161     public ClassDoc[] getInterfaces() {
162         return convert(mRootDoc, mDoc.interfaces());
163     }
164
165     public FieldDoc[] getFields() {
166         return FieldDoc.convert(mRootDoc, mDoc.fields());
167     }
168
169     public FieldDoc[] getSortedFields() {
170         FieldDoc[] docs = (FieldDoc[])getFields().clone();
171         Arrays.sort(docs);
172         return docs;
173     }
174
175     public MethodDoc[] getMethods() {
176         return MethodDoc.convert(mRootDoc, mDoc.methods());
177     }
178
179     public MethodDoc[] getSortedMethods() {
180         MethodDoc[] docs = (MethodDoc[])getMethods().clone();
181         Arrays.sort(docs);
182         return docs;
183     }
184
185     public PropertyDoc[] getProperties() {
186         return PropertyDoc.create(mRootDoc, getMethods());
187     }
188
189     public PropertyDoc[] getSortedProperties() {
190         return PropertyDoc.create(mRootDoc, getSortedMethods());
191     }
192
193     public ConstructorDoc[] getConstructors() {
194         return ConstructorDoc.convert(mRootDoc, mDoc.constructors());
195     }
196
197     public ConstructorDoc[] getSortedConstructors() {
198         ConstructorDoc[] docs = (ConstructorDoc[])getConstructors().clone();
199         Arrays.sort(docs);
200         return docs;
201     }
202
203     public ClassDoc[] getInnerClasses() {
204         return convert(mRootDoc, mDoc.innerClasses());
205     }
206
207     public ClassDoc[] getSortedInnerClasses() {
208         ClassDoc[] docs = (ClassDoc[])getInnerClasses().clone();
209         Arrays.sort(docs);
210         return docs;
211     }
212
213     public ClassDoc[] getImportedClasses() {
214         return convert(mRootDoc, mDoc.importedClasses());
215     }
216
217     public PackageDoc[] getImportedPackages() {
218         return PackageDoc.convert(mRootDoc, mDoc.importedPackages());
219     }
220
221     public String JavaDoc getTypeName() {
222         return mDoc.typeName();
223     }
224
225     public String JavaDoc getQualifiedTypeName() {
226         return mDoc.qualifiedTypeName();
227     }
228
229     /**
230      * Converts inner class names (replaces '.' with '$').
231      */

232     public String JavaDoc getTypeNameForFile() {
233         return mDoc.typeName().replace('.', '$');
234     }
235
236     /**
237      * Converts inner class names.
238      */

239     public String JavaDoc getQualifiedTypeNameForFile() {
240
241         String JavaDoc typeName = getTypeNameForFile();
242         String JavaDoc qualifiedTypeName = mDoc.qualifiedTypeName();
243
244         int packageLength = qualifiedTypeName.length() - typeName.length();
245         if (packageLength <= 0) {
246             return typeName;
247         }
248
249         String JavaDoc packagePath = qualifiedTypeName.substring(0, packageLength);
250
251         return packagePath + typeName;
252     }
253
254     /**
255      * Returns the package name. Returns null if the class is unpackaged.
256      */

257     public String JavaDoc getPackageName() {
258         return parseClassName(getQualifiedTypeNameForFile())[0];
259     }
260
261     public String JavaDoc getDimension() {
262         return mDoc.dimension();
263     }
264
265     public String JavaDoc toString() {
266         return mDoc.toString();
267     }
268
269     public ClassDoc getAsClassDoc() {
270         return new ClassDoc(mRootDoc, mDoc.asClassDoc());
271     }
272
273     /**
274      * Get a MethodDoc in this ClassDoc with a name and signature
275      * matching that of the specified MethodDoc
276      */

277     public MethodDoc getMatchingMethod(MethodDoc method) {
278
279         MethodDoc[] methods = getMethods();
280         for (int i = 0; i < methods.length; i++) {
281
282             if (method.getName().equals(methods[i].getName()) &&
283                 method.getSignature().equals(methods[i].getSignature())) {
284                 
285                 return methods[i];
286             }
287         }
288         return null;
289     }
290
291     /**
292      * Get a MethodDoc in this ClassDoc with a name and signature
293      * matching that of the specified MethodDoc and accepted by the
294      * specified MethodFinder
295      */

296     public MethodDoc getMatchingMethod(MethodDoc method, MethodFinder mf) {
297
298         MethodDoc md = getMatchingMethod(method);
299         if (md != null) {
300             if (mf.checkMethod(md)) {
301                 return md;
302             }
303         }
304
305         return null;
306     }
307
308     /**
309      * Find a MethodDoc with a name and signature
310      * matching that of the specified MethodDoc and accepted by the
311      * specified MethodFinder. This method searches the interfaces and
312      * super class ancestry of the class represented by this ClassDoc for
313      * a matching method.
314      */

315     public MethodDoc findMatchingMethod(MethodDoc method, MethodFinder mf) {
316         
317         // Look in this class's interface set
318
MethodDoc md = findMatchingInterfaceMethod(method, mf);
319
320         if (md != null) {
321             return md;
322         }
323
324         // Look in this class's superclass ancestry
325
ClassDoc superClass = getSuperclass();
326         if (superClass != null) {
327
328             md = superClass.getMatchingMethod(method, mf);
329             if (md != null) {
330                 return md;
331             }
332             
333             return superClass.findMatchingMethod(method, mf);
334         }
335
336         return null;
337     }
338
339     private MethodDoc findMatchingInterfaceMethod(MethodDoc method,
340                                                   MethodFinder mf) {
341     
342         ClassDoc[] interfaces = getInterfaces();
343         if (interfaces == null || interfaces.length == 0) {
344             return null;
345         }
346
347         // Look at each interface's methods
348
for (int i = 0; i < interfaces.length; i++) {
349             
350             MethodDoc m =
351                 interfaces[i].getMatchingMethod(method, mf);
352
353             if (m != null) {
354                 return m;
355             }
356         }
357
358         // Look for the method in the interface's interfaces
359
for (int i = 0; i < interfaces.length; i++) {
360             
361             MethodDoc m =
362                 interfaces[i].findMatchingInterfaceMethod(method, mf);
363             
364             if (m != null) {
365                 return m;
366             }
367         }
368
369         return null;
370     }
371
372 }
373
Popular Tags