< prev index next >

test/java/lang/invoke/MethodHandles/privateLookupIn/test/p/PrivateLookupInTests.java

Print this page


   1 /*
   2  * Copyright (c) 2016, 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  */


 109     public void testTargetClassInOpenModule() throws Throwable {
 110         // m1/p1.Type
 111         Class<?> clazz = Class.forName("p1.Type");
 112         assertEquals(clazz.getModule().getName(), "m1");
 113 
 114         // ensure that this module reads m1
 115         Module thisModule = getClass().getModule();
 116         Module m1 = clazz.getModule();
 117         thisModule.addReads(clazz.getModule());
 118         assertTrue(m1.isOpen("p1", thisModule));
 119 
 120         Lookup lookup = MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());
 121         assertTrue(lookup.lookupClass() == clazz);
 122         assertTrue(lookup.hasPrivateAccess());
 123 
 124         // get obj field
 125         MethodHandle mh = lookup.findStaticGetter(clazz, "obj", Object.class);
 126         Object obj = mh.invokeExact();
 127     }
 128 




















 129     // test does not read m2, m2 opens p2 to test
 130     @Test(expectedExceptions = {IllegalAccessException.class})
 131     public void testCallerDoesNotRead() throws Throwable {
 132         // m2/p2.Type
 133         Class<?> clazz = Class.forName("p2.Type");
 134         assertEquals(clazz.getModule().getName(), "m2");
 135 
 136         Module thisModule = getClass().getModule();
 137         Module m2 = clazz.getModule();
 138         assertFalse(thisModule.canRead(m2));
 139         assertTrue(m2.isOpen("p2", thisModule));
 140 
 141         Lookup lookup = MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());
 142     }
 143 
 144     // test reads m3, m3 does not open p3 to test
 145     @Test(expectedExceptions = {IllegalAccessException.class})
 146     public void testNotOpenToCaller() throws Throwable {
 147         // m3/p2.Type
 148         Class<?> clazz = Class.forName("p3.Type");


   1 /*
   2  * Copyright (c) 2016, 2017, 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  */


 109     public void testTargetClassInOpenModule() throws Throwable {
 110         // m1/p1.Type
 111         Class<?> clazz = Class.forName("p1.Type");
 112         assertEquals(clazz.getModule().getName(), "m1");
 113 
 114         // ensure that this module reads m1
 115         Module thisModule = getClass().getModule();
 116         Module m1 = clazz.getModule();
 117         thisModule.addReads(clazz.getModule());
 118         assertTrue(m1.isOpen("p1", thisModule));
 119 
 120         Lookup lookup = MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());
 121         assertTrue(lookup.lookupClass() == clazz);
 122         assertTrue(lookup.hasPrivateAccess());
 123 
 124         // get obj field
 125         MethodHandle mh = lookup.findStaticGetter(clazz, "obj", Object.class);
 126         Object obj = mh.invokeExact();
 127     }
 128 
 129     // test target class in unnamed module
 130     public void testTargetClassInUnnamedModule() throws Throwable {
 131         Class<?> clazz = Class.forName("Unnamed");
 132         assertFalse(clazz.getModule().isNamed());
 133 
 134         // thisModule does not read the unnamed module
 135         Module thisModule = getClass().getModule();
 136         assertFalse(thisModule.canRead(clazz.getModule()));
 137         try {
 138             MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());
 139             assertTrue(false);
 140         } catch (IllegalAccessException expected) { }
 141 
 142         // thisModule reads the unnamed module
 143         thisModule.addReads(clazz.getModule());
 144         Lookup lookup = MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());
 145         assertTrue(lookup.lookupClass() == clazz);
 146         assertTrue(lookup.hasPrivateAccess());
 147     }
 148 
 149     // test does not read m2, m2 opens p2 to test
 150     @Test(expectedExceptions = {IllegalAccessException.class})
 151     public void testCallerDoesNotRead() throws Throwable {
 152         // m2/p2.Type
 153         Class<?> clazz = Class.forName("p2.Type");
 154         assertEquals(clazz.getModule().getName(), "m2");
 155 
 156         Module thisModule = getClass().getModule();
 157         Module m2 = clazz.getModule();
 158         assertFalse(thisModule.canRead(m2));
 159         assertTrue(m2.isOpen("p2", thisModule));
 160 
 161         Lookup lookup = MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());
 162     }
 163 
 164     // test reads m3, m3 does not open p3 to test
 165     @Test(expectedExceptions = {IllegalAccessException.class})
 166     public void testNotOpenToCaller() throws Throwable {
 167         // m3/p2.Type
 168         Class<?> clazz = Class.forName("p3.Type");


< prev index next >