src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/ClassDocCatalog.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -20,267 +20,251 @@
  *
  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
+package jdk.javadoc.internal.doclets.toolkit.util;
 
-package com.sun.tools.doclets.internal.toolkit.util;
-
 import java.util.*;
-import com.sun.javadoc.*;
-import com.sun.tools.doclets.internal.toolkit.Configuration;
 
+import javax.lang.model.element.Element;
+import javax.lang.model.element.PackageElement;
+import javax.lang.model.element.TypeElement;
+
+import jdk.javadoc.internal.doclets.toolkit.Configuration;
+
 /**
- * This class acts as an artificial PackageDoc for classes specified
- * on the command line when running Javadoc.  For example, if you
- * specify several classes from package java.lang, this class will catalog
- * those classes so that we can retrieve all of the classes from a particular
- * package later.
+ * This class acts as an artificial container for classes specified on the command line when
+ * running Javadoc. For example, if you specify several classes from package java.lang, this class
+ * will catalog those classes so that we can retrieve all of the classes from a particular package
+ * later.
  *
- *  <p><b>This is NOT part of any supported API.
- *  If you write code that depends on this, you do so at your own risk.
- *  This code and its internal interfaces are subject to change or
- *  deletion without notice.</b>
+ * <p>
+ * <b>This is NOT part of any supported API. If you write code that depends on this, you do so at
+ * your own risk. This code and its internal interfaces are subject to change or deletion without
+ * notice.</b>
  *
  * @author Jamie Ho
  * @since 1.4
  */
