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  */
  23 package p;
  24 
  25 import java.lang.invoke.MethodHandle;
  26 import java.lang.invoke.MethodHandles;
  27 import java.lang.invoke.MethodHandles.Lookup;
  28 import java.lang.reflect.Modifier;
  29 import java.lang.reflect.Module;
  30 
  31 import static java.lang.invoke.MethodHandles.Lookup.*;
  32 
  33 import org.testng.annotations.BeforeTest;
  34 import org.testng.annotations.Test;
  35 import static org.testng.Assert.*;
  36 
  37 /**
  38  * Unit tests for MethodHandles.privateLookupIn
  39  */
  40 
  41 @Test
  42 public class PrivateLookupInTests {
  43 
  44     /**
  45      * A public and non-public types in the test module but in a different
  46      * package to the test class.
  47      *
  48      * package p.internal;
  49      * public class PublicType {
  50      * }
  51      *
  52      * package p.internal;
  53      * class NonPublicType {
  54      *     private static final Object obj = ...
  55      * }
  56      */
  57     private Class<?> publicType;
  58     private Class<?> nonPublicType;
  59 
  60     // initialize and sanity check publicType/nonPublicType
  61     @BeforeTest
  62     public void init() throws Exception {
  63         publicType = Class.forName("p.internal.PublicType");
  64         assertTrue(this.getClass().getModule() == publicType.getModule());
  65         assertNotEquals(this.getClass().getPackageName(), publicType.getPackageName());
  66         assertTrue(Modifier.isPublic(publicType.getModifiers()));
  67 
  68         nonPublicType = Class.forName("p.internal.NonPublicType");
  69         assertTrue(this.getClass().getModule() == nonPublicType.getModule());
  70         assertNotEquals(this.getClass().getPackageName(), nonPublicType.getPackageName());
  71         assertFalse(Modifier.isPublic(nonPublicType.getModifiers()));
  72     }
  73 
  74     // Invoke MethodHandles.privateLookupIn with a full-power caller
  75     public void testAllAccessCallerSameModule() throws Throwable {
  76         Lookup lookup = MethodHandles.privateLookupIn(nonPublicType, MethodHandles.lookup());
  77         assertTrue(lookup.lookupClass() == nonPublicType);
  78         assertTrue(lookup.hasPrivateAccess());
  79 
  80         // get obj field
  81         MethodHandle mh = lookup.findStaticGetter(nonPublicType, "obj", Object.class);
  82         Object obj = mh.invokeExact();
  83     }
  84 
  85     // Invoke MethodHandles.privateLookupIn with a reduced-power caller
  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         assertTrue(lookup.lookupClass() == nonPublicType);
  94         assertTrue(lookup.hasPrivateAccess());
  95 
  96         // use it
  97         MethodHandle mh = lookup.findStaticGetter(nonPublicType, "obj", Object.class);
  98         Object obj = mh.invokeExact();
  99     }
 100 
 101     // Invoke MethodHandles.privateLookupIn with the public lookup as caller
 102     @Test(expectedExceptions = {IllegalAccessException.class})
 103     public void testPublicLookupSameModule() throws Exception {
 104         Lookup caller = MethodHandles.publicLookup();
 105         Lookup lookup = MethodHandles.privateLookupIn(publicType, caller);
 106     }
 107 
 108     // test reads m1, open module m1 containing p1
 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");
 169         assertEquals(clazz.getModule().getName(), "m3");
 170 
 171         Module thisModule = getClass().getModule();
 172         Module m3 = clazz.getModule();
 173         thisModule.addReads(clazz.getModule());
 174         assertFalse(m3.isOpen("p3", thisModule));
 175 
 176         Lookup lookup = MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());
 177     }
 178 
 179     // Invoke MethodHandles.privateLookupIn with a primitive class
 180     @Test(expectedExceptions = {IllegalArgumentException.class})
 181     public void testPrimitiveClassAsTargetClass() throws Exception {
 182         MethodHandles.privateLookupIn(int.class, MethodHandles.lookup());
 183     }
 184 
 185     // Invoke MethodHandles.privateLookupIn with an array class
 186     @Test(expectedExceptions = {IllegalArgumentException.class})
 187     public void testArrayClassAsTargetClass() throws Exception {
 188         MethodHandles.privateLookupIn(PrivateLookupInTests[].class, MethodHandles.lookup());
 189     }
 190 
 191     // Invoke MethodHandles.privateLookupIn with a primitive array class
 192     @Test(expectedExceptions = {IllegalArgumentException.class})
 193     public void testPrimitiveArrayClassAsTargetClass() throws Exception {
 194         MethodHandles.privateLookupIn(int[].class, MethodHandles.lookup());
 195     }
 196 
 197     // Invoke MethodHandles.privateLookupIn with null
 198     @Test(expectedExceptions = {NullPointerException.class})
 199     public void testNullTargetClass() throws Exception {
 200         MethodHandles.privateLookupIn(null, MethodHandles.lookup());
 201     }
 202 
 203     // Invoke MethodHandles.privateLookupIn with null
 204     @Test(expectedExceptions = {NullPointerException.class})
 205     public void testNullCaller() throws Exception {
 206         MethodHandles.privateLookupIn(getClass(), null);
 207     }
 208 }