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.ExecutableElement;
  32 import javax.lang.model.element.TypeElement;
  33 
  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.PropertyWriter;
  38 
  39 import static jdk.javadoc.internal.doclets.toolkit.util.VisibleMemberTable.Kind.*;
  40 
  41 /**
  42  * Builds documentation for a property.
  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 PropertyBuilder extends AbstractMemberBuilder {
  53 
  54     /**
  55      * The writer to output the property documentation.
  56      */
  57     private final PropertyWriter writer;
  58 
  59     /**
  60      * The list of properties being documented.
  61      */
  62     private final List<? extends Element> properties;
  63 
  64     /**
  65      * The index of the current property that is being documented at this point
  66      * in time.
  67      */
  68     private ExecutableElement currentProperty;
  69 
  70     /**
  71      * Construct a new PropertyBuilder.
  72      *
  73      * @param context  the build context.
  74      * @param typeElement the class whoses members are being documented.
  75      * @param writer the doclet specific writer.
  76      */
  77     private PropertyBuilder(Context context,
  78             TypeElement typeElement,
  79             PropertyWriter writer) {
  80         super(context, typeElement);
  81         this.writer = writer;
  82         properties = getVisibleMembers(PROPERTIES);
  83     }
  84 
  85     /**
  86      * Construct a new PropertyBuilder.
  87      *
  88      * @param context  the build context.
  89      * @param typeElement the class whoses members are being documented.
  90      * @param writer the doclet specific writer.
  91      * @return the new PropertyBuilder
  92      */
  93     public static PropertyBuilder getInstance(Context context,
  94             TypeElement typeElement,
  95             PropertyWriter writer) {
  96         return new PropertyBuilder(context, typeElement, writer);
  97     }
  98 
  99     /**
 100      * Returns whether or not there are members to document.
 101      *
 102      * @return whether or not there are members to document
 103      */
 104     @Override
 105     public boolean hasMembersToDocument() {
 106         return !properties.isEmpty();
 107     }
 108 
 109     /**
 110      * {@inheritDoc}
 111      */
 112     @Override
 113     public void build(Content contentTree) throws DocletException {
 114         buildPropertyDoc(contentTree);
 115     }
 116 
 117     /**
 118      * Build the property documentation.
 119      *
 120      * @param memberDetailsTree the content tree to which the documentation will be added
 121      * @throws DocletException if there is a problem while building the documentation
 122      */
 123     protected void buildPropertyDoc(Content memberDetailsTree) throws DocletException {
 124         if (writer == null) {
 125             return;
 126         }
 127         if (hasMembersToDocument()) {
 128             Content propertyDetailsTreeHeader = writer.getPropertyDetailsTreeHeader(typeElement,
 129                     memberDetailsTree);
 130             Content propertyDetailsTree = writer.getMemberTreeHeader();
 131 
 132             for (Element property : properties) {
 133                 currentProperty = (ExecutableElement)property;
 134                 Content propertyDocTree = writer.getPropertyDocTreeHeader(currentProperty,
 135                         propertyDetailsTree);
 136 
 137                 buildSignature(propertyDocTree);
 138                 buildPropertyComments(propertyDocTree);
 139                 buildTagInfo(propertyDocTree);
 140 
 141                 propertyDetailsTree.add(writer.getPropertyDoc(propertyDocTree));
 142             }
 143             memberDetailsTree.add(
 144                     writer.getPropertyDetails(propertyDetailsTreeHeader, propertyDetailsTree));
 145         }
 146     }
 147 
 148     /**
 149      * Build the signature.
 150      *
 151      * @param propertyDocTree the content tree to which the documentation will be added
 152      */
 153     protected void buildSignature(Content propertyDocTree) {
 154         propertyDocTree.add(writer.getSignature(currentProperty));
 155     }
 156 
 157     /**
 158      * Build the deprecation information.
 159      *
 160      * @param propertyDocTree the content tree to which the documentation will be added
 161      */
 162     protected void buildDeprecationInfo(Content propertyDocTree) {
 163         writer.addDeprecated(currentProperty, propertyDocTree);
 164     }
 165 
 166     /**
 167      * Build the comments for the property.  Do nothing if
 168      * {@link BaseConfiguration#nocomment} is set to true.
 169      *
 170      * @param propertyDocTree the content tree to which the documentation will be added
 171      */
 172     protected void buildPropertyComments(Content propertyDocTree) {
 173         if (!configuration.nocomment) {
 174             writer.addComments(currentProperty, propertyDocTree);
 175         }
 176     }
 177 
 178     /**
 179      * Build the tag information.
 180      *
 181      * @param propertyDocTree the content tree to which the documentation will be added
 182      */
 183     protected void buildTagInfo(Content propertyDocTree) {
 184         writer.addTags(currentProperty, propertyDocTree);
 185     }
 186 
 187     /**
 188      * Return the property writer for this builder.
 189      *
 190      * @return the property writer for this builder.
 191      */
 192     public PropertyWriter getWriter() {
 193         return writer;
 194     }
 195 }