src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/builders/FieldBuilder.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 2013, 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 com.sun.tools.doclets.internal.toolkit.builders;
  27 
  28 import java.util.*;
  29 
  30 import com.sun.javadoc.*;
  31 import com.sun.tools.doclets.internal.toolkit.*;
  32 import com.sun.tools.doclets.internal.toolkit.util.*;
  33 






  34 /**
  35  * Builds documentation for a field.
  36  *
  37  *  <p><b>This is NOT part of any supported API.
  38  *  If you write code that depends on this, you do so at your own risk.
  39  *  This code and its internal interfaces are subject to change or
  40  *  deletion without notice.</b>
  41  *
  42  * @author Jamie Ho
  43  * @author Bhavesh Patel (Modified)
  44  * @since 1.5
  45  */
  46 public class FieldBuilder extends AbstractMemberBuilder {
  47 
  48     /**
  49      * The class whose fields are being documented.
  50      */
  51     private final ClassDoc classDoc;
  52 
  53     /**
  54      * The visible fields for the given class.
  55      */
  56     private final VisibleMemberMap visibleMemberMap;
  57 
  58     /**
  59      * The writer to output the field documentation.
  60      */
  61     private final FieldWriter writer;
  62 
  63     /**
  64      * The list of fields being documented.
  65      */
  66     private final List<ProgramElementDoc> fields;
  67 
  68     /**
  69      * The index of the current field that is being documented at this point
  70      * in time.
  71      */
  72     private int currentFieldIndex;
  73 
  74     /**
  75      * Construct a new FieldBuilder.
  76      *
  77      * @param context  the build context.
  78      * @param classDoc the class whoses members are being documented.
  79      * @param writer the doclet specific writer.
  80      */
  81     private FieldBuilder(Context context,
  82             ClassDoc classDoc,
  83             FieldWriter writer) {
  84         super(context);
  85         this.classDoc = classDoc;
  86         this.writer = writer;
  87         visibleMemberMap =
  88                 new VisibleMemberMap(
  89                 classDoc,
  90                 VisibleMemberMap.FIELDS,
  91                 configuration);
  92         fields = new ArrayList<>(visibleMemberMap.getLeafClassMembers(configuration));
  93         if (configuration.getMemberComparator() != null) {
  94             Collections.sort(fields, configuration.getMemberComparator());
  95         }
  96     }
  97 
  98     /**
  99      * Construct a new FieldBuilder.
 100      *
 101      * @param context  the build context.
 102      * @param classDoc the class whoses members are being documented.
 103      * @param writer the doclet specific writer.
 104      */
 105     public static FieldBuilder getInstance(Context context,
 106             ClassDoc classDoc,
 107             FieldWriter writer) {
 108         return new FieldBuilder(context, classDoc, writer);
 109     }
 110 
 111     /**
 112      * {@inheritDoc}
 113      */

 114     public String getName() {
 115         return "FieldDetails";
 116     }
 117 
 118     /**
 119      * Returns a list of fields that will be documented for the given class.
 120      * This information can be used for doclet specific documentation
 121      * generation.
 122      *
 123      * @param classDoc the {@link ClassDoc} we want to check.
 124      * @return a list of fields that will be documented.
 125      */
 126     public List<ProgramElementDoc> members(ClassDoc classDoc) {
 127         return visibleMemberMap.getMembersFor(classDoc);
 128     }
 129 
 130     /**
 131      * Returns the visible member map for the fields of this class.
 132      *
 133      * @return the visible member map for the fields of this class.
 134      */
 135     public VisibleMemberMap getVisibleMemberMap() {
 136         return visibleMemberMap;
 137     }
 138 
 139     /**
 140      * summaryOrder.size()
 141      */

 142     public boolean hasMembersToDocument() {
 143         return fields.size() > 0;
 144     }
 145 
 146     /**
 147      * Build the field documentation.
 148      *
 149      * @param node the XML element that specifies which components to document
 150      * @param memberDetailsTree the content tree to which the documentation will be added
 151      */
 152     public void buildFieldDoc(XMLNode node, Content memberDetailsTree) {
 153         if (writer == null) {
 154             return;
 155         }
 156         int size = fields.size();
 157         if (size > 0) {
 158             Content fieldDetailsTree = writer.getFieldDetailsTreeHeader(
 159                     classDoc, memberDetailsTree);
 160             for (currentFieldIndex = 0; currentFieldIndex < size;
 161                     currentFieldIndex++) {
 162                 Content fieldDocTree = writer.getFieldDocTreeHeader(
 163                         (FieldDoc) fields.get(currentFieldIndex),
 164                         fieldDetailsTree);
 165                 buildChildren(node, fieldDocTree);
 166                 fieldDetailsTree.addContent(writer.getFieldDoc(
 167                         fieldDocTree, (currentFieldIndex == size - 1)));
 168             }
 169             memberDetailsTree.addContent(
 170                     writer.getFieldDetails(fieldDetailsTree));
 171         }
 172     }
 173 
 174     /**
 175      * Build the signature.
 176      *
 177      * @param node the XML element that specifies which components to document
 178      * @param fieldDocTree the content tree to which the documentation will be added
 179      */
 180     public void buildSignature(XMLNode node, Content fieldDocTree) {
 181         fieldDocTree.addContent(
 182                 writer.getSignature((FieldDoc) fields.get(currentFieldIndex)));
 183     }
 184 
 185     /**
 186      * Build the deprecation information.
 187      *
 188      * @param node the XML element that specifies which components to document
 189      * @param fieldDocTree the content tree to which the documentation will be added
 190      */
 191     public void buildDeprecationInfo(XMLNode node, Content fieldDocTree) {
 192         writer.addDeprecated(
 193                 (FieldDoc) fields.get(currentFieldIndex), fieldDocTree);
 194     }
 195 
 196     /**
 197      * Build the comments for the field.  Do nothing if
 198      * {@link Configuration#nocomment} is set to true.
 199      *
 200      * @param node the XML element that specifies which components to document
 201      * @param fieldDocTree the content tree to which the documentation will be added
 202      */
 203     public void buildFieldComments(XMLNode node, Content fieldDocTree) {
 204         if (!configuration.nocomment) {
 205             writer.addComments((FieldDoc) fields.get(currentFieldIndex), fieldDocTree);
 206         }
 207     }
 208 
 209     /**
 210      * Build the tag information.
 211      *
 212      * @param node the XML element that specifies which components to document
 213      * @param fieldDocTree the content tree to which the documentation will be added
 214      */
 215     public void buildTagInfo(XMLNode node, Content fieldDocTree) {
 216         writer.addTags((FieldDoc) fields.get(currentFieldIndex), fieldDocTree);
 217     }
 218 
 219     /**
 220      * Return the field writer for this builder.
 221      *
 222      * @return the field writer for this builder.
 223      */
 224     public FieldWriter getWriter() {
 225         return writer;
 226     }
 227 }
   1 /*
   2  * Copyright (c) 2003, 2016, 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 import javax.lang.model.element.VariableElement;
  33 
  34 import jdk.javadoc.internal.doclets.toolkit.Configuration;
  35 import jdk.javadoc.internal.doclets.toolkit.Content;
  36 import jdk.javadoc.internal.doclets.toolkit.FieldWriter;
  37 import jdk.javadoc.internal.doclets.toolkit.util.VisibleMemberMap;
  38 
  39 
  40 /**
  41  * Builds documentation for a field.
  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 Jamie Ho
  49  * @author Bhavesh Patel (Modified)
  50  * @since 1.5
  51  */
  52 public class FieldBuilder extends AbstractMemberBuilder {
  53 
  54     /**
  55      * The class whose fields are being documented.
  56      */
  57     private final TypeElement typeElement;
  58 
  59     /**
  60      * The visible fields for the given class.
  61      */
  62     private final VisibleMemberMap visibleMemberMap;
  63 
  64     /**
  65      * The writer to output the field documentation.
  66      */
  67     private final FieldWriter writer;
  68 
  69     /**
  70      * The list of fields being documented.
  71      */
  72     private final SortedSet<Element> fields;
  73 
  74     /**
  75      * The index of the current field that is being documented at this point
  76      * in time.
  77      */
  78     private VariableElement currentElement;
  79 
  80     /**
  81      * Construct a new FieldBuilder.
  82      *
  83      * @param context  the build context.
  84      * @param typeElement the class whoses members are being documented.
  85      * @param writer the doclet specific writer.
  86      */
  87     private FieldBuilder(Context context,
  88             TypeElement typeElement,
  89             FieldWriter writer) {
  90         super(context);
  91         this.typeElement = typeElement;
  92         this.writer = writer;
  93         visibleMemberMap =
  94                 new VisibleMemberMap(
  95                 typeElement,
  96                 VisibleMemberMap.Kind.FIELDS,
  97                 configuration);
  98         fields = visibleMemberMap.getLeafClassMembers();


  99     }

 100 
 101     /**
 102      * Construct a new FieldBuilder.
 103      *
 104      * @param context  the build context.
 105      * @param typeElement the class whoses members are being documented.
 106      * @param writer the doclet specific writer.
 107      */
 108     public static FieldBuilder getInstance(Context context,
 109             TypeElement typeElement,
 110             FieldWriter writer) {
 111         return new FieldBuilder(context, typeElement, writer);
 112     }
 113 
 114     /**
 115      * {@inheritDoc}
 116      */
 117     @Override
 118     public String getName() {
 119         return "FieldDetails";
 120     }
 121 
 122     /**
 123      * Returns a list of fields that will be documented for the given class.
 124      * This information can be used for doclet specific documentation
 125      * generation.
 126      *
 127      * @param typeElement the {@link TypeElement} we want to check.
 128      * @return a list of fields that will be documented.
 129      */
 130     public SortedSet<Element> members(TypeElement typeElement) {
 131         return visibleMemberMap.getMembersFor(typeElement);
 132     }
 133 
 134     /**
 135      * Returns the visible member map for the fields of this class.
 136      *
 137      * @return the visible member map for the fields of this class.
 138      */
 139     public VisibleMemberMap getVisibleMemberMap() {
 140         return visibleMemberMap;
 141     }
 142 
 143     /**
 144      * summaryOrder.size()
 145      */
 146     @Override
 147     public boolean hasMembersToDocument() {
 148         return !fields.isEmpty();
 149     }
 150 
 151     /**
 152      * Build the field documentation.
 153      *
 154      * @param node the XML element that specifies which components to document
 155      * @param memberDetailsTree the content tree to which the documentation will be added
 156      */
 157     public void buildFieldDoc(XMLNode node, Content memberDetailsTree) {
 158         if (writer == null) {
 159             return;
 160         }
 161         if (!fields.isEmpty()) {
 162             Content fieldDetailsTree = writer.getFieldDetailsTreeHeader(typeElement, memberDetailsTree);
 163             for (Element element : fields) {
 164                 currentElement = (VariableElement)element;
 165                 Content fieldDocTree = writer.getFieldDocTreeHeader(currentElement, fieldDetailsTree);




 166                 buildChildren(node, fieldDocTree);
 167                 fieldDetailsTree.addContent(writer.getFieldDoc(
 168                         fieldDocTree, currentElement.equals(fields.last())));
 169             }
 170             memberDetailsTree.addContent(
 171                     writer.getFieldDetails(fieldDetailsTree));
 172         }
 173     }
 174 
 175     /**
 176      * Build the signature.
 177      *
 178      * @param node the XML element that specifies which components to document
 179      * @param fieldDocTree the content tree to which the documentation will be added
 180      */
 181     public void buildSignature(XMLNode node, Content fieldDocTree) {
 182         fieldDocTree.addContent(writer.getSignature(currentElement));

 183     }
 184 
 185     /**
 186      * Build the deprecation information.
 187      *
 188      * @param node the XML element that specifies which components to document
 189      * @param fieldDocTree the content tree to which the documentation will be added
 190      */
 191     public void buildDeprecationInfo(XMLNode node, Content fieldDocTree) {
 192         writer.addDeprecated(currentElement, fieldDocTree);

 193     }
 194 
 195     /**
 196      * Build the comments for the field.  Do nothing if
 197      * {@link Configuration#nocomment} is set to true.
 198      *
 199      * @param node the XML element that specifies which components to document
 200      * @param fieldDocTree the content tree to which the documentation will be added
 201      */
 202     public void buildFieldComments(XMLNode node, Content fieldDocTree) {
 203         if (!configuration.nocomment) {
 204             writer.addComments(currentElement, fieldDocTree);
 205         }
 206     }
 207 
 208     /**
 209      * Build the tag information.
 210      *
 211      * @param node the XML element that specifies which components to document
 212      * @param fieldDocTree the content tree to which the documentation will be added
 213      */
 214     public void buildTagInfo(XMLNode node, Content fieldDocTree) {
 215         writer.addTags(currentElement, fieldDocTree);
 216     }
 217 
 218     /**
 219      * Return the field writer for this builder.
 220      *
 221      * @return the field writer for this builder.
 222      */
 223     public FieldWriter getWriter() {
 224         return writer;
 225     }
 226 }