Wed, 10 Oct 2012 16:48:21 -0700
8000665: fix "internal API" comments on javadoc files
Reviewed-by: darcy
1 /*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.tools.doclets.formats.html;
28 import java.lang.reflect.Modifier;
29 import java.util.*;
31 import com.sun.javadoc.*;
32 import com.sun.tools.doclets.formats.html.markup.*;
33 import com.sun.tools.doclets.internal.toolkit.*;
34 import com.sun.tools.doclets.internal.toolkit.taglets.*;
35 import com.sun.tools.doclets.internal.toolkit.util.*;
37 /**
38 * The base class for member writers.
39 *
40 * <p><b>This is NOT part of any supported API.
41 * If you write code that depends on this, you do so at your own risk.
42 * This code and its internal interfaces are subject to change or
43 * deletion without notice.</b>
44 *
45 * @author Robert Field
46 * @author Atul M Dambalkar
47 * @author Jamie Ho (Re-write)
48 * @author Bhavesh Patel (Modified)
49 */
50 public abstract class AbstractMemberWriter {
52 protected boolean printedSummaryHeader = false;
53 protected final SubWriterHolderWriter writer;
54 protected final ClassDoc classdoc;
55 public final boolean nodepr;
57 public AbstractMemberWriter(SubWriterHolderWriter writer,
58 ClassDoc classdoc) {
59 this.writer = writer;
60 this.nodepr = configuration().nodeprecated;
61 this.classdoc = classdoc;
62 }
64 public AbstractMemberWriter(SubWriterHolderWriter writer) {
65 this(writer, null);
66 }
68 /*** abstracts ***/
70 /**
71 * Add the summary label for the member.
72 *
73 * @param memberTree the content tree to which the label will be added
74 */
75 public abstract void addSummaryLabel(Content memberTree);
77 /**
78 * Get the summary for the member summary table.
79 *
80 * @return a string for the table summary
81 */
82 public abstract String getTableSummary();
84 /**
85 * Get the caption for the member summary table.
86 *
87 * @return a string for the table caption
88 */
89 public abstract String getCaption();
91 /**
92 * Get the summary table header for the member.
93 *
94 * @param member the member to be documented
95 * @return the summary table header
96 */
97 public abstract String[] getSummaryTableHeader(ProgramElementDoc member);
99 /**
100 * Add inherited summary lable for the member.
101 *
102 * @param cd the class doc to which to link to
103 * @param inheritedTree the content tree to which the inherited summary label will be added
104 */
105 public abstract void addInheritedSummaryLabel(ClassDoc cd, Content inheritedTree);
107 /**
108 * Add the anchor for the summary section of the member.
109 *
110 * @param cd the class doc to be documented
111 * @param memberTree the content tree to which the summary anchor will be added
112 */
113 public abstract void addSummaryAnchor(ClassDoc cd, Content memberTree);
115 /**
116 * Add the anchor for the inherited summary section of the member.
117 *
118 * @param cd the class doc to be documented
119 * @param inheritedTree the content tree to which the inherited summary anchor will be added
120 */
121 public abstract void addInheritedSummaryAnchor(ClassDoc cd, Content inheritedTree);
123 /**
124 * Add the summary type for the member.
125 *
126 * @param member the member to be documented
127 * @param tdSummaryType the content tree to which the type will be added
128 */
129 protected abstract void addSummaryType(ProgramElementDoc member,
130 Content tdSummaryType);
132 /**
133 * Add the summary link for the member.
134 *
135 * @param cd the class doc to be documented
136 * @param member the member to be documented
137 * @param tdSummary the content tree to which the link will be added
138 */
139 protected void addSummaryLink(ClassDoc cd, ProgramElementDoc member,
140 Content tdSummary) {
141 addSummaryLink(LinkInfoImpl.CONTEXT_MEMBER, cd, member, tdSummary);
142 }
144 /**
145 * Add the summary link for the member.
146 *
147 * @param context the id of the context where the link will be printed
148 * @param cd the class doc to be documented
149 * @param member the member to be documented
150 * @param tdSummary the content tree to which the summary link will be added
151 */
152 protected abstract void addSummaryLink(int context,
153 ClassDoc cd, ProgramElementDoc member, Content tdSummary);
155 /**
156 * Add the inherited summary link for the member.
157 *
158 * @param cd the class doc to be documented
159 * @param member the member to be documented
160 * @param linksTree the content tree to which the inherited summary link will be added
161 */
162 protected abstract void addInheritedSummaryLink(ClassDoc cd,
163 ProgramElementDoc member, Content linksTree);
165 /**
166 * Get the deprecated link.
167 *
168 * @param member the member being linked to
169 * @return a content tree representing the link
170 */
171 protected abstract Content getDeprecatedLink(ProgramElementDoc member);
173 /**
174 * Get the navigation summary link.
175 *
176 * @param cd the class doc to be documented
177 * @param link true if its a link else the label to be printed
178 * @return a content tree for the navigation summary link.
179 */
180 protected abstract Content getNavSummaryLink(ClassDoc cd, boolean link);
182 /**
183 * Add the navigation detail link.
184 *
185 * @param link true if its a link else the label to be printed
186 * @param liNav the content tree to which the navigation detail link will be added
187 */
188 protected abstract void addNavDetailLink(boolean link, Content liNav);
190 /*** ***/
192 protected void print(String str) {
193 writer.print(str);
194 writer.displayLength += str.length();
195 }
197 protected void print(char ch) {
198 writer.print(ch);
199 writer.displayLength++;
200 }
202 protected void strong(String str) {
203 writer.strong(str);
204 writer.displayLength += str.length();
205 }
207 /**
208 * Add the member name to the content tree and modifies the display length.
209 *
210 * @param name the member name to be added to the content tree.
211 * @param htmltree the content tree to which the name will be added.
212 */
213 protected void addName(String name, Content htmltree) {
214 htmltree.addContent(name);
215 writer.displayLength += name.length();
216 }
218 /**
219 * Return a string describing the access modifier flags.
220 * Don't include native or synchronized.
221 *
222 * The modifier names are returned in canonical order, as
223 * specified by <em>The Java Language Specification</em>.
224 */
225 protected String modifierString(MemberDoc member) {
226 int ms = member.modifierSpecifier();
227 int no = Modifier.NATIVE | Modifier.SYNCHRONIZED;
228 return Modifier.toString(ms & ~no);
229 }
231 protected String typeString(MemberDoc member) {
232 String type = "";
233 if (member instanceof MethodDoc) {
234 type = ((MethodDoc)member).returnType().toString();
235 } else if (member instanceof FieldDoc) {
236 type = ((FieldDoc)member).type().toString();
237 }
238 return type;
239 }
241 /**
242 * Add the modifier for the member.
243 *
244 * @param member the member for which teh modifier will be added.
245 * @param htmltree the content tree to which the modifier information will be added.
246 */
247 protected void addModifiers(MemberDoc member, Content htmltree) {
248 String mod = modifierString(member);
249 // According to JLS, we should not be showing public modifier for
250 // interface methods.
251 if ((member.isField() || member.isMethod()) &&
252 writer instanceof ClassWriterImpl &&
253 ((ClassWriterImpl) writer).getClassDoc().isInterface()) {
254 mod = Util.replaceText(mod, "public", "").trim();
255 }
256 if(mod.length() > 0) {
257 htmltree.addContent(mod);
258 htmltree.addContent(writer.getSpace());
259 }
260 }
262 protected String makeSpace(int len) {
263 if (len <= 0) {
264 return "";
265 }
266 StringBuffer sb = new StringBuffer(len);
267 for(int i = 0; i < len; i++) {
268 sb.append(' ');
269 }
270 return sb.toString();
271 }
273 /**
274 * Add the modifier and type for the member in the member summary.
275 *
276 * @param member the member to add the type for
277 * @param type the type to add
278 * @param tdSummaryType the content tree to which the modified and type will be added
279 */
280 protected void addModifierAndType(ProgramElementDoc member, Type type,
281 Content tdSummaryType) {
282 HtmlTree code = new HtmlTree(HtmlTag.CODE);
283 addModifier(member, code);
284 if (type == null) {
285 if (member.isClass()) {
286 code.addContent("class");
287 } else {
288 code.addContent("interface");
289 }
290 code.addContent(writer.getSpace());
291 } else {
292 if (member instanceof ExecutableMemberDoc &&
293 ((ExecutableMemberDoc) member).typeParameters().length > 0) {
294 //Code to avoid ugly wrapping in member summary table.
295 int displayLength = ((AbstractExecutableMemberWriter) this).addTypeParameters(
296 (ExecutableMemberDoc) member, code);
297 if (displayLength > 10) {
298 code.addContent(new HtmlTree(HtmlTag.BR));
299 }
300 code.addContent(new RawHtml(
301 writer.getLink(new LinkInfoImpl(
302 LinkInfoImpl.CONTEXT_SUMMARY_RETURN_TYPE, type))));
303 } else {
304 code.addContent(new RawHtml(
305 writer.getLink(new LinkInfoImpl(
306 LinkInfoImpl.CONTEXT_SUMMARY_RETURN_TYPE, type))));
307 }
309 }
310 tdSummaryType.addContent(code);
311 }
313 private void printModifier(ProgramElementDoc member) {
314 if (member.isProtected()) {
315 print("protected ");
316 } else if (member.isPrivate()) {
317 print("private ");
318 } else if (!member.isPublic()) { // Package private
319 writer.printText("doclet.Package_private");
320 print(" ");
321 }
322 if (member.isMethod() && ((MethodDoc)member).isAbstract()) {
323 print("abstract ");
324 }
325 if (member.isStatic()) {
326 print("static");
327 }
328 }
330 /**
331 * Add the modifier for the member.
332 *
333 * @param member the member to add the type for
334 * @param code the content tree to which the modified will be added
335 */
336 private void addModifier(ProgramElementDoc member, Content code) {
337 if (member.isProtected()) {
338 code.addContent("protected ");
339 } else if (member.isPrivate()) {
340 code.addContent("private ");
341 } else if (!member.isPublic()) { // Package private
342 code.addContent(configuration().getText("doclet.Package_private"));
343 code.addContent(" ");
344 }
345 if (member.isMethod() && ((MethodDoc)member).isAbstract()) {
346 code.addContent("abstract ");
347 }
348 if (member.isStatic()) {
349 code.addContent("static ");
350 }
351 }
353 /**
354 * Add the deprecated information for the given member.
355 *
356 * @param member the member being documented.
357 * @param contentTree the content tree to which the deprecated information will be added.
358 */
359 protected void addDeprecatedInfo(ProgramElementDoc member, Content contentTree) {
360 String output = (new DeprecatedTaglet()).getTagletOutput(member,
361 writer.getTagletWriterInstance(false)).toString().trim();
362 if (!output.isEmpty()) {
363 Content deprecatedContent = new RawHtml(output);
364 Content div = HtmlTree.DIV(HtmlStyle.block, deprecatedContent);
365 contentTree.addContent(div);
366 }
367 }
369 /**
370 * Add the comment for the given member.
371 *
372 * @param member the member being documented.
373 * @param htmltree the content tree to which the comment will be added.
374 */
375 protected void addComment(ProgramElementDoc member, Content htmltree) {
376 if (member.inlineTags().length > 0) {
377 writer.addInlineComment(member, htmltree);
378 }
379 }
381 protected String name(ProgramElementDoc member) {
382 return member.name();
383 }
385 /**
386 * Get the header for the section.
387 *
388 * @param member the member being documented.
389 * @return a header content for the section.
390 */
391 protected Content getHead(MemberDoc member) {
392 Content memberContent = new RawHtml(member.name());
393 Content heading = HtmlTree.HEADING(HtmlConstants.MEMBER_HEADING, memberContent);
394 return heading;
395 }
397 /**
398 * Return true if the given <code>ProgramElement</code> is inherited
399 * by the class that is being documented.
400 *
401 * @param ped The <code>ProgramElement</code> being checked.
402 * return true if the <code>ProgramElement</code> is being inherited and
403 * false otherwise.
404 */
405 protected boolean isInherited(ProgramElementDoc ped){
406 if(ped.isPrivate() || (ped.isPackagePrivate() &&
407 ! ped.containingPackage().equals(classdoc.containingPackage()))){
408 return false;
409 }
410 return true;
411 }
413 /**
414 * Add deprecated information to the documentation tree
415 *
416 * @param deprmembers list of deprecated members
417 * @param headingKey the caption for the deprecated members table
418 * @param tableSummary the summary for the deprecated members table
419 * @param tableHeader table headers for the deprecated members table
420 * @param contentTree the content tree to which the deprecated members table will be added
421 */
422 protected void addDeprecatedAPI(List<Doc> deprmembers, String headingKey,
423 String tableSummary, String[] tableHeader, Content contentTree) {
424 if (deprmembers.size() > 0) {
425 Content table = HtmlTree.TABLE(0, 3, 0, tableSummary,
426 writer.getTableCaption(configuration().getText(headingKey)));
427 table.addContent(writer.getSummaryTableHeader(tableHeader, "col"));
428 Content tbody = new HtmlTree(HtmlTag.TBODY);
429 for (int i = 0; i < deprmembers.size(); i++) {
430 ProgramElementDoc member =(ProgramElementDoc)deprmembers.get(i);
431 HtmlTree td = HtmlTree.TD(HtmlStyle.colOne, getDeprecatedLink(member));
432 if (member.tags("deprecated").length > 0)
433 writer.addInlineDeprecatedComment(member,
434 member.tags("deprecated")[0], td);
435 HtmlTree tr = HtmlTree.TR(td);
436 if (i%2 == 0)
437 tr.addStyle(HtmlStyle.altColor);
438 else
439 tr.addStyle(HtmlStyle.rowColor);
440 tbody.addContent(tr);
441 }
442 table.addContent(tbody);
443 Content li = HtmlTree.LI(HtmlStyle.blockList, table);
444 Content ul = HtmlTree.UL(HtmlStyle.blockList, li);
445 contentTree.addContent(ul);
446 }
447 }
449 /**
450 * Add use information to the documentation tree.
451 *
452 * @param mems list of program elements for which the use information will be added
453 * @param heading the section heading
454 * @param tableSummary the summary for the use table
455 * @param contentTree the content tree to which the use information will be added
456 */
457 protected void addUseInfo(List<? extends ProgramElementDoc> mems,
458 String heading, String tableSummary, Content contentTree) {
459 if (mems == null) {
460 return;
461 }
462 List<? extends ProgramElementDoc> members = mems;
463 boolean printedUseTableHeader = false;
464 if (members.size() > 0) {
465 Content table = HtmlTree.TABLE(0, 3, 0, tableSummary,
466 writer.getTableCaption(heading));
467 Content tbody = new HtmlTree(HtmlTag.TBODY);
468 Iterator<? extends ProgramElementDoc> it = members.iterator();
469 for (int i = 0; it.hasNext(); i++) {
470 ProgramElementDoc pgmdoc = it.next();
471 ClassDoc cd = pgmdoc.containingClass();
472 if (!printedUseTableHeader) {
473 table.addContent(writer.getSummaryTableHeader(
474 this.getSummaryTableHeader(pgmdoc), "col"));
475 printedUseTableHeader = true;
476 }
477 HtmlTree tr = new HtmlTree(HtmlTag.TR);
478 if (i % 2 == 0) {
479 tr.addStyle(HtmlStyle.altColor);
480 } else {
481 tr.addStyle(HtmlStyle.rowColor);
482 }
483 HtmlTree tdFirst = new HtmlTree(HtmlTag.TD);
484 tdFirst.addStyle(HtmlStyle.colFirst);
485 writer.addSummaryType(this, pgmdoc, tdFirst);
486 tr.addContent(tdFirst);
487 HtmlTree tdLast = new HtmlTree(HtmlTag.TD);
488 tdLast.addStyle(HtmlStyle.colLast);
489 if (cd != null && !(pgmdoc instanceof ConstructorDoc)
490 && !(pgmdoc instanceof ClassDoc)) {
491 HtmlTree name = new HtmlTree(HtmlTag.SPAN);
492 name.addStyle(HtmlStyle.strong);
493 name.addContent(cd.name() + ".");
494 tdLast.addContent(name);
495 }
496 addSummaryLink(pgmdoc instanceof ClassDoc ?
497 LinkInfoImpl.CONTEXT_CLASS_USE : LinkInfoImpl.CONTEXT_MEMBER,
498 cd, pgmdoc, tdLast);
499 writer.addSummaryLinkComment(this, pgmdoc, tdLast);
500 tr.addContent(tdLast);
501 tbody.addContent(tr);
502 }
503 table.addContent(tbody);
504 contentTree.addContent(table);
505 }
506 }
508 /**
509 * Add the navigation detail link.
510 *
511 * @param members the members to be linked
512 * @param liNav the content tree to which the navigation detail link will be added
513 */
514 protected void addNavDetailLink(List<?> members, Content liNav) {
515 addNavDetailLink(members.size() > 0 ? true : false, liNav);
516 }
518 /**
519 * Add the navigation summary link.
520 *
521 * @param members members to be linked
522 * @param visibleMemberMap the visible inherited members map
523 * @param liNav the content tree to which the navigation summary link will be added
524 */
525 protected void addNavSummaryLink(List<?> members,
526 VisibleMemberMap visibleMemberMap, Content liNav) {
527 if (members.size() > 0) {
528 liNav.addContent(getNavSummaryLink(null, true));
529 return;
530 }
531 ClassDoc icd = classdoc.superclass();
532 while (icd != null) {
533 List<?> inhmembers = visibleMemberMap.getMembersFor(icd);
534 if (inhmembers.size() > 0) {
535 liNav.addContent(getNavSummaryLink(icd, true));
536 return;
537 }
538 icd = icd.superclass();
539 }
540 liNav.addContent(getNavSummaryLink(null, false));
541 }
543 protected void serialWarning(SourcePosition pos, String key, String a1, String a2) {
544 if (configuration().serialwarn) {
545 ConfigurationImpl.getInstance().getDocletSpecificMsg().warning(pos, key, a1, a2);
546 }
547 }
549 public ProgramElementDoc[] eligibleMembers(ProgramElementDoc[] members) {
550 return nodepr? Util.excludeDeprecatedMembers(members): members;
551 }
553 public ConfigurationImpl configuration() {
554 return writer.configuration;
555 }
557 /**
558 * Add the member summary for the given class.
559 *
560 * @param classDoc the class that is being documented
561 * @param member the member being documented
562 * @param firstSentenceTags the first sentence tags to be added to the summary
563 * @param tableTree the content tree to which the documentation will be added
564 * @param counter the counter for determing style for the table row
565 */
566 public void addMemberSummary(ClassDoc classDoc, ProgramElementDoc member,
567 Tag[] firstSentenceTags, Content tableTree, int counter) {
568 HtmlTree tdSummaryType = new HtmlTree(HtmlTag.TD);
569 tdSummaryType.addStyle(HtmlStyle.colFirst);
570 writer.addSummaryType(this, member, tdSummaryType);
571 HtmlTree tdSummary = new HtmlTree(HtmlTag.TD);
572 setSummaryColumnStyle(tdSummary);
573 addSummaryLink(classDoc, member, tdSummary);
574 writer.addSummaryLinkComment(this, member, firstSentenceTags, tdSummary);
575 HtmlTree tr = HtmlTree.TR(tdSummaryType);
576 tr.addContent(tdSummary);
577 if (counter%2 == 0)
578 tr.addStyle(HtmlStyle.altColor);
579 else
580 tr.addStyle(HtmlStyle.rowColor);
581 tableTree.addContent(tr);
582 }
584 /**
585 * Set the style for the summary column.
586 *
587 * @param tdTree the column for which the style will be set
588 */
589 public void setSummaryColumnStyle(HtmlTree tdTree) {
590 tdTree.addStyle(HtmlStyle.colLast);
591 }
593 /**
594 * Add inherited member summary for the given class and member.
595 *
596 * @param classDoc the class the inherited member belongs to
597 * @param nestedClass the inherited member that is summarized
598 * @param isFirst true if this is the first member in the list
599 * @param isLast true if this is the last member in the list
600 * @param linksTree the content tree to which the summary will be added
601 */
602 public void addInheritedMemberSummary(ClassDoc classDoc,
603 ProgramElementDoc nestedClass, boolean isFirst, boolean isLast,
604 Content linksTree) {
605 writer.addInheritedMemberSummary(this, classDoc, nestedClass, isFirst,
606 linksTree);
607 }
609 /**
610 * Get the inherited summary header for the given class.
611 *
612 * @param classDoc the class the inherited member belongs to
613 * @return a content tree for the inherited summary header
614 */
615 public Content getInheritedSummaryHeader(ClassDoc classDoc) {
616 Content inheritedTree = writer.getMemberTreeHeader();
617 writer.addInheritedSummaryHeader(this, classDoc, inheritedTree);
618 return inheritedTree;
619 }
621 /**
622 * Get the inherited summary links tree.
623 *
624 * @return a content tree for the inherited summary links
625 */
626 public Content getInheritedSummaryLinksTree() {
627 return new HtmlTree(HtmlTag.CODE);
628 }
630 /**
631 * Get the summary table tree for the given class.
632 *
633 * @param classDoc the class for which the summary table is generated
634 * @return a content tree for the summary table
635 */
636 public Content getSummaryTableTree(ClassDoc classDoc) {
637 return writer.getSummaryTableTree(this, classDoc);
638 }
640 /**
641 * Get the member tree to be documented.
642 *
643 * @param memberTree the content tree of member to be documented
644 * @return a content tree that will be added to the class documentation
645 */
646 public Content getMemberTree(Content memberTree) {
647 return writer.getMemberTree(memberTree);
648 }
650 /**
651 * Get the member tree to be documented.
652 *
653 * @param memberTree the content tree of member to be documented
654 * @param isLastContent true if the content to be added is the last content
655 * @return a content tree that will be added to the class documentation
656 */
657 public Content getMemberTree(Content memberTree, boolean isLastContent) {
658 if (isLastContent)
659 return HtmlTree.UL(HtmlStyle.blockListLast, memberTree);
660 else
661 return HtmlTree.UL(HtmlStyle.blockList, memberTree);
662 }
663 }