< prev index next >

test/testlibrary/com/oracle/java/testlibrary/ByteCodeLoader.java

Print this page
rev 7510 : 8066497: Update c.o.j.t.ByteCodeLoader to be able really reload given class
Reviewed-by:
Contributed-by: pavel.chistyakov@oracle.com


  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      */


  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     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      */
< prev index next >