< prev index next >

test/runtime/defineAnonClass/NestedUnsafe.java

Print this page




  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8058575
  27  * @summary Creates an anonymous class inside of an anonymous class.
  28  * @library /test/lib
  29  * @modules java.base/jdk.internal.misc
  30  *          java.compiler
  31  *          java.management
  32  * @run main p.NestedUnsafe
  33  */
  34 
  35 package p;
  36 
  37 import java.security.ProtectionDomain;
  38 import java.io.InputStream;
  39 import java.lang.*;
  40 import jdk.test.lib.*;
  41 import jdk.internal.misc.Unsafe;
  42 import jdk.test.lib.unsafe.UnsafeHelper;
  43 
  44 
  45 // Test that an anonymous class in package 'p' cannot define its own anonymous class
  46 // in another package.
  47 public class NestedUnsafe {
  48     // The String concatenation should create the nested anonymous class.
  49     static byte klassbuf[] = InMemoryJavaCompiler.compile("q.TestClass",
  50         "package q; " +
  51         "public class TestClass { " +
  52         "    public static void concat(String one, String two) throws Throwable { " +
  53         "        System.out.println(one + two);" +
  54         " } } ");
  55 
  56     public static void main(String args[]) throws Exception {
  57         Unsafe unsafe = UnsafeHelper.getUnsafe();
  58 
  59         // The anonymous class calls defineAnonymousClass creating a nested anonymous class.
  60         byte klassbuf2[] = InMemoryJavaCompiler.compile("p.TestClass2",
  61             "package p; " +
  62             "import jdk.internal.misc.Unsafe; " +
  63             "public class TestClass2 { " +
  64             "    public static void doit() throws Throwable { " +
  65             "        Unsafe unsafe = jdk.internal.misc.Unsafe.getUnsafe(); " +
  66             "        Class klass2 = unsafe.defineAnonymousClass(TestClass2.class, p.NestedUnsafe.klassbuf, new Object[0]); " +
  67             "        unsafe.ensureClassInitialized(klass2); " +
  68             "        Class[] dArgs = new Class[2]; " +
  69             "        dArgs[0] = String.class; " +
  70             "        dArgs[1] = String.class; " +
  71             "        try { " +
  72             "            klass2.getMethod(\"concat\", dArgs).invoke(null, \"CC\", \"DD\"); " +
  73             "        } catch (Throwable ex) { " +
  74             "            throw new RuntimeException(\"Exception: \" + ex.toString()); " +
  75             "        } " +
  76             "} } ",
  77             "--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED");


  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8058575
  27  * @summary Creates an anonymous class inside of an anonymous class.
  28  * @library /test/lib
  29  * @modules java.base/jdk.internal.misc
  30  *          java.compiler
  31  *          java.management
  32  * @run main p.NestedUnsafe
  33  */
  34 
  35 package p;
  36 
  37 import java.security.ProtectionDomain;
  38 import java.io.InputStream;
  39 import java.lang.*;
  40 import jdk.test.lib.*;
  41 import jdk.internal.misc.Unsafe;

  42 
  43 
  44 // Test that an anonymous class in package 'p' cannot define its own anonymous class
  45 // in another package.
  46 public class NestedUnsafe {
  47     // The String concatenation should create the nested anonymous class.
  48     static byte klassbuf[] = InMemoryJavaCompiler.compile("q.TestClass",
  49         "package q; " +
  50         "public class TestClass { " +
  51         "    public static void concat(String one, String two) throws Throwable { " +
  52         "        System.out.println(one + two);" +
  53         " } } ");
  54 
  55     public static void main(String args[]) throws Exception {
  56         Unsafe unsafe = Unsafe.getUnsafe();
  57 
  58         // The anonymous class calls defineAnonymousClass creating a nested anonymous class.
  59         byte klassbuf2[] = InMemoryJavaCompiler.compile("p.TestClass2",
  60             "package p; " +
  61             "import jdk.internal.misc.Unsafe; " +
  62             "public class TestClass2 { " +
  63             "    public static void doit() throws Throwable { " +
  64             "        Unsafe unsafe = jdk.internal.misc.Unsafe.getUnsafe(); " +
  65             "        Class klass2 = unsafe.defineAnonymousClass(TestClass2.class, p.NestedUnsafe.klassbuf, new Object[0]); " +
  66             "        unsafe.ensureClassInitialized(klass2); " +
  67             "        Class[] dArgs = new Class[2]; " +
  68             "        dArgs[0] = String.class; " +
  69             "        dArgs[1] = String.class; " +
  70             "        try { " +
  71             "            klass2.getMethod(\"concat\", dArgs).invoke(null, \"CC\", \"DD\"); " +
  72             "        } catch (Throwable ex) { " +
  73             "            throw new RuntimeException(\"Exception: \" + ex.toString()); " +
  74             "        } " +
  75             "} } ",
  76             "--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED");
< prev index next >