1 /*
   2  * Copyright (c) 2013, 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.AnnotationTypeFieldWriter;
  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 
  40 /**
  41  * Builds documentation for annotation type fields.
  42  *
  43  *  <p><b>This is NOT part of any supported API.
  44  *  If you write code that depends on this, you do so at your own risk.
  45  *  This code and its internal interfaces are subject to change or
  46  *  deletion without notice.</b>
  47  *
  48  * @author Bhavesh Patel
  49  */
  50 public class AnnotationTypeFieldBuilder extends AbstractMemberBuilder {
  51 
  52     /**
  53      * The writer to output the member documentation.
  54      */
  55     protected AnnotationTypeFieldWriter 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 AnnotationTypeFieldsBuilder.
  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 type of member that is being documented.
  75      */
  76     protected AnnotationTypeFieldBuilder(Context context,
  77             TypeElement typeElement,
  78             AnnotationTypeFieldWriter 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 AnnotationTypeFieldBuilder.
  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 the new AnnotationTypeFieldBuilder
  93      */
  94     public static AnnotationTypeFieldBuilder getInstance(
  95             Context context, TypeElement typeElement,
  96             AnnotationTypeFieldWriter writer) {
  97         return new AnnotationTypeFieldBuilder(context, typeElement,
  98                     writer, VisibleMemberTable.Kind.ANNOTATION_TYPE_FIELDS);
  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         buildAnnotationTypeField(contentTree);
 116     }
 117 
 118     /**
 119      * Build the annotation type field 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 buildAnnotationTypeField(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 there is a problem while building the documentation
 134      */
 135     protected void buildAnnotationTypeMember(Content memberDetailsTree)
 136             throws DocletException {
 137         if (writer == null) {
 138             return;
 139         }
 140         if (hasMembersToDocument()) {
 141             writer.addAnnotationFieldDetailsMarker(memberDetailsTree);
 142             Content annotationDetailsTreeHeader = writer.getAnnotationDetailsTreeHeader(typeElement);
 143             Content detailsTree = writer.getMemberTreeHeader();
 144 
 145             for (Element member : members) {
 146                 currentMember = member;
 147                 Content annotationDocTree = writer.getAnnotationDocTreeHeader(currentMember,
 148                         detailsTree);
 149 
 150                 buildSignature(annotationDocTree);
 151                 buildDeprecationInfo(annotationDocTree);
 152                 buildMemberComments(annotationDocTree);
 153                 buildTagInfo(annotationDocTree);
 154 
 155                 detailsTree.add(writer.getAnnotationDoc(annotationDocTree));
 156             }
 157             memberDetailsTree.add(writer.getAnnotationDetails(annotationDetailsTreeHeader, detailsTree));
 158         }
 159     }
 160 
 161     /**
 162      * Build the signature.
 163      *
 164      * @param annotationDocTree the content tree to which the documentation will be added
 165      */
 166     protected void buildSignature(Content annotationDocTree) {
 167         annotationDocTree.add(
 168                 writer.getSignature(currentMember));
 169     }
 170 
 171     /**
 172      * Build the deprecation information.
 173      *
 174      * @param annotationDocTree the content tree to which the documentation will be added
 175      */
 176     protected void buildDeprecationInfo(Content annotationDocTree) {
 177         writer.addDeprecated(currentMember, annotationDocTree);
 178     }
 179 
 180     /**
 181      * Build the comments for the member.  Do nothing if
 182      * {@link BaseConfiguration#nocomment} is set to true.
 183      *
 184      * @param annotationDocTree the content tree to which the documentation will be added
 185      */
 186     protected void buildMemberComments(Content annotationDocTree) {
 187         if (!configuration.nocomment) {
 188             writer.addComments(currentMember, annotationDocTree);
 189         }
 190     }
 191 
 192     /**
 193      * Build the tag information.
 194      *
 195      * @param annotationDocTree the content tree to which the documentation will be added
 196      */
 197     protected void buildTagInfo(Content annotationDocTree) {
 198         writer.addTags(currentMember, annotationDocTree);
 199     }
 200 
 201     /**
 202      * Return the annotation type field writer for this builder.
 203      *
 204      * @return the annotation type field writer for this builder.
 205      */
 206     public AnnotationTypeFieldWriter getWriter() {
 207         return writer;
 208     }
 209 }