< prev index next >

test/runtime/defineAnonClass/NestedUnsafe2.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.NestedUnsafe2
  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 that gets put in its host's package cannot define
  46 // an anonymous class in another package.
  47 public class NestedUnsafe2 {
  48     // The String concatenation should create the nested anonymous class.
  49     public 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("TestClass2",
  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.NestedUnsafe2.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");
  77 


  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.NestedUnsafe2
  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 that gets put in its host's package cannot define
  45 // an anonymous class in another package.
  46 public class NestedUnsafe2 {
  47     // The String concatenation should create the nested anonymous class.
  48     public 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("TestClass2",
  60             "import jdk.internal.misc.Unsafe; " +
  61             "public class TestClass2 { " +
  62             "    public static void doit() throws Throwable { " +
  63             "        Unsafe unsafe = jdk.internal.misc.Unsafe.getUnsafe(); " +
  64             "        Class klass2 = unsafe.defineAnonymousClass(TestClass2.class, p.NestedUnsafe2.klassbuf, new Object[0]); " +
  65             "        unsafe.ensureClassInitialized(klass2); " +
  66             "        Class[] dArgs = new Class[2]; " +
  67             "        dArgs[0] = String.class; " +
  68             "        dArgs[1] = String.class; " +
  69             "        try { " +
  70             "            klass2.getMethod(\"concat\", dArgs).invoke(null, \"CC\", \"DD\"); " +
  71             "        } catch (Throwable ex) { " +
  72             "            throw new RuntimeException(\"Exception: \" + ex.toString()); " +
  73             "        } " +
  74             "} } ",
  75             "--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED");
  76 
< prev index next >