src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlDocletWriter.java
changeset 55197 0ca8e495bbba
parent 54936 8c63164bd540
child 55266 fb250e9cfe67
equal deleted inserted replaced
55196:7586561128e3 55197:0ca8e495bbba
  1713      * @param packageElement the package to write annotations for.
  1713      * @param packageElement the package to write annotations for.
  1714      * @param htmltree the documentation tree to which the annotation info will be
  1714      * @param htmltree the documentation tree to which the annotation info will be
  1715      *        added
  1715      *        added
  1716      */
  1716      */
  1717     public void addAnnotationInfo(PackageElement packageElement, Content htmltree) {
  1717     public void addAnnotationInfo(PackageElement packageElement, Content htmltree) {
  1718         addAnnotationInfo(packageElement, packageElement.getAnnotationMirrors(), htmltree);
  1718         addAnnotationInfo(packageElement.getAnnotationMirrors(), htmltree);
  1719     }
       
  1720 
       
  1721     /**
       
  1722      * Add the annotation types of the executable receiver.
       
  1723      *
       
  1724      * @param method the executable to write the receiver annotations for.
       
  1725      * @param descList a list of annotation mirrors.
       
  1726      * @param htmltree the documentation tree to which the annotation info will be
       
  1727      *        added
       
  1728      */
       
  1729     public void addReceiverAnnotationInfo(ExecutableElement method, List<AnnotationMirror> descList,
       
  1730             Content htmltree) {
       
  1731         addAnnotationInfo(0, method, descList, false, htmltree);
       
  1732     }
  1719     }
  1733 
  1720 
  1734     /*
  1721     /*
  1735      * this is a hack to delay dealing with Annotations in the writers, the assumption
  1722      * this is a hack to delay dealing with Annotations in the writers, the assumption
  1736      * is that all necessary checks have been made to get here.
  1723      * is that all necessary checks have been made to get here.
  1737      */
  1724      */
  1738     public void addReceiverAnnotationInfo(ExecutableElement method, TypeMirror rcvrTypeMirror,
  1725     public void addReceiverAnnotationInfo(ExecutableElement method, TypeMirror rcvrTypeMirror,
  1739             List<? extends AnnotationMirror> annotationMirrors, Content htmltree) {
  1726             List<? extends AnnotationMirror> annotationMirrors, Content htmltree) {
  1740         TypeMirror rcvrType = method.getReceiverType();
  1727         TypeMirror rcvrType = method.getReceiverType();
  1741         List<? extends AnnotationMirror> annotationMirrors1 = rcvrType.getAnnotationMirrors();
  1728         List<? extends AnnotationMirror> annotationMirrors1 = rcvrType.getAnnotationMirrors();
  1742         addAnnotationInfo(0, method, annotationMirrors1, false, htmltree);
  1729         htmltree.add(getAnnotationInfo(annotationMirrors1, false));
  1743     }
  1730     }
  1744 
  1731 
  1745     /**
  1732     /**
  1746      * Adds the annotation types for the given element.
  1733      * Adds the annotation types for the given element.
  1747      *
  1734      *
  1748      * @param element the package to write annotations for
  1735      * @param element the package to write annotations for
  1749      * @param htmltree the content tree to which the annotation types will be added
  1736      * @param htmltree the content tree to which the annotation types will be added
  1750      */
  1737      */
  1751     public void addAnnotationInfo(Element element, Content htmltree) {
  1738     public void addAnnotationInfo(Element element, Content htmltree) {
  1752         addAnnotationInfo(element, element.getAnnotationMirrors(), htmltree);
  1739         addAnnotationInfo(element.getAnnotationMirrors(), htmltree);
  1753     }
  1740     }
  1754 
  1741 
  1755     /**
  1742     /**
  1756      * Add the annotatation types for the given element and parameter.
  1743      * Add the annotatation types for the given element and parameter.
  1757      *
  1744      *
  1758      * @param indent the number of spaces to indent the parameters.
       
  1759      * @param element the element to write annotations for.
       
  1760      * @param param the parameter to write annotations for.
  1745      * @param param the parameter to write annotations for.
  1761      * @param tree the content tree to which the annotation types will be added
  1746      * @param tree the content tree to which the annotation types will be added
  1762      */
  1747      */
  1763     public boolean addAnnotationInfo(int indent, Element element, VariableElement param,
  1748     public boolean addAnnotationInfo(VariableElement param, Content tree) {
  1764             Content tree) {
  1749         Content annotaionInfo = getAnnotationInfo(param.getAnnotationMirrors(), false);
  1765         return addAnnotationInfo(indent, element, param.getAnnotationMirrors(), false, tree);
  1750         if (annotaionInfo.isEmpty()) {
       
  1751             return false;
       
  1752         }
       
  1753         tree.add(annotaionInfo);
       
  1754         return true;
  1766     }
  1755     }
  1767 
  1756 
  1768     /**
  1757     /**
  1769      * Adds the annotatation types for the given Element.
  1758      * Adds the annotatation types for the given Element.
  1770      *
  1759      *
  1771      * @param element the element to write annotations for.
       
  1772      * @param descList a list of annotation mirrors.
  1760      * @param descList a list of annotation mirrors.
  1773      * @param htmltree the documentation tree to which the annotation info will be
  1761      * @param htmltree the documentation tree to which the annotation info will be
  1774      *        added
  1762      *        added
  1775      */
  1763      */
  1776     private void addAnnotationInfo(Element element, List<? extends AnnotationMirror> descList,
  1764     private void addAnnotationInfo(List<? extends AnnotationMirror> descList, Content htmltree) {
  1777             Content htmltree) {
  1765         htmltree.add(getAnnotationInfo(descList, true));
  1778         addAnnotationInfo(0, element, descList, true, htmltree);
  1766     }
  1779     }
  1767 
  1780 
  1768     /**
  1781     /**
  1769      * Return a content tree containing the annotation types for the given element.
  1782      * Adds the annotation types for the given element.
  1770      *
  1783      *
       
  1784      * @param indent the number of extra spaces to indent the annotations.
       
  1785      * @param element the element to write annotations for.
       
  1786      * @param descList a list of annotation mirrors.
  1771      * @param descList a list of annotation mirrors.
  1787      * @param htmltree the documentation tree to which the annotation info will be
  1772      * @return the documentation tree containing the annotation info.
  1788      *        added
  1773      */
  1789      */
  1774     Content getAnnotationInfo(List<? extends AnnotationMirror> descList, boolean lineBreak) {
  1790     private boolean addAnnotationInfo(int indent, Element element,
  1775         List<Content> annotations = getAnnotations(descList, lineBreak);
  1791             List<? extends AnnotationMirror> descList, boolean lineBreak, Content htmltree) {
       
  1792         List<Content> annotations = getAnnotations(indent, descList, lineBreak);
       
  1793         String sep = "";
  1776         String sep = "";
  1794         if (annotations.isEmpty()) {
  1777         ContentBuilder builder = new ContentBuilder();
  1795             return false;
       
  1796         }
       
  1797         for (Content annotation: annotations) {
  1778         for (Content annotation: annotations) {
  1798             htmltree.add(sep);
  1779             builder.add(sep);
  1799             htmltree.add(annotation);
  1780             builder.add(annotation);
  1800             if (!lineBreak) {
  1781             if (!lineBreak) {
  1801                 sep = " ";
  1782                 sep = " ";
  1802             }
  1783             }
  1803         }
  1784         }
  1804         return true;
  1785         return builder;
  1805     }
  1786     }
  1806 
  1787 
  1807    /**
  1788     /**
  1808      * Return the string representations of the annotation types for
  1789      * Return the string representations of the annotation types for
  1809      * the given doc.
  1790      * the given doc.
  1810      *
  1791      *
  1811      * @param indent the number of extra spaces to indent the annotations.
       
  1812      * @param descList a list of annotation mirrors.
  1792      * @param descList a list of annotation mirrors.
  1813      * @param linkBreak if true, add new line between each member value.
  1793      * @param linkBreak if true, add new line between each member value.
  1814      * @return a list of strings representing the annotations being
  1794      * @return a list of strings representing the annotations being
  1815      *         documented.
  1795      *         documented.
  1816      */
  1796      */
  1817     private List<Content> getAnnotations(int indent, List<? extends AnnotationMirror> descList, boolean linkBreak) {
  1797     public List<Content> getAnnotations(List<? extends AnnotationMirror> descList, boolean linkBreak) {
  1818         return getAnnotations(indent, descList, linkBreak, true);
       
  1819     }
       
  1820 
       
  1821     private List<Content> getAnnotations(int indent, AnnotationMirror amirror, boolean linkBreak) {
       
  1822         List<AnnotationMirror> descList = new ArrayList<>();
       
  1823         descList.add(amirror);
       
  1824         return getAnnotations(indent, descList, linkBreak, true);
       
  1825     }
       
  1826 
       
  1827     /**
       
  1828      * Return the string representations of the annotation types for
       
  1829      * the given doc.
       
  1830      *
       
  1831      * A {@code null} {@code elementType} indicates that all the
       
  1832      * annotations should be returned without any filtering.
       
  1833      *
       
  1834      * @param indent the number of extra spaces to indent the annotations.
       
  1835      * @param descList a list of annotation mirrors.
       
  1836      * @param linkBreak if true, add new line between each member value.
       
  1837      * @param isJava5DeclarationLocation
       
  1838      * @return a list of strings representing the annotations being
       
  1839      *         documented.
       
  1840      */
       
  1841     public List<Content> getAnnotations(int indent, List<? extends AnnotationMirror> descList,
       
  1842             boolean linkBreak, boolean isJava5DeclarationLocation) {
       
  1843         List<Content> results = new ArrayList<>();
  1798         List<Content> results = new ArrayList<>();
  1844         ContentBuilder annotation;
  1799         ContentBuilder annotation;
  1845         for (AnnotationMirror aDesc : descList) {
  1800         for (AnnotationMirror aDesc : descList) {
  1846             TypeElement annotationElement = (TypeElement)aDesc.getAnnotationType().asElement();
  1801             TypeElement annotationElement = (TypeElement)aDesc.getAnnotationType().asElement();
  1847             // If an annotation is not documented, do not add it to the list. If
  1802             // If an annotation is not documented, do not add it to the list. If
  1851             // but its container is documented, it will be added to the list.
  1806             // but its container is documented, it will be added to the list.
  1852             if (!utils.isDocumentedAnnotation(annotationElement) &&
  1807             if (!utils.isDocumentedAnnotation(annotationElement) &&
  1853                 (!isAnnotationDocumented && !isContainerDocumented)) {
  1808                 (!isAnnotationDocumented && !isContainerDocumented)) {
  1854                 continue;
  1809                 continue;
  1855             }
  1810             }
  1856             /* TODO: check logic here to correctly handle declaration
       
  1857              * and type annotations.
       
  1858             if  (utils.isDeclarationAnnotation(annotationElement, isJava5DeclarationLocation)) {
       
  1859                 continue;
       
  1860             }*/
       
  1861             annotation = new ContentBuilder();
  1811             annotation = new ContentBuilder();
  1862             isAnnotationDocumented = false;
  1812             isAnnotationDocumented = false;
  1863             LinkInfoImpl linkInfo = new LinkInfoImpl(configuration,
  1813             LinkInfoImpl linkInfo = new LinkInfoImpl(configuration,
  1864                                                      LinkInfoImpl.Kind.ANNOTATION, annotationElement);
  1814                                                      LinkInfoImpl.Kind.ANNOTATION, annotationElement);
  1865             Map<? extends ExecutableElement, ? extends AnnotationValue> pairs = aDesc.getElementValues();
  1815             Map<? extends ExecutableElement, ? extends AnnotationValue> pairs = aDesc.getElementValues();
  1898                     List<AnnotationValue> annotationTypeValues = new ArrayList<>();
  1848                     List<AnnotationValue> annotationTypeValues = new ArrayList<>();
  1899                     for (AnnotationValue a :  pairs.values()) {
  1849                     for (AnnotationValue a :  pairs.values()) {
  1900                         new SimpleAnnotationValueVisitor9<Void, List<AnnotationValue>>() {
  1850                         new SimpleAnnotationValueVisitor9<Void, List<AnnotationValue>>() {
  1901                             @Override
  1851                             @Override
  1902                             public Void visitArray(List<? extends AnnotationValue> vals, List<AnnotationValue> annotationTypeValues) {
  1852                             public Void visitArray(List<? extends AnnotationValue> vals, List<AnnotationValue> annotationTypeValues) {
  1903                                for (AnnotationValue av : vals) {
  1853                                annotationTypeValues.addAll(vals);
  1904                                    annotationTypeValues.add(av);
       
  1905                                }
       
  1906                                return null;
  1854                                return null;
  1907                             }
  1855                             }
  1908                         }.visit(a, annotationTypeValues);
  1856                         }.visit(a, annotationTypeValues);
  1909                     }
  1857                     }
  1910                     String sep = "";
  1858                     String sep = "";
  1915                     }
  1863                     }
  1916                 }
  1864                 }
  1917                 // If the container has 1 or more value defined and if the
  1865                 // If the container has 1 or more value defined and if the
  1918                 // repeatable type annotation is not documented, print the container.
  1866                 // repeatable type annotation is not documented, print the container.
  1919                 else {
  1867                 else {
  1920                     addAnnotations(annotationElement, linkInfo, annotation, pairs,
  1868                     addAnnotations(annotationElement, linkInfo, annotation, pairs, false);
  1921                                    indent, false);
       
  1922                 }
  1869                 }
  1923             }
  1870             }
  1924             else {
  1871             else {
  1925                 addAnnotations(annotationElement, linkInfo, annotation, pairs,
  1872                 addAnnotations(annotationElement, linkInfo, annotation, pairs, linkBreak);
  1926                                indent, linkBreak);
       
  1927             }
  1873             }
  1928             annotation.add(linkBreak ? DocletConstants.NL : "");
  1874             annotation.add(linkBreak ? DocletConstants.NL : "");
  1929             results.add(annotation);
  1875             results.add(annotation);
  1930         }
  1876         }
  1931         return results;
  1877         return results;
  1936      *
  1882      *
  1937      * @param annotationDoc the annotation being documented
  1883      * @param annotationDoc the annotation being documented
  1938      * @param linkInfo the information about the link
  1884      * @param linkInfo the information about the link
  1939      * @param annotation the annotation string to which the annotation will be added
  1885      * @param annotation the annotation string to which the annotation will be added
  1940      * @param map annotation type element to annotation value pairs
  1886      * @param map annotation type element to annotation value pairs
  1941      * @param indent the number of extra spaces to indent the annotations.
       
  1942      * @param linkBreak if true, add new line between each member value
  1887      * @param linkBreak if true, add new line between each member value
  1943      */
  1888      */
  1944     private void addAnnotations(TypeElement annotationDoc, LinkInfoImpl linkInfo,
  1889     private void addAnnotations(TypeElement annotationDoc, LinkInfoImpl linkInfo,
  1945                                 ContentBuilder annotation,
  1890                                 ContentBuilder annotation,
  1946                                 Map<? extends ExecutableElement, ? extends AnnotationValue> map,
  1891                                 Map<? extends ExecutableElement, ? extends AnnotationValue> map,
  1947                                 int indent, boolean linkBreak) {
  1892                                 boolean linkBreak) {
  1948         linkInfo.label = new StringContent("@");
  1893         linkInfo.label = new StringContent("@");
  1949         linkInfo.label.add(annotationDoc.getSimpleName());
  1894         linkInfo.label.add(annotationDoc.getSimpleName());
  1950         annotation.add(getLink(linkInfo));
  1895         annotation.add(getLink(linkInfo));
  1951         if (!map.isEmpty()) {
  1896         if (!map.isEmpty()) {
  1952             annotation.add("(");
  1897             annotation.add("(");
  1959                 } else {
  1904                 } else {
  1960                     annotation.add(",");
  1905                     annotation.add(",");
  1961                     if (linkBreak) {
  1906                     if (linkBreak) {
  1962                         annotation.add(DocletConstants.NL);
  1907                         annotation.add(DocletConstants.NL);
  1963                         int spaces = annotationDoc.getSimpleName().length() + 2;
  1908                         int spaces = annotationDoc.getSimpleName().length() + 2;
  1964                         for (int k = 0; k < (spaces + indent); k++) {
  1909                         for (int k = 0; k < (spaces); k++) {
  1965                             annotation.add(" ");
  1910                             annotation.add(" ");
  1966                         }
  1911                         }
  1967                     }
  1912                     }
  1968                 }
  1913                 }
  1969                 String simpleName = element.getSimpleName().toString();
  1914                 String simpleName = element.getSimpleName().toString();
  2072                     }
  2017                     }
  2073                 }.visit(t);
  2018                 }.visit(t);
  2074             }
  2019             }
  2075             @Override
  2020             @Override
  2076             public Content visitAnnotation(AnnotationMirror a, Void p) {
  2021             public Content visitAnnotation(AnnotationMirror a, Void p) {
  2077                 List<Content> list = getAnnotations(0, a, false);
  2022                 List<Content> list = getAnnotations(List.of(a), false);
  2078                 ContentBuilder buf = new ContentBuilder();
  2023                 ContentBuilder buf = new ContentBuilder();
  2079                 for (Content c : list) {
  2024                 for (Content c : list) {
  2080                     buf.add(c);
  2025                     buf.add(c);
  2081                 }
  2026                 }
  2082                 return buf;
  2027                 return buf;