1 /* 2 * Copyright 2004-2005 Sun Microsystems, Inc. All Rights Reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Sun designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Sun in the LICENSE file that accompanied this code. 10 * 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 22 * CA 95054 USA or visit www.sun.com if you need additional information or 23 * have any questions. 24 */ 25 26 package com.sun.tools.apt.mirror.declaration; 27 28 29 import java.util.Collection; 30 import java.util.ArrayList; 31 32 import com.sun.mirror.declaration.*; 33 import com.sun.mirror.type.ReferenceType; 34 import com.sun.mirror.util.DeclarationVisitor; 35 import com.sun.tools.apt.mirror.AptEnv; 36 import com.sun.tools.javac.code.*; 37 import com.sun.tools.javac.code.Symbol.*; 38 39 40 /** 41 * Implementation of TypeParameterDeclaration 42 */ 43 44 public class TypeParameterDeclarationImpl extends DeclarationImpl 45 implements TypeParameterDeclaration 46 { 47 protected TypeSymbol sym; 48 49 50 TypeParameterDeclarationImpl(AptEnv env, TypeSymbol sym) { 51 super(env, sym); 52 this.sym = sym; 53 } 54 55 56 /** 57 * Returns the type parameter's name along with any "extends" clause. 58 * Class names are qualified. No implicit "extends Object" is added. 59 */ 60 public String toString() { 61 return toString(env, (Type.TypeVar) sym.type); 62 } 63 64 /** 65 * {@inheritDoc} 66 */ 67 public Collection<ReferenceType> getBounds() { 68 ArrayList<ReferenceType> res = new ArrayList<ReferenceType>(); 69 for (Type t : env.jctypes.getBounds((Type.TypeVar) sym.type)) { 70 res.add((ReferenceType) env.typeMaker.getType(t)); 71 } 72 return res; 73 } 74 75 /** 76 * {@inheritDoc} 77 */ 78 public Declaration getOwner() { 79 Symbol owner = sym.owner; 80 return ((owner.kind & Kinds.TYP) != 0) 81 ? env.declMaker.getTypeDeclaration((ClassSymbol) owner) 82 : env.declMaker.getExecutableDeclaration((MethodSymbol) owner); 83 } 84 85 86 87 /** 88 * {@inheritDoc} 89 */ 90 public void accept(DeclarationVisitor v) { 91 v.visitTypeParameterDeclaration(this); 92 } 93 94 95 /** 96 * Returns the type parameter's name along with any "extends" clause. 97 * See {@link #toString()} for details. 98 */ 99 static String toString(AptEnv env, Type.TypeVar tv) { 100 StringBuilder s = new StringBuilder(); 101 s.append(tv); 102 boolean first = true; 103 for (Type bound : getExtendsBounds(env, tv)) { 104 s.append(first ? " extends " : " & "); 105 s.append(env.typeMaker.typeToString(bound)); 106 first = false; 107 } 108 return s.toString(); 109 } 110 111 /** 112 * Returns the bounds of a type variable, eliding java.lang.Object 113 * if it appears alone. 114 */ 115 private static Iterable<Type> getExtendsBounds(AptEnv env, 116 Type.TypeVar tv) { 117 return (tv.getUpperBound().tsym == env.symtab.objectType.tsym) 118 ? com.sun.tools.javac.util.List.<Type>nil() 119 : env.jctypes.getBounds(tv); 120 } 121 }