1 /* 2 * Copyright (c) 2015, 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 testng ModuleSetAccessibleTest 30 * @summary Test java.lang.reflect.AccessibleObject with modules 31 */ 32 33 import java.lang.module.ModuleDescriptor; 34 import java.lang.reflect.AccessibleObject; 35 import java.lang.reflect.Constructor; 36 import java.lang.reflect.Field; 37 import java.lang.reflect.InaccessibleObjectException; 38 import java.lang.reflect.InvocationTargetException; 39 import java.lang.reflect.Method; 40 import java.lang.reflect.Module; 41 42 import jdk.internal.misc.Unsafe; 43 44 import org.testng.annotations.Test; 45 import static org.testng.Assert.*; 46 47 @Test 48 public class ModuleSetAccessibleTest { 49 50 /** 51 * Invoke a private constructor on a public class in an exported package 52 */ 53 public void testPrivateConstructorInExportedPackage() throws Exception { 54 Constructor<?> ctor = Unsafe.class.getDeclaredConstructor(); 55 56 try { 57 ctor.newInstance(); 58 assertTrue(false); 59 } catch (IllegalAccessException expected) { } 60 61 ctor.setAccessible(true); 62 Unsafe unsafe = (Unsafe) ctor.newInstance(); 63 } 64 65 66 /** 67 * Invoke a private method on a public class in an exported package 68 */ 69 public void testPrivateMethodInExportedPackage() throws Exception { 70 Method m = Unsafe.class.getDeclaredMethod("throwIllegalAccessError"); 71 try { 72 m.invoke(null); 73 assertTrue(false); 74 } catch (IllegalAccessException expected) { } 75 76 m.setAccessible(true); 77 try { 78 m.invoke(null); 79 assertTrue(false); 80 } catch (InvocationTargetException e) { 81 // thrown by throwIllegalAccessError 82 assertTrue(e.getCause() instanceof IllegalAccessError); 83 } 84 } 85 86 87 /** 88 * Access a private field in a public class that is an exported package 89 */ 90 public void testPrivateFieldInExportedPackage() throws Exception { 91 Field f = Unsafe.class.getDeclaredField("theUnsafe"); 92 93 try { 94 f.get(null); 95 assertTrue(false); 96 } catch (IllegalAccessException expected) { } 97 98 f.setAccessible(true); 99 Unsafe unsafe = (Unsafe) f.get(null); 100 } 101 102 103 /** 104 * Invoke a public constructor on a public class in a non-exported package 105 */ 106 public void testPublicConstructorInNonExportedPackage() throws Exception { 107 Class<?> clazz = Class.forName("sun.security.x509.X500Name"); 108 Constructor<?> ctor = clazz.getConstructor(String.class); 109 110 try { 111 ctor.newInstance("cn=duke"); 112 assertTrue(false); 113 } catch (IllegalAccessException expected) { } 114 115 try { 116 ctor.setAccessible(true); 117 assertTrue(false); 118 } catch (InaccessibleObjectException expected) { } 119 120 ctor.setAccessible(false); // should succeed 121 } 122 123 124 /** 125 * Access a public field in a public class that in a non-exported package 126 */ 127 public void testPublicFieldInNonExportedPackage() throws Exception { 128 Class<?> clazz = Class.forName("sun.security.x509.X500Name"); 129 Field f = clazz.getField("SERIALNUMBER_OID"); 130 131 try { 132 f.get(null); 133 assertTrue(false); 134 } catch (IllegalAccessException expected) { } 135 136 try { 137 f.setAccessible(true); 138 assertTrue(false); 139 } catch (InaccessibleObjectException expected) { } 140 141 f.setAccessible(false); // should succeed 142 } 143 144 145 /** 146 * Test that the Class constructor cannot be make accessible. 147 */ 148 public void testJavaLangClass() throws Exception { 149 150 // non-public constructor 151 Constructor<?> ctor 152 = Class.class.getDeclaredConstructor(ClassLoader.class, Class.class); 153 AccessibleObject[] ctors = { ctor }; 154 155 try { 156 ctor.setAccessible(true); 157 assertTrue(false); 158 } catch (SecurityException expected) { } 159 160 try { 161 AccessibleObject.setAccessible(ctors, true); 162 assertTrue(false); 163 } catch (SecurityException expected) { } 164 165 // should succeed 166 ctor.setAccessible(false); 167 AccessibleObject.setAccessible(ctors, false); 168 169 } 170 171 }