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                         //System.out.println("seeking "+name);
  38                         c = super.loadClass(name, resolve);
  39                         //System.out.println("found "+c);
  40                         return c;
  41                 }
  42                 
  43                 c = findLoadedClass(name);
  44                 if (c != null) {
  45                         return c;
  46                 }
  47                 
  48                 byte[] buf = readClassFile(name);
  49                 c = defineClass(name, buf, 0, buf.length);
  50                 resolveClass(c);
  51                 
  52                 if (c.getClassLoader() != this) {
  53                         throw new AssertionError();
  54                 }
  55                 
  56                 Test8003720.println("loaded " + c + "#" + System.identityHashCode(c) + " in " + c.getClassLoader());
  57                 return c;
  58         }
  59 
  60         static byte[] readClassFile(String name) {
  61                 try {
  62                         String rname = name.substring(name.lastIndexOf('.') + 1) + ".class";
  63                         java.net.URL url = VictimClassLoader.class.getResource(rname);
  64                         Test8003720.println("found " + rname + " = " + url);
  65                         
  66                         java.net.URLConnection connection = url.openConnection();
  67                         int contentLength = connection.getContentLength();
  68                         byte[] buf = readFully(connection.getInputStream(), contentLength);
  69 
  70                         return Asmator.fixup(buf);
  71                 } catch (java.io.IOException ex) {
  72                         throw new Error(ex);
  73                 }
  74         }
  75 
  76         static byte[] readFully(java.io.InputStream in, int len) throws java.io.IOException {
  77                 // Warning here:
  78                 return sun.misc.IOUtils.readFully(in, len, true);
  79         }
  80 
  81         public void finalize() {
  82                 Test8003720.println("Goodbye from " + this);
  83         }
  84 
  85         public String toString() {
  86                 return "VictimClassLoader#" + which;
  87         }
  88 }
  89