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

Mon, 02 May 2011 02:13:02 -0700

author
bpatel
date
Mon, 02 May 2011 02:13:02 -0700
changeset 995
62bc3775d5bb
parent 554
9d9f26857129
child 1357
c75be5bc5283
permissions
-rw-r--r--

6492694: @deprecated tag doesn't work in package-info files.
Reviewed-by: jjg

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

mercurial