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