jdk/test/java/lang/invoke/AccessControlTest.java
author jrose
Sat, 05 Oct 2013 05:30:40 -0700
changeset 20535 cc85c8626435
parent 19388 188246066dbb
child 21596 0e3a39f29dbc
permissions -rw-r--r--
8024438: JSR 292 API specification maintenance for JDK 8 Summary: add wildcard to unreflectConstructor, various clarifications and minor edits Reviewed-by: mchung, darcy, twisti
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13044
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
     1
/*
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
     4
 *
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    10
 *
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    15
 * accompanied this code).
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    16
 *
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    20
 *
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    23
 * questions.
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    24
 */
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    25
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    26
/* @test
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    27
 * @summary test access checking by java.lang.invoke.MethodHandles.Lookup
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    28
 * @library ../../../..
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    29
 * @build test.java.lang.invoke.AccessControlTest
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    30
 * @build test.java.lang.invoke.AccessControlTest_subpkg.Acquaintance_remote
19388
188246066dbb 8022749: Convert junit tests to testng in test/java/lang/invoke
henryjen
parents: 13044
diff changeset
    31
 * @run testng/othervm test.java.lang.invoke.AccessControlTest
13044
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    32
 */
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    33
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    34
package test.java.lang.invoke;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    35
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    36
import java.lang.invoke.*;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    37
import java.lang.reflect.*;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    38
import java.util.*;
19388
188246066dbb 8022749: Convert junit tests to testng in test/java/lang/invoke
henryjen
parents: 13044
diff changeset
    39
import org.testng.*;
188246066dbb 8022749: Convert junit tests to testng in test/java/lang/invoke
henryjen
parents: 13044
diff changeset
    40
import org.testng.annotations.*;
13044
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    41
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    42
import static java.lang.invoke.MethodHandles.*;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    43
import static java.lang.invoke.MethodHandles.Lookup.*;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    44
import static java.lang.invoke.MethodType.*;
19388
188246066dbb 8022749: Convert junit tests to testng in test/java/lang/invoke
henryjen
parents: 13044
diff changeset
    45
import static org.testng.Assert.*;
188246066dbb 8022749: Convert junit tests to testng in test/java/lang/invoke
henryjen
parents: 13044
diff changeset
    46
