Skip to content

Commit 4ffc8cc

Browse files
committed
8309574: Improve core reflection tests for JEP 445
Reviewed-by: mchung
1 parent e3f3ac0 commit 4ffc8cc

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (c) 2023, 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 8306112
27+
* @enablePreview
28+
* @build Unnamed TestUnnamedClass
29+
* @run main TestUnnamedClass
30+
* @summary Basic checks of java.lang.Class support for unnamed classes
31+
*/
32+
33+
import java.lang.reflect.Modifier;
34+
35+
public class TestUnnamedClass {
36+
public static void main(String... args) throws Exception {
37+
Class<?> unnamedClass = Class.forName("Unnamed");
38+
39+
/*
40+
* From JEP 445 JLS changes:
41+
*
42+
* "An unnamed class compilation unit implicitly declares a class that satisfies the following
43+
* properties:
44+
* It is always a top level class.
45+
* It is always an unnamed class (it has no canonical or fully qualified name (6.7)).
46+
* It is never abstract (8.1.1.1).
47+
* It is always final (8.1.1.2).
48+
* It is always a member of an unnamed package (7.4.2) and has package access.
49+
* Its direct superclass type is always Object (8.1.4).
50+
* It never has any direct superinterface types (8.1.5).
51+
*/
52+
53+
check(unnamedClass.isUnnamedClass(), "Unnamed class was isUnnamedClass() false");
54+
55+
check(!(unnamedClass.isInterface() ||
56+
unnamedClass.isEnum() ||
57+
unnamedClass.isRecord() ||
58+
unnamedClass.isHidden()),
59+
"Unnamed class was not a normal class");
60+
61+
check(!(unnamedClass.isLocalClass() ||
62+
unnamedClass.isMemberClass() ||
63+
unnamedClass.isPrimitive() ||
64+
unnamedClass.isArray()),
65+
"Unnamed class was not top-level");
66+
67+
check("Unnamed".equals(unnamedClass.getName()), "Unexpected Class.getName result");
68+
check("Unnamed".equals(unnamedClass.getTypeName()), "Unexpected Class.getTypeName result");
69+
check("".equals(unnamedClass.getSimpleName()), "Unexpected Class.getSimpleName result");
70+
check(unnamedClass.getCanonicalName() == null, "Unexpected non-null Class.getCanonicalName");
71+
72+
int modifiers = unnamedClass.getModifiers();
73+
check((modifiers & Modifier.ABSTRACT) == 0, "Unnamed class was abstract");
74+
check((modifiers & Modifier.FINAL) != 0, "Unnamed class was not final");
75+
check((modifiers & (Modifier.PUBLIC |
76+
Modifier.PRIVATE |
77+
Modifier.PROTECTED)) == 0, "Unnamed class was not package access");
78+
79+
check(unnamedClass.isSynthetic(), "Unnamed class was not synthetic");
80+
81+
check("".equals(unnamedClass.getPackage().getName()), "Unnamed class not in an unnamed package");
82+
83+
check(unnamedClass.getSuperclass() == Object.class, "Superclass was not java.lang.Object");
84+
85+
check(unnamedClass.getInterfaces().length == 0, "Unnamed class had super interfaces");
86+
}
87+
88+
private static void check(boolean predicate, String message) {
89+
if (!predicate) {
90+
throw new RuntimeException(message);
91+
}
92+
}
93+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2023, 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+
/**
26+
* Exemplar unnamed class.
27+
*/
28+
public void main() {
29+
System.out.println("Hello, world.");
30+
}

0 commit comments

Comments
 (0)