home *** CD-ROM | disk | FTP | other *** search
/ Internet News 1999 October / INEWS_10_CD.ISO / pc / jdk / jdk1.2.2 / docs / tooldocs / javadoc / source / standard / MethodSubWriter.java < prev    next >
Encoding:
Java Source  |  1999-09-19  |  8.6 KB  |  253 lines

  1. /*
  2.  * @(#)MethodSubWriter.java    1.19 98/08/05
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package com.sun.tools.doclets.standard;
  16.  
  17. import java.util.*;
  18. import com.sun.tools.doclets.*;
  19. import com.sun.javadoc.*;
  20.  
  21. /**
  22.  *
  23.  * @author Robert Field
  24.  * @author Atul M Dambalkar
  25.  */
  26. public class MethodSubWriter extends ExecutableMemberSubWriter {
  27.  
  28.     MethodSubWriter(SubWriterHolderWriter writer) {
  29.         super(writer);
  30.     }
  31.  
  32.     public ProgramElementDoc[] members(ClassDoc cd) {
  33.         return eligibleMembers(cd.methods());
  34.     }
  35.  
  36.     public void printSummaryLabel(ClassDoc cd) {
  37.         writer.boldText("doclet.Method_Summary"); 
  38.     }
  39.  
  40.     public void printSummaryAnchor(ClassDoc cd) {
  41.         writer.anchor("method_summary");
  42.     }
  43.    
  44.     public void printInheritedMembersSummary(ClassDoc cd) {
  45.         List tcms = Group.asList(cd.methods()); // this class' methods
  46.         Map imsmap = new HashMap();   // inherited methods
  47.         List hierarchy = new ArrayList();
  48.         if (cd.isClass()) {
  49.             imsmap = getInheritedMethodMapForClass(cd, tcms, imsmap, hierarchy);
  50.         } else {
  51.             imsmap = getInheritedMethodMapForInterface(cd, tcms, imsmap,
  52.                                                        hierarchy);
  53.         }
  54.         if (hierarchy.size() > 0) {
  55.             for (int i = 0; i < hierarchy.size(); i++) {
  56.                 ClassDoc classkey = (ClassDoc)hierarchy.get(i);
  57.                 List methodlist = (List)imsmap.get(classkey);
  58.                 printInheritedMembersInfo(classkey, methodlist);
  59.             }
  60.         }
  61.     }
  62.  
  63.  
  64.     protected void printInheritedMembersInfo(ClassDoc icd, List members) {
  65.         if (members.size() > 0) {
  66.             Collections.sort(members);
  67.             printInheritedSummaryHeader(icd);
  68.             printInheritedSummaryMember(icd, (ProgramElementDoc)members.get(0));
  69.             for (int i = 1; i < members.size(); ++i) {
  70.                 writer.println(", ");
  71.                 printInheritedSummaryMember(icd, 
  72.                                             (ProgramElementDoc)members.get(i));
  73.             }
  74.             printInheritedSummaryFooter(icd);
  75.         }
  76.     }
  77.  
  78.  
  79.     public void printInheritedSummaryAnchor(ClassDoc cd) {
  80.         writer.anchor("methods_inherited_from_class_" + cd.qualifiedName());
  81.     }   
  82.     
  83.     public void printInheritedSummaryLabel(ClassDoc cd) {
  84.         String classlink = writer.getPreQualifiedClassLink(cd);
  85.         writer.bold();
  86.         if (cd.isClass()) {
  87.             writer.printText("doclet.Methods_Inherited_From_Class", classlink);
  88.         } else {
  89.             writer.printText("doclet.Methods_Inherited_From_Interface",
  90.                               classlink);
  91.         } 
  92.         writer.boldEnd();
  93.     }
  94.  
  95.     protected void printSummaryType(ProgramElementDoc member) {
  96.         MethodDoc meth = (MethodDoc)member;
  97.         printModifierAndType(meth, meth.returnType());
  98.     }
  99.  
  100.     protected void printReturnTag(Tag[] returns) {
  101.         if (returns.length > 0) {
  102.             writer.dt();
  103.             writer.boldText("doclet.Returns");
  104.             writer.dd();
  105.             writer.printInlineComment(returns[0]);
  106.         }
  107.     }
  108.  
  109.     protected void printOverridden(ClassDoc overridden, MethodDoc method) {
  110.         if (overridden != null) {
  111.             String overriddenclasslink = writer.getClassLink(overridden);
  112.             String methlink = "";
  113.             String name = method.name();
  114.             writer.dt();
  115.             writer.boldText("doclet.Overrides");
  116.             writer.dd();
  117.             methlink = writer.getClassLink(overridden, 
  118.                                            name + method.signature(), 
  119.                                            name, false);
  120.             writer.printText("doclet.in_class", methlink, overriddenclasslink);
  121.         }
  122.     }
  123.  
  124.     protected void printTags(ProgramElementDoc member) {
  125.         MethodDoc method = (MethodDoc)member;
  126.         ParamTag[] params = method.paramTags();
  127.         Tag[] returns = method.tags("return");
  128.         Tag[] sinces = method.tags("since");
  129.         ThrowsTag[] thrown = method.throwsTags();
  130.         SeeTag[] sees = method.seeTags();
  131.         ClassDoc[] intfacs = member.containingClass().interfaces();
  132.         ClassDoc overridden = method.overriddenClass();
  133.         if (params.length + returns.length + thrown.length + sinces.length
  134.             + intfacs.length + sees.length > 0 ||
  135.             overridden != null) {
  136.             writer.dd();
  137.             writer.dl();
  138.             printImplementsInfo(method);
  139.             printParamTags(params);
  140.             printReturnTag(returns);
  141.             printThrowsTags(thrown);
  142.             printOverridden(overridden, method);
  143.             writer.printSinceTag(method);
  144.             writer.printSeeTags(method);
  145.             writer.dlEnd();
  146.             writer.ddEnd();
  147.         }
  148.     }
  149.                                
  150.     protected void printSignature(ExecutableMemberDoc member) {
  151.         writer.displayLength = 0;
  152.     writer.pre();
  153.     printModifiers(member);
  154.     printReturnType((MethodDoc)member);
  155.     bold(member.name());
  156.     printParameters(member);
  157.     printExceptions(member);
  158.     writer.preEnd();
  159.     }
  160.       
  161.     protected void printReturnType(MethodDoc method) {
  162.         Type type = method.returnType();
  163.         if (type != null) {
  164.             printTypeLink(type);
  165.             print(' ');
  166.         }
  167.     }
  168.     
  169.     protected void printHeader(ClassDoc cd) {
  170.         writer.anchor("method_detail");
  171.         writer.printTableHeadingBackground(writer.
  172.                                               getText("doclet.Method_Detail"));
  173.     }
  174.  
  175.     protected void printNavSummaryLink(ClassDoc cd, boolean link) {
  176.         if (link) {
  177.             writer.printHyperLink("", (cd == null)?
  178.                                           "method_summary":
  179.                                           "methods_inherited_from_class_" +
  180.                                            cd.qualifiedName(),
  181.                                       writer.getText("doclet.navMethod"));
  182.         } else {
  183.             writer.printText("doclet.navMethod");
  184.         }
  185.     }
  186.  
  187.     protected void printNavDetailLink(boolean link) {
  188.         if (link) {
  189.             writer.printHyperLink("", "method_detail",
  190.                                   writer.getText("doclet.navMethod"));
  191.         } else {
  192.             writer.printText("doclet.navMethod");
  193.         }
  194.     }
  195.  
  196.     protected Map getInheritedMethodMapForClass(ClassDoc cd, List tcms, 
  197.                                                 Map imsmap, List hierarchy) {
  198.         ClassDoc icd = cd.superclass();
  199.         while (icd != null) {
  200.             composeInheritedMethodMap(icd, tcms, imsmap, hierarchy);
  201.             icd = icd.superclass();
  202.         }
  203.         return imsmap;
  204.     }
  205.  
  206.     // IMPORTANT: This code assumes that there is no ambiguity for a method
  207.     // in the extended interfaces. If there is an ambiguity, Java compiler will
  208.     // give compilation error for such a case. So the Java code to be
  209.     // documented has to be compile time error free.
  210.  
  211.     protected Map getInheritedMethodMapForInterface(ClassDoc cd, List tims, 
  212.                                                   Map imsmap, List hierarchy) {
  213.         ClassDoc[] iin = cd.interfaces(); // implemented interfaces
  214.         for (int i = 0; i < iin.length; i++) {
  215.             composeInheritedMethodMap(iin[i], tims, imsmap, hierarchy);
  216.         }
  217.         for (int i = 0; i < iin.length; i++) {
  218.             getInheritedMethodMapForInterface(iin[i], tims, imsmap, hierarchy);
  219.         }
  220.         return imsmap;
  221.     }
  222.  
  223.     protected void composeInheritedMethodMap(ClassDoc icd, List tcms, 
  224.                                              Map imsmap, List hierarchy) {
  225.         MethodDoc[] methods = icd.methods();
  226.         List methodlist = new ArrayList();
  227.         for (int i = 0; i < methods.length; i++) {
  228.             if (!contains(tcms, methods[i])) {
  229.                 methodlist.add(methods[i]);
  230.                 tcms.add(methods[i]);
  231.             }
  232.         }
  233.         imsmap.put(icd, methodlist);
  234.         hierarchy.add(icd);
  235.     }
  236.  
  237.     /**
  238.      * The simple search. Order `n'.
  239.      */
  240.     protected boolean contains(List tcmethods, MethodDoc method) {
  241.         for (int i = 0; i < tcmethods.size(); i++) {
  242.             MethodDoc tcmethod = (MethodDoc)tcmethods.get(i);
  243.             if (tcmethod.name().equals(method.name()) &&
  244.                    tcmethod.signature().equals(method.signature())) {
  245.                 return true;
  246.             }
  247.         }
  248.         return false;
  249.     }
  250. }  
  251.     
  252.     
  253.