< 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

@@ -35,10 +35,11 @@
  * @see InMemoryCompiler
  */
 public class ByteCodeLoader extends SecureClassLoader {
     private final String className;
     private final byte[] byteCode;
+    private volatile Class<?> holder;
 
     /**
      * Creates a new {@code ByteCodeLoader} ready to load a class with the
      * given name and the given byte code.
      *

@@ -49,10 +50,25 @@
         this.className = className;
         this.byteCode = byteCode;
     }
 
     @Override
+    public Class<?> loadClass(String name) throws ClassNotFoundException {
+        if (!name.equals(className)) {
+            return super.loadClass(name);
+        }
+        if (holder == null) {
+            synchronized(this) {
+                if (holder == null) {
+                    holder = findClass(name);
+                }
+            }
+        }
+        return holder;
+    }
+
+    @Override
     protected Class<?> findClass(String name) throws ClassNotFoundException {
         if (!name.equals(className)) {
             throw new ClassNotFoundException(name);
         }
 
< prev index next >