1 /*
   2  * Copyright (c) 2003, 2019, 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.AnnotationTypeRequiredMemberWriter;
  34 import jdk.javadoc.internal.doclets.toolkit.BaseConfiguration;
  35 import jdk.javadoc.internal.doclets.toolkit.Content;
  36 import jdk.javadoc.internal.doclets.toolkit.DocletException;
  37 import jdk.javadoc.internal.doclets.toolkit.util.VisibleMemberTable;
  38 
  39 import static jdk.javadoc.internal.doclets.toolkit.util.VisibleMemberTable.Kind.*;
  40 
  41 /**
  42  * Builds documentation for required annotation type members.
  43  *
  44  *  <p><b>This is NOT part of any supported API.
  45  *  If you write code that depends on this, you do so at your own risk.
  46  *  This code and its internal interfaces are subject to change or
  47  *  deletion without notice.</b>
  48  */
  49 public class AnnotationTypeRequiredMemberBuilder extends AbstractMemberBuilder {
  50 
  51 
  52     /**
  53      * The writer to output the member documentation.
  54      */
  55     protected AnnotationTypeRequiredMemberWriter writer;
  56 
  57     /**
  58      * The list of members being documented.
  59      */
  60     protected List<? extends Element> members;
  61 
  62     /**
  63      * The index of the current member that is being documented at this point
  64      * in time.
  65      */
  66     protected Element currentMember;
  67 
  68     /**
  69      * Construct a new AnnotationTypeRequiredMemberBuilder.
  70      *
  71      * @param context  the build context.
  72      * @param typeElement the class whose members are being documented.
  73      * @param writer the doclet specific writer.
  74      * @param memberType the kind of member this builder processes.
  75      */
  76     protected AnnotationTypeRequiredMemberBuilder(Context context,
  77             TypeElement typeElement,
  78             AnnotationTypeRequiredMemberWriter writer,
  79             VisibleMemberTable.Kind memberType) {
  80         super(context, typeElement);
  81         this.writer = writer;
  82         this.members = getVisibleMembers(memberType);
  83     }
  84 
  85 
  86     /**
  87      * Construct a new AnnotationTypeMemberBuilder.
  88      *
  89      * @param context  the build context.
  90      * @param typeElement the class whose members are being documented.
  91      * @param writer the doclet specific writer.
  92      * @return an instance of this object
  93      */
  94     public static AnnotationTypeRequiredMemberBuilder getInstance(
  95             Context context, TypeElement typeElement,
  96             AnnotationTypeRequiredMemberWriter writer) {
  97         return new AnnotationTypeRequiredMemberBuilder(context, typeElement,
  98                 writer, ANNOTATION_TYPE_MEMBER_REQUIRED);
  99     }
 100 
 101     /**
 102      * Returns whether or not there are members to document.
 103      * @return whether or not there are members to document
 104      */
 105     @Override
 106     public boolean hasMembersToDocument() {
 107         return !members.isEmpty();
 108     }
 109 
 110     /**
 111      * {@inheritDoc}
 112      */
 113     @Override
 114     public void build(Content contentTree) throws DocletException {
 115         buildAnnotationTypeRequiredMember(contentTree);
 116     }
 117 
 118     /**
 119      * Build the annotation type required member documentation.
 120      *
 121      * @param memberDetailsTree the content tree to which the documentation will be added
 122      * @throws DocletException if there is a problem while building the documentation
 123      */
 124     protected void buildAnnotationTypeRequiredMember(Content memberDetailsTree)
 125             throws DocletException {
 126         buildAnnotationTypeMember(memberDetailsTree);
 127     }
 128 
 129     /**
 130      * Build the member documentation.
 131      *
 132      * @param memberDetailsTree the content tree to which the documentation will be added
 133      * @throws DocletException if an error occurs
 134      */
 135     protected void buildAnnotationTypeMember(Content memberDetailsTree)
 136             throws DocletException {
 137         if (writer == null) {
 138             return;
 139         }
 140         if (hasMembersToDocument()) {
 141             writer.addAnnotationDetailsMarker(memberDetailsTree);
 142             Content annotationDetailsTreeHeader = writer.getAnnotationDetailsTreeHeader();
 143             Content detailsTree = writer.getMemberTreeHeader();
 144 
 145             for (Element member : members) {
 146                 currentMember = member;
 147                 Content annotationDocTree = writer.getAnnotationDocTreeHeader(currentMember);
 148 
 149                 buildAnnotationTypeMemberChildren(annotationDocTree);
 150 
 151                 detailsTree.add(writer.getAnnotationDoc(annotationDocTree));
 152             }
 153             memberDetailsTree.add(writer.getAnnotationDetails(annotationDetailsTreeHeader, detailsTree));
 154         }
 155     }
 156 
 157     protected void buildAnnotationTypeMemberChildren(Content annotationDocTree) {
 158         buildSignature(annotationDocTree);
 159         buildDeprecationInfo(annotationDocTree);
 160         buildMemberComments(annotationDocTree);
 161         buildTagInfo(annotationDocTree);
 162     }
 163 
 164     /**
 165      * Build the signature.
 166      *
 167      * @param annotationDocTree the content tree to which the documentation will be added
 168      */
 169     protected void buildSignature(Content annotationDocTree) {
 170         annotationDocTree.add(writer.getSignature(currentMember));
 171     }
 172 
 173     /**
 174      * Build the deprecation information.
 175      *
 176      * @param annotationDocTree the content tree to which the documentation will be added
 177      */
 178     protected void buildDeprecationInfo(Content annotationDocTree) {
 179         writer.addDeprecated(currentMember, annotationDocTree);
 180     }
 181 
 182     /**
 183      * Build the comments for the member.  Do nothing if
 184      * {@link BaseConfiguration#nocomment} is set to true.
 185      *
 186      * @param annotationDocTree the content tree to which the documentation will be added
 187      */
 188     protected void buildMemberComments(Content annotationDocTree) {
 189         if (!configuration.nocomment) {
 190             writer.addComments(currentMember, annotationDocTree);
 191         }
 192     }
 193 
 194     /**
 195      * Build the tag information.
 196      *
 197      * @param annotationDocTree the content tree to which the documentation will be added
 198      */
 199     protected void buildTagInfo(Content annotationDocTree) {
 200         writer.addTags(currentMember, annotationDocTree);
 201     }
 202 
 203     /**
 204      * Return the annotation type required member writer for this builder.
 205      *
 206      * @return the annotation type required member constant writer for this
 207      * builder.
 208      */
 209     public AnnotationTypeRequiredMemberWriter getWriter() {
 210         return writer;
 211     }
 212 }