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.lang.annotation.Annotation;
  30 import java.lang.annotation.Inherited;
  31 import java.util.ArrayList;
  32 import java.util.Collection;
  33 
  34 import com.sun.mirror.declaration.*;
  35 import com.sun.mirror.type.ClassType;
  36 import com.sun.mirror.util.DeclarationVisitor;
  37 import com.sun.tools.apt.mirror.AptEnv;
  38 import com.sun.tools.javac.code.*;
  39 import com.sun.tools.javac.code.Symbol.*;
  40 
  41 
  42 /**
  43  * Implementation of ClassDeclaration
  44  */
  45 @SuppressWarnings("deprecation")
  46 public class ClassDeclarationImpl extends TypeDeclarationImpl
  47                                   implements ClassDeclaration {
  48 
  49     ClassDeclarationImpl(AptEnv env, ClassSymbol sym) {
  50         super(env, sym);
  51     }
  52 
  53 
  54     /**
  55      * {@inheritDoc}
  56      * Overridden here to handle @Inherited.
  57      */
  58     public <A extends Annotation> A getAnnotation(Class<A> annoType) {
  59 
  60         boolean inherited = annoType.isAnnotationPresent(Inherited.class);
  61         for (Type t = sym.type;
  62              t.tsym != env.symtab.objectType.tsym && !t.isErroneous();
  63              t = env.jctypes.supertype(t)) {
  64 
  65             A result = getAnnotation(annoType, t.tsym);
  66             if (result != null || !inherited) {
  67                 return result;
  68             }
  69         }
  70         return null;
  71     }
  72 
  73     /**
  74      * {@inheritDoc}
  75      */
  76     public ClassType getSuperclass() {
  77         //  java.lang.Object has no superclass
  78         if (sym == env.symtab.objectType.tsym) {
  79             return null;
  80         }
  81         Type t = env.jctypes.supertype(sym.type);
  82         return (ClassType) env.typeMaker.getType(t);
  83     }
  84 
  85     /**
  86      * {@inheritDoc}
  87      */
  88     public Collection<ConstructorDeclaration> getConstructors() {
  89         ArrayList<ConstructorDeclaration> res =
  90             new ArrayList<ConstructorDeclaration>();
  91         for (Symbol s : getMembers(true)) {
  92             if (s.isConstructor()) {
  93                 MethodSymbol m = (MethodSymbol) s;
  94                 res.add((ConstructorDeclaration)
  95                         env.declMaker.getExecutableDeclaration(m));
  96             }
  97         }
  98         return res;
  99     }
 100 
 101     /**
 102      * {@inheritDoc}
 103      */
 104     public Collection<MethodDeclaration> getMethods() {
 105         return identityFilter.filter(super.getMethods(),
 106                                      MethodDeclaration.class);
 107     }
 108 
 109     /**
 110      * {@inheritDoc}
 111      */
 112     public void accept(DeclarationVisitor v) {
 113         v.visitClassDeclaration(this);
 114     }
 115 }