1 /*
   2  * Copyright (c) 2013, 2015, 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 package jdk.test.lib;
  25 
  26 import java.security.SecureClassLoader;
  27 
  28 /**
  29  * {@code ByteCodeLoader} can be used for easy loading of byte code already
  30  * present in memory.
  31  *
  32  * {@code InMemoryCompiler} can be used for compiling source code in a string
  33  * into byte code, which then can be loaded with {@code ByteCodeLoader}.
  34  *
  35  * @see InMemoryCompiler
  36  */
  37 public class ByteCodeLoader extends SecureClassLoader {
  38     private final String className;
  39     private final byte[] byteCode;
  40     private volatile Class<?> holder;
  41 
  42     /**
  43      * Creates a new {@code ByteCodeLoader} ready to load a class with the
  44      * given name and the given byte code.
  45      *
  46      * @param className The name of the class
  47      * @param byteCode The byte code of the class
  48      */
  49     public ByteCodeLoader(String className, byte[] byteCode) {
  50         this.className = className;
  51         this.byteCode = byteCode;
  52     }
  53 
  54     @Override
  55     public Class<?> loadClass(String name) throws ClassNotFoundException {
  56         if (!name.equals(className)) {
  57             return super.loadClass(name);
  58         }
  59         if (holder == null) {
  60             synchronized(this) {
  61                 if (holder == null) {
  62                     holder = findClass(name);
  63                 }
  64             }
  65         }
  66         return holder;
  67     }
  68 
  69     @Override
  70     protected Class<?> findClass(String name) throws ClassNotFoundException {
  71         if (!name.equals(className)) {
  72             throw new ClassNotFoundException(name);
  73         }
  74 
  75         return defineClass(name, byteCode, 0, byteCode.length);
  76     }
  77 
  78     /**
  79      * Utility method for creating a new {@code ByteCodeLoader} and then
  80      * directly load the given byte code.
  81      *
  82      * @param className The name of the class
  83      * @param byteCode The byte code for the class
  84      * @throws ClassNotFoundException if the class can't be loaded
  85      * @return A {@see Class} object representing the class
  86      */
  87     public static Class<?> load(String className, byte[] byteCode) throws ClassNotFoundException {
  88         return new ByteCodeLoader(className, byteCode).loadClass(className);
  89     }
  90 }