src/share/classes/com/sun/tools/doclets/internal/toolkit/builders/SerializedFormBuilder.java

Mon, 19 Nov 2012 16:10:34 -0800

author
bpatel
date
Mon, 19 Nov 2012 16:10:34 -0800
changeset 1417
522a1ee72340
parent 1410
bfec2a1cc869
child 1985
0e6577980181
permissions
-rw-r--r--

8002304: Group methods by types in methods summary section
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 2003, 2012, 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  */
    26 package com.sun.tools.doclets.internal.toolkit.builders;
    28 import java.io.*;
    29 import java.util.*;
    31 import com.sun.javadoc.*;
    32 import com.sun.tools.doclets.internal.toolkit.*;
    33 import com.sun.tools.doclets.internal.toolkit.util.*;
    35 /**
    36  * Builds the serialized form.
    37  *
    38  *  <p><b>This is NOT part of any supported API.
    39  *  If you write code that depends on this, you do so at your own risk.
    40  *  This code and its internal interfaces are subject to change or
    41  *  deletion without notice.</b>
    42  *
    43  * @author Jamie Ho
    44  * @author Bhavesh Patel (Modified)
    45  * @since 1.5
    46  */
    47 public class SerializedFormBuilder extends AbstractBuilder {
    49     /**
    50      * The root element of the serialized form XML is {@value}.
    51      */
    52     public static final String NAME = "SerializedForm";
    54     /**
    55      * The writer for this builder.
    56      */
    57     private SerializedFormWriter writer;
    59     /**
    60      * The writer for serializable fields.
    61      */
    62     private SerializedFormWriter.SerialFieldWriter fieldWriter;
    64     /**
    65      * The writer for serializable method documentation.
    66      */
    67     private SerializedFormWriter.SerialMethodWriter methodWriter;
    69     /**
    70      * The header for the serial version UID.  Save the string
    71      * here instead of the properties file because we do not want
    72      * this string to be localized.
    73      */
    74     private static final String SERIAL_VERSION_UID_HEADER = "serialVersionUID:";
    76     /**
    77      * The current package being documented.
    78      */
    79     private PackageDoc currentPackage;
    81     /**
    82      * The current class being documented.
    83      */
    84     private ClassDoc currentClass;
    86     /**
    87      * The current member being documented.
    88      */
    89     protected MemberDoc currentMember;
    91     /**
    92      * The content that will be added to the serialized form documentation tree.
    93      */
    94     private Content contentTree;
    97     /**
    98      * Construct a new SerializedFormBuilder.
    99      * @param context  the build context.
   100      */
   101     private SerializedFormBuilder(Context context) {
   102         super(context);
   103     }
   105     /**
   106      * Construct a new SerializedFormBuilder.
   107      * @param context  the build context.
   108      */
   109     public static SerializedFormBuilder getInstance(Context context) {
   110         return new SerializedFormBuilder(context);
   111     }
   113     /**
   114      * Build the serialized form.
   115      */
   116     public void build() throws IOException {
   117         if (! serialClassFoundToDocument(configuration.root.classes())) {
   118             //Nothing to document.
   119             return;
   120         }
   121         try {
   122             writer = configuration.getWriterFactory().getSerializedFormWriter();
   123             if (writer == null) {
   124                 //Doclet does not support this output.
   125                 return;
   126             }
   127         } catch (Exception e) {
   128             throw new DocletAbortException();
   129         }
   130         build(layoutParser.parseXML(NAME), contentTree);
   131         writer.close();
   132     }
   134     /**
   135      * {@inheritDoc}
   136      */
   137     public String getName() {
   138         return NAME;
   139     }
   141     /**
   142      * Build the serialized form.
   143      *
   144      * @param node the XML element that specifies which components to document
   145      * @param serializedTree content tree to which the documentation will be added
   146      */
   147     public void buildSerializedForm(XMLNode node, Content serializedTree) throws Exception {
   148         serializedTree = writer.getHeader(configuration.getText(
   149                 "doclet.Serialized_Form"));
   150         buildChildren(node, serializedTree);
   151         writer.addFooter(serializedTree);
   152         writer.printDocument(serializedTree);
   153         writer.close();
   154     }
   156     /**
   157      * Build the serialized form summaries.
   158      *
   159      * @param node the XML element that specifies which components to document
   160      * @param serializedTree content tree to which the documentation will be added
   161      */
   162     public void buildSerializedFormSummaries(XMLNode node, Content serializedTree) {
   163         Content serializedSummariesTree = writer.getSerializedSummariesHeader();
   164         PackageDoc[] packages = configuration.packages;
   165         for (int i = 0; i < packages.length; i++) {
   166             currentPackage = packages[i];
   167             buildChildren(node, serializedSummariesTree);
   168         }
   169         serializedTree.addContent(writer.getSerializedContent(
   170                 serializedSummariesTree));
   171     }
   173     /**
   174      * Build the package serialized form for the current package being processed.
   175      *
   176      * @param node the XML element that specifies which components to document
   177      * @param serializedSummariesTree content tree to which the documentation will be added
   178      */
   179     public void buildPackageSerializedForm(XMLNode node, Content serializedSummariesTree) {
   180         Content packageSerializedTree = writer.getPackageSerializedHeader();
   181         String foo = currentPackage.name();
   182         ClassDoc[] classes = currentPackage.allClasses(false);
   183         if (classes == null || classes.length == 0) {
   184             return;
   185         }
   186         if (!serialInclude(currentPackage)) {
   187             return;
   188         }
   189         if (!serialClassFoundToDocument(classes)) {
   190             return;
   191         }
   192         buildChildren(node, packageSerializedTree);
   193         serializedSummariesTree.addContent(packageSerializedTree);
   194     }
   196     /**
   197      * Build the package header.
   198      *
   199      * @param node the XML element that specifies which components to document
   200      * @param packageSerializedTree content tree to which the documentation will be added
   201      */
   202     public void buildPackageHeader(XMLNode node, Content packageSerializedTree) {
   203         packageSerializedTree.addContent(writer.getPackageHeader(
   204                 Util.getPackageName(currentPackage)));
   205     }
   207     /**
   208      * Build the class serialized form.
   209      *
   210      * @param node the XML element that specifies which components to document
   211      * @param packageSerializedTree content tree to which the documentation will be added
   212      */
   213     public void buildClassSerializedForm(XMLNode node, Content packageSerializedTree) {
   214         Content classSerializedTree = writer.getClassSerializedHeader();
   215         ClassDoc[] classes = currentPackage.allClasses(false);
   216         Arrays.sort(classes);
   217         for (int j = 0; j < classes.length; j++) {
   218             currentClass = classes[j];
   219             fieldWriter = writer.getSerialFieldWriter(currentClass);
   220             methodWriter = writer.getSerialMethodWriter(currentClass);
   221             if(currentClass.isClass() && currentClass.isSerializable()) {
   222                 if(!serialClassInclude(currentClass)) {
   223                     continue;
   224                 }
   225                 Content classTree = writer.getClassHeader(currentClass);
   226                 buildChildren(node, classTree);
   227                 classSerializedTree.addContent(classTree);
   228             }
   229         }
   230         packageSerializedTree.addContent(classSerializedTree);
   231     }
   233     /**
   234      * Build the serial UID information for the given class.
   235      *
   236      * @param node the XML element that specifies which components to document
   237      * @param classTree content tree to which the serial UID information will be added
   238      */
   239     public void buildSerialUIDInfo(XMLNode node, Content classTree) {
   240         Content serialUidTree = writer.getSerialUIDInfoHeader();
   241         FieldDoc[] fields = currentClass.fields(false);
   242         for (int i = 0; i < fields.length; i++) {
   243             if (fields[i].name().equals("serialVersionUID") &&
   244                 fields[i].constantValueExpression() != null) {
   245                 writer.addSerialUIDInfo(SERIAL_VERSION_UID_HEADER,
   246                         fields[i].constantValueExpression(), serialUidTree);
   247                 break;
   248             }
   249         }
   250         classTree.addContent(serialUidTree);
   251     }
   253     /**
   254      * Build the summaries for the methods and fields.
   255      *
   256      * @param node the XML element that specifies which components to document
   257      * @param classTree content tree to which the documentation will be added
   258      */
   259     public void buildClassContent(XMLNode node, Content classTree) {
   260         Content classContentTree = writer.getClassContentHeader();
   261         buildChildren(node, classContentTree);
   262         classTree.addContent(classContentTree);
   263     }
   265     /**
   266      * Build the summaries for the methods that belong to the given
   267      * class.
   268      *
   269      * @param node the XML element that specifies which components to document
   270      * @param classContentTree content tree to which the documentation will be added
   271      */
   272     public void buildSerializableMethods(XMLNode node, Content classContentTree) {
   273         Content serializableMethodTree = methodWriter.getSerializableMethodsHeader();
   274         MemberDoc[] members = currentClass.serializationMethods();
   275         int membersLength = members.length;
   276         if (membersLength > 0) {
   277             for (int i = 0; i < membersLength; i++) {
   278                 currentMember = members[i];
   279                 Content methodsContentTree = methodWriter.getMethodsContentHeader(
   280                         (i == membersLength - 1));
   281                 buildChildren(node, methodsContentTree);
   282                 serializableMethodTree.addContent(methodsContentTree);
   283             }
   284         }
   285         if (currentClass.serializationMethods().length > 0) {
   286             classContentTree.addContent(methodWriter.getSerializableMethods(
   287                     configuration.getText("doclet.Serialized_Form_methods"),
   288                     serializableMethodTree));
   289             if (currentClass.isSerializable() && !currentClass.isExternalizable()) {
   290                 if (currentClass.serializationMethods().length == 0) {
   291                     Content noCustomizationMsg = methodWriter.getNoCustomizationMsg(
   292                             configuration.getText(
   293                             "doclet.Serializable_no_customization"));
   294                     classContentTree.addContent(methodWriter.getSerializableMethods(
   295                     configuration.getText("doclet.Serialized_Form_methods"),
   296                     noCustomizationMsg));
   297                 }
   298             }
   299         }
   300     }
   302     /**
   303      * Build the method sub header.
   304      *
   305      * @param node the XML element that specifies which components to document
   306      * @param methodsContentTree content tree to which the documentation will be added
   307      */
   308     public void buildMethodSubHeader(XMLNode node, Content methodsContentTree)  {
   309         methodWriter.addMemberHeader((MethodDoc)currentMember, methodsContentTree);
   310     }
   312     /**
   313      * Build the deprecated method description.
   314      *
   315      * @param node the XML element that specifies which components to document
   316      * @param methodsContentTree content tree to which the documentation will be added
   317      */
   318     public void buildDeprecatedMethodInfo(XMLNode node, Content methodsContentTree) {
   319         methodWriter.addDeprecatedMemberInfo((MethodDoc) currentMember, methodsContentTree);
   320     }
   322     /**
   323      * Build the information for the method.
   324      *
   325      * @param node the XML element that specifies which components to document
   326      * @param methodsContentTree content tree to which the documentation will be added
   327      */
   328     public void buildMethodInfo(XMLNode node, Content methodsContentTree)  {
   329         if(configuration.nocomment){
   330             return;
   331         }
   332         buildChildren(node, methodsContentTree);
   333     }
   335     /**
   336      * Build method description.
   337      *
   338      * @param node the XML element that specifies which components to document
   339      * @param methodsContentTree content tree to which the documentation will be added
   340      */
   341     public void buildMethodDescription(XMLNode node, Content methodsContentTree) {
   342         methodWriter.addMemberDescription((MethodDoc) currentMember, methodsContentTree);
   343     }
   345     /**
   346      * Build the method tags.
   347      *
   348      * @param node the XML element that specifies which components to document
   349      * @param methodsContentTree content tree to which the documentation will be added
   350      */
   351     public void buildMethodTags(XMLNode node, Content methodsContentTree) {
   352         methodWriter.addMemberTags((MethodDoc) currentMember, methodsContentTree);
   353         MethodDoc method = (MethodDoc)currentMember;
   354         if (method.name().compareTo("writeExternal") == 0
   355                 && method.tags("serialData").length == 0) {
   356             if (configuration.serialwarn) {
   357                 configuration.getDocletSpecificMsg().warning(
   358                         currentMember.position(), "doclet.MissingSerialDataTag",
   359                         method.containingClass().qualifiedName(), method.name());
   360             }
   361         }
   362     }
   364     /**
   365      * Build the field header.
   366      *
   367      * @param node the XML element that specifies which components to document
   368      * @param classContentTree content tree to which the documentation will be added
   369      */
   370     public void buildFieldHeader(XMLNode node, Content classContentTree) {
   371         if (currentClass.serializableFields().length > 0) {
   372             buildFieldSerializationOverview(currentClass, classContentTree);
   373         }
   374     }
   376     /**
   377      * Build the serialization overview for the given class.
   378      *
   379      * @param classDoc the class to print the overview for.
   380      * @param classContentTree content tree to which the documentation will be added
   381      */
   382     public void buildFieldSerializationOverview(ClassDoc classDoc, Content classContentTree) {
   383         if (classDoc.definesSerializableFields()) {
   384             FieldDoc serialPersistentField = classDoc.serializableFields()[0];
   385             // Check to see if there are inline comments, tags or deprecation
   386             // information to be printed.
   387             if (fieldWriter.shouldPrintOverview(serialPersistentField)) {
   388                 Content serializableFieldsTree = fieldWriter.getSerializableFieldsHeader();
   389                 Content fieldsOverviewContentTree = fieldWriter.getFieldsContentHeader(true);
   390                 fieldWriter.addMemberDeprecatedInfo(serialPersistentField,
   391                         fieldsOverviewContentTree);
   392                 if (!configuration.nocomment) {
   393                     fieldWriter.addMemberDescription(serialPersistentField,
   394                             fieldsOverviewContentTree);
   395                     fieldWriter.addMemberTags(serialPersistentField,
   396                             fieldsOverviewContentTree);
   397                 }
   398                 serializableFieldsTree.addContent(fieldsOverviewContentTree);
   399                 classContentTree.addContent(fieldWriter.getSerializableFields(
   400                         configuration.getText("doclet.Serialized_Form_class"),
   401                         serializableFieldsTree));
   402             }
   403         }
   404     }
   406     /**
   407      * Build the summaries for the fields that belong to the given class.
   408      *
   409      * @param node the XML element that specifies which components to document
   410      * @param classContentTree content tree to which the documentation will be added
   411      */
   412     public void buildSerializableFields(XMLNode node, Content classContentTree) {
   413         MemberDoc[] members = currentClass.serializableFields();
   414         int membersLength = members.length;
   415         if (membersLength > 0) {
   416             Content serializableFieldsTree = fieldWriter.getSerializableFieldsHeader();
   417             for (int i = 0; i < membersLength; i++) {
   418                 currentMember = members[i];
   419                 if (!currentClass.definesSerializableFields()) {
   420                     Content fieldsContentTree = fieldWriter.getFieldsContentHeader(
   421                             (i == membersLength - 1));
   422                     buildChildren(node, fieldsContentTree);
   423                     serializableFieldsTree.addContent(fieldsContentTree);
   424                 }
   425                 else {
   426                     buildSerialFieldTagsInfo(serializableFieldsTree);
   427                 }
   428             }
   429             classContentTree.addContent(fieldWriter.getSerializableFields(
   430                     configuration.getText("doclet.Serialized_Form_fields"),
   431                     serializableFieldsTree));
   432         }
   433     }
   435     /**
   436      * Build the field sub header.
   437      *
   438      * @param node the XML element that specifies which components to document
   439      * @param fieldsContentTree content tree to which the documentation will be added
   440      */
   441     public void buildFieldSubHeader(XMLNode node, Content fieldsContentTree) {
   442         if (!currentClass.definesSerializableFields()) {
   443             FieldDoc field = (FieldDoc) currentMember;
   444             fieldWriter.addMemberHeader(field.type().asClassDoc(),
   445                     field.type().typeName(), field.type().dimension(), field.name(),
   446                     fieldsContentTree);
   447         }
   448     }
   450     /**
   451      * Build the field deprecation information.
   452      *
   453      * @param node the XML element that specifies which components to document
   454      * @param fieldsContentTree content tree to which the documentation will be added
   455      */
   456     public void buildFieldDeprecationInfo(XMLNode node, Content fieldsContentTree) {
   457         if (!currentClass.definesSerializableFields()) {
   458             FieldDoc field = (FieldDoc)currentMember;
   459             fieldWriter.addMemberDeprecatedInfo(field, fieldsContentTree);
   460         }
   461     }
   463     /**
   464      * Build the serial field tags information.
   465      *
   466      * @param serializableFieldsTree content tree to which the documentation will be added
   467      */
   468     public void buildSerialFieldTagsInfo(Content serializableFieldsTree) {
   469         if(configuration.nocomment){
   470             return;
   471         }
   472         FieldDoc field = (FieldDoc)currentMember;
   473         // Process Serializable Fields specified as array of
   474         // ObjectStreamFields. Print a member for each serialField tag.
   475         // (There should be one serialField tag per ObjectStreamField
   476         // element.)
   477         SerialFieldTag[] tags = field.serialFieldTags();
   478         Arrays.sort(tags);
   479         int tagsLength = tags.length;
   480         for (int i = 0; i < tagsLength; i++) {
   481             if (tags[i].fieldName() == null || tags[i].fieldType() == null) // ignore malformed @serialField tags
   482                 continue;
   483             Content fieldsContentTree = fieldWriter.getFieldsContentHeader(
   484                     (i == tagsLength - 1));
   485             fieldWriter.addMemberHeader(tags[i].fieldTypeDoc(),
   486                     tags[i].fieldType(), "", tags[i].fieldName(), fieldsContentTree);
   487             fieldWriter.addMemberDescription(tags[i], fieldsContentTree);
   488             serializableFieldsTree.addContent(fieldsContentTree);
   489         }
   490     }
   492     /**
   493      * Build the field information.
   494      *
   495      * @param node the XML element that specifies which components to document
   496      * @param fieldsContentTree content tree to which the documentation will be added
   497      */
   498     public void buildFieldInfo(XMLNode node, Content fieldsContentTree) {
   499         if(configuration.nocomment){
   500             return;
   501         }
   502         FieldDoc field = (FieldDoc)currentMember;
   503         ClassDoc cd = field.containingClass();
   504         // Process default Serializable field.
   505         if ((field.tags("serial").length == 0) && ! field.isSynthetic()
   506                 && configuration.serialwarn) {
   507             configuration.message.warning(field.position(),
   508                     "doclet.MissingSerialTag", cd.qualifiedName(),
   509                     field.name());
   510         }
   511         fieldWriter.addMemberDescription(field, fieldsContentTree);
   512         fieldWriter.addMemberTags(field, fieldsContentTree);
   513     }
   515     /**
   516      * Return true if the given Doc should be included
   517      * in the serialized form.
   518      *
   519      * @param doc the Doc object to check for serializability.
   520      */
   521     public static boolean serialInclude(Doc doc) {
   522         if (doc == null) {
   523             return false;
   524         }
   525         return doc.isClass() ?
   526             serialClassInclude((ClassDoc)doc) :
   527             serialDocInclude(doc);
   528     }
   530     /**
   531      * Return true if the given ClassDoc should be included
   532      * in the serialized form.
   533      *
   534      * @param cd the ClassDoc object to check for serializability.
   535      */
   536     private static boolean serialClassInclude(ClassDoc cd) {
   537         if (cd.isEnum()) {
   538             return false;
   539         }
   540         try {
   541             cd.superclassType();
   542         } catch (NullPointerException e) {
   543             //Workaround for null pointer bug in ClassDoc.superclassType().
   544             return false;
   545         }
   546         if (cd.isSerializable()) {
   547             if (cd.tags("serial").length > 0) {
   548                 return serialDocInclude(cd);
   549             } else if (cd.isPublic() || cd.isProtected()) {
   550                 return true;
   551             } else {
   552                 return false;
   553             }
   554         }
   555         return false;
   556     }
   558     /**
   559      * Return true if the given Doc should be included
   560      * in the serialized form.
   561      *
   562      * @param doc the Doc object to check for serializability.
   563      */
   564     private static boolean serialDocInclude(Doc doc) {
   565         if (doc.isEnum()) {
   566             return false;
   567         }
   568         Tag[] serial = doc.tags("serial");
   569         if (serial.length > 0) {
   570             String serialtext = serial[0].text().toLowerCase();
   571             if (serialtext.indexOf("exclude") >= 0) {
   572                 return false;
   573             } else if (serialtext.indexOf("include") >= 0) {
   574                 return true;
   575             }
   576         }
   577         return true;
   578     }
   580     /**
   581      * Return true if any of the given classes have a @serialinclude tag.
   582      *
   583      * @param classes the classes to check.
   584      * @return true if any of the given classes have a @serialinclude tag.
   585      */
   586     private boolean serialClassFoundToDocument(ClassDoc[] classes) {
   587         for (int i = 0; i < classes.length; i++) {
   588             if (serialClassInclude(classes[i])) {
   589                 return true;
   590             }
   591         }
   592         return false;
   593     }
   594 }

mercurial