1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javadoc/TypeVariableImpl.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,151 @@ 1.4 +/* 1.5 + * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.javadoc; 1.30 + 1.31 +import com.sun.javadoc.*; 1.32 + 1.33 +import com.sun.tools.javac.code.Attribute; 1.34 +import com.sun.tools.javac.code.Attribute.TypeCompound; 1.35 +import com.sun.tools.javac.code.Kinds; 1.36 +import com.sun.tools.javac.code.Symbol; 1.37 +import com.sun.tools.javac.code.Symbol.ClassSymbol; 1.38 +import com.sun.tools.javac.code.Symbol.MethodSymbol; 1.39 +import com.sun.tools.javac.code.Type; 1.40 +import com.sun.tools.javac.code.Type.TypeVar; 1.41 +import com.sun.tools.javac.util.List; 1.42 +import com.sun.tools.javac.util.Name; 1.43 +import com.sun.tools.javac.util.Names; 1.44 + 1.45 +/** 1.46 + * Implementation of <code>TypeVariable</code>, which 1.47 + * represents a type variable. 1.48 + * 1.49 + * <p><b>This is NOT part of any supported API. 1.50 + * If you write code that depends on this, you do so at your own risk. 1.51 + * This code and its internal interfaces are subject to change or 1.52 + * deletion without notice.</b> 1.53 + * 1.54 + * @author Scott Seligman 1.55 + * @since 1.5 1.56 + */ 1.57 +public class TypeVariableImpl extends AbstractTypeImpl implements TypeVariable { 1.58 + 1.59 + TypeVariableImpl(DocEnv env, TypeVar type) { 1.60 + super(env, type); 1.61 + } 1.62 + 1.63 + /** 1.64 + * Return the bounds of this type variable. 1.65 + */ 1.66 + public com.sun.javadoc.Type[] bounds() { 1.67 + return TypeMaker.getTypes(env, getBounds((TypeVar)type, env)); 1.68 + } 1.69 + 1.70 + /** 1.71 + * Return the class, interface, method, or constructor within 1.72 + * which this type variable is declared. 1.73 + */ 1.74 + public ProgramElementDoc owner() { 1.75 + Symbol osym = type.tsym.owner; 1.76 + if ((osym.kind & Kinds.TYP) != 0) { 1.77 + return env.getClassDoc((ClassSymbol)osym); 1.78 + } 1.79 + Names names = osym.name.table.names; 1.80 + if (osym.name == names.init) { 1.81 + return env.getConstructorDoc((MethodSymbol)osym); 1.82 + } else { 1.83 + return env.getMethodDoc((MethodSymbol)osym); 1.84 + } 1.85 + } 1.86 + 1.87 + /** 1.88 + * Return the ClassDoc of the erasure of this type variable. 1.89 + */ 1.90 + @Override 1.91 + public ClassDoc asClassDoc() { 1.92 + return env.getClassDoc((ClassSymbol)env.types.erasure(type).tsym); 1.93 + } 1.94 + 1.95 + @Override 1.96 + public TypeVariable asTypeVariable() { 1.97 + return this; 1.98 + } 1.99 + 1.100 + @Override 1.101 + public String toString() { 1.102 + return typeVarToString(env, (TypeVar)type, true); 1.103 + } 1.104 + 1.105 + 1.106 + /** 1.107 + * Return the string form of a type variable along with any 1.108 + * "extends" clause. Class names are qualified if "full" is true. 1.109 + */ 1.110 + static String typeVarToString(DocEnv env, TypeVar v, boolean full) { 1.111 + StringBuilder s = new StringBuilder(v.toString()); 1.112 + List<Type> bounds = getBounds(v, env); 1.113 + if (bounds.nonEmpty()) { 1.114 + boolean first = true; 1.115 + for (Type b : bounds) { 1.116 + s.append(first ? " extends " : " & "); 1.117 + s.append(TypeMaker.getTypeString(env, b, full)); 1.118 + first = false; 1.119 + } 1.120 + } 1.121 + return s.toString(); 1.122 + } 1.123 + 1.124 + /** 1.125 + * Get the bounds of a type variable as listed in the "extends" clause. 1.126 + */ 1.127 + private static List<Type> getBounds(TypeVar v, DocEnv env) { 1.128 + final Type upperBound = v.getUpperBound(); 1.129 + Name boundname = upperBound.tsym.getQualifiedName(); 1.130 + if (boundname == boundname.table.names.java_lang_Object 1.131 + && !upperBound.isAnnotated()) { 1.132 + return List.nil(); 1.133 + } else { 1.134 + return env.types.getBounds(v); 1.135 + } 1.136 + } 1.137 + 1.138 + /** 1.139 + * Get the annotations of this program element. 1.140 + * Return an empty array if there are none. 1.141 + */ 1.142 + public AnnotationDesc[] annotations() { 1.143 + if (!type.isAnnotated()) { 1.144 + return new AnnotationDesc[0]; 1.145 + } 1.146 + List<? extends TypeCompound> tas = type.getAnnotationMirrors(); 1.147 + AnnotationDesc res[] = new AnnotationDesc[tas.length()]; 1.148 + int i = 0; 1.149 + for (Attribute.Compound a : tas) { 1.150 + res[i++] = new AnnotationDescImpl(env, a); 1.151 + } 1.152 + return res; 1.153 + } 1.154 +}