Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Class/getEnclosingClass/EnclosingClassTest.java
38828 views
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.
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
* @test
26
* @bug 4992173 4992170
27
*
28
* @run shell make_src.sh
29
* @run shell build.sh
30
* @run main/othervm -esa -ea EnclosingClassTest
31
*
32
* @summary Check getEnclosingClass and other methods
33
* @author Peter von der Ah\u00e9
34
*/
35
36
/*
37
* We have five kinds of classes:
38
* a) Top level classes
39
* b) Nested classes (static member classes)
40
* c) Inner classes (non-static member classes)
41
* d) Local classes (named classes declared within a method)
42
* e) Anonymous classes
43
*
44
* Each one can be within a package or not.
45
* Kinds b-e can/must be within kinds a-e.
46
* This gives us a three dimensional space:
47
* 1. dimension: b-e
48
* 2. dimension: a-e
49
* 3. dimension: packages
50
*
51
* We make a two dimensional matrix of (b-e)x(a-e) and change the
52
* package configuration on that:
53
*
54
* b c d e
55
* a x x x x
56
* b x x x x
57
* c o x x x where o means "not legal"
58
* d o x x x
59
* e o x x x
60
*/
61
62
import java.util.List;
63
import java.util.LinkedList;
64
import java.lang.reflect.Field;
65
import common.TestMe;
66
67
public class EnclosingClassTest {
68
static void info(Class<?> c, Class<?> encClass, String desc) {
69
if (!"".equals(desc))
70
System.out.println(desc + ":");
71
System.out.println(c);
72
System.out.println("\tis enclosed by:\t\t" + encClass);
73
System.out.println("\thas simple name:\t`" +
74
c.getSimpleName() + "'");
75
System.out.println("\thas canonical name:\t`" +
76
c.getCanonicalName() + "'");
77
}
78
79
static void match(String actual, String expected) {
80
assert((actual == null && expected == null) || actual.equals(expected));
81
System.out.println("\t`" +
82
actual + "' matches expected `" +
83
expected + "'");
84
}
85
86
static void check(Class<?> c, Class<?> enc,
87
String encName, String encNameExpected,
88
String simpleName, String simpleNameExpected,
89
String canonicalName, String canonicalNameExpected) {
90
match(encName, encNameExpected);
91
match(simpleName, simpleNameExpected);
92
match(canonicalName, canonicalNameExpected);
93
}
94
95
static void testClass(Class<?> c, TestMe annotation, Field f) {
96
if (Void.class.equals(c))
97
return;
98
Class<?> encClass = c.getEnclosingClass();
99
c.getEnclosingMethod(); // make sure it does not crash
100
c.getEnclosingConstructor(); // make sure it does not crash
101
info(c, encClass, annotation.desc());
102
check(c, encClass,
103
""+encClass, annotation.encl(),
104
c.getSimpleName(), annotation.simple(),
105
c.getCanonicalName(),
106
annotation.hasCanonical() ? annotation.canonical() : null);
107
if (void.class.equals(c))
108
return;
109
Class<?> array = java.lang.reflect.Array.newInstance(c, 0).getClass();
110
check(array, array.getEnclosingClass(),
111
"", "",
112
array.getSimpleName(), annotation.simple()+"[]",
113
array.getCanonicalName(),
114
annotation.hasCanonical() ? annotation.canonical()+"[]" : null);
115
}
116
117
static void test(Object tests) {
118
for (Field f : tests.getClass().getFields()) {
119
TestMe annotation = f.getAnnotation(TestMe.class);
120
if (annotation != null) {
121
try {
122
testClass((Class<?>)f.get(tests), annotation, f);
123
} catch (AssertionError ex) {
124
System.err.println("Error in " +
125
tests.getClass().getName() +
126
"." + f.getName());
127
throw ex;
128
} catch (IllegalAccessException ex) {
129
ex.printStackTrace();
130
throw new RuntimeException(ex);
131
}
132
}
133
}
134
}
135
public static void main(String[] args) {
136
test(new EnclosingClass());
137
test(new pkg1.EnclosingClass());
138
test(new pkg1.pkg2.EnclosingClass());
139
}
140
}
141
142