Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/Class/getEnclosingClass/EnclosingClassTest.java
41153 views
1
/*
2
* Copyright (c) 2004, 2021, 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
* @library /test/lib
28
* @modules jdk.compiler
29
* @run testng/othervm EnclosingClassTest
30
* @summary Check getEnclosingClass and other methods
31
* @author Peter von der Ah\u00e9
32
*/
33
34
import java.io.BufferedReader;
35
import java.io.FileReader;
36
import java.io.FileWriter;
37
import java.io.IOException;
38
import java.io.PrintWriter;
39
import java.lang.reflect.Field;
40
import java.lang.reflect.InvocationTargetException;
41
import java.nio.file.Files;
42
import java.nio.file.Path;
43
44
import common.TestMe;
45
import jdk.test.lib.compiler.CompilerUtils;
46
import jdk.test.lib.util.FileUtils;
47
import org.testng.Assert;
48
import org.testng.annotations.BeforeClass;
49
import org.testng.annotations.Test;
50
51
/*
52
* We have five kinds of classes:
53
* a) Top level classes
54
* b) Nested classes (static member classes)
55
* c) Inner classes (non-static member classes)
56
* d) Local classes (named classes declared within a method)
57
* e) Anonymous classes
58
*
59
* Each one can be within a package or not.
60
* Kinds b-e can/must be within kinds a-e.
61
* This gives us a three dimensional space:
62
* 1. dimension: b-e
63
* 2. dimension: a-e
64
* 3. dimension: packages
65
*
66
* We make a two dimensional matrix of (b-e)x(a-e) and change the
67
* package configuration on that:
68
*
69
* b c d e
70
* a x x x x
71
* b x x x x
72
* c o x x x where o means "not legal"
73
* d o x x x
74
* e o x x x
75
*/
76
77
public class EnclosingClassTest {
78
private static final String SRC_DIR = System.getProperty("test.src");
79
private static final Path ENCLOSING_CLASS_SRC = Path.of(SRC_DIR, "EnclosingClass.java");
80
private static final String GEN_SRC_DIR = "gensrc";
81
82
@BeforeClass
83
public void createEnclosingClasses() throws IOException {
84
Path pkg1Dir = Path.of(GEN_SRC_DIR, "pkg1");
85
Path pkg2Dir = Path.of(GEN_SRC_DIR, "pkg1", "pkg2");
86
Path pkg1File = pkg1Dir.resolve("EnclosingClass.java");
87
Path pkg2File = pkg2Dir.resolve("EnclosingClass.java");
88
89
if (!Files.notExists(pkg1Dir)) {
90
FileUtils.deleteFileTreeWithRetry(pkg1Dir);
91
}
92
Files.createDirectories(pkg2Dir);
93
createAndWriteEnclosingClasses(ENCLOSING_CLASS_SRC, pkg1File, "pkg1");
94
createAndWriteEnclosingClasses(ENCLOSING_CLASS_SRC, pkg2File, "pkg1.pkg2");
95
96
Assert.assertTrue(CompilerUtils.compile(ENCLOSING_CLASS_SRC, Path.of(System.getProperty("test.classes")),
97
"--source-path", SRC_DIR));
98
Assert.assertTrue(CompilerUtils.compile(pkg1File, Path.of(System.getProperty("test.classes")),
99
"-classpath", System.getProperty("test.class.path")));
100
Assert.assertTrue(CompilerUtils.compile(pkg2File, Path.of(System.getProperty("test.classes")),
101
"-classpath", System.getProperty("test.class.path")));
102
}
103
104
@Test
105
public void testEnclosingClasses() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException,
106
InvocationTargetException, InstantiationException {
107
test(Class.forName("EnclosingClass").getDeclaredConstructor().newInstance());
108
}
109
110
@Test
111
public void testEnclosingClassesInPackage() throws ClassNotFoundException, NoSuchMethodException,
112
IllegalAccessException, InvocationTargetException, InstantiationException {
113
test(Class.forName("pkg1.EnclosingClass").getDeclaredConstructor().newInstance());
114
}
115
116
@Test
117
public void testEnclosingClassesInNestedPackage() throws ClassNotFoundException, NoSuchMethodException,
118
IllegalAccessException, InvocationTargetException, InstantiationException {
119
test(Class.forName("pkg1.pkg2.EnclosingClass").getDeclaredConstructor().newInstance());
120
}
121
122
private void createAndWriteEnclosingClasses(Path source, Path target, String packageName) throws IOException {
123
String className = packageName + ".EnclosingClass";
124
try (BufferedReader br = new BufferedReader(new FileReader(source.toFile()));
125
PrintWriter bw = new PrintWriter(new FileWriter(target.toFile()))) {
126
String line;
127
while ((line = br.readLine()) != null) {
128
if (line.contains("canonical=\"EnclosingClass")) {
129
line = line.replaceAll("canonical=\"EnclosingClass", "canonical=\"" + className);
130
} else if (line.contains("\"class EnclosingClass")) {
131
line = line.replaceAll("\"class EnclosingClass", "\"class " + className);
132
} else if (line.contains("//package")) {
133
line = line.replaceAll("//package", "package " + packageName + ";");
134
}
135
bw.println(line);
136
}
137
}
138
}
139
140
private void info(Class<?> c, Class<?> encClass, String desc) {
141
if (!"".equals(desc)) {
142
System.out.println(desc + ":");
143
}
144
System.out.println(c);
145
System.out.println("\tis enclosed by:\t\t" + encClass);
146
System.out.println("\thas simple name:\t`" +
147
c.getSimpleName() + "'");
148
System.out.println("\thas canonical name:\t`" +
149
c.getCanonicalName() + "'");
150
}
151
152
private void match(String actual, String expected) {
153
Assert.assertTrue((actual == null && expected == null) || actual.equals(expected));
154
System.out.println("\t`" +
155
actual + "' matches expected `" +
156
expected + "'");
157
}
158
159
private void check(Class<?> c, Class<?> enc,
160
String encName, String encNameExpected,
161
String simpleName, String simpleNameExpected,
162
String canonicalName, String canonicalNameExpected) {
163
match(encName, encNameExpected);
164
match(simpleName, simpleNameExpected);
165
match(canonicalName, canonicalNameExpected);
166
}
167
168
private void testClass(Class<?> c, TestMe annotation, Field f) {
169
if (Void.class.equals(c))
170
return;
171
Class<?> encClass = c.getEnclosingClass();
172
c.getEnclosingMethod(); // make sure it does not crash
173
c.getEnclosingConstructor(); // make sure it does not crash
174
info(c, encClass, annotation.desc());
175
check(c, encClass,
176
""+encClass, annotation.encl(),
177
c.getSimpleName(), annotation.simple(),
178
c.getCanonicalName(),
179
annotation.hasCanonical() ? annotation.canonical() : null);
180
if (void.class.equals(c))
181
return;
182
Class<?> array = java.lang.reflect.Array.newInstance(c, 0).getClass();
183
check(array, array.getEnclosingClass(),
184
"", "",
185
array.getSimpleName(), annotation.simple()+"[]",
186
array.getCanonicalName(),
187
annotation.hasCanonical() ? annotation.canonical()+"[]" : null);
188
}
189
190
private void test(Object tests) {
191
for (Field f : tests.getClass().getFields()) {
192
TestMe annotation = f.getAnnotation(TestMe.class);
193
if (annotation != null) {
194
try {
195
testClass((Class<?>)f.get(tests), annotation, f);
196
} catch (AssertionError ex) {
197
System.err.println("Error in " +
198
tests.getClass().getName() +
199
"." + f.getName());
200
throw ex;
201
} catch (IllegalAccessException ex) {
202
ex.printStackTrace();
203
throw new RuntimeException(ex);
204
}
205
}
206
}
207
}
208
}
209
210
211