1 /*
2 * Copyright (c) 2022, 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 import java.lang.reflect.Modifier;
25 import java.lang.annotation.*;
26
27 /*
28 * @test
29 * @bug 8296743
30 * @summary Verify array classes and primitives have expected modifiers
31 */
32 @ExpectedModifiers(Modifier.PUBLIC | Modifier.FINAL | Modifier.ABSTRACT)
33 public class TestPrimitiveAndArrayModifiers {
34
35 /*
36 * Relevant excerpt of the Class.getModifiers() specification:
37 * <p> If the underlying class is an array class:
38 * <ul>
39 * <li> its {@code public}, {@code private} and {@code protected}
40 * modifiers are the same as those of its component type
41 * <li> its {@code final} and {@code abstract} modifiers are always
42 * {@code true}
43 * <li> its interface modifier is always {@code false}, even when
44 * the component type is an interface
45 * </ul>
46 */
47
48 public static void main(String... args) throws Exception {
49 testPrimitives();
50 testArrays();
51 }
52
53 private static void testArrays() {
54 Class<?>[] testCases = {
55 TestPrimitiveAndArrayModifiers.class,
56
57 PackagePrivateClass.class,
58 ProtectedClass.class,
59 PrivateClass.class,
60
61 PublicInterface.class,
62 PackagePrivateInterface.class,
63 ProtectedInterface.class,
64 PrivateInterface.class,
65 };
66
67 for(var testCase : testCases) {
68 int expectedModifiers =
69 testCase.getAnnotation(ExpectedModifiers.class).value();
70 Class<?> arrayClass = testCase.arrayType();
71 int actualModifiers = arrayClass.getModifiers();
72 if (expectedModifiers != actualModifiers) {
73 throw new RuntimeException("Expected " + Modifier.toString(expectedModifiers) +
74 "on " + testCase.getCanonicalName() +
75 ", but got " + Modifier.toString(actualModifiers));
76 }
77 }
78 }
79
80 @ExpectedModifiers(Modifier.FINAL | Modifier.ABSTRACT)
81 class PackagePrivateClass {}
82
83 @ExpectedModifiers(Modifier.FINAL | Modifier.ABSTRACT | Modifier.PROTECTED)
84 protected class ProtectedClass {}
85
86 @ExpectedModifiers(Modifier.FINAL | Modifier.ABSTRACT | Modifier.PRIVATE)
87 private class PrivateClass {}
88
89 @ExpectedModifiers(Modifier.FINAL | Modifier.ABSTRACT | Modifier.PUBLIC)
90 public interface PublicInterface {}
91
92 @ExpectedModifiers(Modifier.FINAL | Modifier.ABSTRACT)
93 interface PackagePrivateInterface {}
94
|
1 /*
2 * Copyright (c) 2022, 2026, 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 import jdk.internal.misc.PreviewFeatures;
25
26 import java.lang.classfile.ClassFile;
27 import java.lang.reflect.Modifier;
28 import java.lang.annotation.*;
29
30 /*
31 * @test
32 * @bug 8296743
33 * @modules java.base/jdk.internal.misc
34 * @summary Verify array classes and primitives have expected modifiers
35 * @run main/othervm TestPrimitiveAndArrayModifiers
36 * @run main/othervm --enable-preview TestPrimitiveAndArrayModifiers
37 */
38 @ExpectedModifiers(Modifier.PUBLIC | Modifier.FINAL | Modifier.ABSTRACT)
39 public class TestPrimitiveAndArrayModifiers {
40
41 /*
42 * Relevant excerpt of the Class.getModifiers() specification:
43 * <p> If the underlying class is an array class:
44 * <ul>
45 * <li> its {@code public}, {@code private} and {@code protected}
46 * modifiers are the same as those of its component type
47 * <li> its {@code final} and {@code abstract} modifiers are always
48 * {@code true}
49 * <li> its interface modifier is always {@code false}, even when
50 * the component type is an interface
51 * </ul>
52 */
53
54 public static void main(String... args) throws Exception {
55 testPrimitives();
56 testArrays();
57 }
58
59 private static void testArrays() {
60 Class<?>[] testCases = {
61 TestPrimitiveAndArrayModifiers.class,
62
63 PackagePrivateClass.class,
64 ProtectedClass.class,
65 PrivateClass.class,
66
67 PublicInterface.class,
68 PackagePrivateInterface.class,
69 ProtectedInterface.class,
70 PrivateInterface.class,
71 };
72
73 for(var testCase : testCases) {
74 int expectedModifiers =
75 testCase.getAnnotation(ExpectedModifiers.class).value();
76 if (PreviewFeatures.isEnabled()) {
77 // All arrays under preview also have IDENTITY
78 expectedModifiers |= ClassFile.ACC_IDENTITY;
79 }
80 Class<?> arrayClass = testCase.arrayType();
81 int actualModifiers = arrayClass.getModifiers();
82 if (expectedModifiers != actualModifiers) {
83 throw new RuntimeException("Expected " + Modifier.toString(expectedModifiers) +
84 " on " + testCase.getCanonicalName() +
85 ", but got " + Modifier.toString(actualModifiers));
86 }
87 }
88 }
89
90 @ExpectedModifiers(Modifier.FINAL | Modifier.ABSTRACT)
91 class PackagePrivateClass {}
92
93 @ExpectedModifiers(Modifier.FINAL | Modifier.ABSTRACT | Modifier.PROTECTED)
94 protected class ProtectedClass {}
95
96 @ExpectedModifiers(Modifier.FINAL | Modifier.ABSTRACT | Modifier.PRIVATE)
97 private class PrivateClass {}
98
99 @ExpectedModifiers(Modifier.FINAL | Modifier.ABSTRACT | Modifier.PUBLIC)
100 public interface PublicInterface {}
101
102 @ExpectedModifiers(Modifier.FINAL | Modifier.ABSTRACT)
103 interface PackagePrivateInterface {}
104
|