< prev index next >

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

Print this page


   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  */


  65         assertTrue(Modifier.isPublic(publicType.getModifiers()));
  66 
  67         nonPublicType = Class.forName("p.internal.NonPublicType");
  68         assertTrue(this.getClass().getModule() == nonPublicType.getModule());
  69         assertNotEquals(this.getClass().getPackageName(), nonPublicType.getPackageName());
  70         assertFalse(Modifier.isPublic(nonPublicType.getModifiers()));
  71     }
  72 
  73     // Invoke MethodHandles.privateLookupIn with a full-power caller
  74     public void testAllAccessCallerSameModule() throws Throwable {
  75         Lookup lookup = MethodHandles.privateLookupIn(nonPublicType, MethodHandles.lookup());
  76         assertTrue(lookup.lookupClass() == nonPublicType);
  77         assertTrue(lookup.hasPrivateAccess());
  78 
  79         // get obj field
  80         MethodHandle mh = lookup.findStaticGetter(nonPublicType, "obj", Object.class);
  81         Object obj = mh.invokeExact();
  82     }
  83 
  84     // Invoke MethodHandles.privateLookupIn with a reduced-power caller

  85     public void testReducedAccessCallerSameModule() throws Throwable {
  86         Lookup caller = MethodHandles.lookup().dropLookupMode(PACKAGE);
  87         assertTrue((caller.lookupModes() & PRIVATE) == 0);
  88         assertTrue((caller.lookupModes() & PACKAGE) == 0);
  89         assertTrue((caller.lookupModes() & MODULE) != 0);
  90 
  91         Lookup lookup = MethodHandles.privateLookupIn(nonPublicType, caller);
  92         assertTrue(lookup.lookupClass() == nonPublicType);
  93         assertTrue(lookup.hasPrivateAccess());
  94 
  95         // use it
  96         MethodHandle mh = lookup.findStaticGetter(nonPublicType, "obj", Object.class);
  97         Object obj = mh.invokeExact();
  98     }
  99 
 100     // Invoke MethodHandles.privateLookupIn with the public lookup as caller
 101     @Test(expectedExceptions = {IllegalAccessException.class})
 102     public void testPublicLookupSameModule() throws Exception {
 103         Lookup caller = MethodHandles.publicLookup();
 104         Lookup lookup = MethodHandles.privateLookupIn(publicType, caller);
 105     }
 106 
 107     // test reads m1, open module m1 containing p1
 108     public void testTargetClassInOpenModule() throws Throwable {
 109         // m1/p1.Type
 110         Class<?> clazz = Class.forName("p1.Type");
 111         assertEquals(clazz.getModule().getName(), "m1");
 112 
 113         // ensure that this module reads m1
 114         Module thisModule = getClass().getModule();
 115         Module m1 = clazz.getModule();
 116         thisModule.addReads(clazz.getModule());
 117         assertTrue(m1.isOpen("p1", thisModule));


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


  65         assertTrue(Modifier.isPublic(publicType.getModifiers()));
  66 
  67         nonPublicType = Class.forName("p.internal.NonPublicType");
  68         assertTrue(this.getClass().getModule() == nonPublicType.getModule());
  69         assertNotEquals(this.getClass().getPackageName(), nonPublicType.getPackageName());
  70         assertFalse(Modifier.isPublic(nonPublicType.getModifiers()));
  71     }
  72 
  73     // Invoke MethodHandles.privateLookupIn with a full-power caller
  74     public void testAllAccessCallerSameModule() throws Throwable {
  75         Lookup lookup = MethodHandles.privateLookupIn(nonPublicType, MethodHandles.lookup());
  76         assertTrue(lookup.lookupClass() == nonPublicType);
  77         assertTrue(lookup.hasPrivateAccess());
  78 
  79         // get obj field
  80         MethodHandle mh = lookup.findStaticGetter(nonPublicType, "obj", Object.class);
  81         Object obj = mh.invokeExact();
  82     }
  83 
  84     // Invoke MethodHandles.privateLookupIn with a reduced-power caller
  85     @Test(expectedExceptions = {IllegalAccessException.class})
  86     public void testReducedAccessCallerSameModule() throws Throwable {
  87         Lookup caller = MethodHandles.lookup().dropLookupMode(PACKAGE);
  88         assertTrue((caller.lookupModes() & PRIVATE) == 0);
  89         assertTrue((caller.lookupModes() & PACKAGE) == 0);
  90         assertTrue((caller.lookupModes() & MODULE) != 0);
  91 
  92         Lookup lookup = MethodHandles.privateLookupIn(nonPublicType, caller);






  93     }
  94 
  95     // Invoke MethodHandles.privateLookupIn with the public lookup as caller
  96     @Test(expectedExceptions = {IllegalAccessException.class})
  97     public void testPublicLookupSameModule() throws Exception {
  98         Lookup caller = MethodHandles.publicLookup();
  99         Lookup lookup = MethodHandles.privateLookupIn(publicType, caller);
 100     }
 101 
 102     // test reads m1, open module m1 containing p1
 103     public void testTargetClassInOpenModule() throws Throwable {
 104         // m1/p1.Type
 105         Class<?> clazz = Class.forName("p1.Type");
 106         assertEquals(clazz.getModule().getName(), "m1");
 107 
 108         // ensure that this module reads m1
 109         Module thisModule = getClass().getModule();
 110         Module m1 = clazz.getModule();
 111         thisModule.addReads(clazz.getModule());
 112         assertTrue(m1.isOpen("p1", thisModule));


< prev index next >