hotspot/test/testlibrary/com/oracle/java/testlibrary/ByteCodeLoader.java
changeset 19322 e35f9ed4f081
child 28391 899a849e55f8
equal deleted inserted replaced
19320:fc08f2a0c5af 19322:e35f9ed4f081
       
     1 /*
       
     2  * Copyright (c) 2013, 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 com.oracle.java.testlibrary;
       
    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 
       
    41     /**
       
    42      * Creates a new {@code ByteCodeLoader} ready to load a class with the
       
    43      * given name and the given byte code.
       
    44      *
       
    45      * @param className The name of the class
       
    46      * @param byteCode The byte code of the class
       
    47      */
       
    48     public ByteCodeLoader(String className, byte[] byteCode) {
       
    49         this.className = className;
       
    50         this.byteCode = byteCode;
       
    51     }
       
    52 
       
    53     @Override
       
    54     protected Class<?> findClass(String name) throws ClassNotFoundException {
       
    55         if (!name.equals(className)) {
       
    56             throw new ClassNotFoundException(name);
       
    57         }
       
    58 
       
    59         return defineClass(name, byteCode, 0, byteCode.length);
       
    60     }
       
    61 
       
    62     /**
       
    63      * Utility method for creating a new {@code ByteCodeLoader} and then
       
    64      * directly load the given byte code.
       
    65      *
       
    66      * @param className The name of the class
       
    67      * @param byteCode The byte code for the class
       
    68      * @throws ClassNotFoundException if the class can't be loaded
       
    69      * @return A {@see Class} object representing the class
       
    70      */
       
    71     public static Class<?> load(String className, byte[] byteCode) throws ClassNotFoundException {
       
    72         return new ByteCodeLoader(className, byteCode).loadClass(className);
       
    73     }
       
    74 }