Tue, 25 May 2010 15:54:51 -0700
6943119: Rebrand source copyright notices
Reviewed-by: darcy
duke@1 | 1 | /* |
ohair@554 | 2 | * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.doclets.internal.toolkit.builders; |
duke@1 | 27 | |
duke@1 | 28 | import com.sun.tools.doclets.internal.toolkit.util.*; |
duke@1 | 29 | import com.sun.tools.doclets.internal.toolkit.*; |
duke@1 | 30 | import com.sun.javadoc.*; |
duke@1 | 31 | import java.io.*; |
duke@1 | 32 | import java.util.*; |
duke@1 | 33 | import java.lang.reflect.*; |
duke@1 | 34 | |
duke@1 | 35 | /** |
duke@1 | 36 | * Builds the Constants Summary Page. |
duke@1 | 37 | * |
duke@1 | 38 | * This code is not part of an API. |
duke@1 | 39 | * It is implementation that is subject to change. |
duke@1 | 40 | * Do not use it as an API |
duke@1 | 41 | * |
duke@1 | 42 | * @author Jamie Ho |
duke@1 | 43 | * @since 1.5 |
duke@1 | 44 | */ |
duke@1 | 45 | public class ConstantsSummaryBuilder extends AbstractBuilder { |
duke@1 | 46 | |
duke@1 | 47 | /** |
duke@1 | 48 | * The root element of the constant summary XML is {@value}. |
duke@1 | 49 | */ |
duke@1 | 50 | public static final String ROOT = "ConstantSummary"; |
duke@1 | 51 | |
duke@1 | 52 | /** |
duke@1 | 53 | * The maximum number of package directories shown in the constant |
duke@1 | 54 | * value index. |
duke@1 | 55 | */ |
duke@1 | 56 | public static final int MAX_CONSTANT_VALUE_INDEX_LENGTH = 2; |
duke@1 | 57 | |
duke@1 | 58 | /** |
duke@1 | 59 | * The writer used to write the results. |
duke@1 | 60 | */ |
duke@1 | 61 | protected ConstantsSummaryWriter writer; |
duke@1 | 62 | |
duke@1 | 63 | /** |
duke@1 | 64 | * The set of ClassDocs that have constant fields. |
duke@1 | 65 | */ |
jjg@74 | 66 | protected Set<ClassDoc> classDocsWithConstFields; |
duke@1 | 67 | |
duke@1 | 68 | /** |
duke@1 | 69 | * The set of printed package headers. |
duke@1 | 70 | */ |
jjg@74 | 71 | protected Set<String> printedPackageHeaders; |
duke@1 | 72 | |
duke@1 | 73 | /** |
duke@1 | 74 | * The current package being documented. |
duke@1 | 75 | */ |
duke@1 | 76 | private PackageDoc currentPackage; |
duke@1 | 77 | |
duke@1 | 78 | /** |
duke@1 | 79 | * The current class being documented. |
duke@1 | 80 | */ |
duke@1 | 81 | private ClassDoc currentClass; |
duke@1 | 82 | |
duke@1 | 83 | /** |
duke@1 | 84 | * Construct a new ConstantsSummaryBuilder. |
duke@1 | 85 | * |
duke@1 | 86 | * @param configuration the current configuration of the |
duke@1 | 87 | * doclet. |
duke@1 | 88 | */ |
duke@1 | 89 | private ConstantsSummaryBuilder(Configuration configuration) { |
duke@1 | 90 | super(configuration); |
duke@1 | 91 | } |
duke@1 | 92 | |
duke@1 | 93 | /** |
duke@1 | 94 | * Construct a ConstantsSummaryBuilder. |
duke@1 | 95 | * |
duke@1 | 96 | * @param configuration the configuration used in this run |
duke@1 | 97 | * of the doclet. |
duke@1 | 98 | * @param writer the writer for the summary. |
duke@1 | 99 | */ |
duke@1 | 100 | public static ConstantsSummaryBuilder getInstance( |
duke@1 | 101 | Configuration configuration, ConstantsSummaryWriter writer) { |
duke@1 | 102 | ConstantsSummaryBuilder builder = new ConstantsSummaryBuilder( |
duke@1 | 103 | configuration); |
duke@1 | 104 | builder.writer = writer; |
jjg@74 | 105 | builder.classDocsWithConstFields = new HashSet<ClassDoc>(); |
duke@1 | 106 | return builder; |
duke@1 | 107 | } |
duke@1 | 108 | |
duke@1 | 109 | /** |
duke@1 | 110 | * {@inheritDoc} |
duke@1 | 111 | */ |
mcimadamore@184 | 112 | public void invokeMethod(String methodName, Class<?>[] paramClasses, |
duke@1 | 113 | Object[] params) |
duke@1 | 114 | throws Exception { |
duke@1 | 115 | if (DEBUG) { |
duke@1 | 116 | configuration.root.printError("DEBUG: " + this.getClass().getName() |
duke@1 | 117 | + "." + methodName); |
duke@1 | 118 | } |
duke@1 | 119 | Method method = this.getClass().getMethod(methodName, paramClasses); |
duke@1 | 120 | method.invoke(this, params); |
duke@1 | 121 | } |
duke@1 | 122 | |
duke@1 | 123 | /** |
duke@1 | 124 | * {@inheritDoc} |
duke@1 | 125 | */ |
duke@1 | 126 | public void build() throws IOException { |
duke@1 | 127 | if (writer == null) { |
duke@1 | 128 | //Doclet does not support this output. |
duke@1 | 129 | return; |
duke@1 | 130 | } |
duke@1 | 131 | build(LayoutParser.getInstance(configuration).parseXML(ROOT)); |
duke@1 | 132 | } |
duke@1 | 133 | |
duke@1 | 134 | /** |
duke@1 | 135 | * {@inheritDoc} |
duke@1 | 136 | */ |
duke@1 | 137 | public String getName() { |
duke@1 | 138 | return ROOT; |
duke@1 | 139 | } |
duke@1 | 140 | |
duke@1 | 141 | /** |
duke@1 | 142 | * Build the constant summary. |
duke@1 | 143 | * |
duke@1 | 144 | * @param elements the list of elements describing constant summary |
duke@1 | 145 | * documentation. |
duke@1 | 146 | */ |
mcimadamore@184 | 147 | public void buildConstantSummary(List<?> elements) throws Exception { |
duke@1 | 148 | build(elements); |
duke@1 | 149 | writer.close(); |
duke@1 | 150 | } |
duke@1 | 151 | |
duke@1 | 152 | /** |
duke@1 | 153 | * Build the header. |
duke@1 | 154 | */ |
duke@1 | 155 | public void buildHeader() { |
duke@1 | 156 | writer.writeHeader(); |
duke@1 | 157 | } |
duke@1 | 158 | |
duke@1 | 159 | /** |
duke@1 | 160 | * Build the footer. |
duke@1 | 161 | */ |
duke@1 | 162 | public void buildFooter() { |
duke@1 | 163 | writer.writeFooter(); |
duke@1 | 164 | } |
duke@1 | 165 | |
duke@1 | 166 | /** |
duke@1 | 167 | * Build the table of contents. |
duke@1 | 168 | */ |
duke@1 | 169 | public void buildContents() { |
duke@1 | 170 | writer.writeContentsHeader(); |
duke@1 | 171 | PackageDoc[] packages = configuration.packages; |
jjg@74 | 172 | printedPackageHeaders = new HashSet<String>(); |
duke@1 | 173 | for (int i = 0; i < packages.length; i++) { |
duke@1 | 174 | if (hasConstantField(packages[i]) && ! hasPrintedPackageIndex(packages[i].name())) { |
duke@1 | 175 | writer.writeLinkToPackageContent(packages[i], |
duke@1 | 176 | parsePackageName(packages[i].name()), |
duke@1 | 177 | printedPackageHeaders); |
duke@1 | 178 | } |
duke@1 | 179 | } |
duke@1 | 180 | writer.writeContentsFooter(); |
duke@1 | 181 | } |
duke@1 | 182 | |
duke@1 | 183 | /** |
duke@1 | 184 | * Build the summary for each documented package. |
duke@1 | 185 | * |
duke@1 | 186 | * @param elements the XML elements that represent the components |
duke@1 | 187 | * of documentation for each package. |
duke@1 | 188 | */ |
mcimadamore@184 | 189 | public void buildConstantSummaries(List<?> elements) { |
duke@1 | 190 | PackageDoc[] packages = configuration.packages; |
jjg@74 | 191 | printedPackageHeaders = new HashSet<String>(); |
duke@1 | 192 | for (int i = 0; i < packages.length; i++) { |
duke@1 | 193 | if (hasConstantField(packages[i])) { |
duke@1 | 194 | currentPackage = packages[i]; |
duke@1 | 195 | //Build the documentation for the current package. |
duke@1 | 196 | build(elements); |
duke@1 | 197 | } |
duke@1 | 198 | } |
duke@1 | 199 | } |
duke@1 | 200 | |
duke@1 | 201 | /** |
duke@1 | 202 | * Build the summary for the current package. |
duke@1 | 203 | * |
duke@1 | 204 | * @param elements the list of XML elements that make up package |
duke@1 | 205 | * documentation. |
duke@1 | 206 | */ |
mcimadamore@184 | 207 | public void buildPackageConstantSummary(List<?> elements) { |
duke@1 | 208 | build(elements); |
duke@1 | 209 | } |
duke@1 | 210 | |
duke@1 | 211 | /** |
duke@1 | 212 | * Build the summary for the current class. |
duke@1 | 213 | * |
duke@1 | 214 | * @param elements the list of XML elements that make up the class |
duke@1 | 215 | * constant summary. |
duke@1 | 216 | */ |
mcimadamore@184 | 217 | public void buildClassConstantSummary(List<?> elements) { |
duke@1 | 218 | ClassDoc[] classes = currentPackage.name().length() > 0 ? |
duke@1 | 219 | currentPackage.allClasses() : |
duke@1 | 220 | configuration.classDocCatalog.allClasses( |
duke@1 | 221 | DocletConstants.DEFAULT_PACKAGE_NAME); |
duke@1 | 222 | Arrays.sort(classes); |
duke@1 | 223 | for (int i = 0; i < classes.length; i++) { |
duke@1 | 224 | if (! classDocsWithConstFields.contains(classes[i]) || |
duke@1 | 225 | ! classes[i].isIncluded()) { |
duke@1 | 226 | continue; |
duke@1 | 227 | } |
duke@1 | 228 | currentClass = classes[i]; |
duke@1 | 229 | //Build the documentation for the current class. |
duke@1 | 230 | build(elements); |
duke@1 | 231 | } |
duke@1 | 232 | } |
duke@1 | 233 | |
duke@1 | 234 | /** |
duke@1 | 235 | * Build the header for the given class. |
duke@1 | 236 | */ |
duke@1 | 237 | public void buildPackageHeader() { |
duke@1 | 238 | String parsedPackageName = parsePackageName(currentPackage.name()); |
duke@1 | 239 | if (! printedPackageHeaders.contains(parsedPackageName)) { |
duke@1 | 240 | writer.writePackageName(currentPackage, |
duke@1 | 241 | parsePackageName(currentPackage.name())); |
duke@1 | 242 | printedPackageHeaders.add(parsedPackageName); |
duke@1 | 243 | } |
duke@1 | 244 | } |
duke@1 | 245 | |
duke@1 | 246 | /** |
duke@1 | 247 | * Build the header for the given class. |
duke@1 | 248 | */ |
duke@1 | 249 | public void buildClassHeader() { |
duke@1 | 250 | writer.writeConstantMembersHeader(currentClass); |
duke@1 | 251 | } |
duke@1 | 252 | |
duke@1 | 253 | /** |
duke@1 | 254 | * Print summary of constant members in the |
duke@1 | 255 | * class. |
duke@1 | 256 | */ |
duke@1 | 257 | public void buildConstantMembers() { |
duke@1 | 258 | new ConstantFieldBuilder(currentClass).buildMembersSummary(); |
duke@1 | 259 | } |
duke@1 | 260 | |
duke@1 | 261 | /** |
duke@1 | 262 | * Build the footer for the given class. |
duke@1 | 263 | */ |
duke@1 | 264 | public void buildClassFooter() { |
duke@1 | 265 | writer.writeConstantMembersFooter(currentClass); |
duke@1 | 266 | } |
duke@1 | 267 | |
duke@1 | 268 | /** |
duke@1 | 269 | * Return true if the given package has constant fields to document. |
duke@1 | 270 | * |
duke@1 | 271 | * @param pkg the package being checked. |
duke@1 | 272 | * @return true if the given package has constant fields to document. |
duke@1 | 273 | */ |
duke@1 | 274 | private boolean hasConstantField(PackageDoc pkg) { |
duke@1 | 275 | ClassDoc[] classes; |
duke@1 | 276 | if (pkg.name().length() > 0) { |
duke@1 | 277 | classes = pkg.allClasses(); |
duke@1 | 278 | } else { |
duke@1 | 279 | classes = configuration.classDocCatalog.allClasses( |
duke@1 | 280 | DocletConstants.DEFAULT_PACKAGE_NAME); |
duke@1 | 281 | } |
duke@1 | 282 | boolean found = false; |
duke@1 | 283 | for (int j = 0; j < classes.length; j++){ |
duke@1 | 284 | if (classes[j].isIncluded() && hasConstantField(classes[j])) { |
duke@1 | 285 | found = true; |
duke@1 | 286 | } |
duke@1 | 287 | } |
duke@1 | 288 | return found; |
duke@1 | 289 | } |
duke@1 | 290 | |
duke@1 | 291 | /** |
duke@1 | 292 | * Return true if the given class has constant fields to document. |
duke@1 | 293 | * |
duke@1 | 294 | * @param classDoc the class being checked. |
duke@1 | 295 | * @return true if the given package has constant fields to document. |
duke@1 | 296 | */ |
duke@1 | 297 | private boolean hasConstantField (ClassDoc classDoc) { |
duke@1 | 298 | VisibleMemberMap visibleMemberMapFields = new VisibleMemberMap(classDoc, |
duke@1 | 299 | VisibleMemberMap.FIELDS, configuration.nodeprecated); |
mcimadamore@184 | 300 | List<?> fields = visibleMemberMapFields.getLeafClassMembers(configuration); |
mcimadamore@184 | 301 | for (Iterator<?> iter = fields.iterator(); iter.hasNext(); ) { |
duke@1 | 302 | FieldDoc field = (FieldDoc) iter.next(); |
duke@1 | 303 | if (field.constantValueExpression() != null) { |
duke@1 | 304 | classDocsWithConstFields.add(classDoc); |
duke@1 | 305 | return true; |
duke@1 | 306 | } |
duke@1 | 307 | } |
duke@1 | 308 | return false; |
duke@1 | 309 | } |
duke@1 | 310 | |
duke@1 | 311 | /** |
duke@1 | 312 | * Return true if the given package name has been printed. Also |
duke@1 | 313 | * return true if the root of this package has been printed. |
duke@1 | 314 | * |
duke@1 | 315 | * @param pkgname the name of the package to check. |
duke@1 | 316 | */ |
duke@1 | 317 | private boolean hasPrintedPackageIndex(String pkgname) { |
jjg@74 | 318 | String[] list = printedPackageHeaders.toArray(new String[] {}); |
duke@1 | 319 | for (int i = 0; i < list.length; i++) { |
duke@1 | 320 | if (pkgname.startsWith(list[i])) { |
duke@1 | 321 | return true; |
duke@1 | 322 | } |
duke@1 | 323 | } |
duke@1 | 324 | return false; |
duke@1 | 325 | } |
duke@1 | 326 | |
duke@1 | 327 | /** |
duke@1 | 328 | * Print the table of constants. |
duke@1 | 329 | * |
duke@1 | 330 | * @author Jamie Ho |
duke@1 | 331 | * @since 1.4 |
duke@1 | 332 | */ |
duke@1 | 333 | private class ConstantFieldBuilder { |
duke@1 | 334 | |
duke@1 | 335 | /** |
duke@1 | 336 | * The map used to get the visible variables. |
duke@1 | 337 | */ |
duke@1 | 338 | protected VisibleMemberMap visibleMemberMapFields = null; |
duke@1 | 339 | |
duke@1 | 340 | /** |
duke@1 | 341 | * The map used to get the visible variables. |
duke@1 | 342 | */ |
duke@1 | 343 | protected VisibleMemberMap visibleMemberMapEnumConst = null; |
duke@1 | 344 | |
duke@1 | 345 | /** |
duke@1 | 346 | * The classdoc that we are examining constants for. |
duke@1 | 347 | */ |
duke@1 | 348 | protected ClassDoc classdoc; |
duke@1 | 349 | |
duke@1 | 350 | /** |
duke@1 | 351 | * Construct a ConstantFieldSubWriter. |
duke@1 | 352 | * @param classdoc the classdoc that we are examining constants for. |
duke@1 | 353 | */ |
duke@1 | 354 | public ConstantFieldBuilder(ClassDoc classdoc) { |
duke@1 | 355 | this.classdoc = classdoc; |
duke@1 | 356 | visibleMemberMapFields = new VisibleMemberMap(classdoc, |
duke@1 | 357 | VisibleMemberMap.FIELDS, configuration.nodeprecated); |
duke@1 | 358 | visibleMemberMapEnumConst = new VisibleMemberMap(classdoc, |
duke@1 | 359 | VisibleMemberMap.ENUM_CONSTANTS, configuration.nodeprecated); |
duke@1 | 360 | } |
duke@1 | 361 | |
duke@1 | 362 | /** |
duke@1 | 363 | * Builds the table of constants for a given class. |
duke@1 | 364 | */ |
duke@1 | 365 | protected void buildMembersSummary() { |
jjg@74 | 366 | List<FieldDoc> members = new ArrayList<FieldDoc>(members()); |
duke@1 | 367 | if (members.size() > 0) { |
duke@1 | 368 | Collections.sort(members); |
duke@1 | 369 | writer.writeConstantMembers(classdoc, members); |
duke@1 | 370 | } |
duke@1 | 371 | } |
duke@1 | 372 | |
duke@1 | 373 | /** |
duke@1 | 374 | * Return the list of visible constant fields for the given classdoc. |
duke@1 | 375 | * @param cd the classdoc to examine. |
duke@1 | 376 | * @return the list of visible constant fields for the given classdoc. |
duke@1 | 377 | */ |
jjg@74 | 378 | protected List<FieldDoc> members() { |
jjg@74 | 379 | List<ProgramElementDoc> l = visibleMemberMapFields.getLeafClassMembers(configuration); |
duke@1 | 380 | l.addAll(visibleMemberMapEnumConst.getLeafClassMembers(configuration)); |
jjg@74 | 381 | Iterator<ProgramElementDoc> iter; |
duke@1 | 382 | |
duke@1 | 383 | if(l != null){ |
duke@1 | 384 | iter = l.iterator(); |
duke@1 | 385 | } else { |
duke@1 | 386 | return null; |
duke@1 | 387 | } |
jjg@74 | 388 | List<FieldDoc> inclList = new LinkedList<FieldDoc>(); |
duke@1 | 389 | FieldDoc member; |
duke@1 | 390 | while(iter.hasNext()){ |
duke@1 | 391 | member = (FieldDoc)iter.next(); |
duke@1 | 392 | if(member.constantValue() != null){ |
duke@1 | 393 | inclList.add(member); |
duke@1 | 394 | } |
duke@1 | 395 | } |
duke@1 | 396 | return inclList; |
duke@1 | 397 | } |
duke@1 | 398 | } |
duke@1 | 399 | |
duke@1 | 400 | /** |
duke@1 | 401 | * Parse the package name. We only want to display package name up to |
duke@1 | 402 | * 2 levels. |
duke@1 | 403 | */ |
duke@1 | 404 | private String parsePackageName(String pkgname) { |
duke@1 | 405 | int index = -1; |
duke@1 | 406 | for (int j = 0; j < MAX_CONSTANT_VALUE_INDEX_LENGTH; j++) { |
duke@1 | 407 | index = pkgname.indexOf(".", index + 1); |
duke@1 | 408 | } |
duke@1 | 409 | if (index != -1) { |
duke@1 | 410 | pkgname = pkgname.substring(0, index); |
duke@1 | 411 | } |
duke@1 | 412 | return pkgname; |
duke@1 | 413 | } |
duke@1 | 414 | } |