1 /*
2 * Copyright (c) 2015, 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 /*
25 * @test
26 * @build ModuleSetAccessibleTest
27 * @modules java.base/java.lang:open
28 * java.base/jdk.internal.misc:+open
29 * @run junit/othervm ModuleSetAccessibleTest
30 * @summary Test java.lang.reflect.AccessibleObject with modules
31 */
32
33 import java.lang.reflect.AccessibleObject;
34 import java.lang.reflect.Constructor;
35 import java.lang.reflect.Field;
36 import java.lang.reflect.InaccessibleObjectException;
37 import java.lang.reflect.InvocationTargetException;
38 import java.lang.reflect.Method;
39 import java.security.ProtectionDomain;
40
41 import jdk.internal.misc.Unsafe;
42
43 import static org.junit.jupiter.api.Assertions.*;
44 import org.junit.jupiter.api.Test;
45
46 public class ModuleSetAccessibleTest {
47
48 /**
49 * Invoke a private constructor on a public class in an exported package
50 */
51 @Test
52 public void testPrivateConstructorInExportedPackage() throws Exception {
53 Constructor<?> ctor = Unsafe.class.getDeclaredConstructor();
54
55 assertThrows(IllegalAccessException.class, () -> ctor.newInstance());
56
57 ctor.setAccessible(true);
58 Unsafe unsafe = (Unsafe) ctor.newInstance();
59 }
60
61
62 /**
63 * Invoke a private method on a public class in an exported package
64 */
65 @Test
66 public void testPrivateMethodInExportedPackage() throws Exception {
67 Method m = Unsafe.class.getDeclaredMethod("throwIllegalAccessError");
68 assertThrows(IllegalAccessException.class, () -> m.invoke(null));
69
70 m.setAccessible(true);
71 InvocationTargetException e = assertThrows(InvocationTargetException.class, () ->
72 m.invoke(null));
73 // thrown by throwIllegalAccessError
74 assertInstanceOf(IllegalAccessError.class, e.getCause());
75 }
76
77
78 /**
79 * Access a private field in a public class that is an exported package
80 */
81 @Test
82 public void testPrivateFieldInExportedPackage() throws Exception {
83 Field f = Unsafe.class.getDeclaredField("theUnsafe");
84 assertThrows(IllegalAccessException.class, () -> f.get(null));
85
86 f.setAccessible(true);
87 Unsafe unsafe = (Unsafe) f.get(null);
88 }
89
90
91 /**
92 * Invoke a public constructor on a public class in a non-exported package
93 */
94 @Test
95 public void testPublicConstructorInNonExportedPackage() throws Exception {
96 Class<?> clazz = Class.forName("sun.security.x509.X500Name");
97 Constructor<?> ctor = clazz.getConstructor(String.class);
98
99 assertThrows(IllegalAccessException.class, () -> ctor.newInstance("cn=duke"));
100
101 assertThrows(InaccessibleObjectException.class, () -> ctor.setAccessible(true));
102
103 ctor.setAccessible(false); // should succeed
104 }
105
106
107 /**
108 * Access a public field in a public class that in a non-exported package
109 */
110 @Test
111 public void testPublicFieldInNonExportedPackage() throws Exception {
112 Class<?> clazz = Class.forName("sun.security.x509.X500Name");
113 Field f = clazz.getField("SERIALNUMBER_OID");
114
115 assertThrows(IllegalAccessException.class, () -> f.get(null));
116
117 assertThrows(InaccessibleObjectException.class, () -> f.setAccessible(true));
118
119 f.setAccessible(false); // should succeed
120 }
121
122
123 /**
124 * Test that the Class constructor cannot be make accessible.
125 */
126 @Test
127 public void testJavaLangClass() throws Exception {
128
129 // non-public constructor
130 Constructor<?> ctor
131 = Class.class.getDeclaredConstructor(ClassLoader.class, Class.class, char.class,
132 ProtectionDomain.class, boolean.class, boolean.class, char.class);
133 AccessibleObject[] ctors = { ctor };
134
135 assertThrows(SecurityException.class, () -> ctor.setAccessible(true));
136 assertThrows(SecurityException.class, () -> AccessibleObject.setAccessible(ctors, true));
137
138 // should succeed
139 ctor.setAccessible(false);
140 AccessibleObject.setAccessible(ctors, false);
141
142 }
143
144 }