test/runtime/8003720/VictimClassLoader.java

Thu, 01 May 2014 14:57:02 -0700

author
amurillo
date
Thu, 01 May 2014 14:57:02 -0700
changeset 6651
4bc28e6b9aba
parent 0
f90c822e73f8
child 9804
8b80409d5840
permissions
-rw-r--r--

Added tag hs25.20-b13 for changeset 798f5b02be89

     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  */
    25 public class VictimClassLoader extends ClassLoader {
    26     public static long counter = 0;
    28     private int which = (int) ++counter;
    30     protected VictimClassLoader() {
    31         super(VictimClassLoader.class.getClassLoader());
    32     }
    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         }
    41         c = findLoadedClass(name);
    42         if (c != null) {
    43             return c;
    44         }
    46         byte[] buf = readClassFile(name);
    47         c = defineClass(name, buf, 0, buf.length);
    48         resolveClass(c);
    50         if (c.getClassLoader() != this) {
    51             throw new AssertionError();
    52         }
    54         Test8003720.println("loaded " + c + "#" + System.identityHashCode(c) + " in " + c.getClassLoader());
    55         return c;
    56     }
    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);
    64             java.net.URLConnection connection = url.openConnection();
    65             int contentLength = connection.getContentLength();
    66             byte[] buf = readFully(connection.getInputStream(), contentLength);
    68             return Asmator.fixup(buf);
    69         } catch (java.io.IOException ex) {
    70             throw new Error(ex);
    71         }
    72     }
    74     static byte[] readFully(java.io.InputStream in, int len) throws java.io.IOException {
    75         // Warning here:
    76         return sun.misc.IOUtils.readFully(in, len, true);
    77     }
    79     public void finalize() {
    80         Test8003720.println("Goodbye from " + this);
    81     }
    83     public String toString() {
    84         return "VictimClassLoader#" + which;
    85     }
    86 }

mercurial