src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/builders/ConstructorBuilder.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 constructor.
  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 ConstructorBuilder extends AbstractMemberBuilder {
  47 
  48     /**
  49      * The name of this builder.
  50      */
  51     public static final String NAME = "ConstructorDetails";
  52 
  53     /**
  54      * The index of the current field that is being documented at this point
  55      * in time.
  56      */
  57     private int currentConstructorIndex;
  58 
  59     /**
  60      * The class whose constructors are being documented.
  61      */
  62     private final ClassDoc classDoc;
  63 
  64     /**
  65      * The visible constructors for the given class.
  66      */
  67     private final VisibleMemberMap visibleMemberMap;
  68 
  69     /**
  70      * The writer to output the constructor documentation.
  71      */
  72     private final ConstructorWriter writer;
  73 
  74     /**
  75      * The constructors being documented.
  76      */
  77     private final List<ProgramElementDoc> constructors;
  78 
  79     /**
  80      * Construct a new ConstructorBuilder.
  81      *
  82      * @param context  the build context.
  83      * @param classDoc the class whoses members are being documented.
  84      * @param writer the doclet specific writer.
  85      */
  86     private ConstructorBuilder(Context context,
  87             ClassDoc classDoc,
  88             ConstructorWriter writer) {
  89         super(context);
  90         this.classDoc = classDoc;
  91         this.writer = writer;
  92         visibleMemberMap =
  93                 new VisibleMemberMap(
  94                 classDoc,
  95                 VisibleMemberMap.CONSTRUCTORS,
  96                 configuration);
  97         constructors = new ArrayList<>(visibleMemberMap.getMembersFor(classDoc));
  98         for (ProgramElementDoc constructor : constructors) {
  99             if (constructor.isProtected() || constructor.isPrivate()) {
 100                 writer.setFoundNonPubConstructor(true);
 101             }
 102         }
 103         if (configuration.getMemberComparator() != null) {
 104             Collections.sort(constructors,configuration.getMemberComparator());
 105         }
 106     }
 107 
 108     /**
 109      * Construct a new ConstructorBuilder.
 110      *
 111      * @param context  the build context.
 112      * @param classDoc the class whoses members are being documented.
 113      * @param writer the doclet specific writer.
 114      */
 115     public static ConstructorBuilder getInstance(Context context,
 116             ClassDoc classDoc, ConstructorWriter writer) {
 117         return new ConstructorBuilder(context, classDoc, writer);
 118     }
 119 
 120     /**
 121      * {@inheritDoc}
 122      */

 123     public String getName() {
 124         return NAME;
 125     }
 126 
 127     /**
 128      * {@inheritDoc}
 129      */

 130     public boolean hasMembersToDocument() {
 131         return constructors.size() > 0;
 132     }
 133 
 134     /**
 135      * Returns a list of constructors that will be documented for the given class.
 136      * This information can be used for doclet specific documentation
 137      * generation.
 138      *
 139      * @return a list of constructors that will be documented.
 140      */
 141     public List<ProgramElementDoc> members(ClassDoc classDoc) {
 142         return visibleMemberMap.getMembersFor(classDoc);
 143     }
 144 
 145     /**
 146      * Return the constructor writer for this builder.
 147      *
 148      * @return the constructor writer for this builder.
 149      */
 150     public ConstructorWriter getWriter() {
 151         return writer;
 152     }
 153 
 154     /**
 155      * Build the constructor documentation.
 156      *
 157      * @param node the XML element that specifies which components to document
 158      * @param memberDetailsTree the content tree to which the documentation will be added
 159      */
 160     public void buildConstructorDoc(XMLNode node, Content memberDetailsTree) {
 161         if (writer == null) {
 162             return;
 163         }
 164         int size = constructors.size();
 165         if (size > 0) {
 166             Content constructorDetailsTree = writer.getConstructorDetailsTreeHeader(
 167                     classDoc, memberDetailsTree);
 168             for (currentConstructorIndex = 0; currentConstructorIndex < size;
 169                     currentConstructorIndex++) {
 170                 Content constructorDocTree = writer.getConstructorDocTreeHeader(
 171                         (ConstructorDoc) constructors.get(currentConstructorIndex),
 172                         constructorDetailsTree);
 173                 buildChildren(node, constructorDocTree);
 174                 constructorDetailsTree.addContent(writer.getConstructorDoc(
 175                         constructorDocTree, (currentConstructorIndex == size - 1)));
 176             }
 177             memberDetailsTree.addContent(
 178                     writer.getConstructorDetails(constructorDetailsTree));
 179         }
 180     }
 181 
 182     /**
 183      * Build the signature.
 184      *
 185      * @param node the XML element that specifies which components to document
 186      * @param constructorDocTree the content tree to which the documentation will be added
 187      */
 188     public void buildSignature(XMLNode node, Content constructorDocTree) {
 189         constructorDocTree.addContent(
 190                 writer.getSignature(
 191                 (ConstructorDoc) constructors.get(currentConstructorIndex)));
 192     }
 193 
 194     /**
 195      * Build the deprecation information.
 196      *
 197      * @param node the XML element that specifies which components to document
 198      * @param constructorDocTree the content tree to which the documentation will be added
 199      */
 200     public void buildDeprecationInfo(XMLNode node, Content constructorDocTree) {
 201         writer.addDeprecated(
 202                 (ConstructorDoc) constructors.get(currentConstructorIndex), constructorDocTree);
 203     }
 204 
 205     /**
 206      * Build the comments for the constructor.  Do nothing if
 207      * {@link Configuration#nocomment} is set to true.
 208      *
 209      * @param node the XML element that specifies which components to document
 210      * @param constructorDocTree the content tree to which the documentation will be added
 211      */
 212     public void buildConstructorComments(XMLNode node, Content constructorDocTree) {
 213         if (!configuration.nocomment) {
 214             writer.addComments(
 215                     (ConstructorDoc) constructors.get(currentConstructorIndex),
 216                     constructorDocTree);
 217         }
 218     }
 219 
 220     /**
 221      * Build the tag information.
 222      *
 223      * @param node the XML element that specifies which components to document
 224      * @param constructorDocTree the content tree to which the documentation will be added
 225      */
 226     public void buildTagInfo(XMLNode node, Content constructorDocTree) {
 227         writer.addTags((ConstructorDoc) constructors.get(currentConstructorIndex),
 228                 constructorDocTree);
 229     }
 230 }
   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.ExecutableElement;
  32 import javax.lang.model.element.TypeElement;
  33 
  34 import jdk.javadoc.internal.doclets.toolkit.Configuration;
  35 import jdk.javadoc.internal.doclets.toolkit.ConstructorWriter;
  36 import jdk.javadoc.internal.doclets.toolkit.Content;
  37 import jdk.javadoc.internal.doclets.toolkit.util.VisibleMemberMap;
  38 
  39 
  40 /**
  41  * Builds documentation for a constructor.
  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 ConstructorBuilder extends AbstractMemberBuilder {
  53 
  54     /**
  55      * The name of this builder.
  56      */
  57     public static final String NAME = "ConstructorDetails";
  58 
  59     /**
  60      * The current constructor that is being documented at this point in time.

  61      */
  62     private ExecutableElement constructor;
  63 
  64     /**
  65      * The class whose constructors are being documented.
  66      */
  67     private final TypeElement typeElement;
  68 
  69     /**
  70      * The visible constructors for the given class.
  71      */
  72     private final VisibleMemberMap visibleMemberMap;
  73 
  74     /**
  75      * The writer to output the constructor documentation.
  76      */
  77     private final ConstructorWriter writer;
  78 
  79     /**
  80      * The constructors being documented.
  81      */
  82     private final SortedSet<Element> constructors;
  83 
  84     /**
  85      * Construct a new ConstructorBuilder.
  86      *
  87      * @param context  the build context.
  88      * @param typeElement the class whoses members are being documented.
  89      * @param writer the doclet specific writer.
  90      */
  91     private ConstructorBuilder(Context context,
  92             TypeElement typeElement,
  93             ConstructorWriter writer) {
  94         super(context);
  95         this.typeElement = typeElement;
  96         this.writer = writer;
  97         visibleMemberMap =
  98                 new VisibleMemberMap(
  99                 typeElement,
 100                 VisibleMemberMap.Kind.CONSTRUCTORS,
 101                 configuration);
 102         constructors = visibleMemberMap.getMembersFor(typeElement);
 103         for (Element ctor : constructors) {
 104             if (utils.isProtected(ctor) || utils.isPrivate(ctor)) {
 105                 writer.setFoundNonPubConstructor(true);
 106             }
 107         }


 108     }

 109 
 110     /**
 111      * Construct a new ConstructorBuilder.
 112      *
 113      * @param context  the build context.
 114      * @param typeElement the class whoses members are being documented.
 115      * @param writer the doclet specific writer.
 116      */
 117     public static ConstructorBuilder getInstance(Context context,
 118             TypeElement typeElement, ConstructorWriter writer) {
 119         return new ConstructorBuilder(context, typeElement, writer);
 120     }
 121 
 122     /**
 123      * {@inheritDoc}
 124      */
 125     @Override
 126     public String getName() {
 127         return NAME;
 128     }
 129 
 130     /**
 131      * {@inheritDoc}
 132      */
 133     @Override
 134     public boolean hasMembersToDocument() {
 135         return !constructors.isEmpty();
 136     }
 137 
 138     /**
 139      * Returns a list of constructors that will be documented for the given class.
 140      * This information can be used for doclet specific documentation
 141      * generation.
 142      *
 143      * @return a list of constructors that will be documented.
 144      */
 145     public SortedSet<Element> members(TypeElement typeElement) {
 146         return visibleMemberMap.getMembersFor(typeElement);
 147     }
 148 
 149     /**
 150      * Return the constructor writer for this builder.
 151      *
 152      * @return the constructor writer for this builder.
 153      */
 154     public ConstructorWriter getWriter() {
 155         return writer;
 156     }
 157 
 158     /**
 159      * Build the constructor documentation.
 160      *
 161      * @param node the XML element that specifies which components to document
 162      * @param memberDetailsTree the content tree to which the documentation will be added
 163      */
 164     public void buildConstructorDoc(XMLNode node, Content memberDetailsTree) {
 165         if (writer == null) {
 166             return;
 167         }
 168         int size = constructors.size();
 169         if (size > 0) {
 170             Content constructorDetailsTree = writer.getConstructorDetailsTreeHeader(typeElement,
 171                     memberDetailsTree);
 172             for (Element ctor : constructors) {
 173                 constructor = (ExecutableElement)ctor;
 174                 Content constructorDocTree = writer.getConstructorDocTreeHeader(
 175                         constructor, constructorDetailsTree);

 176                 buildChildren(node, constructorDocTree);
 177                 constructorDetailsTree.addContent(writer.getConstructorDoc(constructorDocTree,
 178                         constructors.last().equals(constructor)));
 179             }
 180             memberDetailsTree.addContent(
 181                     writer.getConstructorDetails(constructorDetailsTree));
 182         }
 183     }
 184 
 185     /**
 186      * Build the signature.
 187      *
 188      * @param node the XML element that specifies which components to document
 189      * @param constructorDocTree the content tree to which the documentation will be added
 190      */
 191     public void buildSignature(XMLNode node, Content constructorDocTree) {
 192         constructorDocTree.addContent(writer.getSignature(constructor));


 193     }
 194 
 195     /**
 196      * Build the deprecation information.
 197      *
 198      * @param node the XML element that specifies which components to document
 199      * @param constructorDocTree the content tree to which the documentation will be added
 200      */
 201     public void buildDeprecationInfo(XMLNode node, Content constructorDocTree) {
 202         writer.addDeprecated(constructor, constructorDocTree);

 203     }
 204 
 205     /**
 206      * Build the comments for the constructor.  Do nothing if
 207      * {@link Configuration#nocomment} is set to true.
 208      *
 209      * @param node the XML element that specifies which components to document
 210      * @param constructorDocTree the content tree to which the documentation will be added
 211      */
 212     public void buildConstructorComments(XMLNode node, Content constructorDocTree) {
 213         if (!configuration.nocomment) {
 214             writer.addComments(constructor, constructorDocTree);


 215         }
 216     }
 217 
 218     /**
 219      * Build the tag information.
 220      *
 221      * @param node the XML element that specifies which components to document
 222      * @param constructorDocTree the content tree to which the documentation will be added
 223      */
 224     public void buildTagInfo(XMLNode node, Content constructorDocTree) {
 225         writer.addTags(constructor, constructorDocTree);

 226     }
 227 }