001    /*
002     * Copyright 2004-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.apt.mirror.declaration;
027    
028    
029    import java.util.Collection;
030    import java.util.ArrayList;
031    
032    import com.sun.mirror.declaration.*;
033    import com.sun.mirror.util.DeclarationVisitor;
034    import com.sun.tools.apt.mirror.AptEnv;
035    import com.sun.tools.javac.code.Symbol;
036    import com.sun.tools.javac.code.Symbol.ClassSymbol;
037    import com.sun.tools.javac.code.Type;
038    
039    
040    /**
041     * Implementation of MemberDeclaration
042     */
043    
044    public abstract class MemberDeclarationImpl extends DeclarationImpl
045                                                implements MemberDeclaration {
046    
047        protected MemberDeclarationImpl(AptEnv env, Symbol sym) {
048            super(env, sym);
049        }
050    
051    
052        /**
053         * {@inheritDoc}
054         */
055        public TypeDeclaration getDeclaringType() {
056            ClassSymbol c = getDeclaringClassSymbol();
057            return (c == null)
058                ? null
059                : env.declMaker.getTypeDeclaration(c);
060        }
061    
062        /**
063         * {@inheritDoc}
064         * For methods, constructors, and types.
065         */
066        public Collection<TypeParameterDeclaration> getFormalTypeParameters() {
067            ArrayList<TypeParameterDeclaration> res =
068                new ArrayList<TypeParameterDeclaration>();
069            for (Type t : sym.type.getTypeArguments()) {
070                res.add(env.declMaker.getTypeParameterDeclaration(t.tsym));
071            }
072            return res;
073        }
074    
075        /**
076         * {@inheritDoc}
077         */
078        public void accept(DeclarationVisitor v) {
079            v.visitMemberDeclaration(this);
080        }
081    
082    
083        /**
084         * Returns the ClassSymbol of the declaring type,
085         * or null if this is a top-level type.
086         */
087        private ClassSymbol getDeclaringClassSymbol() {
088            return sym.owner.enclClass();
089        }
090    
091        /**
092         * Returns the formal type parameters of a type, member or constructor
093         * as an angle-bracketed string.  Each parameter consists of the simple
094         * type variable name and any bounds (with no implicit "extends Object"
095         * clause added).  Type names are qualified.
096         * Returns "" if there are no type parameters.
097         */
098        protected static String typeParamsToString(AptEnv env, Symbol sym) {
099            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    }