1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  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); " +
  74             "        Class[] dArgs = new Class[2]; " +
  75             "        dArgs[0] = String.class; " +
  76             "        dArgs[1] = String.class; " +
  77             "        try { " +
  78             "            klass2.getMethod(\"concat\", dArgs).invoke(null, \"CC\", \"DD\"); " +
  79             "        } catch (Throwable ex) { " +
  80             "            throw new RuntimeException(\"Exception: \" + ex.toString()); " +
  81             "        } " +
  82             "} } ",
  83             "--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED");
  84         Class klass2 = unsafe.defineAnonymousClass(NestedUnsafe.class, klassbuf2, new Object[0]);
  85         try {
  86             klass2.getMethod("doit").invoke(null);
  87         } catch (Throwable ex) {
  88             throw new RuntimeException("Exception: " + ex.toString());
  89         }
  90     }
  91 }