1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/apt/mirror/declaration/ClassDecl.java Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,250 @@ 1.4 +/* 1.5 + * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + */ 1.26 + 1.27 + 1.28 +/* 1.29 + * @test 1.30 + * @bug 4853450 4997614 1.31 + * @summary ClassDeclaration tests 1.32 + * @library ../../lib 1.33 + * @compile -source 1.5 ClassDecl.java 1.34 + * @run main ClassDecl 1.35 + */ 1.36 + 1.37 + 1.38 +import java.io.Serializable; 1.39 +import java.util.*; 1.40 +import com.sun.mirror.declaration.*; 1.41 +import com.sun.mirror.type.*; 1.42 +import com.sun.mirror.util.*; 1.43 + 1.44 + 1.45 +/** 1.46 + * Sed Quis custodiet ipsos custodes? 1.47 + */ 1.48 +@AT1 1.49 +@AT2 1.50 +public class ClassDecl extends Tester { 1.51 + 1.52 + public static void main(String[] args) { 1.53 + (new ClassDecl()).run(); 1.54 + } 1.55 + 1.56 + 1.57 + private ClassDeclaration nested = null; // a nested type 1.58 + private ClassDeclaration object = null; // java.lang.object 1.59 + 1.60 + // A constructor to be found 1.61 + private ClassDecl() { 1.62 + } 1.63 + 1.64 + // Another constructor to be found 1.65 + private ClassDecl(int i) { 1.66 + this(); 1.67 + } 1.68 + 1.69 + // An extra field to be found 1.70 + static int i; 1.71 + 1.72 + // Static initializer isn't among this class's methods. 1.73 + static { 1.74 + i = 7; 1.75 + } 1.76 + 1.77 + // A nested class with some accoutrements 1.78 + private static strictfp class NestedClass<T> implements Serializable { 1.79 + void m1() {} 1.80 + void m2() {} 1.81 + void m2(int i) {} 1.82 + } 1.83 + 1.84 + protected void init() { 1.85 + nested = (ClassDeclaration) 1.86 + thisClassDecl.getNestedTypes().iterator().next(); 1.87 + object = (ClassDeclaration) 1.88 + env.getTypeDeclaration("java.lang.Object"); 1.89 + } 1.90 + 1.91 + 1.92 + // Declaration methods 1.93 + 1.94 + @Test(result="class") 1.95 + Collection<String> accept() { 1.96 + final Collection<String> res = new ArrayList<String>(); 1.97 + 1.98 + thisClassDecl.accept(new SimpleDeclarationVisitor() { 1.99 + public void visitTypeDeclaration(TypeDeclaration t) { 1.100 + res.add("type"); 1.101 + } 1.102 + public void visitClassDeclaration(ClassDeclaration c) { 1.103 + res.add("class"); 1.104 + } 1.105 + public void visitEnumDeclaration(EnumDeclaration e) { 1.106 + res.add("enum"); 1.107 + } 1.108 + }); 1.109 + return res; 1.110 + } 1.111 + 1.112 + @Test(result={"@AT1", "@AT2"}) 1.113 + Collection<AnnotationMirror> getAnnotationMirrors() { 1.114 + return thisClassDecl.getAnnotationMirrors(); 1.115 + } 1.116 + 1.117 + @Test(result=" Sed Quis custodiet ipsos custodes?\n") 1.118 + String getDocComment() { 1.119 + return thisClassDecl.getDocComment(); 1.120 + } 1.121 + 1.122 + @Test(result={"public"}) 1.123 + Collection<Modifier> getModifiers1() { 1.124 + return thisClassDecl.getModifiers(); 1.125 + } 1.126 + 1.127 + // Check that static nested class has "static" modifier, even though 1.128 + // the VM doesn't set that bit. 1.129 + @Test(result={"private", "static", "strictfp"}) 1.130 + Collection<Modifier> getModifiers2() { 1.131 + return nested.getModifiers(); 1.132 + } 1.133 + 1.134 + @Test(result="ClassDecl.java") 1.135 + String getPosition() { 1.136 + return thisClassDecl.getPosition().file().getName(); 1.137 + } 1.138 + 1.139 + @Test(result="ClassDecl") 1.140 + String getSimpleName1() { 1.141 + return thisClassDecl.getSimpleName(); 1.142 + } 1.143 + 1.144 + @Test(result="NestedClass") 1.145 + String getSimpleName2() { 1.146 + return nested.getSimpleName(); 1.147 + } 1.148 + 1.149 + 1.150 + // MemberDeclaration method 1.151 + 1.152 + @Test(result="null") 1.153 + TypeDeclaration getDeclaringType1() { 1.154 + return thisClassDecl.getDeclaringType(); 1.155 + } 1.156 + 1.157 + @Test(result="ClassDecl") 1.158 + TypeDeclaration getDeclaringType2() { 1.159 + return nested.getDeclaringType(); 1.160 + } 1.161 + 1.162 + 1.163 + // TypeDeclaration methods 1.164 + 1.165 + @Test(result={"nested", "object", "i"}) 1.166 + Collection<FieldDeclaration> getFields() { 1.167 + return thisClassDecl.getFields(); 1.168 + } 1.169 + 1.170 + @Test(result={}) 1.171 + Collection<TypeParameterDeclaration> getFormalTypeParameters1() { 1.172 + return thisClassDecl.getFormalTypeParameters(); 1.173 + } 1.174 + 1.175 + @Test(result="T") 1.176 + Collection<TypeParameterDeclaration> getFormalTypeParameters2() { 1.177 + return nested.getFormalTypeParameters(); 1.178 + } 1.179 + 1.180 + @Test(result="ClassDecl.NestedClass<T>") 1.181 + Collection<TypeDeclaration> getNestedTypes() { 1.182 + return thisClassDecl.getNestedTypes(); 1.183 + } 1.184 + 1.185 + @Test(result="") 1.186 + PackageDeclaration getPackage1() { 1.187 + return thisClassDecl.getPackage(); 1.188 + } 1.189 + 1.190 + @Test(result="java.lang") 1.191 + PackageDeclaration getPackage2() { 1.192 + return object.getPackage(); 1.193 + } 1.194 + 1.195 + @Test(result="ClassDecl") 1.196 + String getQualifiedName1() { 1.197 + return thisClassDecl.getQualifiedName(); 1.198 + } 1.199 + 1.200 + @Test(result="ClassDecl.NestedClass") 1.201 + String getQualifiedName2() { 1.202 + return nested.getQualifiedName(); 1.203 + } 1.204 + 1.205 + @Test(result="java.lang.Object") 1.206 + String getQualifiedName3() { 1.207 + return object.getQualifiedName(); 1.208 + } 1.209 + 1.210 + @Test(result="java.io.Serializable") 1.211 + Collection<InterfaceType> getSuperinterfaces() { 1.212 + return nested.getSuperinterfaces(); 1.213 + } 1.214 + 1.215 + 1.216 + // ClassDeclaration methods 1.217 + 1.218 + @Test(result={"ClassDecl()", "ClassDecl(int)"}) 1.219 + Collection<ConstructorDeclaration> getConstructors1() { 1.220 + return thisClassDecl.getConstructors(); 1.221 + } 1.222 + 1.223 + // Check for default constructor. 1.224 + // 4997614: visitConstructionDeclaration reports info when there is no ctor 1.225 + @Test(result={"NestedClass()"}) 1.226 + Collection<ConstructorDeclaration> getConstructors2() { 1.227 + return nested.getConstructors(); 1.228 + } 1.229 + 1.230 + @Test(result={"m1()", "m2()", "m2(int)"}) 1.231 + Collection<MethodDeclaration> getMethods() { 1.232 + return nested.getMethods(); 1.233 + } 1.234 + 1.235 + @Test(result={"Tester"}) 1.236 + ClassType getSuperclass() { 1.237 + return thisClassDecl.getSuperclass(); 1.238 + } 1.239 + 1.240 + @Test(result={"null"}) 1.241 + ClassType objectHasNoSuperclass() { 1.242 + return object.getSuperclass(); 1.243 + } 1.244 +} 1.245 + 1.246 + 1.247 +// Annotations used for testing. 1.248 + 1.249 +@interface AT1 { 1.250 +} 1.251 + 1.252 +@interface AT2 { 1.253 +}