hotspot/test/runtime/MirrorFrame/VictimClassLoader.java
author amurillo
Fri, 06 May 2016 06:23:29 -0700
changeset 38208 ff63d43b0480
parent 38074 8475fdc6dcc3
child 41705 332239c052cc
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14582
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
     1
/*
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
     4
 *
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
     7
 * published by the Free Software Foundation.
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
     8
 *
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    13
 * accompanied this code).
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    14
 *
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    18
 *
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    21
 * questions.
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    22
 *
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    23
 */
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    24
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    25
public class VictimClassLoader extends ClassLoader {
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    26
    public static long counter = 0;
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    27
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    28
    private int which = (int) ++counter;
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    29
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    30
    protected VictimClassLoader() {
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    31
        super(VictimClassLoader.class.getClassLoader());
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    32
    }
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    33
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    34
    protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    35
        Class c;
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    36
        if (!name.endsWith("Victim")) {
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    37
            c = super.loadClass(name, resolve);
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    38
            return c;
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    39
        }
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    40
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    41
        c = findLoadedClass(name);
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    42
        if (c != null) {
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    43
            return c;
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    44
        }
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    45
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    46
        byte[] buf = readClassFile(name);
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    47
        c = defineClass(name, buf, 0, buf.length);
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    48
        resolveClass(c);
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    49
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    50
        if (c.getClassLoader() != this) {
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    51
            throw new AssertionError();
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    52
        }
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    53
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    54
        Test8003720.println("loaded " + c + "#" + System.identityHashCode(c) + " in " + c.getClassLoader());
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    55
        return c;
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    56
    }
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    57
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    58
    static byte[] readClassFile(String name) {
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    59
        try {
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    60
            String rname = name.substring(name.lastIndexOf('.') + 1) + ".class";
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    61
            java.net.URL url = VictimClassLoader.class.getResource(rname);
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    62
            Test8003720.println("found " + rname + " = " + url);
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    63
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    64
            java.net.URLConnection connection = url.openConnection();
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    65
            int contentLength = connection.getContentLength();
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    66
            byte[] buf = readFully(connection.getInputStream(), contentLength);
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    67
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    68
            return Asmator.fixup(buf);
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    69
        } catch (java.io.IOException ex) {
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    70
            throw new Error(ex);
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    71
        }
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    72
    }
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    73
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    74
    static byte[] readFully(java.io.InputStream in, int len) throws java.io.IOException {
32924
89da05adaa00 8138978: Examine usages of sun.misc.IOUtils
chegar
parents: 14582
diff changeset
    75
        byte[] b = in.readAllBytes();
89da05adaa00 8138978: Examine usages of sun.misc.IOUtils
chegar
parents: 14582
diff changeset
    76
        if (len != -1 && b.length != len)
89da05adaa00 8138978: Examine usages of sun.misc.IOUtils
chegar
parents: 14582
diff changeset
    77
            throw new java.io.IOException("Expected:" + len + ", actual:" + b.length);
89da05adaa00 8138978: Examine usages of sun.misc.IOUtils
chegar
parents: 14582
diff changeset
    78
        return b;
14582
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    79
    }
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    80
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    81
    public void finalize() {
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    82
        Test8003720.println("Goodbye from " + this);
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    83
    }
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    84
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    85
    public String toString() {
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    86
        return "VictimClassLoader#" + which;
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    87
    }
490bb6c0df7c 8003720: NPG: Method in interpreter stack frame can be deallocated
stefank
parents:
diff changeset
    88
}