jdk/test/java/lang/invoke/MethodHandles/privateLookupIn/test/p/PrivateLookupInTests.java
author alanb
Fri, 07 Apr 2017 08:05:54 +0000
changeset 44545 83b611b88ac8
parent 43712 5dfd0950317c
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     1
/*
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
     2
 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     4
 *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     8
 *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    13
 * accompanied this code).
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    14
 *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    18
 *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    21
 * questions.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    22
 */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    23
package p;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    24
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    25
import java.lang.invoke.MethodHandle;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    26
import java.lang.invoke.MethodHandles;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    27
import java.lang.invoke.MethodHandles.Lookup;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    28
import java.lang.reflect.Modifier;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    29
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    30
import static java.lang.invoke.MethodHandles.Lookup.*;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    31
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    32
import org.testng.annotations.BeforeTest;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    33
import org.testng.annotations.Test;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    34
import static org.testng.Assert.*;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    35
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    36
/**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    37
 * Unit tests for MethodHandles.privateLookupIn
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    38
 */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    39
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    40
@Test
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    41
public class PrivateLookupInTests {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    42
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    43
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    44
     * A public and non-public types in the test module but in a different
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    45
     * package to the test class.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    46
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    47
     * package p.internal;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    48
     * public class PublicType {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    49
     * }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    50
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    51
     * package p.internal;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    52
     * class NonPublicType {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    53
     *     private static final Object obj = ...
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    54
     * }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    55
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    56
    private Class<?> publicType;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    57
    private Class<?> nonPublicType;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    58
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    59
    // initialize and sanity check publicType/nonPublicType
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    60
    @BeforeTest
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    61
    public void init() throws Exception {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    62
        publicType = Class.forName("p.internal.PublicType");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    63
        assertTrue(this.getClass().getModule() == publicType.getModule());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    64
        assertNotEquals(this.getClass().getPackageName(), publicType.getPackageName());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    65
        assertTrue(Modifier.isPublic(publicType.getModifiers()));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    66
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    67
        nonPublicType = Class.forName("p.internal.NonPublicType");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    68
        assertTrue(this.getClass().getModule() == nonPublicType.getModule());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    69
        assertNotEquals(this.getClass().getPackageName(), nonPublicType.getPackageName());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    70
        assertFalse(Modifier.isPublic(nonPublicType.getModifiers()));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    71
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    72
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    73
    // Invoke MethodHandles.privateLookupIn with a full-power caller
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    74
    public void testAllAccessCallerSameModule() throws Throwable {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    75
        Lookup lookup = MethodHandles.privateLookupIn(nonPublicType, MethodHandles.lookup());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    76
        assertTrue(lookup.lookupClass() == nonPublicType);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    77
        assertTrue(lookup.hasPrivateAccess());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    78
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    79
        // get obj field
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    80
        MethodHandle mh = lookup.findStaticGetter(nonPublicType, "obj", Object.class);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    81
        Object obj = mh.invokeExact();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    82
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    83
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    84
    // Invoke MethodHandles.privateLookupIn with a reduced-power caller
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    85
    public void testReducedAccessCallerSameModule() throws Throwable {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    86
        Lookup caller = MethodHandles.lookup().dropLookupMode(PACKAGE);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    87
        assertTrue((caller.lookupModes() & PRIVATE) == 0);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    88
        assertTrue((caller.lookupModes() & PACKAGE) == 0);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    89
        assertTrue((caller.lookupModes() & MODULE) != 0);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    90
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    91
        Lookup lookup = MethodHandles.privateLookupIn(nonPublicType, caller);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    92
        assertTrue(lookup.lookupClass() == nonPublicType);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    93
        assertTrue(lookup.hasPrivateAccess());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    94
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    95
        // use it
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    96
        MethodHandle mh = lookup.findStaticGetter(nonPublicType, "obj", Object.class);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    97
        Object obj = mh.invokeExact();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    98
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
    99
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   100
    // Invoke MethodHandles.privateLookupIn with the public lookup as caller
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   101
    @Test(expectedExceptions = {IllegalAccessException.class})
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   102
    public void testPublicLookupSameModule() throws Exception {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   103
        Lookup caller = MethodHandles.publicLookup();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   104
        Lookup lookup = MethodHandles.privateLookupIn(publicType, caller);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   105
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   106
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   107
    // test reads m1, open module m1 containing p1
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   108
    public void testTargetClassInOpenModule() throws Throwable {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   109
        // m1/p1.Type
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   110
        Class<?> clazz = Class.forName("p1.Type");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   111
        assertEquals(clazz.getModule().getName(), "m1");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   112
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   113
        // ensure that this module reads m1
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   114
        Module thisModule = getClass().getModule();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   115
        Module m1 = clazz.getModule();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   116
        thisModule.addReads(clazz.getModule());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   117
        assertTrue(m1.isOpen("p1", thisModule));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   118
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   119
        Lookup lookup = MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   120
        assertTrue(lookup.lookupClass() == clazz);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   121
        assertTrue(lookup.hasPrivateAccess());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   122
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   123
        // get obj field
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   124
        MethodHandle mh = lookup.findStaticGetter(clazz, "obj", Object.class);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   125
        Object obj = mh.invokeExact();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   126
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   127
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   128
    // test target class in unnamed module
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   129
    public void testTargetClassInUnnamedModule() throws Throwable {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   130
        Class<?> clazz = Class.forName("Unnamed");
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   131
        assertFalse(clazz.getModule().isNamed());
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   132
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   133
        // thisModule does not read the unnamed module
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   134
        Module thisModule = getClass().getModule();
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   135
        assertFalse(thisModule.canRead(clazz.getModule()));
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   136
        try {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   137
            MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   138
            assertTrue(false);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   139
        } catch (IllegalAccessException expected) { }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   140
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   141
        // thisModule reads the unnamed module
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   142
        thisModule.addReads(clazz.getModule());
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   143
        Lookup lookup = MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   144
        assertTrue(lookup.lookupClass() == clazz);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   145
        assertTrue(lookup.hasPrivateAccess());
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   146
    }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   147
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   148
    // test does not read m2, m2 opens p2 to test
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   149
    @Test(expectedExceptions = {IllegalAccessException.class})
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   150
    public void testCallerDoesNotRead() throws Throwable {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   151
        // m2/p2.Type
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   152
        Class<?> clazz = Class.forName("p2.Type");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   153
        assertEquals(clazz.getModule().getName(), "m2");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   154
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   155
        Module thisModule = getClass().getModule();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   156
        Module m2 = clazz.getModule();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   157
        assertFalse(thisModule.canRead(m2));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   158
        assertTrue(m2.isOpen("p2", thisModule));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   159
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   160
        Lookup lookup = MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   161
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   162
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   163
    // test reads m3, m3 does not open p3 to test
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   164
    @Test(expectedExceptions = {IllegalAccessException.class})
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   165
    public void testNotOpenToCaller() throws Throwable {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   166
        // m3/p2.Type
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   167
        Class<?> clazz = Class.forName("p3.Type");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   168
        assertEquals(clazz.getModule().getName(), "m3");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   169
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   170
        Module thisModule = getClass().getModule();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   171
        Module m3 = clazz.getModule();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   172
        thisModule.addReads(clazz.getModule());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   173
        assertFalse(m3.isOpen("p3", thisModule));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   174
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   175
        Lookup lookup = MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   176
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   177
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   178
    // Invoke MethodHandles.privateLookupIn with a primitive class
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   179
    @Test(expectedExceptions = {IllegalArgumentException.class})
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   180
    public void testPrimitiveClassAsTargetClass() throws Exception {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   181
        MethodHandles.privateLookupIn(int.class, MethodHandles.lookup());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   182
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   183
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   184
    // Invoke MethodHandles.privateLookupIn with an array class
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   185
    @Test(expectedExceptions = {IllegalArgumentException.class})
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   186
    public void testArrayClassAsTargetClass() throws Exception {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   187
        MethodHandles.privateLookupIn(PrivateLookupInTests[].class, MethodHandles.lookup());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   188
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   189
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   190
    // Invoke MethodHandles.privateLookupIn with a primitive array class
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   191
    @Test(expectedExceptions = {IllegalArgumentException.class})
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   192
    public void testPrimitiveArrayClassAsTargetClass() throws Exception {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   193
        MethodHandles.privateLookupIn(int[].class, MethodHandles.lookup());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   194
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   195
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   196
    // Invoke MethodHandles.privateLookupIn with null
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   197
    @Test(expectedExceptions = {NullPointerException.class})
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   198
    public void testNullTargetClass() throws Exception {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   199
        MethodHandles.privateLookupIn(null, MethodHandles.lookup());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   200
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   201
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   202
    // Invoke MethodHandles.privateLookupIn with null
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   203
    @Test(expectedExceptions = {NullPointerException.class})
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   204
    public void testNullCaller() throws Exception {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   205
        MethodHandles.privateLookupIn(getClass(), null);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   206
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents:
diff changeset
   207
}