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  */
  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 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");
 149         assertEquals(clazz.getModule().getName(), "m3");
 150 
 151         Module thisModule = getClass().getModule();
 152         Module m3 = clazz.getModule();
 153         thisModule.addReads(clazz.getModule());
 154         assertFalse(m3.isOpen("p3", thisModule));
 155 
 156         Lookup lookup = MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());
 157     }
 158 
 159     // Invoke MethodHandles.privateLookupIn with a primitive class
 160     @Test(expectedExceptions = {IllegalArgumentException.class})
 161     public void testPrimitiveClassAsTargetClass() throws Exception {
 162         MethodHandles.privateLookupIn(int.class, MethodHandles.lookup());
 163     }
 164 
 165     // Invoke MethodHandles.privateLookupIn with an array class
 166     @Test(expectedExceptions = {IllegalArgumentException.class})
 167     public void testArrayClassAsTargetClass() throws Exception {
 168         MethodHandles.privateLookupIn(PrivateLookupInTests[].class, MethodHandles.lookup());
 169     }
 170 
 171     // Invoke MethodHandles.privateLookupIn with a primitive array class
 172     @Test(expectedExceptions = {IllegalArgumentException.class})
 173     public void testPrimitiveArrayClassAsTargetClass() throws Exception {
 174         MethodHandles.privateLookupIn(int[].class, MethodHandles.lookup());
 175     }
 176 
 177     // Invoke MethodHandles.privateLookupIn with null
 178     @Test(expectedExceptions = {NullPointerException.class})
 179     public void testNullTargetClass() throws Exception {
 180         MethodHandles.privateLookupIn(null, MethodHandles.lookup());
 181     }
 182 
 183     // Invoke MethodHandles.privateLookupIn with null
 184     @Test(expectedExceptions = {NullPointerException.class})
 185     public void testNullCaller() throws Exception {
 186         MethodHandles.privateLookupIn(getClass(), null);
 187     }
 188 }