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.util.DeclarationVisitor;
  34 import com.sun.tools.apt.mirror.AptEnv;
  35 import com.sun.tools.javac.code.Symbol;
  36 import com.sun.tools.javac.code.Symbol.ClassSymbol;
  37 import com.sun.tools.javac.code.Type;
  38 
  39 
  40 /**
  41  * Implementation of MemberDeclaration
  42  */
  43 @SuppressWarnings("deprecation")
  44 public abstract class MemberDeclarationImpl extends DeclarationImpl
  45                                             implements MemberDeclaration {
  46 
  47     protected MemberDeclarationImpl(AptEnv env, Symbol sym) {
  48         super(env, sym);
  49     }
  50 
  51 
  52     /**
  53      * {@inheritDoc}
  54      */
  55     public TypeDeclaration getDeclaringType() {
  56         ClassSymbol c = getDeclaringClassSymbol();
  57         return (c == null)
  58             ? null
  59             : env.declMaker.getTypeDeclaration(c);
  60     }
  61 
  62     /**
  63      * {@inheritDoc}
  64      * For methods, constructors, and types.
  65      */
  66     public Collection<TypeParameterDeclaration> getFormalTypeParameters() {
  67         ArrayList<TypeParameterDeclaration> res =
  68             new ArrayList<TypeParameterDeclaration>();
  69         for (Type t : sym.type.getTypeArguments()) {
  70             res.add(env.declMaker.getTypeParameterDeclaration(t.tsym));
  71         }
  72         return res;
  73     }
  74 
  75     /**
  76      * {@inheritDoc}
  77      */
  78     public void accept(DeclarationVisitor v) {
  79         v.visitMemberDeclaration(this);
  80     }
  81 
  82 
  83     /**
  84      * Returns the ClassSymbol of the declaring type,
  85      * or null if this is a top-level type.
  86      */
  87     private ClassSymbol getDeclaringClassSymbol() {
  88         return sym.owner.enclClass();
  89     }
  90 
  91     /**
  92      * Returns the formal type parameters of a type, member or constructor
  93      * as an angle-bracketed string.  Each parameter consists of the simple
  94      * type variable name and any bounds (with no implicit "extends Object"
  95      * clause added).  Type names are qualified.
  96      * Returns "" if there are no type parameters.
  97      */
  98     protected static String typeParamsToString(AptEnv env, Symbol sym) {
  99         if (sym.type.getTypeArguments().isEmpty()) {
 100             return "";
 101         }
 102         StringBuilder s = new StringBuilder();
 103         for (Type t : sym.type.getTypeArguments()) {
 104             Type.TypeVar tv = (Type.TypeVar) t;
 105             s.append(s.length() == 0 ? "<" : ", ")
 106              .append(TypeParameterDeclarationImpl.toString(env, tv));
 107         }
 108         s.append(">");
 109         return s.toString();
 110     }
 111 }