Advertisement
Guest User

Pascal Thivent

a guest
Apr 11th, 2010
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.24 KB | None | 0 0
  1. /*
  2.  * Copyright 2005 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20.  * CA 95054 USA or visit www.sun.com if you need additional information or
  21.  * have any questions.
  22.  */
  23.  
  24. package sun.tools.evalexpr;
  25.  
  26. import java.util.Map;
  27.  
  28. /**
  29.  * A class loader which loads classes from byte arrays.
  30.  * <p>
  31.  * <b>This is NOT part of any API supported by Sun Microsystems. If you write code that depends on this, you do so at
  32.  * your own risk. This code and its internal interfaces are subject to change or deletion without notice.</b>
  33.  * </p>
  34.  *
  35.  * @author Peter von der Ah&eacute;
  36.  */
  37. public class ByteArrayClassLoader extends ClassLoader {
  38.     /**
  39.      * Maps binary class names to class files stored as byte arrays.
  40.      */
  41.     private final Map<String, byte[]> classes;
  42.  
  43.     /**
  44.      * Creates a new instance of ByteArrayClassLoader
  45.      *
  46.      * @param classes a map from binary class names to class files stored as byte arrays
  47.      */
  48.     public ByteArrayClassLoader(Map<String, byte[]> classes) {
  49.         this.classes = classes;
  50.     }
  51.  
  52.     @Override
  53.     public Class<?> loadClass(String name) throws ClassNotFoundException {
  54.         try {
  55.             return super.loadClass(name);
  56.         } catch (ClassNotFoundException e) {
  57.             byte[] classData = classes.get(name);
  58.             return defineClass(name, classData, 0, classData.length);
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement