1 /*
   2  * Copyright (c) 2001, 2017, 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  */
  25 package jdk.javadoc.internal.doclets.toolkit.util;
  26 
  27 import java.util.*;
  28 
  29 import javax.lang.model.element.Element;
  30 import javax.lang.model.element.PackageElement;
  31 import javax.lang.model.element.TypeElement;
  32 
  33 import jdk.javadoc.internal.doclets.toolkit.BaseConfiguration;
  34 
  35 /**
  36  * This class acts as an artificial container for classes specified on the command line when
  37  * running Javadoc. For example, if you specify several classes from package java.lang, this class
  38  * will catalog those classes so that we can retrieve all of the classes from a particular package
  39  * later.
  40  *
  41  * <p>
  42  * <b>This is NOT part of any supported API. If you write code that depends on this, you do so at
  43  * your own risk. This code and its internal interfaces are subject to change or deletion without
  44  * notice.</b>
  45  *
  46  * @author Jamie Ho
  47  */
  48 public class TypeElementCatalog {
  49 
  50     /**
  51      * Stores the set of packages that the classes specified on the command line belong to. Note
  52      * that the default package is "".
  53      */
  54     private final SortedSet<PackageElement> packageSet;
  55 
  56     /**
  57      * Stores all classes for each package
  58      */
  59     private final Map<PackageElement, SortedSet<TypeElement>> allClasses;
  60 
  61     /**
  62      * Stores ordinary classes (excluding Exceptions and Errors) for each package
  63      */
  64     private final Map<PackageElement, SortedSet<TypeElement>> ordinaryClasses;
  65 
  66     /**
  67      * Stores exceptions for each package
  68      */
  69     private final Map<PackageElement, SortedSet<TypeElement>> exceptions;
  70 
  71     /**
  72      * Stores enums for each package.
  73      */
  74     private final Map<PackageElement, SortedSet<TypeElement>> enums;
  75 
  76     /**
  77      * Stores records for each package.
  78      */
  79     private final Map<PackageElement, SortedSet<TypeElement>> records;
  80 
  81     /**
  82      * Stores annotation types for each package.
  83      */
  84     private final Map<PackageElement, SortedSet<TypeElement>> annotationTypes;
  85 
  86     /**
  87      * Stores errors for each package
  88      */
  89     private final Map<PackageElement, SortedSet<TypeElement>> errors;
  90 
  91     /**
  92      * Stores interfaces for each package
  93      */
  94     private final Map<PackageElement, SortedSet<TypeElement>> interfaces;
  95 
  96     private final BaseConfiguration configuration;
  97     private final Utils utils;
  98     private final Comparator<Element> comparator;
  99 
 100     /**
 101      * Construct a new TypeElementCatalog.
 102      *
 103      * @param typeElements the array of TypeElements to catalog
 104      */
 105     public TypeElementCatalog(Iterable<TypeElement> typeElements, BaseConfiguration config) {
 106         this(config);
 107         for (TypeElement typeElement : typeElements) {
 108             addTypeElement(typeElement);
 109         }
 110     }
 111 
 112     /**
 113      * Construct a new TypeElementCatalog.
 114      *
 115      */
 116     public TypeElementCatalog(BaseConfiguration config) {
 117         this.configuration = config;
 118         this.utils = config.utils;
 119         comparator = utils.makeGeneralPurposeComparator();
 120         allClasses = new HashMap<>();
 121         ordinaryClasses = new HashMap<>();
 122         exceptions = new HashMap<>();
 123         enums = new HashMap<>();
 124         records = new HashMap<>();
 125         annotationTypes = new HashMap<>();
 126         errors = new HashMap<>();
 127         interfaces = new HashMap<>();
 128         packageSet = new TreeSet<>(comparator);
 129     }
 130 
 131     /**
 132      * Add the given class to the catalog.
 133      *
 134      * @param typeElement the TypeElement to add to the catalog.
 135      */
 136     public final void addTypeElement(TypeElement typeElement) {
 137         if (typeElement == null) {
 138             return;
 139         }
 140         addTypeElement(typeElement, allClasses);
 141         if (utils.isOrdinaryClass(typeElement)) {
 142             addTypeElement(typeElement, ordinaryClasses);
 143         } else if (utils.isException(typeElement)) {
 144             addTypeElement(typeElement, exceptions);
 145         } else if (utils.isEnum(typeElement)) {
 146             addTypeElement(typeElement, enums);
 147         } else if (utils.isRecord(typeElement)) {
 148             addTypeElement(typeElement, records);
 149         } else if (utils.isAnnotationType(typeElement)) {
 150             addTypeElement(typeElement, annotationTypes);
 151         } else if (utils.isError(typeElement)) {
 152             addTypeElement(typeElement, errors);
 153         } else if (utils.isInterface(typeElement)) {
 154             addTypeElement(typeElement, interfaces);
 155         }
 156     }
 157 
 158     /**
 159      * Add the given class to the given map.
 160      *
 161      * @param typeElement the class to add to the catalog.
 162      * @param map the Map to add the TypeElement to.
 163      */
 164     private void addTypeElement(TypeElement typeElement, Map<PackageElement, SortedSet<TypeElement>> map) {
 165 
 166         PackageElement pkg = utils.containingPackage(typeElement);
 167         if (utils.isSpecified(pkg) || configuration.nodeprecated && utils.isDeprecated(pkg)) {
 168             // No need to catalog this class if it's package is
 169             // specified on the command line or if -nodeprecated option is set
 170             return;
 171         }
 172 
 173         SortedSet<TypeElement> s = map.get(pkg);
 174         if (s == null) {
 175             packageSet.add(pkg);
 176             s = new TreeSet<>(comparator);
 177         }
 178         s.add(typeElement);
 179         map.put(pkg, s);
 180 
 181     }
 182 
 183     private SortedSet<TypeElement> getSet(Map<PackageElement, SortedSet<TypeElement>> m, PackageElement key) {
 184         SortedSet<TypeElement> s = m.get(key);
 185         if (s != null) {
 186             return s;
 187         }
 188         return new TreeSet<>(comparator);
 189     }
 190     /**
 191      * Return all of the classes specified on the command-line that belong to the given package.
 192      *
 193      * @param packageElement the package to return the classes for.
 194      */
 195     public SortedSet<TypeElement> allClasses(PackageElement packageElement) {
 196         return utils.isSpecified(packageElement)
 197                 ? utils.getTypeElementsAsSortedSet(utils.getEnclosedTypeElements(packageElement))
 198                 : getSet(allClasses, packageElement);
 199     }
 200 
 201     /**
 202      * Return all of the classes specified on the command-line that belong to the unnamed package.
 203      */
 204     public SortedSet<TypeElement> allUnnamedClasses() {
 205         for (PackageElement pkg : allClasses.keySet()) {
 206             if (pkg.isUnnamed()) {
 207                 return allClasses.get(pkg);
 208             }
 209         }
 210         return new TreeSet<>(comparator);
 211     }
 212 
 213     /**
 214      * Return a SortedSet of packages that this catalog stores.
 215      */
 216     public SortedSet<PackageElement> packages() {
 217          return packageSet;
 218     }
 219 
 220     /**
 221      * Return all of the errors specified on the command-line that belong to the given package.
 222      *
 223      * @param pkg the name of the package specified on the command-line.
 224      */
 225     public SortedSet<TypeElement> errors(PackageElement pkg) {
 226         return getSet(errors, pkg);
 227     }
 228 
 229     /**
 230      * Return all of the exceptions specified on the command-line that belong to the given package.
 231      *
 232      * @param pkg the name of the package specified on the command-line.
 233      */
 234     public SortedSet<TypeElement> exceptions(PackageElement pkg) {
 235         return getSet(exceptions, pkg);
 236     }
 237 
 238     /**
 239      * Return all of the enums specified on the command-line that belong to the given package.
 240      *
 241      * @param pkg the name of the package specified on the command-line.
 242      */
 243     public SortedSet<TypeElement> enums(PackageElement pkg) {
 244         return getSet(enums, pkg);
 245     }
 246 
 247     /**
 248      * Return all of the records specified on the command-line that belong to the given package.
 249      *
 250      * @param pkg the name of the package specified on the command-line.
 251      */
 252     public SortedSet<TypeElement> records(PackageElement pkg) {
 253         return getSet(records, pkg);
 254     }
 255 
 256     /**
 257      * Return all of the annotation types specified on the command-line that belong to the given
 258      * package.
 259      *
 260      * @param pkg the name of the package specified on the command-line.
 261      */
 262     public SortedSet<TypeElement> annotationTypes(PackageElement pkg) {
 263         return getSet(annotationTypes, pkg);
 264     }
 265 
 266     /**
 267      * Return all of the interfaces specified on the command-line that belong to the given package.
 268      *
 269      * @param pkg the name of the package specified on the command-line.
 270      */
 271     public SortedSet<TypeElement> interfaces(PackageElement pkg) {
 272         return getSet(interfaces, pkg);
 273     }
 274 
 275     /**
 276      * Return all of the ordinary classes specified on the command-line that belong to the given
 277      * package.
 278      *
 279      * @param pkg the name of the package specified on the command-line.
 280      */
 281     public SortedSet<TypeElement> ordinaryClasses(PackageElement pkg) {
 282         return getSet(ordinaryClasses, pkg);
 283     }
 284 }