test/hotspot/jtreg/runtime/testlibrary/ClassUnloadCommon.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 45825 hotspot/test/runtime/testlibrary/ClassUnloadCommon.java@4fa7bd62eb84
child 51747 9bf5205655ee
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17311
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
     1
/*
35917
463d67f86eaa 8079408: Reimplement TraceClassLoading, TraceClassUnloading, and TraceClassLoaderData with Unified Logging.
mockner
parents: 17311
diff changeset
     2
 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
17311
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
     4
 *
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
     8
 *
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    13
 * accompanied this code).
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    14
 *
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    18
 *
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    21
 * questions.
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    22
 */
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    23
35917
463d67f86eaa 8079408: Reimplement TraceClassLoading, TraceClassUnloading, and TraceClassLoaderData with Unified Logging.
mockner
parents: 17311
diff changeset
    24
463d67f86eaa 8079408: Reimplement TraceClassLoading, TraceClassUnloading, and TraceClassLoaderData with Unified Logging.
mockner
parents: 17311
diff changeset
    25
/*
463d67f86eaa 8079408: Reimplement TraceClassLoading, TraceClassUnloading, and TraceClassLoaderData with Unified Logging.
mockner
parents: 17311
diff changeset
    26
 * To use ClassUnloadCommon from a sub-process, see hotspot/test/runtime/logging/ClassLoadUnloadTest.java
463d67f86eaa 8079408: Reimplement TraceClassLoading, TraceClassUnloading, and TraceClassLoaderData with Unified Logging.
mockner
parents: 17311
diff changeset
    27
 * for an example.
463d67f86eaa 8079408: Reimplement TraceClassLoading, TraceClassUnloading, and TraceClassLoaderData with Unified Logging.
mockner
parents: 17311
diff changeset
    28
 */
463d67f86eaa 8079408: Reimplement TraceClassLoading, TraceClassUnloading, and TraceClassLoaderData with Unified Logging.
mockner
parents: 17311
diff changeset
    29
17311
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    30
import java.io.File;
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    31
import java.net.MalformedURLException;
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    32
import java.net.URL;
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    33
import java.net.URLClassLoader;
45825
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    34
import java.nio.file.Path;
17311
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    35
import java.nio.file.Paths;
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    36
import java.util.ArrayList;
45825
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    37
import java.util.stream.Stream;
17311
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    38
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    39
public class ClassUnloadCommon {
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    40
    public static class TestFailure extends RuntimeException {
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    41
        TestFailure(String msg) {
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    42
            super(msg);
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    43
        }
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    44
    }
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    45
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    46
    public static void failIf(boolean value, String msg) {
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    47
        if (value) throw new TestFailure("Test failed: " + msg);
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    48
    }
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    49
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    50
    private static volatile Object dummy = null;
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    51
    private static void allocateMemory(int kilobytes) {
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    52
        ArrayList<byte[]> l = new ArrayList<>();
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    53
        dummy = l;
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    54
        for (int i = kilobytes; i > 0; i -= 1) {
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    55
            l.add(new byte[1024]);
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    56
        }
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    57
        l = null;
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    58
        dummy = null;
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    59
    }
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    60
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    61
    public static void triggerUnloading() {
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    62
        allocateMemory(16 * 1024); // yg size is 8m with cms, force young collection
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    63
        System.gc();
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    64
    }
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    65
45825
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    66
    /**
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    67
     * Creates a class loader that loads classes from {@code ${test.class.path}}
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    68
     * before delegating to the system class loader.
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    69
     */
17311
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
    70
    public static ClassLoader newClassLoader() {
45825
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    71
        String cp = System.getProperty("test.class.path", ".");
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    72
        URL[] urls = Stream.of(cp.split(File.pathSeparator))
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    73
                .map(Paths::get)
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    74
                .map(ClassUnloadCommon::toURL)
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    75
                .toArray(URL[]::new);
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    76
        return new URLClassLoader(urls) {
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    77
            @Override
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    78
            public Class<?> loadClass(String cn, boolean resolve)
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    79
                throws ClassNotFoundException
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    80
            {
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    81
                synchronized (getClassLoadingLock(cn)) {
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    82
                    Class<?> c = findLoadedClass(cn);
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    83
                    if (c == null) {
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    84
                        try {
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    85
                            c = findClass(cn);
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    86
                        } catch (ClassNotFoundException e) {
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    87
                            c = getParent().loadClass(cn);
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    88
                        }
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    89
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    90
                    }
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    91
                    if (resolve) {
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    92
                        resolveClass(c);
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    93
                    }
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    94
                    return c;
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    95
                }
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    96
            }
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    97
        };
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    98
    }
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
    99
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
   100
    static URL toURL(Path path) {
17311
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
   101
        try {
45825
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
   102
            return path.toUri().toURL();
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
   103
        } catch (MalformedURLException e) {
4fa7bd62eb84 8183503: Update hotspot tests to allow for unique test classes directory
alanb
parents: 35917
diff changeset
   104
            throw new RuntimeException(e);
17311
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
   105
        }
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
   106
    }
6ba3841cb7f9 8009577: Test test/closed/runtime/classunload broken
ctornqvi
parents:
diff changeset
   107
}