+public class ClassDocCatalog {
 
- public class ClassDocCatalog {
-
      /**
-      * Stores the set of packages that the classes specified on the command line
-      * belong to.  Note that the default package is "".
+     * Stores the set of packages that the classes specified on the command line belong to. Note
+     * that the default package is "".
       */
-     private Set<String> packageSet;
+    private final SortedSet<PackageElement> packageSet;
 
-
      /**
       * Stores all classes for each package
       */
-     private Map<String,Set<ClassDoc>> allClasses;
+    private final Map<PackageElement, SortedSet<TypeElement>> allClasses;
 
      /**
-      * Stores ordinary classes (excluding Exceptions and Errors) for each
-      * package
+     * Stores ordinary classes (excluding Exceptions and Errors) for each package
       */
-     private Map<String,Set<ClassDoc>> ordinaryClasses;
+    private final Map<PackageElement, SortedSet<TypeElement>> ordinaryClasses;
 
      /**
       * Stores exceptions for each package
       */
-     private Map<String,Set<ClassDoc>> exceptions;
+    private final Map<PackageElement, SortedSet<TypeElement>> exceptions;
 
     /**
      * Stores enums for each package.
      */
-    private Map<String,Set<ClassDoc>> enums;
+    private final Map<PackageElement, SortedSet<TypeElement>> enums;
 
     /**
      * Stores annotation types for each package.
      */
-    private Map<String,Set<ClassDoc>> annotationTypes;
+    private final Map<PackageElement, SortedSet<TypeElement>> annotationTypes;
 
      /**
       * Stores errors for each package
       */
-     private Map<String,Set<ClassDoc>> errors;
+    private final Map<PackageElement, SortedSet<TypeElement>> errors;
 
      /**
       * Stores interfaces for each package
       */
-     private Map<String,Set<ClassDoc>> interfaces;
+    private final Map<PackageElement, SortedSet<TypeElement>> interfaces;
 
-     private Configuration configuration;
-     private Utils utils;
-
+    private final Configuration configuration;
+    private final Utils utils;
+    private final Comparator<Element> comparator;
      /**
       * Construct a new ClassDocCatalog.
       *
-      * @param classdocs the array of ClassDocs to catalog
+     * @param typeElements the array of ClassDocs to catalog
       */
-     public ClassDocCatalog (ClassDoc[] classdocs, Configuration config) {
-         init();
-         this.configuration = config;
-         this.utils = config.utils;
-         for (ClassDoc classdoc : classdocs) {
-             addClassDoc(classdoc);
+    public ClassDocCatalog(Iterable<TypeElement> typeElements, Configuration config) {
+        this(config);
+        for (TypeElement typeElement : typeElements) {
+            addClassDoc(typeElement);
          }
      }
 
      /**
       * Construct a new ClassDocCatalog.
       *
       */
-     public ClassDocCatalog () {
-         init();
-     }
-
-     private void init() {
+    public ClassDocCatalog(Configuration config) {
+        this.configuration = config;
+        this.utils = config.utils;
+        comparator = utils.makeGeneralPurposeComparator();
          allClasses = new HashMap<>();
          ordinaryClasses = new HashMap<>();
          exceptions = new HashMap<>();
          enums = new HashMap<>();
          annotationTypes = new HashMap<>();
          errors = new HashMap<>();
          interfaces = new HashMap<>();
-         packageSet = new HashSet<>();
+        packageSet = new TreeSet<>(comparator);
      }
 
      /**
       * Add the given class to the catalog.
-      * @param classdoc the ClassDoc to add to the catelog.
+     *
+     * @param typeElement the TypeElement to add to the catalog.
       */
-      public void addClassDoc(ClassDoc classdoc) {
-        if (classdoc == null) {
+    public final void addClassDoc(TypeElement typeElement) {
+        if (typeElement == null) {
             return;
         }
-        addClass(classdoc, allClasses);
-        if (classdoc.isOrdinaryClass()) {
-            addClass(classdoc, ordinaryClasses);
-        } else if (classdoc.isException()) {
-            addClass(classdoc, exceptions);
-        } else if (classdoc.isEnum()) {
-            addClass(classdoc, enums);
-        } else if (classdoc.isAnnotationType()) {
-            addClass(classdoc, annotationTypes);
-        } else if (classdoc.isError()) {
-            addClass(classdoc, errors);
-        } else if (classdoc.isInterface()) {
-            addClass(classdoc, interfaces);
+        addClass(typeElement, allClasses);
+        if (utils.isOrdinaryClass(typeElement)) {
+            addClass(typeElement, ordinaryClasses);
+        } else if (utils.isException(typeElement)) {
+            addClass(typeElement, exceptions);
+        } else if (utils.isEnum(typeElement)) {
+            addClass(typeElement, enums);
+        } else if (utils.isAnnotationType(typeElement)) {
+            addClass(typeElement, annotationTypes);
+        } else if (utils.isError(typeElement)) {
+            addClass(typeElement, errors);
+        } else if (utils.isInterface(typeElement)) {
+            addClass(typeElement, interfaces);
         }
       }
 
       /**
        * Add the given class to the given map.
-       * @param classdoc the ClassDoc to add to the catelog.
-       * @param map the Map to add the ClassDoc to.
+     *
+     * @param typeElement the ClassDoc to add to the catalog.
+     * @param map the Map to add the TypeElement to.
        */
-      private void addClass(ClassDoc classdoc, Map<String,Set<ClassDoc>> map) {
+    private void addClass(TypeElement typeElement, Map<PackageElement, SortedSet<TypeElement>> map) {
 
-          PackageDoc pkg = classdoc.containingPackage();
-          if (pkg.isIncluded() || (configuration.nodeprecated && utils.isDeprecated(pkg))) {
-              //No need to catalog this class if it's package is
-              //included on the command line or if -nodeprecated option is set
+        PackageElement pkg = utils.containingPackage(typeElement);
+        if (utils.isIncluded(pkg) || (configuration.nodeprecated && utils.isDeprecated(pkg))) {
+            // No need to catalog this class if it's package is
+            // included on the command line or if -nodeprecated option is set
               // and the containing package is marked as deprecated.
               return;
           }
-          String key = utils.getPackageName(pkg);
-          Set<ClassDoc> s = map.get(key);
+
+        SortedSet<TypeElement> s = map.get(pkg);
           if (s == null) {
-              packageSet.add(key);
-              s = new HashSet<>();
+            packageSet.add(pkg);
+            s = new TreeSet<>(comparator);
           }
-          s.add(classdoc);
-          map.put(key, s);
+        s.add(typeElement);
+        map.put(pkg, s);
 
       }
 
-      private ClassDoc[] getArray(Map<String,Set<ClassDoc>> m, String key) {
-          Set<ClassDoc> s = m.get(key);
-          if (s == null) {
-              return new ClassDoc[] {};
-          } else {
-              return s.toArray(new ClassDoc[] {});
+    private SortedSet<TypeElement> getSet(Map<PackageElement, SortedSet<TypeElement>> m, PackageElement key) {
+        SortedSet<TypeElement> s = m.get(key);
+        if (s != null) {
+            return s;
           }
+        return new TreeSet<>(comparator);
       }
-
       /**
-       * Return all of the classes specified on the command-line that
-       * belong to the given package.
-       * @param pkgDoc the package to return the classes for.
+     * Return all of the classes specified on the command-line that belong to the given package.
+     *
+     * @param packageElement the package to return the classes for.
        */
-      public ClassDoc[] allClasses(PackageDoc pkgDoc) {
-          return pkgDoc.isIncluded() ?
-                pkgDoc.allClasses() :
-                getArray(allClasses, utils.getPackageName(pkgDoc));
+    public SortedSet<TypeElement> allClasses(PackageElement packageElement) {
+        return utils.isIncluded(packageElement)
+                ? utils.getTypeElementsAsSortedSet(utils.getEnclosedTypeElements(packageElement))
+                : getSet(allClasses, packageElement);
       }
 
       /**
-       * Return all of the classes specified on the command-line that
-       * belong to the given package.
-       * @param packageName the name of the package specified on the
-       * command-line.
+     * Return all of the classes specified on the command-line that belong to the given package.
+     *
+     * @param packageName the name of the package specified on the command-line.
        */
-      public ClassDoc[] allClasses(String packageName) {
-          return getArray(allClasses, packageName);
+    public SortedSet<TypeElement> allUnnamedClasses() {
+        for (PackageElement pkg : allClasses.keySet()) {
+            if (pkg.isUnnamed()) {
+                return allClasses.get(pkg);
       }
-
-     /**
-      * Return the array of package names that this catalog stores
-      * ClassDocs for.
-      */
-     public String[] packageNames() {
-         return packageSet.toArray(new String[] {});
      }
+        return new TreeSet<>(comparator);
+    }
 
      /**
-      * Return true if the given package is known to this catalog.
-      * @param packageName the name to check.
-      * @return true if this catalog has any information about
-      * classes in the given package.
+     * Return a SortedSet of packages that this catalog stores.
       */
-     public boolean isKnownPackage(String packageName) {
-         return packageSet.contains(packageName);
+    public SortedSet<PackageElement> packages() {
+         return packageSet;
      }
 
-
       /**
-       * Return all of the errors specified on the command-line
-       * that belong to the given package.
-       * @param packageName the name of the package specified on the
-       * command-line.
+     * Return all of the errors specified on the command-line that belong to the given package.
+     *
+     * @param packageName the name of the package specified on the command-line.
        */
-      public ClassDoc[] errors(String packageName) {
-          return getArray(errors, packageName);
+    public SortedSet<TypeElement> errors(PackageElement pkg) {
+        return getSet(errors, pkg);
       }
 
       /**
-       * Return all of the exceptions specified on the command-line
-       * that belong to the given package.
-       * @param packageName the name of the package specified on the
-       * command-line.
+     * Return all of the exceptions specified on the command-line that belong to the given package.
+     *
+     * @param packageName the name of the package specified on the command-line.
        */
-      public ClassDoc[] exceptions(String packageName) {
-          return getArray(exceptions, packageName);
+    public SortedSet<TypeElement> exceptions(PackageElement pkg) {
+        return getSet(exceptions, pkg);
       }
 
       /**
-       * Return all of the enums specified on the command-line
-       * that belong to the given package.
-       * @param packageName the name of the package specified on the
-       * command-line.
+     * Return all of the enums specified on the command-line that belong to the given package.
+     *
+     * @param packageName the name of the package specified on the command-line.
        */
-      public ClassDoc[] enums(String packageName) {
-          return getArray(enums, packageName);
+    public SortedSet<TypeElement> enums(PackageElement pkg) {
+        return getSet(enums, pkg);
       }
 
       /**
-       * Return all of the annotation types specified on the command-line
-       * that belong to the given package.
-       * @param packageName the name of the package specified on the
-       * command-line.
+     * Return all of the annotation types specified on the command-line that belong to the given
+     * package.
+     *
+     * @param packageName the name of the package specified on the command-line.
        */
-      public ClassDoc[] annotationTypes(String packageName) {
-          return getArray(annotationTypes, packageName);
+    public SortedSet<TypeElement> annotationTypes(PackageElement pkg) {
+        return getSet(annotationTypes, pkg);
       }
 
       /**
-       * Return all of the interfaces specified on the command-line
-       * that belong to the given package.
-       * @param packageName the name of the package specified on the
-       * command-line.
+     * Return all of the interfaces specified on the command-line that belong to the given package.
+     *
+     * @param packageName the name of the package specified on the command-line.
        */
-      public ClassDoc[] interfaces(String packageName) {
-          return getArray(interfaces, packageName);
+    public SortedSet<TypeElement> interfaces(PackageElement pkg) {
+        return getSet(interfaces, pkg);
       }
 
       /**
-       * Return all of the ordinary classes specified on the command-line
-       * that belong to the given package.
-       * @param packageName the name of the package specified on the
-       * command-line.
+     * Return all of the ordinary classes specified on the command-line that belong to the given
+     * package.
+     *
+     * @param packageName the name of the package specified on the command-line.
        */
-      public ClassDoc[] ordinaryClasses(String packageName) {
-          return getArray(ordinaryClasses, packageName);
+    public SortedSet<TypeElement> ordinaryClasses(PackageElement pkg) {
+        return getSet(ordinaryClasses, pkg);
       }
 }