test/jdk/java/lang/invoke/VarHandles/accessibility/TestFieldLookupAccessibility.java
author afarley
Tue, 26 Mar 2019 15:53:36 -0700
changeset 54295 49c4b23d8d0a
parent 47216 71c04702a3d5
permissions -rw-r--r--
8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36974
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
     1
/*
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
     4
 *
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
     7
 * published by the Free Software Foundation.
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
     8
 *
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    13
 * accompanied this code).
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    14
 *
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    18
 *
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    21
 * questions.
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    22
 */
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    23
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    24
/* @test
54295
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
    25
 * @bug 8152645 8216558
36974
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    26
 * @summary test field lookup accessibility of MethodHandles and VarHandles
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    27
 * @compile TestFieldLookupAccessibility.java
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    28
 *          pkg/A.java pkg/B_extends_A.java pkg/C.java
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    29
 *          pkg/subpkg/B_extends_A.java pkg/subpkg/C.java
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    30
 * @run testng/othervm TestFieldLookupAccessibility
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    31
 */
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    32
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    33
import org.testng.Assert;
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    34
import org.testng.annotations.DataProvider;
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    35
import org.testng.annotations.Test;
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    36
import pkg.B_extends_A;
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    37
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    38
import java.lang.invoke.MethodHandles;
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    39
import java.lang.reflect.Field;
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    40
import java.lang.reflect.Modifier;
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    41
import java.util.ArrayList;
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    42
import java.util.HashMap;
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    43
import java.util.HashSet;
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    44
import java.util.List;
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    45
import java.util.Map;
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    46
import java.util.Set;
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    47
import java.util.stream.Collectors;
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    48
import java.util.stream.Stream;
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    49
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    50
public class TestFieldLookupAccessibility {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    51
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    52
    // The set of possible field lookup mechanisms
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    53
    enum FieldLookup {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    54
        MH_GETTER() {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    55
            Object lookup(MethodHandles.Lookup l, Field f) throws Exception {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    56
                return l.findGetter(f.getDeclaringClass(), f.getName(), f.getType());
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    57
            }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    58
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    59
            boolean isAccessible(Field f) {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    60
                return !Modifier.isStatic(f.getModifiers());
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    61
            }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    62
        },
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    63
        MH_SETTER() {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    64
            Object lookup(MethodHandles.Lookup l, Field f) throws Exception {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    65
                return l.findSetter(f.getDeclaringClass(), f.getName(), f.getType());
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    66
            }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    67
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    68
            boolean isAccessible(Field f) {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    69
                return !Modifier.isStatic(f.getModifiers()) && !Modifier.isFinal(f.getModifiers());
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    70
            }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    71
        },
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    72
        MH_STATIC_GETTER() {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    73
            Object lookup(MethodHandles.Lookup l, Field f) throws Exception {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    74
                return l.findStaticGetter(f.getDeclaringClass(), f.getName(), f.getType());
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    75
            }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    76
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    77
            boolean isAccessible(Field f) {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    78
                return Modifier.isStatic(f.getModifiers());
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    79
            }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    80
        },
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    81
        MH_STATIC_SETTER() {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    82
            Object lookup(MethodHandles.Lookup l, Field f) throws Exception {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    83
                return l.findStaticSetter(f.getDeclaringClass(), f.getName(), f.getType());
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    84
            }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    85
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    86
            boolean isAccessible(Field f) {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    87
                return Modifier.isStatic(f.getModifiers()) && !Modifier.isFinal(f.getModifiers());
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    88
            }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    89
        },
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    90
        MH_UNREFLECT_GETTER() {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    91
            Object lookup(MethodHandles.Lookup l, Field f) throws Exception {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    92
                return l.unreflectGetter(f);
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    93
            }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    94
        },
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    95
        MH_UNREFLECT_GETTER_ACCESSIBLE() {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    96
            Object lookup(MethodHandles.Lookup l, Field f) throws Exception {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    97
                return l.unreflectGetter(cloneAndSetAccessible(f));
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
    98
            }
54295
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
    99
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
   100
            // Setting the accessibility bit of a Field grants access under
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
   101
            // all conditions for MethodHandle getters.
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
   102
            Set<String> inaccessibleFields(Set<String> inaccessibleFields) {
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
   103
                return new HashSet<>();
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
   104
            }
36974
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   105
        },
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   106
        MH_UNREFLECT_SETTER() {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   107
            Object lookup(MethodHandles.Lookup l, Field f) throws Exception {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   108
                return l.unreflectSetter(f);
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   109
            }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   110
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   111
            boolean isAccessible(Field f) {
54295
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
   112
                return f.isAccessible() && !Modifier.isStatic(f.getModifiers()) || !Modifier.isFinal(f.getModifiers());
36974
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   113
            }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   114
        },
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   115
        MH_UNREFLECT_SETTER_ACCESSIBLE() {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   116
            Object lookup(MethodHandles.Lookup l, Field f) throws Exception {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   117
                return l.unreflectSetter(cloneAndSetAccessible(f));
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   118
            }
54295
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
   119
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
   120
            boolean isAccessible(Field f) {
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
   121
                return !(Modifier.isStatic(f.getModifiers()) && Modifier.isFinal(f.getModifiers()));
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
   122
            }
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
   123
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
   124
            // Setting the accessibility bit of a Field grants access to non-static
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
   125
            // final fields for MethodHandle setters.
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
   126
            Set<String> inaccessibleFields(Set<String>inaccessibleFields) {
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
   127
                Set<String> result = new HashSet<>();
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
   128
                inaccessibleFields.stream()
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
   129
                                  .filter(f -> (f.contains("static") && f.contains("final")))
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
   130
                                  .forEach(result::add);
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
   131
                return result;
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
   132
            }
36974
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   133
        },
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   134
        VH() {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   135
            Object lookup(MethodHandles.Lookup l, Field f) throws Exception {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   136
                return l.findVarHandle(f.getDeclaringClass(), f.getName(), f.getType());
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   137
            }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   138
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   139
            boolean isAccessible(Field f) {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   140
                return !Modifier.isStatic(f.getModifiers());
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   141
            }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   142
        },
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   143
        VH_STATIC() {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   144
            Object lookup(MethodHandles.Lookup l, Field f) throws Exception {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   145
                return l.findStaticVarHandle(f.getDeclaringClass(), f.getName(), f.getType());
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   146
            }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   147
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   148
            boolean isAccessible(Field f) {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   149
                return Modifier.isStatic(f.getModifiers());
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   150
            }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   151
        },
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   152
        VH_UNREFLECT() {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   153
            Object lookup(MethodHandles.Lookup l, Field f) throws Exception {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   154
                return l.unreflectVarHandle(f);
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   155
            }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   156
        };
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   157
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   158
        // Look up a handle to a field
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   159
        abstract Object lookup(MethodHandles.Lookup l, Field f) throws Exception;
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   160
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   161
        boolean isAccessible(Field f) {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   162
            return true;
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   163
        }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   164
54295
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
   165
        Set<String> inaccessibleFields(Set<String> inaccessibleFields) {
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
   166
            return new HashSet<>(inaccessibleFields);
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
   167
        }
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
   168
36974
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   169
        static Field cloneAndSetAccessible(Field f) throws Exception {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   170
            // Clone to avoid mutating source field
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   171
            f = f.getDeclaringClass().getDeclaredField(f.getName());
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   172
            f.setAccessible(true);
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   173
            return f;
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   174
        }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   175
    }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   176
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   177
    @DataProvider
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   178
    public Object[][] lookupProvider() throws Exception {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   179
        Stream<List<Object>> baseCases = Stream.of(
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   180
                // Look up from same package
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   181
                List.of(pkg.A.class, pkg.A.lookup(), pkg.A.inaccessibleFields()),
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   182
                List.of(pkg.A.class, pkg.A.lookup(), pkg.A.inaccessibleFields()),
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   183
                List.of(pkg.A.class, B_extends_A.lookup(), B_extends_A.inaccessibleFields()),
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   184
                List.of(pkg.A.class, pkg.C.lookup(), pkg.C.inaccessibleFields()),
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   185
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   186
                // Look up from sub-package
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   187
                List.of(pkg.A.class, pkg.subpkg.B_extends_A.lookup(), pkg.subpkg.B_extends_A.inaccessibleFields()),
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   188
                List.of(pkg.A.class, pkg.subpkg.C.lookup(), pkg.subpkg.C.inaccessibleFields())
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   189
        );
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   190
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   191
        // Cross product base cases with the field lookup classes
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   192
        return baseCases.
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   193
                flatMap(l -> Stream.of(FieldLookup.values()).map(fl -> prepend(fl, l))).
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   194
                toArray(Object[][]::new);
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   195
    }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   196
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   197
    private static Object[] prepend(Object o, List<Object> l) {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   198
        List<Object> pl = new ArrayList<>();
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   199
        pl.add(o);
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   200
        pl.addAll(l);
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   201
        return pl.toArray();
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   202
    }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   203
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   204
    @Test(dataProvider = "lookupProvider")
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   205
    public void test(FieldLookup fl, Class<?> src, MethodHandles.Lookup l, Set<String> inaccessibleFields) {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   206
        // Add to the expected failures all inaccessible fields due to accessibility modifiers
54295
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
   207
        Set<String> expected = fl.inaccessibleFields(inaccessibleFields);
36974
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   208
        Map<Field, Throwable> actual = new HashMap<>();
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   209
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   210
        for (Field f : fields(src)) {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   211
            // Add to the expected failures all inaccessible fields due to static/final modifiers
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   212
            if (!fl.isAccessible(f)) {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   213
                expected.add(f.getName());
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   214
            }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   215
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   216
            try {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   217
                fl.lookup(l, f);
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   218
            }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   219
            catch (Throwable t) {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   220
                // Lookup failed, add to the actual failures
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   221
                actual.put(f, t);
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   222
            }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   223
        }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   224
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   225
        Set<String> actualFieldNames = actual.keySet().stream().map(Field::getName).
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   226
                collect(Collectors.toSet());
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   227
        if (!actualFieldNames.equals(expected)) {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   228
            if (actualFieldNames.isEmpty()) {
54295
49c4b23d8d0a 8216558: Lookup.unreflectSetter(Field) fails to throw IllegalAccessException for final fields
afarley
parents: 47216
diff changeset
   229
                Assert.assertEquals(actualFieldNames, expected, "No accessibility failures:");
36974
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   230
            }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   231
            else {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   232
                Assert.assertEquals(actualFieldNames, expected, "Accessibility failures differ:");
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   233
            }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   234
        }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   235
        else {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   236
            if (!actual.values().stream().allMatch(IllegalAccessException.class::isInstance)) {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   237
                Assert.fail("Expecting an IllegalArgumentException for all failures " + actual);
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   238
            }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   239
        }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   240
    }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   241
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   242
    static List<Field> fields(Class<?> src) {
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   243
        return List.of(src.getDeclaredFields());
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   244
    }
86f49af093e5 8152645: VarHandle lookup access control tests
psandoz
parents:
diff changeset
   245
}