< prev index next >

test/runtime/Unsafe/NestedUnsafe.java

Print this page




  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 /*
  25  * @test
  26  * @summary Creates an anonymous class inside of an anonymous class.
  27  * @library /test/lib
  28  * @modules java.base/jdk.internal.misc
  29  *          java.compiler
  30  *          java.management
  31  * @run main NestedUnsafe
  32  */
  33 
  34 import java.security.ProtectionDomain;
  35 import java.io.InputStream;
  36 import java.lang.*;
  37 import jdk.test.lib.InMemoryJavaCompiler;
  38 import jdk.test.lib.unsafe.UnsafeHelper;
  39 import jdk.internal.misc.Unsafe;
  40 import static jdk.test.lib.Asserts.*;
  41 
  42 // package p;
  43 
  44 public class NestedUnsafe {
  45     // The String concatenation should create the nested anonymous class.
  46     static byte klassbuf[] = InMemoryJavaCompiler.compile("TestClass",
  47         "public class TestClass { " +
  48         "    public static void concat(String one, String two) throws Throwable { " +
  49         "        System.out.println(one + two);" +
  50         " } } ");
  51 
  52     public static void main(String args[]) throws Exception {
  53         Unsafe unsafe = UnsafeHelper.getUnsafe();
  54 
  55         Class klass = unsafe.defineAnonymousClass(NestedUnsafe.class, klassbuf, new Object[0]);
  56         unsafe.ensureClassInitialized(klass);
  57         Class[] cArgs = new Class[2];
  58         cArgs[0] = String.class;
  59         cArgs[1] = String.class;
  60         try {
  61             klass.getMethod("concat", cArgs).invoke(null, "AA", "BB");
  62         } catch (Throwable ex) {
  63             throw new RuntimeException("Exception: " + ex.toString());
  64         }
  65 
  66         // The anonymous class calls defineAnonymousClass creating a nested anonymous class.
  67         byte klassbuf2[] = InMemoryJavaCompiler.compile("TestClass2",
  68             "import jdk.internal.misc.Unsafe; " +
  69             "public class TestClass2 { " +
  70             "    public static void doit() throws Throwable { " +
  71             "        Unsafe unsafe = jdk.internal.misc.Unsafe.getUnsafe(); " +
  72             "        Class klass2 = unsafe.defineAnonymousClass(TestClass2.class, NestedUnsafe.klassbuf, new Object[0]); " +
  73             "        unsafe.ensureClassInitialized(klass2); " +


  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 /*
  25  * @test
  26  * @summary Creates an anonymous class inside of an anonymous class.
  27  * @library /test/lib
  28  * @modules java.base/jdk.internal.misc
  29  *          java.compiler
  30  *          java.management
  31  * @run main NestedUnsafe
  32  */
  33 
  34 import java.security.ProtectionDomain;
  35 import java.io.InputStream;
  36 import java.lang.*;
  37 import jdk.test.lib.InMemoryJavaCompiler;

  38 import jdk.internal.misc.Unsafe;
  39 import static jdk.test.lib.Asserts.*;
  40 
  41 // package p;
  42 
  43 public class NestedUnsafe {
  44     // The String concatenation should create the nested anonymous class.
  45     static byte klassbuf[] = InMemoryJavaCompiler.compile("TestClass",
  46         "public class TestClass { " +
  47         "    public static void concat(String one, String two) throws Throwable { " +
  48         "        System.out.println(one + two);" +
  49         " } } ");
  50 
  51     public static void main(String args[]) throws Exception {
  52         Unsafe unsafe = Unsafe.getUnsafe();
  53 
  54         Class klass = unsafe.defineAnonymousClass(NestedUnsafe.class, klassbuf, new Object[0]);
  55         unsafe.ensureClassInitialized(klass);
  56         Class[] cArgs = new Class[2];
  57         cArgs[0] = String.class;
  58         cArgs[1] = String.class;
  59         try {
  60             klass.getMethod("concat", cArgs).invoke(null, "AA", "BB");
  61         } catch (Throwable ex) {
  62             throw new RuntimeException("Exception: " + ex.toString());
  63         }
  64 
  65         // The anonymous class calls defineAnonymousClass creating a nested anonymous class.
  66         byte klassbuf2[] = InMemoryJavaCompiler.compile("TestClass2",
  67             "import jdk.internal.misc.Unsafe; " +
  68             "public class TestClass2 { " +
  69             "    public static void doit() throws Throwable { " +
  70             "        Unsafe unsafe = jdk.internal.misc.Unsafe.getUnsafe(); " +
  71             "        Class klass2 = unsafe.defineAnonymousClass(TestClass2.class, NestedUnsafe.klassbuf, new Object[0]); " +
  72             "        unsafe.ensureClassInitialized(klass2); " +
< prev index next >