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