jdk/test/java/lang/Class/getDeclaredField/FieldSetAccessibleTest.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 31673 135283550686
child 36511 9d0388c6b336
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28066
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
     1
/*
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
     4
 *
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
     8
 *
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    13
 * accompanied this code).
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    14
 *
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    18
 *
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    21
 * questions.
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    22
 */
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    23
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    24
import java.io.FilePermission;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    25
import java.io.IOException;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    26
import java.lang.reflect.Field;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    27
import java.lang.reflect.ReflectPermission;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    28
import java.net.URI;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    29
import java.nio.file.FileSystem;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    30
import java.nio.file.FileSystems;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    31
import java.nio.file.Files;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    32
import java.nio.file.Path;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    33
import java.security.CodeSource;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    34
import java.security.Permission;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    35
import java.security.PermissionCollection;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    36
import java.security.Permissions;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    37
import java.security.Policy;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    38
import java.security.ProtectionDomain;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    39
import java.util.ArrayList;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    40
import java.util.Arrays;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    41
import java.util.Collections;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    42
import java.util.Enumeration;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    43
import java.util.Iterator;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    44
import java.util.List;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    45
import java.util.PropertyPermission;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    46
import java.util.concurrent.atomic.AtomicBoolean;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    47
import java.util.concurrent.atomic.AtomicLong;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    48
import java.util.stream.Stream;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    49
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    50
/**
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    51
 * @test
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    52
 * @bug 8065552
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    53
 * @summary test that all fields returned by getDeclaredFields() can be
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    54
 *          set accessible if the right permission is granted; this test
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    55
 *          loads all the classes in the BCL, get their declared fields,
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    56
 *          and call setAccessible(false) followed by setAccessible(true);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    57
 * @run main/othervm FieldSetAccessibleTest UNSECURE
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    58
 * @run main/othervm FieldSetAccessibleTest SECURE
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    59
 *
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    60
 * @author danielfuchs
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    61
 */
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    62
public class FieldSetAccessibleTest {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    63
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    64
    static final List<String> skipped = new ArrayList<>();
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    65
    static final List<String> cantread = new ArrayList<>();
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    66
    static final List<String> failed = new ArrayList<>();
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    67
    static final AtomicLong classCount = new AtomicLong();
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    68
    static final AtomicLong fieldCount = new AtomicLong();
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    69
    static long startIndex = 0;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    70
    static long maxSize = Long.MAX_VALUE;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    71
    static long maxIndex = Long.MAX_VALUE;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    72
    static final ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    73
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    74
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    75
    // Test that all fields for any given class can be made accessibles
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    76
    static void testSetFieldsAccessible(Class<?> c) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    77
        for (Field f : c.getDeclaredFields()) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    78
            fieldCount.incrementAndGet();
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    79
            f.setAccessible(false);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    80
            f.setAccessible(true);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    81
        }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    82
    }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    83
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    84
    // Performs a series of test on the given class.
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    85
    // At this time, we only call testSetFieldsAccessible(c)
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    86
    public static boolean test(Class<?> c) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    87
        //System.out.println(c.getName());
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    88
        classCount.incrementAndGet();
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    89
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    90
        // Call getDeclaredFields() and try to set their accessible flag.
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    91
        testSetFieldsAccessible(c);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    92
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    93
        // add more tests here...
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    94
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    95
        return c == Class.class;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    96
    }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    97
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    98
    // Prints a summary at the end of the test.
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
    99
    static void printSummary(long secs, long millis, long nanos) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   100
        System.out.println("Tested " + fieldCount.get() + " fields of "
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   101
                + classCount.get() + " classes in "
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   102
                + secs + "s " + millis + "ms " + nanos + "ns");
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   103
    }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   104
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   105
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   106
    /**
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   107
     * @param args the command line arguments:
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   108
     *
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   109
     *     SECURE|UNSECURE [startIndex (default=0)] [maxSize (default=Long.MAX_VALUE)]
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   110
     *
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   111
     * @throws java.lang.Exception if the test fails
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   112
     */
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   113
    public static void main(String[] args) throws Exception {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   114
        if (args == null || args.length == 0) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   115
            args = new String[] {"SECURE", "0"};
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   116
        } else if (args.length > 3) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   117
            throw new RuntimeException("Expected at most one argument. Found "
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   118
                    + Arrays.asList(args));
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   119
        }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   120
        try {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   121
            if (args.length > 1) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   122
                startIndex = Long.parseLong(args[1]);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   123
                if (startIndex < 0) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   124
                    throw new IllegalArgumentException("startIndex args[1]: "
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   125
                            + startIndex);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   126
                }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   127
            }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   128
            if (args.length > 2) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   129
                maxSize = Long.parseLong(args[2]);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   130
                if (maxSize <= 0) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   131
                    maxSize = Long.MAX_VALUE;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   132
                }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   133
                maxIndex = (Long.MAX_VALUE - startIndex) < maxSize
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   134
                        ? Long.MAX_VALUE : startIndex + maxSize;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   135
            }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   136
            TestCase.valueOf(args[0]).run();
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   137
        } catch (OutOfMemoryError oome) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   138
            System.err.println(classCount.get());
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   139
            throw oome;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   140
        }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   141
    }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   142
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   143
    public static void run(TestCase test) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   144
        System.out.println("Testing " + test);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   145
        test(listAllClassNames());
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   146
        System.out.println("Passed " + test);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   147
    }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   148
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   149
    static Iterable<String> listAllClassNames() {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   150
        return new ClassNameJrtStreamBuilder();
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   151
    }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   152
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   153
    static void test(Iterable<String> iterable) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   154
        final long start = System.nanoTime();
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   155
        boolean classFound = false;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   156
        int index = 0;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   157
        for (String s: iterable) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   158
            if (index == maxIndex) break;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   159
            try {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   160
                if (index < startIndex) continue;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   161
                if (test(s)) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   162
                    classFound = true;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   163
                }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   164
            } finally {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   165
                index++;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   166
            }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   167
        }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   168
        long elapsed = System.nanoTime() - start;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   169
        long secs = elapsed / 1000_000_000;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   170
        long millis = (elapsed % 1000_000_000) / 1000_000;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   171
        long nanos  = elapsed % 1000_000;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   172
        System.out.println("Unreadable path elements: " + cantread);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   173
        System.out.println("Skipped path elements: " + skipped);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   174
        System.out.println("Failed path elements: " + failed);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   175
        printSummary(secs, millis, nanos);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   176
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   177
        if (!failed.isEmpty()) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   178
            throw new RuntimeException("Test failed for the following classes: " + failed);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   179
        }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   180
        if (!classFound && startIndex == 0 && index < maxIndex) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   181
            // this is just to verify that we have indeed parsed rt.jar
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   182
            // (or the java.base module)
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   183
            throw  new RuntimeException("Test failed: Class.class not found...");
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   184
        }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   185
        if (classCount.get() == 0 && startIndex == 0) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   186
            throw  new RuntimeException("Test failed: no class found?");
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   187
        }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   188
    }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   189
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   190
    static boolean test(String s) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   191
        try {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   192
            if (s.startsWith("WrapperGenerator")) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   193
                System.out.println("Skipping "+ s);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   194
                return false;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   195
            }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   196
            final Class<?> c = Class.forName(
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   197
                    s.replace('/', '.').substring(0, s.length() - 6),
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   198
                    false,
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   199
                    systemClassLoader);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   200
            return test(c);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   201
        } catch (Exception t) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   202
            t.printStackTrace(System.err);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   203
            failed.add(s);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   204
        } catch (NoClassDefFoundError e) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   205
            e.printStackTrace(System.err);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   206
            failed.add(s);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   207
        }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   208
        return false;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   209
    }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   210
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   211
    static class ClassNameJrtStreamBuilder implements Iterable<String>{
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   212
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   213
        final FileSystem jrt;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   214
        final List<Path> roots = new ArrayList<>();
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   215
        ClassNameJrtStreamBuilder() {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   216
             jrt = FileSystems.getFileSystem(URI.create("jrt:/"));
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   217
             for (Path root : jrt.getRootDirectories()) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   218
                 roots.add(root);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   219
             }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   220
        }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   221
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   222
        Stream<String> build() {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   223
            return roots.stream().flatMap(this::toStream)
31673
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 28066
diff changeset
   224
                    .filter(x -> x.getNameCount() > 2)
135283550686 8080511: Refresh of jimage support
jfdenise
parents: 28066
diff changeset
   225
                    .map( x-> x.subpath(2, x.getNameCount()))
28066
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   226
                    .map( x -> x.toString())
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   227
                    .filter(s -> s.endsWith(".class"));
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   228
        }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   229
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   230
        @Override
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   231
        public Iterator<String> iterator() {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   232
            return build().iterator();
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   233
        }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   234
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   235
        private Stream<Path> toStream(Path root) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   236
            try {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   237
                return Files.walk(root);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   238
            } catch(IOException x) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   239
                x.printStackTrace(System.err);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   240
                skipped.add(root.toString());
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   241
            }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   242
            return Collections.<Path>emptyList().stream();
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   243
        }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   244
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   245
    }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   246
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   247
    // Test with or without a security manager
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   248
    public static enum TestCase {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   249
        UNSECURE, SECURE;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   250
        public void run() throws Exception {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   251
            System.out.println("Running test case: " + name());
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   252
            Configure.setUp(this);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   253
            FieldSetAccessibleTest.run(this);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   254
        }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   255
    }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   256
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   257
    // A helper class to configure the security manager for the test,
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   258
    // and bypass it when needed.
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   259
    static class Configure {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   260
        static Policy policy = null;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   261
        static final ThreadLocal<AtomicBoolean> allowAll = new ThreadLocal<AtomicBoolean>() {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   262
            @Override
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   263
            protected AtomicBoolean initialValue() {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   264
                return  new AtomicBoolean(false);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   265
            }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   266
        };
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   267
        static void setUp(TestCase test) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   268
            switch (test) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   269
                case SECURE:
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   270
                    if (policy == null && System.getSecurityManager() != null) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   271
                        throw new IllegalStateException("SecurityManager already set");
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   272
                    } else if (policy == null) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   273
                        policy = new SimplePolicy(TestCase.SECURE, allowAll);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   274
                        Policy.setPolicy(policy);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   275
                        System.setSecurityManager(new SecurityManager());
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   276
                    }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   277
                    if (System.getSecurityManager() == null) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   278
                        throw new IllegalStateException("No SecurityManager.");
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   279
                    }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   280
                    if (policy == null) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   281
                        throw new IllegalStateException("policy not configured");
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   282
                    }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   283
                    break;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   284
                case UNSECURE:
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   285
                    if (System.getSecurityManager() != null) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   286
                        throw new IllegalStateException("SecurityManager already set");
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   287
                    }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   288
                    break;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   289
                default:
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   290
                    throw new InternalError("No such testcase: " + test);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   291
            }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   292
        }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   293
        static void doPrivileged(Runnable run) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   294
            allowAll.get().set(true);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   295
            try {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   296
                run.run();
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   297
            } finally {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   298
                allowAll.get().set(false);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   299
            }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   300
        }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   301
    }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   302
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   303
    // A Helper class to build a set of permissions.
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31673
diff changeset
   304
    static final class PermissionsBuilder {
28066
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   305
        final Permissions perms;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   306
        public PermissionsBuilder() {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   307
            this(new Permissions());
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   308
        }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   309
        public PermissionsBuilder(Permissions perms) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   310
            this.perms = perms;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   311
        }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   312
        public PermissionsBuilder add(Permission p) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   313
            perms.add(p);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   314
            return this;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   315
        }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   316
        public PermissionsBuilder addAll(PermissionCollection col) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   317
            if (col != null) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   318
                for (Enumeration<Permission> e = col.elements(); e.hasMoreElements(); ) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   319
                    perms.add(e.nextElement());
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   320
                }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   321
            }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   322
            return this;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   323
        }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   324
        public Permissions toPermissions() {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   325
            final PermissionsBuilder builder = new PermissionsBuilder();
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   326
            builder.addAll(perms);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   327
            return builder.perms;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   328
        }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   329
    }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   330
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   331
    // Policy for the test...
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   332
    public static class SimplePolicy extends Policy {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   333
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   334
        final Permissions permissions;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   335
        final Permissions allPermissions;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   336
        final ThreadLocal<AtomicBoolean> allowAll;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   337
        public SimplePolicy(TestCase test, ThreadLocal<AtomicBoolean> allowAll) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   338
            this.allowAll = allowAll;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   339
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   340
            // Permission needed by the tested code exercised in the test
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   341
            permissions = new Permissions();
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   342
            permissions.add(new RuntimePermission("fileSystemProvider"));
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   343
            permissions.add(new RuntimePermission("createClassLoader"));
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   344
            permissions.add(new RuntimePermission("closeClassLoader"));
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   345
            permissions.add(new RuntimePermission("getClassLoader"));
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   346
            permissions.add(new RuntimePermission("accessDeclaredMembers"));
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   347
            permissions.add(new ReflectPermission("suppressAccessChecks"));
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   348
            permissions.add(new PropertyPermission("*", "read"));
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   349
            permissions.add(new FilePermission("<<ALL FILES>>", "read"));
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   350
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   351
            // these are used for configuring the test itself...
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   352
            allPermissions = new Permissions();
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   353
            allPermissions.add(new java.security.AllPermission());
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   354
        }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   355
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   356
        @Override
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   357
        public boolean implies(ProtectionDomain domain, Permission permission) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   358
            if (allowAll.get().get()) return allPermissions.implies(permission);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   359
            if (permissions.implies(permission)) return true;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   360
            if (permission instanceof java.lang.RuntimePermission) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   361
                if (permission.getName().startsWith("accessClassInPackage.")) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   362
                    // add these along to the set of permission we have, when we
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   363
                    // discover that we need them.
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   364
                    permissions.add(permission);
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   365
                    return true;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   366
                }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   367
            }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   368
            return false;
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   369
        }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   370
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   371
        @Override
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   372
        public PermissionCollection getPermissions(CodeSource codesource) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   373
            return new PermissionsBuilder().addAll(allowAll.get().get()
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   374
                    ? allPermissions : permissions).toPermissions();
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   375
        }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   376
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   377
        @Override
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   378
        public PermissionCollection getPermissions(ProtectionDomain domain) {
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   379
            return new PermissionsBuilder().addAll(allowAll.get().get()
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   380
                    ? allPermissions : permissions).toPermissions();
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   381
        }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   382
    }
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   383
b0d490c01280 8066612: Add a test that will call getDeclaredFields() on all classes and try to set them accessible.
dfuchs
parents:
diff changeset
   384
}