src/share/classes/com/sun/tools/javadoc/TypeVariableImpl.java

Thu, 18 Apr 2013 20:00:14 -0700

author
jjg
date
Thu, 18 Apr 2013 20:00:14 -0700
changeset 1706
95d29b99e5b3
parent 1644
40adaf938847
child 1755
ddb4a2bfcd82
permissions
-rw-r--r--

8012656: cache frequently used name strings for DocImpl classes
Reviewed-by: darcy

duke@1 1 /*
jjg@1521 2 * Copyright (c) 2003, 2013, 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.javadoc;
duke@1 27
jjg@1521 28 import javax.lang.model.type.TypeKind;
jjg@1521 29
duke@1 30 import com.sun.javadoc.*;
duke@1 31
jjg@1521 32 import com.sun.tools.javac.code.Attribute;
jjg@1521 33 import com.sun.tools.javac.code.Attribute.TypeCompound;
duke@1 34 import com.sun.tools.javac.code.Kinds;
duke@1 35 import com.sun.tools.javac.code.Symbol;
duke@1 36 import com.sun.tools.javac.code.Symbol.ClassSymbol;
duke@1 37 import com.sun.tools.javac.code.Symbol.MethodSymbol;
duke@1 38 import com.sun.tools.javac.code.Type;
duke@1 39 import com.sun.tools.javac.code.Type.TypeVar;
duke@1 40 import com.sun.tools.javac.util.List;
duke@1 41 import com.sun.tools.javac.util.Name;
jjg@113 42 import com.sun.tools.javac.util.Names;
duke@1 43
duke@1 44 /**
duke@1 45 * Implementation of <code>TypeVariable</code>, which
duke@1 46 * represents a type variable.
duke@1 47 *
jjg@1359 48 * <p><b>This is NOT part of any supported API.
jjg@1359 49 * If you write code that depends on this, you do so at your own risk.
jjg@1359 50 * This code and its internal interfaces are subject to change or
jjg@1359 51 * deletion without notice.</b>
jjg@1359 52 *
duke@1 53 * @author Scott Seligman
duke@1 54 * @since 1.5
duke@1 55 */
duke@1 56 public class TypeVariableImpl extends AbstractTypeImpl implements TypeVariable {
duke@1 57
duke@1 58 TypeVariableImpl(DocEnv env, TypeVar type) {
duke@1 59 super(env, type);
duke@1 60 }
duke@1 61
duke@1 62 /**
duke@1 63 * Return the bounds of this type variable.
duke@1 64 */
duke@1 65 public com.sun.javadoc.Type[] bounds() {
duke@1 66 return TypeMaker.getTypes(env, getBounds((TypeVar)type, env));
duke@1 67 }
duke@1 68
duke@1 69 /**
duke@1 70 * Return the class, interface, method, or constructor within
duke@1 71 * which this type variable is declared.
duke@1 72 */
duke@1 73 public ProgramElementDoc owner() {
duke@1 74 Symbol osym = type.tsym.owner;
duke@1 75 if ((osym.kind & Kinds.TYP) != 0) {
duke@1 76 return env.getClassDoc((ClassSymbol)osym);
duke@1 77 }
jjg@113 78 Names names = osym.name.table.names;
duke@1 79 if (osym.name == names.init) {
duke@1 80 return env.getConstructorDoc((MethodSymbol)osym);
duke@1 81 } else {
duke@1 82 return env.getMethodDoc((MethodSymbol)osym);
duke@1 83 }
duke@1 84 }
duke@1 85
duke@1 86 /**
duke@1 87 * Return the ClassDoc of the erasure of this type variable.
duke@1 88 */
jjg@910 89 @Override
duke@1 90 public ClassDoc asClassDoc() {
duke@1 91 return env.getClassDoc((ClassSymbol)env.types.erasure(type).tsym);
duke@1 92 }
duke@1 93
jjg@910 94 @Override
duke@1 95 public TypeVariable asTypeVariable() {
duke@1 96 return this;
duke@1 97 }
duke@1 98
jjg@910 99 @Override
duke@1 100 public String toString() {
duke@1 101 return typeVarToString(env, (TypeVar)type, true);
duke@1 102 }
duke@1 103
duke@1 104
duke@1 105 /**
duke@1 106 * Return the string form of a type variable along with any
duke@1 107 * "extends" clause. Class names are qualified if "full" is true.
duke@1 108 */
duke@1 109 static String typeVarToString(DocEnv env, TypeVar v, boolean full) {
jjg@910 110 StringBuilder s = new StringBuilder(v.toString());
duke@1 111 List<Type> bounds = getBounds(v, env);
duke@1 112 if (bounds.nonEmpty()) {
duke@1 113 boolean first = true;
duke@1 114 for (Type b : bounds) {
duke@1 115 s.append(first ? " extends " : " & ");
duke@1 116 s.append(TypeMaker.getTypeString(env, b, full));
duke@1 117 first = false;
duke@1 118 }
duke@1 119 }
duke@1 120 return s.toString();
duke@1 121 }
duke@1 122
duke@1 123 /**
duke@1 124 * Get the bounds of a type variable as listed in the "extends" clause.
duke@1 125 */
duke@1 126 private static List<Type> getBounds(TypeVar v, DocEnv env) {
jjg@1521 127 final Type upperBound = v.getUpperBound();
jjg@1521 128 Name boundname = upperBound.tsym.getQualifiedName();
jjg@1521 129 if (boundname == boundname.table.names.java_lang_Object
jjg@1644 130 && !upperBound.isAnnotated()) {
duke@1 131 return List.nil();
duke@1 132 } else {
duke@1 133 return env.types.getBounds(v);
duke@1 134 }
duke@1 135 }
jjg@1521 136
jjg@1521 137 /**
jjg@1521 138 * Get the annotations of this program element.
jjg@1521 139 * Return an empty array if there are none.
jjg@1521 140 */
jjg@1521 141 public AnnotationDesc[] annotations() {
jjg@1644 142 if (!type.isAnnotated()) {
jjg@1521 143 return new AnnotationDesc[0];
jjg@1521 144 }
jjg@1521 145 List<TypeCompound> tas = ((com.sun.tools.javac.code.Type.AnnotatedType) type).typeAnnotations;
jjg@1521 146 AnnotationDesc res[] = new AnnotationDesc[tas.length()];
jjg@1521 147 int i = 0;
jjg@1521 148 for (Attribute.Compound a : tas) {
jjg@1521 149 res[i++] = new AnnotationDescImpl(env, a);
jjg@1521 150 }
jjg@1521 151 return res;
jjg@1521 152 }
duke@1 153 }

mercurial