< prev index next >

src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/links/LinkFactory.java

Print this page




  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.util.links;
  27 
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 
  31 import javax.lang.model.element.AnnotationMirror;
  32 import javax.lang.model.element.Element;
  33 import javax.lang.model.element.TypeElement;
  34 import javax.lang.model.element.TypeParameterElement;
  35 import javax.lang.model.type.ArrayType;
  36 import javax.lang.model.type.DeclaredType;
  37 import javax.lang.model.type.TypeMirror;
  38 import javax.lang.model.type.TypeVariable;
  39 import javax.lang.model.type.WildcardType;
  40 import javax.lang.model.util.SimpleTypeVisitor9;
  41 
  42 import jdk.javadoc.internal.doclets.formats.html.LinkInfoImpl;
  43 import jdk.javadoc.internal.doclets.toolkit.Content;
  44 import jdk.javadoc.internal.doclets.toolkit.util.Utils;
  45 
  46 /**
  47  * A factory that constructs links from given link information.
  48  *
  49  *  <p><b>This is NOT part of any supported API.
  50  *  If you write code that depends on this, you do so at your own risk.
  51  *  This code and its internal interfaces are subject to change or
  52  *  deletion without notice.</b>
  53  *
  54  * @author Jamie Ho
  55  */
  56 public abstract class LinkFactory {





  57 
  58     /**
  59      * Return an empty instance of a content object.
  60      *
  61      * @return an empty instance of a content object.
  62      */
  63     protected abstract Content newContent();
  64 
  65     /**
  66      * Constructs a link from the given link information.
  67      *
  68      * @param linkInfo the information about the link.
  69      * @return the output of the link.
  70      */
  71     public Content getLink(LinkInfo linkInfo) {
  72         Utils utils = ((LinkInfoImpl) linkInfo).configuration.utils;
  73         if (linkInfo.type != null) {
  74             SimpleTypeVisitor9<Content, LinkInfo> linkVisitor =
  75                     new SimpleTypeVisitor9<Content, LinkInfo>() {
  76 
  77                 TypeMirror componentType = utils.getComponentType(linkInfo.type);
  78                 Content link = newContent();
  79 
  80                 // handles primitives, no types and error types
  81                 @Override
  82                 protected Content defaultAction(TypeMirror type, LinkInfo linkInfo) {
  83                     link.addContent(utils.getTypeName(type, false));
  84                     return link;
  85                 }
  86 
  87                 int currentDepth = 0;
  88                 @Override
  89                 public Content visitArray(ArrayType type, LinkInfo linkInfo) {
  90                     // keep track of the dimension depth and replace the last dimension
  91                     // specifier with vararags, when the stack is fully unwound.
  92                     currentDepth++;


 190             return linkVisitor.visit(linkInfo.type, linkInfo);
 191         } else if (linkInfo.typeElement != null) {
 192             Content link = newContent();
 193             link.addContent(getClassLink(linkInfo));
 194             if (linkInfo.includeTypeAsSepLink) {
 195                 link.addContent(getTypeParameterLinks(linkInfo, false));
 196             }
 197             return link;
 198         } else {
 199             return null;
 200         }
 201     }
 202 
 203     private void setBoundsLinkInfo(LinkInfo linkInfo, TypeMirror bound) {
 204         linkInfo.typeElement = null;
 205         linkInfo.label = null;
 206         linkInfo.type = bound;
 207     }
 208 
 209     /**
 210      * Return the link to the given class.
 211      *
 212      * @param linkInfo the information about the link to construct.
 213      *
 214      * @return the link for the given class.
 215      */
 216     protected abstract Content getClassLink(LinkInfo linkInfo);
 217 
 218     /**
 219      * Return the link to the given type parameter.
 220      *
 221      * @param linkInfo     the information about the link to construct.
 222      * @param typeParam the type parameter to link to.

 223      */
 224     protected abstract Content getTypeParameterLink(LinkInfo linkInfo, TypeMirror typeParam);
 225 
 226     /**
 227      * Return the links to the type parameters.
 228      *
 229      * @param linkInfo     the information about the link to construct.
 230      * @return the links to the type parameters.
 231      */
 232     public Content getTypeParameterLinks(LinkInfo linkInfo) {
 233         return getTypeParameterLinks(linkInfo, true);
 234     }
 235 
 236     /**
 237      * Return the links to the type parameters.
 238      *
 239      * @param linkInfo     the information about the link to construct.
 240      * @param isClassLabel true if this is a class label.  False if it is
 241      *                     the type parameters portion of the link.
 242      * @return the links to the type parameters.
 243      */
 244     public Content getTypeParameterLinks(LinkInfo linkInfo, boolean isClassLabel) {
 245         Utils utils = ((LinkInfoImpl)linkInfo).utils;
 246         Content links = newContent();
 247         List<TypeMirror> vars = new ArrayList<>();
 248         TypeMirror ctype = linkInfo.type != null
 249                 ? utils.getComponentType(linkInfo.type)
 250                 : null;
 251         if (linkInfo.executableElement != null) {
 252             linkInfo.executableElement.getTypeParameters().stream().forEach((t) -> {
 253                 vars.add(t.asType());
 254             });
 255         } else if (linkInfo.type != null && utils.isDeclaredType(linkInfo.type)) {
 256             ((DeclaredType)linkInfo.type).getTypeArguments().stream().forEach(vars::add);
 257         } else if (ctype != null && utils.isDeclaredType(ctype)) {
 258             ((DeclaredType)ctype).getTypeArguments().stream().forEach(vars::add);
 259         } else if (linkInfo.typeElement != null) {
 260             linkInfo.typeElement.getTypeParameters().stream().forEach((t) -> {
 261                 vars.add(t.asType());
 262             });
 263         } else {
 264             // Nothing to document.
 265             return links;


  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.util.links;
  27 
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 

  31 import javax.lang.model.element.Element;
  32 import javax.lang.model.element.TypeElement;
  33 import javax.lang.model.element.TypeParameterElement;
  34 import javax.lang.model.type.ArrayType;
  35 import javax.lang.model.type.DeclaredType;
  36 import javax.lang.model.type.TypeMirror;
  37 import javax.lang.model.type.TypeVariable;
  38 import javax.lang.model.type.WildcardType;
  39 import javax.lang.model.util.SimpleTypeVisitor9;
  40 

  41 import jdk.javadoc.internal.doclets.toolkit.Content;
  42 import jdk.javadoc.internal.doclets.toolkit.util.Utils;
  43 
  44 /**
  45  * A factory that constructs links from given link information.
  46  *
  47  *  <p><b>This is NOT part of any supported API.
  48  *  If you write code that depends on this, you do so at your own risk.
  49  *  This code and its internal interfaces are subject to change or
  50  *  deletion without notice.</b>
  51  *
  52  * @author Jamie Ho
  53  */
  54 public abstract class LinkFactory {
  55     protected final Utils utils;
  56 
  57     protected LinkFactory(Utils utils) {
  58         this.utils = utils;
  59     }
  60 
  61     /**
  62      * Return an empty instance of a content object.
  63      *
  64      * @return an empty instance of a content object.
  65      */
  66     protected abstract Content newContent();
  67 
  68     /**
  69      * Constructs a link from the given link information.
  70      *
  71      * @param linkInfo the information about the link.
  72      * @return the output of the link.
  73      */
  74     public Content getLink(LinkInfo linkInfo) {

  75         if (linkInfo.type != null) {
  76             SimpleTypeVisitor9<Content, LinkInfo> linkVisitor =
  77                     new SimpleTypeVisitor9<Content, LinkInfo>() {
  78 
  79                 TypeMirror componentType = utils.getComponentType(linkInfo.type);
  80                 Content link = newContent();
  81 
  82                 // handles primitives, no types and error types
  83                 @Override
  84                 protected Content defaultAction(TypeMirror type, LinkInfo linkInfo) {
  85                     link.addContent(utils.getTypeName(type, false));
  86                     return link;
  87                 }
  88 
  89                 int currentDepth = 0;
  90                 @Override
  91                 public Content visitArray(ArrayType type, LinkInfo linkInfo) {
  92                     // keep track of the dimension depth and replace the last dimension
  93                     // specifier with vararags, when the stack is fully unwound.
  94                     currentDepth++;


 192             return linkVisitor.visit(linkInfo.type, linkInfo);
 193         } else if (linkInfo.typeElement != null) {
 194             Content link = newContent();
 195             link.addContent(getClassLink(linkInfo));
 196             if (linkInfo.includeTypeAsSepLink) {
 197                 link.addContent(getTypeParameterLinks(linkInfo, false));
 198             }
 199             return link;
 200         } else {
 201             return null;
 202         }
 203     }
 204 
 205     private void setBoundsLinkInfo(LinkInfo linkInfo, TypeMirror bound) {
 206         linkInfo.typeElement = null;
 207         linkInfo.label = null;
 208         linkInfo.type = bound;
 209     }
 210 
 211     /**
 212      * Returns a link to the given class.
 213      *
 214      * @param linkInfo the information about the link to construct
 215      *
 216      * @return the link for the given class.
 217      */
 218     protected abstract Content getClassLink(LinkInfo linkInfo);
 219 
 220     /**
 221      * Returns a link to the given type parameter.
 222      *
 223      * @param linkInfo     the information about the link to construct
 224      * @param typeParam the type parameter to link to
 225      * @return the link
 226      */
 227     protected abstract Content getTypeParameterLink(LinkInfo linkInfo, TypeMirror typeParam);
 228 
 229     /**
 230      * Returns links to the type parameters.
 231      *
 232      * @param linkInfo     the information about the link to construct
 233      * @return the links to the type parameters.
 234      */
 235     public Content getTypeParameterLinks(LinkInfo linkInfo) {
 236         return getTypeParameterLinks(linkInfo, true);
 237     }
 238 
 239     /**
 240      * Returns links to the type parameters.
 241      *
 242      * @param linkInfo     the information about the link to construct
 243      * @param isClassLabel true if this is a class label, or false if it is
 244      *                     the type parameters portion of the link
 245      * @return the links to the type parameters
 246      */
 247     public Content getTypeParameterLinks(LinkInfo linkInfo, boolean isClassLabel) {

 248         Content links = newContent();
 249         List<TypeMirror> vars = new ArrayList<>();
 250         TypeMirror ctype = linkInfo.type != null
 251                 ? utils.getComponentType(linkInfo.type)
 252                 : null;
 253         if (linkInfo.executableElement != null) {
 254             linkInfo.executableElement.getTypeParameters().stream().forEach((t) -> {
 255                 vars.add(t.asType());
 256             });
 257         } else if (linkInfo.type != null && utils.isDeclaredType(linkInfo.type)) {
 258             ((DeclaredType)linkInfo.type).getTypeArguments().stream().forEach(vars::add);
 259         } else if (ctype != null && utils.isDeclaredType(ctype)) {
 260             ((DeclaredType)ctype).getTypeArguments().stream().forEach(vars::add);
 261         } else if (linkInfo.typeElement != null) {
 262             linkInfo.typeElement.getTypeParameters().stream().forEach((t) -> {
 263                 vars.add(t.asType());
 264             });
 265         } else {
 266             // Nothing to document.
 267             return links;
< prev index next >