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  * @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");
  77 
  78         Class klass2 = unsafe.defineAnonymousClass(p.NestedUnsafe.class, klassbuf2, new Object[0]);
  79         try {
  80             klass2.getMethod("doit").invoke(null);
  81             throw new RuntimeException("Expected exception not thrown");
  82         } catch (Throwable ex) {
  83             Throwable iae = ex.getCause();
  84             if (!iae.toString().contains(
  85                 "IllegalArgumentException: Host class p/NestedUnsafe and anonymous class q/TestClass")) {
  86                 throw new RuntimeException("Exception: " + iae.toString());
  87             }
  88         }
  89     }
  90 }