001: /*
002: * Copyright 2003-2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.javadoc;
027:
028: import com.sun.javadoc.*;
029:
030: import com.sun.tools.javac.code.Kinds;
031: import com.sun.tools.javac.code.Symbol;
032: import com.sun.tools.javac.code.Symbol.ClassSymbol;
033: import com.sun.tools.javac.code.Symbol.MethodSymbol;
034: import com.sun.tools.javac.code.Type;
035: import com.sun.tools.javac.code.Type.TypeVar;
036: import com.sun.tools.javac.util.List;
037: import com.sun.tools.javac.util.Name;
038:
039: /**
040: * Implementation of <code>TypeVariable</code>, which
041: * represents a type variable.
042: *
043: * @author Scott Seligman
044: * @version 1.13 07/05/05
045: * @since 1.5
046: */
047: public class TypeVariableImpl extends AbstractTypeImpl implements
048: TypeVariable {
049:
050: TypeVariableImpl(DocEnv env, TypeVar type) {
051: super (env, type);
052: }
053:
054: /**
055: * Return the bounds of this type variable.
056: */
057: public com.sun.javadoc.Type[] bounds() {
058: return TypeMaker.getTypes(env, getBounds((TypeVar) type, env));
059: }
060:
061: /**
062: * Return the class, interface, method, or constructor within
063: * which this type variable is declared.
064: */
065: public ProgramElementDoc owner() {
066: Symbol osym = type.tsym.owner;
067: if ((osym.kind & Kinds.TYP) != 0) {
068: return env.getClassDoc((ClassSymbol) osym);
069: }
070: Name.Table names = osym.name.table;
071: if (osym.name == names.init) {
072: return env.getConstructorDoc((MethodSymbol) osym);
073: } else {
074: return env.getMethodDoc((MethodSymbol) osym);
075: }
076: }
077:
078: /**
079: * Return the ClassDoc of the erasure of this type variable.
080: */
081: public ClassDoc asClassDoc() {
082: return env
083: .getClassDoc((ClassSymbol) env.types.erasure(type).tsym);
084: }
085:
086: public TypeVariable asTypeVariable() {
087: return this ;
088: }
089:
090: public String toString() {
091: return typeVarToString(env, (TypeVar) type, true);
092: }
093:
094: /**
095: * Return the string form of a type variable along with any
096: * "extends" clause. Class names are qualified if "full" is true.
097: */
098: static String typeVarToString(DocEnv env, TypeVar v, boolean full) {
099: StringBuffer s = new StringBuffer(v.toString());
100: List<Type> bounds = getBounds(v, env);
101: if (bounds.nonEmpty()) {
102: boolean first = true;
103: for (Type b : bounds) {
104: s.append(first ? " extends " : " & ");
105: s.append(TypeMaker.getTypeString(env, b, full));
106: first = false;
107: }
108: }
109: return s.toString();
110: }
111:
112: /**
113: * Get the bounds of a type variable as listed in the "extends" clause.
114: */
115: private static List<Type> getBounds(TypeVar v, DocEnv env) {
116: Name boundname = v.getUpperBound().tsym.getQualifiedName();
117: if (boundname == boundname.table.java_lang_Object) {
118: return List.nil();
119: } else {
120: return env.types.getBounds(v);
121: }
122: }
123: }
|