1 /*
   2  * Copyright 2004-2006 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.ArrayList;
  30 import java.util.Collection;
  31 
  32 import com.sun.mirror.declaration.*;
  33 import com.sun.mirror.util.*;
  34 import com.sun.tools.apt.mirror.AptEnv;
  35 import com.sun.tools.javac.code.*;
  36 import com.sun.tools.javac.code.Symbol.*;
  37 
  38 
  39 /**
  40  * Implementation of PackageDeclaration.
  41  */
  42 @SuppressWarnings("deprecation")
  43 public class PackageDeclarationImpl extends DeclarationImpl
  44                                     implements PackageDeclaration {
  45 
  46     private PackageSymbol sym;
  47 
  48 
  49     public PackageDeclarationImpl(AptEnv env, PackageSymbol sym) {
  50         super(env, sym);
  51         this.sym = sym;
  52     }
  53 
  54 
  55     /**
  56      * Returns the qualified name.
  57      */
  58     public String toString() {
  59         return getQualifiedName();
  60     }
  61 
  62     /**
  63      * {@inheritDoc}
  64      */
  65     public String getQualifiedName() {
  66         return sym.getQualifiedName().toString();
  67     }
  68 
  69     /**
  70      * {@inheritDoc}
  71      */
  72     public Collection<ClassDeclaration> getClasses() {
  73         return identityFilter.filter(getAllTypes(),
  74                                      ClassDeclaration.class);
  75     }
  76 
  77     /**
  78      * {@inheritDoc}
  79      */
  80     public Collection<EnumDeclaration> getEnums() {
  81         return identityFilter.filter(getAllTypes(),
  82                                      EnumDeclaration.class);
  83     }
  84 
  85     /**
  86      * {@inheritDoc}
  87      */
  88     public Collection<InterfaceDeclaration> getInterfaces() {
  89         return identityFilter.filter(getAllTypes(),
  90                                      InterfaceDeclaration.class);
  91     }
  92 
  93     /**
  94      * {@inheritDoc}
  95      */
  96     public Collection<AnnotationTypeDeclaration> getAnnotationTypes() {
  97         return identityFilter.filter(getAllTypes(),
  98                                      AnnotationTypeDeclaration.class);
  99     }
 100 
 101     /**
 102      * {@inheritDoc}
 103      */
 104     public void accept(DeclarationVisitor v) {
 105         v.visitPackageDeclaration(this);
 106     }
 107 
 108 
 109     // Cache of all top-level type declarations in this package.
 110     private Collection<TypeDeclaration> allTypes = null;
 111 
 112     /**
 113      * Caches and returns all top-level type declarations in this package.
 114      * Omits synthetic types.
 115      */
 116     private Collection<TypeDeclaration> getAllTypes() {
 117         if (allTypes != null) {
 118             return allTypes;
 119         }
 120         allTypes = new ArrayList<TypeDeclaration>();
 121         for (Symbol s : getMembers(false)) {
 122             allTypes.add(env.declMaker.getTypeDeclaration((ClassSymbol) s));
 123         }
 124         return allTypes;
 125     }
 126 }