|
1 /* |
|
2 * Copyright 2004-2007 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. |
|
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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 |
|
25 /* |
|
26 * @test |
|
27 * @bug 4853450 4993299 |
|
28 * @summary ConstructorDeclaration tests |
|
29 * @library ../../lib |
|
30 * @compile -source 1.5 ConstructorDecl.java |
|
31 * @run main ConstructorDecl |
|
32 */ |
|
33 |
|
34 |
|
35 import java.util.*; |
|
36 import com.sun.mirror.declaration.*; |
|
37 import com.sun.mirror.type.*; |
|
38 import com.sun.mirror.util.*; |
|
39 |
|
40 |
|
41 public class ConstructorDecl extends Tester { |
|
42 |
|
43 /** |
|
44 * Sed Quis custodiet ipsos custodes? |
|
45 */ |
|
46 @AT1 |
|
47 public ConstructorDecl() { |
|
48 } |
|
49 |
|
50 |
|
51 public static void main(String[] args) { |
|
52 (new ConstructorDecl()).run(); |
|
53 } |
|
54 |
|
55 |
|
56 private ConstructorDeclaration ctor = null; // a constructor |
|
57 private ConstructorDeclaration ctorDef = null; // a default c'tor |
|
58 private ConstructorDeclaration ctorInner = null; // an inner class c'tor |
|
59 |
|
60 protected void init() { |
|
61 ctor = getAConstructor(thisClassDecl); |
|
62 ctorDef = getAConstructor((ClassDeclaration) |
|
63 env.getTypeDeclaration("C1")); |
|
64 ctorInner = getAConstructor((ClassDeclaration) |
|
65 env.getTypeDeclaration("C1.C2")); |
|
66 } |
|
67 |
|
68 // Return a constructor of a class. |
|
69 private ConstructorDeclaration getAConstructor(ClassDeclaration c) { |
|
70 return c.getConstructors().iterator().next(); |
|
71 } |
|
72 |
|
73 |
|
74 // Declaration methods |
|
75 |
|
76 @Test(result="constructor") |
|
77 Collection<String> accept() { |
|
78 final Collection<String> res = new ArrayList<String>(); |
|
79 |
|
80 ctor.accept(new SimpleDeclarationVisitor() { |
|
81 public void visitTypeDeclaration(TypeDeclaration t) { |
|
82 res.add("type"); |
|
83 } |
|
84 public void visitExecutableDeclaration(ExecutableDeclaration e) { |
|
85 res.add("executable"); |
|
86 } |
|
87 public void visitConstructorDeclaration(ConstructorDeclaration c) { |
|
88 res.add("constructor"); |
|
89 } |
|
90 }); |
|
91 return res; |
|
92 } |
|
93 |
|
94 @Test(result={"@AT1"}) |
|
95 Collection<AnnotationMirror> getAnnotationMirrors() { |
|
96 return ctor.getAnnotationMirrors(); |
|
97 } |
|
98 |
|
99 @Test(result=" Sed Quis custodiet ipsos custodes?\n") |
|
100 String getDocComment() { |
|
101 return ctor.getDocComment(); |
|
102 } |
|
103 |
|
104 @Test(result={"public"}) |
|
105 Collection<Modifier> getModifiers() { |
|
106 return ctor.getModifiers(); |
|
107 } |
|
108 |
|
109 @Test(result="ConstructorDecl.java") |
|
110 String getPosition() { |
|
111 return ctor.getPosition().file().getName(); |
|
112 } |
|
113 |
|
114 @Test(result="ConstructorDecl.java") |
|
115 String getPositionDefault() { |
|
116 return ctorDef.getPosition().file().getName(); |
|
117 } |
|
118 |
|
119 @Test(result="ConstructorDecl") |
|
120 String getSimpleName() { |
|
121 return ctor.getSimpleName(); |
|
122 } |
|
123 |
|
124 @Test(result="C2") |
|
125 String getSimpleNameInner() { |
|
126 return ctorInner.getSimpleName(); |
|
127 } |
|
128 |
|
129 |
|
130 // MemberDeclaration method |
|
131 |
|
132 @Test(result="ConstructorDecl") |
|
133 TypeDeclaration getDeclaringType() { |
|
134 return ctor.getDeclaringType(); |
|
135 } |
|
136 |
|
137 |
|
138 // ExecutableDeclaration methods |
|
139 |
|
140 @Test(result={}) |
|
141 Collection<TypeParameterDeclaration> getFormalTypeParameters1() { |
|
142 return ctor.getFormalTypeParameters(); |
|
143 } |
|
144 |
|
145 @Test(result={"N extends java.lang.Number"}) |
|
146 Collection<TypeParameterDeclaration> getFormalTypeParameters2() { |
|
147 return ctorInner.getFormalTypeParameters(); |
|
148 } |
|
149 |
|
150 @Test(result={}) |
|
151 Collection<ParameterDeclaration> getParameters1() { |
|
152 return ctor.getParameters(); |
|
153 } |
|
154 |
|
155 // 4993299: verify synthetic parameters to inner class constructors |
|
156 // aren't visible |
|
157 @Test(result={"N n1", "N n2", "java.lang.String[] ss"}, |
|
158 ordered=true) |
|
159 Collection<ParameterDeclaration> getParameters2() { |
|
160 return ctorInner.getParameters(); |
|
161 } |
|
162 |
|
163 @Test(result={"java.lang.Throwable"}) |
|
164 Collection<ReferenceType> getThrownTypes() { |
|
165 return ctorInner.getThrownTypes(); |
|
166 } |
|
167 |
|
168 @Test(result="false") |
|
169 Boolean isVarArgs1() { |
|
170 return ctor.isVarArgs(); |
|
171 } |
|
172 |
|
173 @Test(result="true") |
|
174 Boolean isVarArgs2() { |
|
175 return ctorInner.isVarArgs(); |
|
176 } |
|
177 |
|
178 |
|
179 // toString |
|
180 |
|
181 @Test(result="<N extends java.lang.Number> C2(N, N, String...)") |
|
182 @Ignore("This is what it would be nice to see.") |
|
183 String toStringTest() { |
|
184 return ctorInner.toString(); |
|
185 } |
|
186 } |
|
187 |
|
188 |
|
189 // Classes and interfaces used for testing. |
|
190 |
|
191 class C1 { |
|
192 class C2 { |
|
193 <N extends Number> C2(N n1, N n2, String... ss) throws Throwable { |
|
194 } |
|
195 } |
|
196 } |
|
197 |
|
198 @interface AT1 { |
|
199 } |