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