src/share/classes/com/sun/tools/doclets/internal/toolkit/util/ClassTree.java

Mon, 15 Oct 2012 17:07:55 -0700

author
jjg
date
Mon, 15 Oct 2012 17:07:55 -0700
changeset 1364
8db45b13526e
parent 1359
25e14ad23cef
child 1606
ccbe7ffdd867
permissions
-rw-r--r--

8000666: javadoc should write directly to Writer instead of composing strings
Reviewed-by: bpatel

     1 /*
     2  * Copyright (c) 1998, 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.internal.toolkit.util;
    28 import java.util.*;
    30 import com.sun.javadoc.*;
    31 import com.sun.tools.doclets.internal.toolkit.*;
    33 /**
    34  * Build Class Hierarchy for all the Classes. This class builds the Class
    35  * Tree and the Interface Tree separately.
    36  *
    37  *  <p><b>This is NOT part of any supported API.
    38  *  If you write code that depends on this, you do so at your own risk.
    39  *  This code and its internal interfaces are subject to change or
    40  *  deletion without notice.</b>
    41  *
    42  * @see java.util.HashMap
    43  * @see java.util.List
    44  * @see com.sun.javadoc.Type
    45  * @see com.sun.javadoc.ClassDoc
    46  * @author Atul M Dambalkar
    47  */
    48 public class ClassTree {
    50     /**
    51      * List of baseclasses. Contains only java.lang.Object. Can be used to get
    52      * the mapped listing of sub-classes.
    53      */
    54     private List<ClassDoc> baseclasses = new ArrayList<ClassDoc>();
    56     /**
    57     * Mapping for each Class with their SubClasses
    58     */
    59     private Map<ClassDoc,List<ClassDoc>> subclasses = new HashMap<ClassDoc,List<ClassDoc>>();
    61     /**
    62      * List of base-interfaces. Contains list of all the interfaces who do not
    63      * have super-interfaces. Can be used to get the mapped listing of
    64      * sub-interfaces.
    65      */
    66     private List<ClassDoc> baseinterfaces = new ArrayList<ClassDoc>();
    68     /**
    69     * Mapping for each Interface with their SubInterfaces
    70     */
    71     private Map<ClassDoc,List<ClassDoc>> subinterfaces = new HashMap<ClassDoc,List<ClassDoc>>();
    73     private List<ClassDoc> baseEnums = new ArrayList<ClassDoc>();
    74     private Map<ClassDoc,List<ClassDoc>> subEnums = new HashMap<ClassDoc,List<ClassDoc>>();
    76     private List<ClassDoc> baseAnnotationTypes = new ArrayList<ClassDoc>();
    77     private Map<ClassDoc,List<ClassDoc>> subAnnotationTypes = new HashMap<ClassDoc,List<ClassDoc>>();
    79     /**
    80     * Mapping for each Interface with classes who implement it.
    81     */
    82     private Map<ClassDoc,List<ClassDoc>> implementingclasses = new HashMap<ClassDoc,List<ClassDoc>>();
    84     /**
    85      * Constructor. Build the Tree using the Root of this Javadoc run.
    86      *
    87      * @param configuration the configuration of the doclet.
    88      * @param noDeprecated Don't add deprecated classes in the class tree, if
    89      * true.
    90      */
    91     public ClassTree(Configuration configuration, boolean noDeprecated) {
    92         configuration.message.notice("doclet.Building_Tree");
    93         buildTree(configuration.root.classes(), configuration);
    94     }
    96     /**
    97      * Constructor. Build the Tree using the Root of this Javadoc run.
    98      *
    99      * @param root Root of the Document.
   100      * @param configuration The curren configuration of the doclet.
   101      */
   102     public ClassTree(RootDoc root, Configuration configuration) {
   103         buildTree(root.classes(), configuration);
   104     }
   106     /**
   107      * Constructor. Build the tree for the given array of classes.
   108      *
   109      * @param classes Array of classes.
   110      * @param configuration The curren configuration of the doclet.
   111      */
   112     public ClassTree(ClassDoc[] classes, Configuration configuration) {
   113         buildTree(classes, configuration);
   114     }
   116     /**
   117      * Generate mapping for the sub-classes for every class in this run.
   118      * Return the sub-class list for java.lang.Object which will be having
   119      * sub-class listing for itself and also for each sub-class itself will
   120      * have their own sub-class lists.
   121      *
   122      * @param classes all the classes in this run.
   123      * @param configuration the current configuration of the doclet.
   124      */
   125     private void buildTree(ClassDoc[] classes, Configuration configuration) {
   126         for (int i = 0; i < classes.length; i++) {
   127             // In the tree page (e.g overview-tree.html) do not include
   128             // information of classes which are deprecated or are a part of a
   129             // deprecated package.
   130             if (configuration.nodeprecated &&
   131                     (Util.isDeprecated(classes[i]) ||
   132                     Util.isDeprecated(classes[i].containingPackage()))) {
   133                 continue;
   134             }
   135             if (classes[i].isEnum()) {
   136                 processType(classes[i], configuration, baseEnums, subEnums);
   137             } else if (classes[i].isClass()) {
   138                 processType(classes[i], configuration, baseclasses, subclasses);
   139             } else if (classes[i].isInterface()) {
   140                 processInterface(classes[i]);
   141                 List<ClassDoc> list = implementingclasses.get(classes[i]);
   142                 if (list != null) {
   143                     Collections.sort(list);
   144                 }
   145             } else if (classes[i].isAnnotationType()) {
   146                 processType(classes[i], configuration, baseAnnotationTypes,
   147                     subAnnotationTypes);
   148             }
   149         }
   151         Collections.sort(baseinterfaces);
   152         for (Iterator<List<ClassDoc>> it = subinterfaces.values().iterator(); it.hasNext(); ) {
   153             Collections.sort(it.next());
   154         }
   155         for (Iterator<List<ClassDoc>> it = subclasses.values().iterator(); it.hasNext(); ) {
   156             Collections.sort(it.next());
   157         }
   158     }
   160     /**
   161      * For the class passed map it to it's own sub-class listing.
   162      * For the Class passed, get the super class,
   163      * if superclass is non null, (it is not "java.lang.Object")
   164      *    get the "value" from the hashmap for this key Class
   165      *    if entry not found create one and get that.
   166      *    add this Class as a sub class in the list
   167      *    Recurse till hits java.lang.Object Null SuperClass.
   168      *
   169      * @param cd class for which sub-class mapping to be generated.
   170      * @param configuration the current configurtation of the doclet.
   171      */
   172     private void processType(ClassDoc cd, Configuration configuration,
   173             List<ClassDoc> bases, Map<ClassDoc,List<ClassDoc>> subs) {
   174         ClassDoc superclass = Util.getFirstVisibleSuperClassCD(cd, configuration);
   175         if (superclass != null) {
   176             if (!add(subs, superclass, cd)) {
   177                 return;
   178             } else {
   179                 processType(superclass, configuration, bases, subs);
   180             }
   181         } else {     // cd is java.lang.Object, add it once to the list
   182             if (!bases.contains(cd)) {
   183                 bases.add(cd);
   184             }
   185         }
   186         List<Type> intfacs = Util.getAllInterfaces(cd, configuration);
   187         for (Iterator<Type> iter = intfacs.iterator(); iter.hasNext();) {
   188             add(implementingclasses, iter.next().asClassDoc(), cd);
   189         }
   190     }
   192     /**
   193      * For the interface passed get the interfaces which it extends, and then
   194      * put this interface in the sub-interface list of those interfaces. Do it
   195      * recursively. If a interface doesn't have super-interface just attach
   196      * that interface in the list of all the baseinterfaces.
   197      *
   198      * @param cd Interface under consideration.
   199      */
   200     private void processInterface(ClassDoc cd) {
   201         ClassDoc[] intfacs = cd.interfaces();
   202         if (intfacs.length > 0) {
   203             for (int i = 0; i < intfacs.length; i++) {
   204                 if (!add(subinterfaces, intfacs[i], cd)) {
   205                     return;
   206                 } else {
   207                     processInterface(intfacs[i]);   // Recurse
   208                 }
   209             }
   210         } else {
   211             // we need to add all the interfaces who do not have
   212             // super-interfaces to baseinterfaces list to traverse them
   213             if (!baseinterfaces.contains(cd)) {
   214                 baseinterfaces.add(cd);
   215             }
   216         }
   217     }
   219     /**
   220      * Adjust the Class Tree. Add the class interface  in to it's super-class'
   221      * or super-interface's sub-interface list.
   222      *
   223      * @param map the entire map.
   224      * @param superclass java.lang.Object or the super-interface.
   225      * @param cd sub-interface to be mapped.
   226      * @returns boolean true if class added, false if class already processed.
   227      */
   228     private boolean add(Map<ClassDoc,List<ClassDoc>> map, ClassDoc superclass, ClassDoc cd) {
   229         List<ClassDoc> list = map.get(superclass);
   230         if (list == null) {
   231             list = new ArrayList<ClassDoc>();
   232             map.put(superclass, list);
   233         }
   234         if (list.contains(cd)) {
   235             return false;
   236         } else {
   237             list.add(cd);
   238         }
   239         return true;
   240     }
   242     /**
   243      * From the map return the list of sub-classes or sub-interfaces. If list
   244      * is null create a new one and return it.
   245      *
   246      * @param map The entire map.
   247      * @param cd class for which the sub-class list is requested.
   248      * @returns List Sub-Class list for the class passed.
   249      */
   250     private List<ClassDoc> get(Map<ClassDoc,List<ClassDoc>> map, ClassDoc cd) {
   251         List<ClassDoc> list = map.get(cd);
   252         if (list == null) {
   253             return new ArrayList<ClassDoc>();
   254         }
   255         return list;
   256     }
   258     /**
   259      *  Return the sub-class list for the class passed.
   260      *
   261      * @param cd class whose sub-class list is required.
   262      */
   263     public List<ClassDoc> subclasses(ClassDoc cd) {
   264         return get(subclasses, cd);
   265     }
   267     /**
   268      *  Return the sub-interface list for the interface passed.
   269      *
   270      * @param cd interface whose sub-interface list is required.
   271      */
   272     public List<ClassDoc> subinterfaces(ClassDoc cd) {
   273         return get(subinterfaces, cd);
   274     }
   276     /**
   277      *  Return the list of classes which implement the interface passed.
   278      *
   279      * @param cd interface whose implementing-classes list is required.
   280      */
   281     public List<ClassDoc> implementingclasses(ClassDoc cd) {
   282         List<ClassDoc> result = get(implementingclasses, cd);
   283         List<ClassDoc> subinterfaces = allSubs(cd, false);
   285         //If class x implements a subinterface of cd, then it follows
   286         //that class x implements cd.
   287         Iterator<ClassDoc> implementingClassesIter, subInterfacesIter = subinterfaces.listIterator();
   288         ClassDoc c;
   289         while(subInterfacesIter.hasNext()){
   290             implementingClassesIter = implementingclasses(
   291                     subInterfacesIter.next()).listIterator();
   292             while(implementingClassesIter.hasNext()){
   293                 c = implementingClassesIter.next();
   294                 if(! result.contains(c)){
   295                     result.add(c);
   296                 }
   297             }
   298         }
   299         Collections.sort(result);
   300         return result;
   301     }
   303     /**
   304      *  Return the sub-class/interface list for the class/interface passed.
   305      *
   306      * @param cd class/interface whose sub-class/interface list is required.
   307      * @param isEnum true if the subclasses should be forced to come from the
   308      * enum tree.
   309      */
   310     public List<ClassDoc> subs(ClassDoc cd, boolean isEnum) {
   311         if (isEnum) {
   312             return get(subEnums, cd);
   313         } else if (cd.isAnnotationType()) {
   314             return get(subAnnotationTypes, cd);
   315         } else if (cd.isInterface()) {
   316             return get(subinterfaces, cd);
   317         } else if (cd.isClass()) {
   318             return get(subclasses, cd);
   319         } else {
   320             return null;
   321         }
   323     }
   325     /**
   326      * Return a list of all direct or indirect, sub-classes and subinterfaces
   327      * of the ClassDoc argument.
   328      *
   329      * @param cd ClassDoc whose sub-classes or sub-interfaces are requested.
   330      * @param isEnum true if the subclasses should be forced to come from the
   331      * enum tree.
   332      */
   333     public List<ClassDoc> allSubs(ClassDoc cd, boolean isEnum) {
   334         List<ClassDoc> list = subs(cd, isEnum);
   335         for (int i = 0; i < list.size(); i++) {
   336             cd = list.get(i);
   337             List<ClassDoc> tlist = subs(cd, isEnum);
   338             for (int j = 0; j < tlist.size(); j++) {
   339                 ClassDoc tcd = tlist.get(j);
   340                 if (!list.contains(tcd)) {
   341                     list.add(tcd);
   342                 }
   343             }
   344         }
   345         Collections.sort(list);
   346         return list;
   347     }
   349     /**
   350      *  Return the base-classes list. This will have only one element namely
   351      *  thw classdoc for java.lang.Object, since this is the base class for all
   352      *  classes.
   353      */
   354     public List<ClassDoc> baseclasses() {
   355         return baseclasses;
   356     }
   358     /**
   359      *  Return the list of base interfaces. This is the list of interfaces
   360      *  which do not have super-interface.
   361      */
   362     public List<ClassDoc> baseinterfaces() {
   363         return baseinterfaces;
   364     }
   366     /**
   367      *  Return the list of base enums. This is the list of enums
   368      *  which do not have super-enums.
   369      */
   370     public List<ClassDoc> baseEnums() {
   371         return baseEnums;
   372     }
   374     /**
   375      *  Return the list of base annotation types. This is the list of
   376      *  annotation types which do not have super-annotation types.
   377      */
   378     public List<ClassDoc> baseAnnotationTypes() {
   379         return baseAnnotationTypes;
   380     }
   381 }

mercurial