1 /*
2  * @(#)HelpMemberPrototype.java	1.9 06/10/30
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc.  All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistribution of source code must retain the above copyright
11  *   notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  *   notice, this list of conditions and the following disclaimer in
15  *   the documentation and/or other materials provided with the
16  *   distribution.
17  *
18  * Neither the name of Sun Microsystems, Inc. or the names of
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * This software is provided "AS IS," without a warranty of any
23  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
24  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
26  * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
27  * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
28  * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
29  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
30  * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
31  * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
32  * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
33  * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGES.
35  *
36  * You acknowledge that this software is not designed, licensed or
37  * intended for use in the design, construction, operation or
38  * maintenance of any nuclear facility.
39  */
40 
41 /*
42  * @(#) HelpMemberPrototype.java 1.9 - last change made 10/30/06
43  *
44  * (c) 1997-1998 Sun Microsystems, Inc.  All rights reserved.  Use is
45  * subject to license terms. Sun, Sun Microsystems, the Sun Logo, Solaris,
46  * Java, the Java Coffee Cup Logo, and JavaHelp are trademarks or registered
47  * trademarks of Sun Microsystems, Inc. in  the U.S. and other countries.
48  *
49  * This software is the confidential and proprietary information
50  * of Sun Microsystems, Inc. ("Confidential Information").  You
51  * shall not disclose such Confidential Information and shall use
52  * it only in accordance with the terms of the license agreement
53  * you entered into with Sun.
54  */
55 
56 package sunw.demo.doclet;
57 
58 import com.sun.javadoc.*;
59 
60 /**
61  * This class print the Member documentation for a class
62  *
63  * @author Roger D. Brinkley
64  * @author Eduardo Pelegri-Llopart
65  * @version	1.9	10/30/06
66  */
67 
68 public class HelpMemberPrototype {
69 
70     MemberDoc member;
71 
72     StringBuffer sb = new StringBuffer();
73 
HelpMemberPrototype(MemberDoc member)74     HelpMemberPrototype(MemberDoc member) {
75         this.member = member;
76     }
77 
78     /**
79      * track how long the displayed (non-html) contents
80      * of 'sb' are.
81      */
82     int displayLength = 0;
83 
generate(ExecutableMemberDoc method)84     static String generate(ExecutableMemberDoc method) {
85         HelpMemberPrototype mp = new HelpMemberPrototype(method);
86         return mp.methodPrototype(method);
87     }
88 
generate(FieldDoc field)89     static String generate(FieldDoc field) {
90         HelpMemberPrototype mp = new HelpMemberPrototype(field);
91         return mp.fieldPrototype(field);
92     }
93 
append(String str)94     void append(String str) {
95         sb.append(str);
96         displayLength += str.length();
97     }
98 
append(char ch)99     void append(char ch) {
100         sb.append(ch);
101         displayLength++;
102     }
103 
makeLink(Type type)104     void makeLink(Type type) {
105 	ClassDoc cls = null;
106 	if (type == null) {
107 	    return;
108 	}
109 	try {
110 	    cls = type.asClassDoc();
111 	} catch (NullPointerException e) {
112 	    cls = null;
113 	}
114         if (cls == null) {
115 	    try {
116 		append(type.typeName());
117 	    } catch (NullPointerException e2) {
118 	    }
119         } else {
120             // use sb.append() to by-pass length count
121             sb.append("<a href=\"");
122 	    //            if (cls != member.containingClass()) {
123                 sb.append(cls.qualifiedTypeName());
124 		//            }
125             sb.append(".html\">");
126 	    append(cls.name());
127             sb.append("</a>");
128         }
129     }
130 
appendModifiers()131     void appendModifiers() {
132         String mod = member.modifiers();  // ??? wrong!!!
133         if(mod.length() > 0) {
134             append(mod);
135             append(' ');
136         }
137     }
138 
appendParam(Parameter param)139     void appendParam(Parameter param) {
140 	Type type = param.type();
141         makeLink(type);
142 
143         // the name of the arg is not always available
144         String name = param.name();
145         if(name.length() > 0) {
146             sb.append(' ');
147             sb.append(name);
148         }
149 	if (type != null) {
150 	    sb.append(type.dimension());
151 	}
152     }
153 
154 
makeSpace(int len)155     String makeSpace(int len) {
156         StringBuffer sb = new StringBuffer(len);
157         for(int i = 0; i < len; i++)
158             sb.append(' ');
159 
160         return sb.toString();
161     }
162 
fieldPrototype(FieldDoc field)163     String fieldPrototype(FieldDoc field) {
164         appendModifiers();
165 
166         Type type = field.type();
167         if (type != null) {
168             makeLink(type);
169 
170             append(' ');
171         }
172 
173         append(member.name());
174 
175         // for a field the [] goes with the name, not the type
176         // ??? change after compare!!!
177         append(type.dimension());
178 
179         return sb.toString();
180     }
181 
methodPrototype(ExecutableMemberDoc method)182     String methodPrototype(ExecutableMemberDoc method) {
183         appendModifiers();
184 
185 	if (method instanceof MethodDoc) {
186 	    MethodDoc method2 = (MethodDoc) method;
187 	    Type type = method2.returnType();
188 	    if (type != null) {
189 		makeLink(type);
190 
191 		// for a field the [] goes with the name, not the type
192 		// ??? remove comment after this is fixed after compare!!!
193 		append(type.dimension());
194 
195 		append(' ');
196 	    }
197 	}
198 
199         append(method.name());
200         append('(');
201 
202         String space = "";
203 
204         Parameter[] params = method.parameters();
205         if (params.length > 0) {
206             space = makeSpace(displayLength);
207             appendParam(params[0]);
208         }
209 
210         for (int i = 1; i < params.length; i++) {
211             sb.append(',');
212             sb.append('\n');
213             sb.append(space);
214             appendParam(params[i]);
215         }
216 
217         sb.append(')');
218 
219         Type[] except = method.thrownExceptions();
220         if(except.length > 0) {
221             sb.append(" throws ");
222             makeLink(except[0]);
223 
224             for(int i = 1; i < except.length; i++) {
225                 sb.append(", ");
226                 makeLink(except[i]);
227             }
228         }
229         return sb.toString();
230     }
231 
232     /**
233      * For printf debugging.
234      */
235     private static boolean debug = false;
debug(String str)236     private static void debug(String str) {
237         if (debug) {
238             System.out.println("HelpMemberPrototype: " + str);
239         }
240     }
241 }
242