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 }