1 /*
   2  * Copyright (c) 2004, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * 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.*;
  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 import com.sun.tools.javac.util.Name;
  39 
  40 /**
  41  * Implementation of TypeDeclaration
  42  */
  43 @SuppressWarnings("deprecation")
  44 public class TypeDeclarationImpl extends MemberDeclarationImpl
  45                                  implements TypeDeclaration {
  46 
  47     public ClassSymbol sym;
  48 
  49 
  50     /**
  51      * "sym" should be completed before this constructor is called.
  52      */
  53      protected TypeDeclarationImpl(AptEnv env, ClassSymbol sym) {
  54         super(env, sym);
  55         this.sym = sym;
  56     }
  57 
  58 
  59     /**
  60      * Returns the type's name, with any type parameters (including those
  61      * of outer classes).  Type names are qualified.
  62      */
  63     public String toString() {
  64         return toString(env, sym);
  65     }
  66 
  67     /**
  68      * {@inheritDoc}
  69      */
  70     public PackageDeclaration getPackage() {
  71         return env.declMaker.getPackageDeclaration(sym.packge());
  72     }
  73 
  74     /**
  75      * {@inheritDoc}
  76      */
  77     public String getQualifiedName() {
  78         return sym.toString();
  79     }
  80 
  81     /**
  82      * {@inheritDoc}
  83      */
  84     public Collection<InterfaceType> getSuperinterfaces() {
  85         return env.typeMaker.getTypes(env.jctypes.interfaces(sym.type),
  86                                       InterfaceType.class);
  87     }
  88 
  89     /**
  90      * {@inheritDoc}
  91      */
  92     public Collection<FieldDeclaration> getFields() {
  93         ArrayList<FieldDeclaration> res = new ArrayList<FieldDeclaration>();
  94         for (Symbol s : getMembers(true)) {
  95             if (s.kind == Kinds.VAR) {
  96                 res.add(env.declMaker.getFieldDeclaration((VarSymbol) s));
  97             }
  98         }
  99         return res;
 100     }
 101 
 102     /**
 103      * {@inheritDoc}
 104      */
 105     public Collection<? extends MethodDeclaration> getMethods() {
 106         ArrayList<MethodDeclaration> res = new ArrayList<MethodDeclaration>();
 107         for (Symbol s : getMembers(true)) {
 108             if (s.kind == Kinds.MTH && !s.isConstructor() &&
 109                 !env.names.clinit.equals(s.name) ) { // screen out static initializers
 110                 MethodSymbol m = (MethodSymbol) s;
 111                 res.add((MethodDeclaration)
 112                         env.declMaker.getExecutableDeclaration(m));
 113             }
 114         }
 115         return res;
 116     }
 117 
 118     /**
 119      * {@inheritDoc}
 120      */
 121     public Collection<TypeDeclaration> getNestedTypes() {
 122         ArrayList<TypeDeclaration> res = new ArrayList<TypeDeclaration>();
 123         for (Symbol s : getMembers(true)) {
 124             if (s.kind == Kinds.TYP) {
 125                 res.add(env.declMaker.getTypeDeclaration((ClassSymbol) s));
 126             }
 127         }
 128         return res;
 129     }
 130 
 131     /**
 132      * {@inheritDoc}
 133      */
 134     public void accept(DeclarationVisitor v) {
 135         v.visitTypeDeclaration(this);
 136     }
 137 
 138 
 139     /**
 140      * Returns a type's name, with any type parameters (including those
 141      * of outer classes).  Type names are qualified.
 142      */
 143     static String toString(AptEnv env, ClassSymbol c) {
 144         StringBuilder sb = new StringBuilder();
 145         if (c.isInner()) {
 146             // c is an inner class, so include type params of outer.
 147             ClassSymbol enclosing = c.owner.enclClass();
 148             sb.append(toString(env, enclosing))
 149               .append('.')
 150               .append(c.name);
 151         } else {
 152             sb.append(c);
 153         }
 154         sb.append(typeParamsToString(env, c));
 155         return sb.toString();
 156     }
 157 }