aoqi@0: /* aoqi@0: * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.doclets.internal.toolkit.util.links; aoqi@0: aoqi@0: import com.sun.javadoc.*; aoqi@0: import com.sun.tools.doclets.internal.toolkit.Content; aoqi@0: aoqi@0: /** aoqi@0: * A factory that constructs links from given link information. aoqi@0: * aoqi@0: *

This is NOT part of any supported API. aoqi@0: * If you write code that depends on this, you do so at your own risk. aoqi@0: * This code and its internal interfaces are subject to change or aoqi@0: * deletion without notice. aoqi@0: * aoqi@0: * @author Jamie Ho aoqi@0: * @since 1.5 aoqi@0: */ aoqi@0: public abstract class LinkFactory { aoqi@0: aoqi@0: /** aoqi@0: * Return an empty instance of a content object. aoqi@0: * aoqi@0: * @return an empty instance of a content object. aoqi@0: */ aoqi@0: protected abstract Content newContent(); aoqi@0: aoqi@0: /** aoqi@0: * Constructs a link from the given link information. aoqi@0: * aoqi@0: * @param linkInfo the information about the link. aoqi@0: * @return the output of the link. aoqi@0: */ aoqi@0: public Content getLink(LinkInfo linkInfo) { aoqi@0: if (linkInfo.type != null) { aoqi@0: Type type = linkInfo.type; aoqi@0: Content link = newContent(); aoqi@0: if (type.isPrimitive()) { aoqi@0: //Just a primitive. aoqi@0: link.addContent(type.typeName()); aoqi@0: } else if (type.asAnnotatedType() != null && type.dimension().length() == 0) { aoqi@0: link.addContent(getTypeAnnotationLinks(linkInfo)); aoqi@0: linkInfo.type = type.asAnnotatedType().underlyingType(); aoqi@0: link.addContent(getLink(linkInfo)); aoqi@0: return link; aoqi@0: } else if (type.asWildcardType() != null) { aoqi@0: //Wildcard type. aoqi@0: linkInfo.isTypeBound = true; aoqi@0: link.addContent("?"); aoqi@0: WildcardType wildcardType = type.asWildcardType(); aoqi@0: Type[] extendsBounds = wildcardType.extendsBounds(); aoqi@0: for (int i = 0; i < extendsBounds.length; i++) { aoqi@0: link.addContent(i > 0 ? ", " : " extends "); aoqi@0: setBoundsLinkInfo(linkInfo, extendsBounds[i]); aoqi@0: link.addContent(getLink(linkInfo)); aoqi@0: } aoqi@0: Type[] superBounds = wildcardType.superBounds(); aoqi@0: for (int i = 0; i < superBounds.length; i++) { aoqi@0: link.addContent(i > 0 ? ", " : " super "); aoqi@0: setBoundsLinkInfo(linkInfo, superBounds[i]); aoqi@0: link.addContent(getLink(linkInfo)); aoqi@0: } aoqi@0: } else if (type.asTypeVariable()!= null) { aoqi@0: link.addContent(getTypeAnnotationLinks(linkInfo)); aoqi@0: linkInfo.isTypeBound = true; aoqi@0: //A type variable. aoqi@0: Doc owner = type.asTypeVariable().owner(); aoqi@0: if ((! linkInfo.excludeTypeParameterLinks) && aoqi@0: owner instanceof ClassDoc) { aoqi@0: linkInfo.classDoc = (ClassDoc) owner; aoqi@0: Content label = newContent(); aoqi@0: label.addContent(type.typeName()); aoqi@0: linkInfo.label = label; aoqi@0: link.addContent(getClassLink(linkInfo)); aoqi@0: } else { aoqi@0: //No need to link method type parameters. aoqi@0: link.addContent(type.typeName()); aoqi@0: } aoqi@0: aoqi@0: Type[] bounds = type.asTypeVariable().bounds(); aoqi@0: if (! linkInfo.excludeTypeBounds) { aoqi@0: linkInfo.excludeTypeBounds = true; aoqi@0: for (int i = 0; i < bounds.length; i++) { aoqi@0: link.addContent(i > 0 ? " & " : " extends "); aoqi@0: setBoundsLinkInfo(linkInfo, bounds[i]); aoqi@0: link.addContent(getLink(linkInfo)); aoqi@0: } aoqi@0: } aoqi@0: } else if (type.asClassDoc() != null) { aoqi@0: //A class type. aoqi@0: if (linkInfo.isTypeBound && aoqi@0: linkInfo.excludeTypeBoundsLinks) { aoqi@0: //Since we are excluding type parameter links, we should not aoqi@0: //be linking to the type bound. aoqi@0: link.addContent(type.typeName()); aoqi@0: link.addContent(getTypeParameterLinks(linkInfo)); aoqi@0: return link; aoqi@0: } else { aoqi@0: linkInfo.classDoc = type.asClassDoc(); aoqi@0: link = newContent(); aoqi@0: link.addContent(getClassLink(linkInfo)); aoqi@0: if (linkInfo.includeTypeAsSepLink) { aoqi@0: link.addContent(getTypeParameterLinks(linkInfo, false)); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (linkInfo.isVarArg) { aoqi@0: if (type.dimension().length() > 2) { aoqi@0: //Javadoc returns var args as array. aoqi@0: //Strip out the first [] from the var arg. aoqi@0: link.addContent(type.dimension().substring(2)); aoqi@0: } aoqi@0: link.addContent("..."); aoqi@0: } else { aoqi@0: while (type != null && type.dimension().length() > 0) { aoqi@0: if (type.asAnnotatedType() != null) { aoqi@0: linkInfo.type = type; aoqi@0: link.addContent(" "); aoqi@0: link.addContent(getTypeAnnotationLinks(linkInfo)); aoqi@0: link.addContent("[]"); aoqi@0: type = type.asAnnotatedType().underlyingType().getElementType(); aoqi@0: } else { aoqi@0: link.addContent("[]"); aoqi@0: type = type.getElementType(); aoqi@0: } aoqi@0: } aoqi@0: linkInfo.type = type; aoqi@0: Content newLink = newContent(); aoqi@0: newLink.addContent(getTypeAnnotationLinks(linkInfo)); aoqi@0: newLink.addContent(link); aoqi@0: link = newLink; aoqi@0: } aoqi@0: return link; aoqi@0: } else if (linkInfo.classDoc != null) { aoqi@0: //Just a class link aoqi@0: Content link = newContent(); aoqi@0: link.addContent(getClassLink(linkInfo)); aoqi@0: if (linkInfo.includeTypeAsSepLink) { aoqi@0: link.addContent(getTypeParameterLinks(linkInfo, false)); aoqi@0: } aoqi@0: return link; aoqi@0: } else { aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private void setBoundsLinkInfo(LinkInfo linkInfo, Type bound) { aoqi@0: linkInfo.classDoc = null; aoqi@0: linkInfo.label = null; aoqi@0: linkInfo.type = bound; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return the link to the given class. aoqi@0: * aoqi@0: * @param linkInfo the information about the link to construct. aoqi@0: * aoqi@0: * @return the link for the given class. aoqi@0: */ aoqi@0: protected abstract Content getClassLink(LinkInfo linkInfo); aoqi@0: aoqi@0: /** aoqi@0: * Return the link to the given type parameter. aoqi@0: * aoqi@0: * @param linkInfo the information about the link to construct. aoqi@0: * @param typeParam the type parameter to link to. aoqi@0: */ aoqi@0: protected abstract Content getTypeParameterLink(LinkInfo linkInfo, aoqi@0: Type typeParam); aoqi@0: aoqi@0: protected abstract Content getTypeAnnotationLink(LinkInfo linkInfo, aoqi@0: AnnotationDesc annotation); aoqi@0: aoqi@0: /** aoqi@0: * Return the links to the type parameters. aoqi@0: * aoqi@0: * @param linkInfo the information about the link to construct. aoqi@0: * @return the links to the type parameters. aoqi@0: */ aoqi@0: public Content getTypeParameterLinks(LinkInfo linkInfo) { aoqi@0: return getTypeParameterLinks(linkInfo, true); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return the links to the type parameters. aoqi@0: * aoqi@0: * @param linkInfo the information about the link to construct. aoqi@0: * @param isClassLabel true if this is a class label. False if it is aoqi@0: * the type parameters portion of the link. aoqi@0: * @return the links to the type parameters. aoqi@0: */ aoqi@0: public Content getTypeParameterLinks(LinkInfo linkInfo, boolean isClassLabel) { aoqi@0: Content links = newContent(); aoqi@0: Type[] vars; aoqi@0: if (linkInfo.executableMemberDoc != null) { aoqi@0: vars = linkInfo.executableMemberDoc.typeParameters(); aoqi@0: } else if (linkInfo.type != null && aoqi@0: linkInfo.type.asParameterizedType() != null){ aoqi@0: vars = linkInfo.type.asParameterizedType().typeArguments(); aoqi@0: } else if (linkInfo.classDoc != null){ aoqi@0: vars = linkInfo.classDoc.typeParameters(); aoqi@0: } else { aoqi@0: //Nothing to document. aoqi@0: return links; aoqi@0: } aoqi@0: if (((linkInfo.includeTypeInClassLinkLabel && isClassLabel) || aoqi@0: (linkInfo.includeTypeAsSepLink && ! isClassLabel) aoqi@0: ) aoqi@0: && vars.length > 0) { aoqi@0: links.addContent("<"); aoqi@0: for (int i = 0; i < vars.length; i++) { aoqi@0: if (i > 0) { aoqi@0: links.addContent(","); aoqi@0: } aoqi@0: links.addContent(getTypeParameterLink(linkInfo, vars[i])); aoqi@0: } aoqi@0: links.addContent(">"); aoqi@0: } aoqi@0: return links; aoqi@0: } aoqi@0: aoqi@0: public Content getTypeAnnotationLinks(LinkInfo linkInfo) { aoqi@0: Content links = newContent(); aoqi@0: if (linkInfo.type.asAnnotatedType() == null) aoqi@0: return links; aoqi@0: AnnotationDesc[] annotations = linkInfo.type.asAnnotatedType().annotations(); aoqi@0: for (int i = 0; i < annotations.length; i++) { aoqi@0: if (i > 0) { aoqi@0: links.addContent(" "); aoqi@0: } aoqi@0: links.addContent(getTypeAnnotationLink(linkInfo, annotations[i])); aoqi@0: } aoqi@0: aoqi@0: links.addContent(" "); aoqi@0: return links; aoqi@0: } aoqi@0: }