13044
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    47
import test.java.lang.invoke.AccessControlTest_subpkg.Acquaintance_remote;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    48
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    49
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    50
/**
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    51
 * Test many combinations of Lookup access and cross-class lookupStatic.
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    52
 * @author jrose
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    53
 */
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    54
public class AccessControlTest {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    55
    static final Class<?> THIS_CLASS = AccessControlTest.class;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    56
    // How much output?
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    57
    static int verbosity = 0;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    58
    static {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    59
        String vstr = System.getProperty(THIS_CLASS.getSimpleName()+".verbosity");
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    60
        if (vstr == null)
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    61
            vstr = System.getProperty(THIS_CLASS.getName()+".verbosity");
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    62
        if (vstr != null)  verbosity = Integer.parseInt(vstr);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    63
    }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    64
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    65
    private class LookupCase implements Comparable<LookupCase> {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    66
        final Lookup   lookup;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    67
        final Class<?> lookupClass;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    68
        final int      lookupModes;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    69
        public LookupCase(Lookup lookup) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    70
            this.lookup = lookup;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    71
            this.lookupClass = lookup.lookupClass();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    72
            this.lookupModes = lookup.lookupModes();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    73
            assert(lookupString().equals(lookup.toString()));
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    74
            numberOf(lookupClass().getClassLoader()); // assign CL#
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    75
        }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    76
        public LookupCase(Class<?> lookupClass, int lookupModes) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    77
            this.lookup = null;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    78
            this.lookupClass = lookupClass;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    79
            this.lookupModes = lookupModes;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    80
            numberOf(lookupClass().getClassLoader()); // assign CL#
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    81
        }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    82
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    83
        public final Class<?> lookupClass() { return lookupClass; }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    84
        public final int      lookupModes() { return lookupModes; }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    85
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    86
        public Lookup lookup() { lookup.getClass(); return lookup; }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    87
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    88
        @Override
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    89
        public int compareTo(LookupCase that) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    90
            Class<?> c1 = this.lookupClass();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    91
            Class<?> c2 = that.lookupClass();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    92
            if (c1 != c2) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    93
                int cmp = c1.getName().compareTo(c2.getName());
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    94
                if (cmp != 0)  return cmp;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    95
                cmp = numberOf(c1.getClassLoader()) - numberOf(c2.getClassLoader());
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    96
                assert(cmp != 0);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    97
                return cmp;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    98
            }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
    99
            return -(this.lookupModes() - that.lookupModes());
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   100
        }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   101
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   102
        @Override
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   103
        public boolean equals(Object that) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   104
            return (that instanceof LookupCase && equals((LookupCase)that));
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   105
        }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   106
        public boolean equals(LookupCase that) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   107
            return (this.lookupClass() == that.lookupClass() &&
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   108
                    this.lookupModes() == that.lookupModes());
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   109
        }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   110
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   111
        @Override
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   112
        public int hashCode() {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   113
            return lookupClass().hashCode() + (lookupModes() * 31);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   114
        }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   115
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   116
        /** Simulate all assertions in the spec. for Lookup.toString. */
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   117
        private String lookupString() {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   118
            String name = lookupClass.getName();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   119
            String suffix = "";
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   120
            if (lookupModes == 0)
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   121
                suffix = "/noaccess";
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   122
            else if (lookupModes == PUBLIC)
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   123
                suffix = "/public";
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   124
            else if (lookupModes == (PUBLIC|PACKAGE))
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   125
                suffix = "/package";
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   126
            else if (lookupModes == (PUBLIC|PACKAGE|PRIVATE))
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   127
                suffix = "/private";
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   128
            else if (lookupModes == (PUBLIC|PACKAGE|PRIVATE|PROTECTED))
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   129
                suffix = "";
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   130
            else
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   131
                suffix = "/#"+Integer.toHexString(lookupModes);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   132
            return name+suffix;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   133
        }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   134
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   135
        /** Simulate all assertions from the spec. for Lookup.in:
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 19388
diff changeset
   136
         * <hr>
13044
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   137
         * Creates a lookup on the specified new lookup class.
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   138
         * [A1] The resulting object will report the specified
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   139
         * class as its own {@link #lookupClass lookupClass}.
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   140
         * <p>
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   141
         * [A2] However, the resulting {@code Lookup} object is guaranteed
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   142
         * to have no more access capabilities than the original.
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   143
         * In particular, access capabilities can be lost as follows:<ul>
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   144
         * <li>[A3] If the new lookup class differs from the old one,
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   145
         * protected members will not be accessible by virtue of inheritance.
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   146
         * (Protected members may continue to be accessible because of package sharing.)
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   147
         * <li>[A4] If the new lookup class is in a different package
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   148
         * than the old one, protected and default (package) members will not be accessible.
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   149
         * <li>[A5] If the new lookup class is not within the same package member
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   150
         * as the old one, private members will not be accessible.
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   151
         * <li>[A6] If the new lookup class is not accessible to the old lookup class,
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   152
         * using the original access modes,
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   153
         * then no members, not even public members, will be accessible.
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   154
         * [A7] (In all other cases, public members will continue to be accessible.)
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   155
         * </ul>
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   156
         * Other than the above cases, the new lookup will have the same
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   157
         * access capabilities as the original. [A8]
20535
cc85c8626435 8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents: 19388
diff changeset
   158
         * <hr>
13044
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   159
         */
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   160
        public LookupCase in(Class<?> c2) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   161
            Class<?> c1 = lookupClass();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   162
            int m1 = lookupModes();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   163
            int changed = 0;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   164
            boolean samePackage = (c1.getClassLoader() == c2.getClassLoader() &&
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   165
                                   packagePrefix(c1).equals(packagePrefix(c2)));
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   166
            boolean sameTopLevel = (topLevelClass(c1) == topLevelClass(c2));
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   167
            boolean sameClass = (c1 == c2);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   168
            assert(samePackage  || !sameTopLevel);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   169
            assert(sameTopLevel || !sameClass);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   170
            boolean accessible = sameClass;  // [A6]
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   171
            if ((m1 & PACKAGE) != 0)  accessible |= samePackage;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   172
            if ((m1 & PUBLIC ) != 0)  accessible |= (c2.getModifiers() & PUBLIC) != 0;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   173
            if (!accessible) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   174
                // Different package and no access to c2; lose all access.
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   175
                changed |= (PUBLIC|PACKAGE|PRIVATE|PROTECTED);  // [A6]
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   176
            }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   177
            if (!samePackage) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   178
                // Different package; lose PACKAGE and lower access.
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   179
                changed |= (PACKAGE|PRIVATE|PROTECTED);  // [A4]
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   180
            }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   181
            if (!sameTopLevel) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   182
                // Different top-level class.  Lose PRIVATE and lower access.
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   183
                changed |= (PRIVATE|PROTECTED);  // [A5]
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   184
            }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   185
            if (!sameClass) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   186
                changed |= (PROTECTED);     // [A3]
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   187
            } else {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   188
                assert(changed == 0);       // [A8] (no deprivation if same class)
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   189
            }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   190
            if (accessible)  assert((changed & PUBLIC) == 0);  // [A7]
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   191
            int m2 = m1 & ~changed;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   192
            LookupCase l2 = new LookupCase(c2, m2);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   193
            assert(l2.lookupClass() == c2); // [A1]
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   194
            assert((m1 | m2) == m1);        // [A2] (no elevation of access)
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   195
            return l2;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   196
        }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   197
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   198
        @Override
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   199
        public String toString() {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   200
            String s = lookupClass().getSimpleName();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   201
            String lstr = lookupString();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   202
            int sl = lstr.indexOf('/');
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   203
            if (sl >= 0)  s += lstr.substring(sl);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   204
            ClassLoader cld = lookupClass().getClassLoader();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   205
            if (cld != THIS_LOADER)  s += "/loader#"+numberOf(cld);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   206
            return s;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   207
        }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   208
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   209
        /** Predict the success or failure of accessing this method. */
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   210
        public boolean willAccess(Method m) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   211
            Class<?> c1 = lookupClass();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   212
            Class<?> c2 = m.getDeclaringClass();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   213
            LookupCase lc = this.in(c2);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   214
            int m1 = lc.lookupModes();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   215
            int m2 = fixMods(m.getModifiers());
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   216
            // privacy is strictly enforced on lookups
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   217
            if (c1 != c2)  m1 &= ~PRIVATE;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   218
            // protected access is sometimes allowed
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   219
            if ((m2 & PROTECTED) != 0) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   220
                int prev = m2;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   221
                m2 |= PACKAGE;  // it acts like a package method also
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   222
                if ((lookupModes() & PROTECTED) != 0 &&
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   223
                    c2.isAssignableFrom(c1))
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   224
                    m2 |= PUBLIC;  // from a subclass, it acts like a public method also
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   225
            }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   226
            if (verbosity >= 2)
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   227
                System.out.println(this+" willAccess "+lc+" m1="+m1+" m2="+m2+" => "+((m2 & m1) != 0));
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   228
            return (m2 & m1) != 0;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   229
        }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   230
    }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   231
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   232
    private static Class<?> topLevelClass(Class<?> cls) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   233
        Class<?> c = cls;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   234
        for (Class<?> ec; (ec = c.getEnclosingClass()) != null; )
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   235
            c = ec;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   236
        assert(c.getEnclosingClass() == null);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   237
        assert(c == cls || cls.getEnclosingClass() != null);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   238
        return c;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   239
    }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   240
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   241
    private static String packagePrefix(Class<?> c) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   242
        while (c.isArray())  c = c.getComponentType();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   243
        String s = c.getName();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   244
        assert(s.indexOf('/') < 0);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   245
        return s.substring(0, s.lastIndexOf('.')+1);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   246
    }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   247
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   248
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   249
    private final TreeSet<LookupCase> CASES = new TreeSet<>();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   250
    private final TreeMap<LookupCase,TreeSet<LookupCase>> CASE_EDGES = new TreeMap<>();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   251
    private final ArrayList<ClassLoader> LOADERS = new ArrayList<>();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   252
    private final ClassLoader THIS_LOADER = this.getClass().getClassLoader();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   253
    { if (THIS_LOADER != null)  LOADERS.add(THIS_LOADER); }  // #1
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   254
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   255
    private LookupCase lookupCase(String name) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   256
        for (LookupCase lc : CASES) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   257
            if (lc.toString().equals(name))
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   258
                return lc;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   259
        }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   260
        throw new AssertionError(name);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   261
    }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   262
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   263
    private int numberOf(ClassLoader cl) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   264
        if (cl == null)  return 0;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   265
        int i = LOADERS.indexOf(cl);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   266
        if (i < 0) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   267
            i = LOADERS.size();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   268
            LOADERS.add(cl);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   269
        }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   270
        return i+1;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   271
    }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   272
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   273
    private void addLookupEdge(LookupCase l1, Class<?> c2, LookupCase l2) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   274
        TreeSet<LookupCase> edges = CASE_EDGES.get(l2);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   275
        if (edges == null)  CASE_EDGES.put(l2, edges = new TreeSet<>());
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   276
        if (edges.add(l1)) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   277
            Class<?> c1 = l1.lookupClass();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   278
            assert(l2.lookupClass() == c2); // [A1]
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   279
            int m1 = l1.lookupModes();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   280
            int m2 = l2.lookupModes();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   281
            assert((m1 | m2) == m1);        // [A2] (no elevation of access)
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   282
            LookupCase expect = l1.in(c2);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   283
            if (!expect.equals(l2))
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   284
                System.out.println("*** expect "+l1+" => "+expect+" but got "+l2);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   285
            assertEquals(expect, l2);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   286
        }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   287
    }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   288
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   289
    private void makeCases(Lookup[] originalLookups) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   290
        // make initial set of lookup test cases
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   291
        CASES.clear(); LOADERS.clear(); CASE_EDGES.clear();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   292
        ArrayList<Class<?>> classes = new ArrayList<>();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   293
        for (Lookup l : originalLookups) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   294
            CASES.add(new LookupCase(l));
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   295
            classes.remove(l.lookupClass());  // no dups please
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   296
            classes.add(l.lookupClass());
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   297
        }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   298
        System.out.println("loaders = "+LOADERS);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   299
        int rounds = 0;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   300
        for (int lastCount = -1; lastCount != CASES.size(); ) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   301
            lastCount = CASES.size();  // if CASES grow in the loop we go round again
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   302
            for (LookupCase lc1 : CASES.toArray(new LookupCase[0])) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   303
                for (Class<?> c2 : classes) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   304
                    LookupCase lc2 = new LookupCase(lc1.lookup().in(c2));
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   305
                    addLookupEdge(lc1, c2, lc2);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   306
                    CASES.add(lc2);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   307
                }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   308
            }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   309
            rounds++;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   310
        }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   311
        System.out.println("filled in "+CASES.size()+" cases from "+originalLookups.length+" original cases in "+rounds+" rounds");
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   312
        if (false) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   313
            System.out.println("CASES: {");
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   314
            for (LookupCase lc : CASES) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   315
                System.out.println(lc);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   316
                Set<LookupCase> edges = CASE_EDGES.get(lc);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   317
                if (edges != null)
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   318
                    for (LookupCase prev : edges) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   319
                        System.out.println("\t"+prev);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   320
                    }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   321
            }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   322
            System.out.println("}");
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   323
        }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   324
    }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   325
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   326
    @Test public void test() {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   327
        makeCases(lookups());
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   328
        if (verbosity > 0) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   329
            verbosity += 9;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   330
            Method pro_in_self = targetMethod(THIS_CLASS, PROTECTED, methodType(void.class));
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   331
            testOneAccess(lookupCase("AccessControlTest/public"),  pro_in_self, "find");
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   332
            testOneAccess(lookupCase("Remote_subclass/public"),    pro_in_self, "find");
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   333
            testOneAccess(lookupCase("Remote_subclass"),           pro_in_self, "find");
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   334
            verbosity -= 9;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   335
        }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   336
        Set<Class<?>> targetClassesDone = new HashSet<>();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   337
        for (LookupCase targetCase : CASES) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   338
            Class<?> targetClass = targetCase.lookupClass();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   339
            if (!targetClassesDone.add(targetClass))  continue;  // already saw this one
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   340
            String targetPlace = placeName(targetClass);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   341
            if (targetPlace == null)  continue;  // Object, String, not a target
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   342
            for (int targetAccess : ACCESS_CASES) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   343
                MethodType methodType = methodType(void.class);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   344
                Method method = targetMethod(targetClass, targetAccess, methodType);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   345
                // Try to access target method from various contexts.
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   346
                for (LookupCase sourceCase : CASES) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   347
                    testOneAccess(sourceCase, method, "find");
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   348
                    testOneAccess(sourceCase, method, "unreflect");
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   349
                }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   350
            }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   351
        }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   352
        System.out.println("tested "+testCount+" access scenarios; "+testCountFails+" accesses were denied");
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   353
    }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   354
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   355
    private int testCount, testCountFails;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   356
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   357
    private void testOneAccess(LookupCase sourceCase, Method method, String kind) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   358
        Class<?> targetClass = method.getDeclaringClass();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   359
        String methodName = method.getName();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   360
        MethodType methodType = methodType(method.getReturnType(), method.getParameterTypes());
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   361
        boolean willAccess = sourceCase.willAccess(method);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   362
        boolean didAccess = false;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   363
        ReflectiveOperationException accessError = null;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   364
        try {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   365
            switch (kind) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   366
            case "find":
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   367
                if ((method.getModifiers() & Modifier.STATIC) != 0)
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   368
                    sourceCase.lookup().findStatic(targetClass, methodName, methodType);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   369
                else
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   370
                    sourceCase.lookup().findVirtual(targetClass, methodName, methodType);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   371
                break;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   372
            case "unreflect":
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   373
                sourceCase.lookup().unreflect(method);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   374
                break;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   375
            default:
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   376
                throw new AssertionError(kind);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   377
            }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   378
            didAccess = true;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   379
        } catch (ReflectiveOperationException ex) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   380
            accessError = ex;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   381
        }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   382
        if (willAccess != didAccess) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   383
            System.out.println(sourceCase+" => "+targetClass.getSimpleName()+"."+methodName+methodType);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   384
            System.out.println("fail on "+method+" ex="+accessError);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   385
            assertEquals(willAccess, didAccess);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   386
        }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   387
        testCount++;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   388
        if (!didAccess)  testCountFails++;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   389
    }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   390
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   391
    static Method targetMethod(Class<?> targetClass, int targetAccess, MethodType methodType) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   392
        String methodName = accessName(targetAccess)+placeName(targetClass);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   393
        if (verbosity >= 2)
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   394
            System.out.println(targetClass.getSimpleName()+"."+methodName+methodType);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   395
        try {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   396
            Method method = targetClass.getDeclaredMethod(methodName, methodType.parameterArray());
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   397
            assertEquals(method.getReturnType(), methodType.returnType());
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   398
            int haveMods = method.getModifiers();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   399
            assert(Modifier.isStatic(haveMods));
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   400
            assert(targetAccess == fixMods(haveMods));
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   401
            return method;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   402
        } catch (NoSuchMethodException ex) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   403
            throw new AssertionError(methodName, ex);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   404
        }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   405
    }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   406
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   407
    static String placeName(Class<?> cls) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   408
        // return "self", "sibling", "nestmate", etc.
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   409
        if (cls == AccessControlTest.class)  return "self";
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   410
        String cln = cls.getSimpleName();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   411
        int under = cln.lastIndexOf('_');
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   412
        if (under < 0)  return null;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   413
        return cln.substring(under+1);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   414
    }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   415
    static String accessName(int acc) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   416
        switch (acc) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   417
        case PUBLIC:     return "pub_in_";
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   418
        case PROTECTED:  return "pro_in_";
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   419
        case PACKAGE:    return "pkg_in_";
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   420
        case PRIVATE:    return "pri_in_";
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   421
        }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   422
        assert(false);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   423
        return "?";
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   424
    }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   425
    private static final int[] ACCESS_CASES = {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   426
        PUBLIC, PACKAGE, PRIVATE, PROTECTED
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   427
    };
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   428
    /** Return one of the ACCESS_CASES. */
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   429
    static int fixMods(int mods) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   430
        mods &= (PUBLIC|PRIVATE|PROTECTED);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   431
        switch (mods) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   432
        case PUBLIC: case PRIVATE: case PROTECTED: return mods;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   433
        case 0:  return PACKAGE;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   434
        }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   435
        throw new AssertionError(mods);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   436
    }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   437
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   438
    static Lookup[] lookups() {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   439
        ArrayList<Lookup> tem = new ArrayList<>();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   440
        Collections.addAll(tem,
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   441
                           AccessControlTest.lookup_in_self(),
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   442
                           Inner_nestmate.lookup_in_nestmate(),
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   443
                           AccessControlTest_sibling.lookup_in_sibling());
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   444
        if (true) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   445
            Collections.addAll(tem,Acquaintance_remote.lookups());
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   446
        } else {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   447
            try {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   448
                Class<?> remc = Class.forName("test.java.lang.invoke.AccessControlTest_subpkg.Acquaintance_remote");
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   449
                Lookup[] remls = (Lookup[]) remc.getMethod("lookups").invoke(null);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   450
                Collections.addAll(tem, remls);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   451
            } catch (ReflectiveOperationException ex) {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   452
                throw new LinkageError("reflection failed", ex);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   453
            }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   454
        }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   455
        tem.add(publicLookup());
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   456
        tem.add(publicLookup().in(String.class));
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   457
        tem.add(publicLookup().in(List.class));
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   458
        return tem.toArray(new Lookup[0]);
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   459
    }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   460
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   461
    static Lookup lookup_in_self() {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   462
        return MethodHandles.lookup();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   463
    }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   464
    static public      void pub_in_self() { }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   465
    static protected   void pro_in_self() { }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   466
    static /*package*/ void pkg_in_self() { }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   467
    static private     void pri_in_self() { }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   468
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   469
    static class Inner_nestmate {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   470
        static Lookup lookup_in_nestmate() {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   471
            return MethodHandles.lookup();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   472
        }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   473
        static public      void pub_in_nestmate() { }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   474
        static protected   void pro_in_nestmate() { }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   475
        static /*package*/ void pkg_in_nestmate() { }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   476
        static private     void pri_in_nestmate() { }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   477
    }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   478
}
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   479
class AccessControlTest_sibling {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   480
    static Lookup lookup_in_sibling() {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   481
        return MethodHandles.lookup();
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   482
    }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   483
    static public      void pub_in_sibling() { }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   484
    static protected   void pro_in_sibling() { }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   485
    static /*package*/ void pkg_in_sibling() { }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   486
    static private     void pri_in_sibling() { }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   487
}
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   488
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   489
// This guy tests access from outside the package:
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   490
/*
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   491
package test.java.lang.invoke.AccessControlTest_subpkg;
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   492
public class Acquaintance_remote {
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   493
    public static Lookup[] lookups() { ...
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   494
    }
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   495
    ...
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   496
}
8411854afc2b 7165628: Issues with java.lang.invoke.MethodHandles.Lookup
jrose
parents:
diff changeset
   497
*/