blob: fe963fb05e0e7531d9361bb389a38fb95c3e904f [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2004 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 * @test
26 * @bug 4962341
27 * @summary Check getEnclosingMethod method
28 * @author Joseph D. Darcy
29 */
30
31import java.lang.reflect.Constructor;
32import java.lang.annotation.*;
33
34public class EnclosingConstructorTests {
35 static Class<?> anonymousClass;
36 static Class<?> localClass;
37 static Class<?> anotherLocalClass;
38
39 static {
40 Cloneable c = new Cloneable() {}; // anonymous cloneable
41 anonymousClass = c.getClass();
42 }
43
44 @ConstructorDescriptor("EnclosingConstructorTests()")
45 EnclosingConstructorTests() {
46 class Local {};
47 Local l = new Local();
48 localClass = l.getClass();
49 }
50
51
52 @ConstructorDescriptor("private EnclosingConstructorTests(int)")
53 private EnclosingConstructorTests(int i) {
54 class Local {};
55 Local l = new Local();
56 anotherLocalClass = l.getClass();
57 }
58
59
60 static int examine(Class enclosedClass, String constructorSig) {
61 Constructor c = enclosedClass.getEnclosingConstructor();
62 if (c == null && constructorSig == null)
63 return 0;
64
65 if (c != null &&
66 c.getAnnotation(ConstructorDescriptor.class).value().equals(constructorSig))
67 return 0; // everything is okay
68 else {
69 System.err.println("\nUnexpected constructor value; expected:\t" + constructorSig +
70 "\ngot:\t" + c);
71 return 1;
72 }
73 }
74
75
76 public static void main(String argv[]) {
77 int failures = 0;
78 class StaticLocal {};
79 EnclosingConstructorTests ect = new EnclosingConstructorTests();
80 ect = new EnclosingConstructorTests(5);
81
82 failures += examine(StaticLocal.class,
83 null);
84
85 failures += examine(localClass,
86 "EnclosingConstructorTests()");
87
88 failures += examine(anotherLocalClass,
89 "private EnclosingConstructorTests(int)");
90
91 failures += examine(anonymousClass,
92 null);
93
94 if (failures > 0)
95 throw new RuntimeException("Test failed.");
96 }
97}
98
99@Retention(RetentionPolicy.RUNTIME)
100@interface ConstructorDescriptor {
101 String value();
102}