1 /*
   2  * Copyright (c) 2013, 2016, 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 
  26 package jdk.javadoc.internal.doclets.toolkit.builders;
  27 
  28 import java.util.*;
  29 
  30 import javax.lang.model.element.Element;
  31 import javax.lang.model.element.TypeElement;
  32 
  33 import jdk.javadoc.internal.doclets.toolkit.AnnotationTypeFieldWriter;
  34 import jdk.javadoc.internal.doclets.toolkit.Configuration;
  35 import jdk.javadoc.internal.doclets.toolkit.Content;
  36 import jdk.javadoc.internal.doclets.toolkit.util.VisibleMemberMap;
  37 
  38 
  39 /**
  40  * Builds documentation for annotation type fields.
  41  *
  42  *  <p><b>This is NOT part of any supported API.
  43  *  If you write code that depends on this, you do so at your own risk.
  44  *  This code and its internal interfaces are subject to change or
  45  *  deletion without notice.</b>
  46  *
  47  * @author Bhavesh Patel
  48  * @since 1.8
  49  */
  50 public class AnnotationTypeFieldBuilder extends AbstractMemberBuilder {
  51 
  52     /**
  53      * The annotation type whose members are being documented.
  54      */
  55     protected TypeElement typeElement;
  56 
  57     /**
  58      * The visible members for the given class.
  59      */
  60     protected VisibleMemberMap visibleMemberMap;
  61 
  62     /**
  63      * The writer to output the member documentation.
  64      */
  65     protected AnnotationTypeFieldWriter writer;
  66 
  67     /**
  68      * The list of members being documented.
  69      */
  70     protected SortedSet<Element> members;
  71 
  72     /**
  73      * The index of the current member that is being documented at this point
  74      * in time.
  75      */
  76     protected Element currentMember;
  77 
  78     /**
  79      * Construct a new AnnotationTypeFieldsBuilder.
  80      *
  81      * @param context  the build context.
  82      * @param typeElement the class whose members are being documented.
  83      * @param writer the doclet specific writer.
  84      * @param memberType the type of member that is being documented.
  85      */
  86     protected AnnotationTypeFieldBuilder(Context context,
  87             TypeElement typeElement,
  88             AnnotationTypeFieldWriter writer,
  89             VisibleMemberMap.Kind memberType) {
  90         super(context);
  91         this.typeElement = typeElement;
  92         this.writer = writer;
  93         this.visibleMemberMap = new VisibleMemberMap(typeElement, memberType, configuration);
  94         this.members = this.visibleMemberMap.getMembersFor(typeElement);
  95     }
  96 
  97 
  98     /**
  99      * Construct a new AnnotationTypeFieldBuilder.
 100      *
 101      * @param context  the build context.
 102      * @param typeElement the class whose members are being documented.
 103      * @param writer the doclet specific writer.
 104      */
 105     public static AnnotationTypeFieldBuilder getInstance(
 106             Context context, TypeElement typeElement,
 107             AnnotationTypeFieldWriter writer) {
 108         return new AnnotationTypeFieldBuilder(context, typeElement,
 109                     writer, VisibleMemberMap.Kind.ANNOTATION_TYPE_FIELDS);
 110     }
 111 
 112     /**
 113      * {@inheritDoc}
 114      */
 115     @Override
 116     public String getName() {
 117         return "AnnotationTypeFieldDetails";
 118     }
 119 
 120     /**
 121      * Returns a list of members that will be documented for the given class.
 122      * This information can be used for doclet specific documentation
 123      * generation.
 124      *
 125      * @param typeElement the {@link TypeElement} we want to check.
 126      * @return a list of members that will be documented.
 127      */
 128     public SortedSet<Element> members(TypeElement typeElement) {
 129         return visibleMemberMap.getMembersFor(typeElement);
 130     }
 131 
 132     /**
 133      * Returns the visible member map for the members of this class.
 134      *
 135      * @return the visible member map for the members of this class.
 136      */
 137     public VisibleMemberMap getVisibleMemberMap() {
 138         return visibleMemberMap;
 139     }
 140 
 141     /**
 142      * summaryOrder.size()
 143      */
 144     @Override
 145     public boolean hasMembersToDocument() {
 146         return members.size() > 0;
 147     }
 148 
 149     /**
 150      * Build the annotation type field documentation.
 151      *
 152      * @param node the XML element that specifies which components to document
 153      * @param memberDetailsTree the content tree to which the documentation will be added
 154      */
 155     public void buildAnnotationTypeField(XMLNode node, Content memberDetailsTree) {
 156         buildAnnotationTypeMember(node, memberDetailsTree);
 157     }
 158 
 159     /**
 160      * Build the member documentation.
 161      *
 162      * @param node the XML element that specifies which components to document
 163      * @param memberDetailsTree the content tree to which the documentation will be added
 164      */
 165     public void buildAnnotationTypeMember(XMLNode node, Content memberDetailsTree) {
 166         if (writer == null) {
 167             return;
 168         }
 169         if (!members.isEmpty()) {
 170             writer.addAnnotationFieldDetailsMarker(memberDetailsTree);
 171             for (Element element : members) {
 172                 currentMember = element;
 173                 Content detailsTree = writer.getMemberTreeHeader();
 174                 writer.addAnnotationDetailsTreeHeader(typeElement, detailsTree);
 175                 Content annotationDocTree = writer.getAnnotationDocTreeHeader(element, detailsTree);
 176                 buildChildren(node, annotationDocTree);
 177                 detailsTree.addContent(writer.getAnnotationDoc(
 178                         annotationDocTree, currentMember == members.last()));
 179                 memberDetailsTree.addContent(writer.getAnnotationDetails(detailsTree));
 180             }
 181         }
 182     }
 183 
 184     /**
 185      * Build the signature.
 186      *
 187      * @param node the XML element that specifies which components to document
 188      * @param annotationDocTree the content tree to which the documentation will be added
 189      */
 190     public void buildSignature(XMLNode node, Content annotationDocTree) {
 191         annotationDocTree.addContent(
 192                 writer.getSignature(currentMember));
 193     }
 194 
 195     /**
 196      * Build the deprecation information.
 197      *
 198      * @param node the XML element that specifies which components to document
 199      * @param annotationDocTree the content tree to which the documentation will be added
 200      */
 201     public void buildDeprecationInfo(XMLNode node, Content annotationDocTree) {
 202         writer.addDeprecated(currentMember, annotationDocTree);
 203     }
 204 
 205     /**
 206      * Build the comments for the member.  Do nothing if
 207      * {@link Configuration#nocomment} is set to true.
 208      *
 209      * @param node the XML element that specifies which components to document
 210      * @param annotationDocTree the content tree to which the documentation will be added
 211      */
 212     public void buildMemberComments(XMLNode node, Content annotationDocTree) {
 213         if(! configuration.nocomment){
 214             writer.addComments(currentMember, annotationDocTree);
 215         }
 216     }
 217 
 218     /**
 219      * Build the tag information.
 220      *
 221      * @param node the XML element that specifies which components to document
 222      * @param annotationDocTree the content tree to which the documentation will be added
 223      */
 224     public void buildTagInfo(XMLNode node, Content annotationDocTree) {
 225         writer.addTags(currentMember, annotationDocTree);
 226     }
 227 
 228     /**
 229      * Return the annotation type field writer for this builder.
 230      *
 231      * @return the annotation type field writer for this builder.
 232      */
 233     public AnnotationTypeFieldWriter getWriter() {
 234         return writer;
 235     }
 236 }