1 /*
   2  * Copyright (c) 2004, 2008, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 /*
  26  * @test
  27  * @bug 4853450 4997614
  28  * @summary ClassDeclaration tests
  29  * @library ../../lib
  30  * @compile -source 1.5 ClassDecl.java
  31  * @run main/othervm ClassDecl
  32  */
  33 
  34 
  35 import java.io.Serializable;
  36 import java.util.*;
  37 import com.sun.mirror.declaration.*;
  38 import com.sun.mirror.type.*;
  39 import com.sun.mirror.util.*;
  40 
  41 
  42 /**
  43  * Sed Quis custodiet ipsos custodes?
  44  */
  45 @AT1
  46 @AT2
  47 public class ClassDecl extends Tester {
  48 
  49     public static void main(String[] args) {
  50         (new ClassDecl()).run();
  51     }
  52 
  53 
  54     private ClassDeclaration nested = null;     // a nested type
  55     private ClassDeclaration object = null;     // java.lang.object
  56 
  57     // A constructor to be found
  58     private ClassDecl() {
  59     }
  60 
  61     // Another constructor to be found
  62     private ClassDecl(int i) {
  63         this();
  64     }
  65 
  66     // An extra field to be found
  67     static int i;
  68 
  69     // Static initializer isn't among this class's methods.
  70     static {
  71         i = 7;
  72     }
  73 
  74     // A nested class with some accoutrements
  75     private static strictfp class NestedClass<T> implements Serializable {
  76         void m1() {}
  77         void m2() {}
  78         void m2(int i) {}
  79     }
  80 
  81     protected void init() {
  82         nested = (ClassDeclaration)
  83             thisClassDecl.getNestedTypes().iterator().next();
  84         object = (ClassDeclaration)
  85             env.getTypeDeclaration("java.lang.Object");
  86     }
  87 
  88 
  89     // Declaration methods
  90 
  91     @Test(result="class")
  92     Collection<String> accept() {
  93         final Collection<String> res = new ArrayList<String>();
  94 
  95         thisClassDecl.accept(new SimpleDeclarationVisitor() {
  96             public void visitTypeDeclaration(TypeDeclaration t) {
  97                 res.add("type");
  98             }
  99             public void visitClassDeclaration(ClassDeclaration c) {
 100                 res.add("class");
 101             }
 102             public void visitEnumDeclaration(EnumDeclaration e) {
 103                 res.add("enum");
 104             }
 105         });
 106         return res;
 107     }
 108 
 109     @Test(result={"@AT1", "@AT2"})
 110     Collection<AnnotationMirror> getAnnotationMirrors() {
 111         return thisClassDecl.getAnnotationMirrors();
 112     }
 113 
 114     @Test(result=" Sed Quis custodiet ipsos custodes?\n")
 115     String getDocComment() {
 116         return thisClassDecl.getDocComment();
 117     }
 118 
 119     @Test(result={"public"})
 120     Collection<Modifier> getModifiers1() {
 121         return thisClassDecl.getModifiers();
 122     }
 123 
 124     // Check that static nested class has "static" modifier, even though
 125     // the VM doesn't set that bit.
 126     @Test(result={"private", "static", "strictfp"})
 127     Collection<Modifier> getModifiers2() {
 128         return nested.getModifiers();
 129     }
 130 
 131     @Test(result="ClassDecl.java")
 132     String getPosition() {
 133         return thisClassDecl.getPosition().file().getName();
 134     }
 135 
 136     @Test(result="ClassDecl")
 137     String getSimpleName1() {
 138         return thisClassDecl.getSimpleName();
 139     }
 140 
 141     @Test(result="NestedClass")
 142     String getSimpleName2() {
 143         return nested.getSimpleName();
 144     }
 145 
 146 
 147     // MemberDeclaration method
 148 
 149     @Test(result="null")
 150     TypeDeclaration getDeclaringType1() {
 151         return thisClassDecl.getDeclaringType();
 152     }
 153 
 154     @Test(result="ClassDecl")
 155     TypeDeclaration getDeclaringType2() {
 156         return nested.getDeclaringType();
 157     }
 158 
 159 
 160     // TypeDeclaration methods
 161 
 162     @Test(result={"nested", "object", "i"})
 163     Collection<FieldDeclaration> getFields() {
 164         return thisClassDecl.getFields();
 165     }
 166 
 167     @Test(result={})
 168     Collection<TypeParameterDeclaration> getFormalTypeParameters1() {
 169         return thisClassDecl.getFormalTypeParameters();
 170     }
 171 
 172     @Test(result="T")
 173     Collection<TypeParameterDeclaration> getFormalTypeParameters2() {
 174         return nested.getFormalTypeParameters();
 175     }
 176 
 177     @Test(result="ClassDecl.NestedClass<T>")
 178     Collection<TypeDeclaration> getNestedTypes() {
 179         return thisClassDecl.getNestedTypes();
 180     }
 181 
 182     @Test(result="")
 183     PackageDeclaration getPackage1() {
 184         return thisClassDecl.getPackage();
 185     }
 186 
 187     @Test(result="java.lang")
 188     PackageDeclaration getPackage2() {
 189         return object.getPackage();
 190     }
 191 
 192     @Test(result="ClassDecl")
 193     String getQualifiedName1() {
 194         return thisClassDecl.getQualifiedName();
 195     }
 196 
 197     @Test(result="ClassDecl.NestedClass")
 198     String getQualifiedName2() {
 199         return nested.getQualifiedName();
 200     }
 201 
 202     @Test(result="java.lang.Object")
 203     String getQualifiedName3() {
 204         return object.getQualifiedName();
 205     }
 206 
 207     @Test(result="java.io.Serializable")
 208     Collection<InterfaceType> getSuperinterfaces() {
 209         return nested.getSuperinterfaces();
 210     }
 211 
 212 
 213     // ClassDeclaration methods
 214 
 215     @Test(result={"ClassDecl()", "ClassDecl(int)"})
 216     Collection<ConstructorDeclaration> getConstructors1() {
 217         return thisClassDecl.getConstructors();
 218     }
 219 
 220     // Check for default constructor.
 221     // 4997614: visitConstructionDeclaration reports info when there is no ctor
 222     @Test(result={"NestedClass()"})
 223     Collection<ConstructorDeclaration> getConstructors2() {
 224         return nested.getConstructors();
 225     }
 226 
 227     @Test(result={"m1()", "m2()", "m2(int)"})
 228     Collection<MethodDeclaration> getMethods() {
 229         return nested.getMethods();
 230     }
 231 
 232     @Test(result={"Tester"})
 233     ClassType getSuperclass() {
 234         return thisClassDecl.getSuperclass();
 235     }
 236 
 237     @Test(result={"null"})
 238     ClassType objectHasNoSuperclass() {
 239         return object.getSuperclass();
 240     }
 241 }
 242 
 243 
 244 // Annotations used for testing.
 245 
 246 @interface AT1 {
 247 }
 248 
 249 @interface AT2 {
 250 }