hotspot/test/runtime/Unsafe/NestedUnsafe.java
author mchung
Tue, 30 Aug 2016 17:48:27 -0700
changeset 40623 ea81d1ecdb26
parent 40385 a7011a835634
child 40671 0338471d9019
permissions -rw-r--r--
8160851: Remove old launcher module-related options Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40385
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
     1
/*
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
     4
 *
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
     8
 *
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    13
 * accompanied this code).
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    14
 *
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    18
 *
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    21
 * questions.
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    22
 */
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    23
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    24
/*
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    25
 * @test
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    26
 * @summary Creates an anonymous class inside of an anonymous class.
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    27
 * @library /testlibrary
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    28
 * @modules java.base/jdk.internal.misc
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    29
 *          java.compiler
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    30
 *          java.management
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    31
 * @run main NestedUnsafe
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    32
 */
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    33
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    34
import java.security.ProtectionDomain;
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    35
import java.io.InputStream;
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    36
import java.lang.*;
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    37
import jdk.test.lib.*;
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    38
import jdk.internal.misc.Unsafe;
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    39
import static jdk.test.lib.Asserts.*;
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    40
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    41
// package p;
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    42
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    43
public class NestedUnsafe {
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    44
    // The String concatenation should create the nested anonymous class.
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    45
    static byte klassbuf[] = InMemoryJavaCompiler.compile("TestClass",
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    46
        "public class TestClass { " +
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    47
        "    public static void concat(String one, String two) throws Throwable { " +
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    48
        "        System.out.println(one + two);" +
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    49
        " } } ");
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    50
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    51
    public static void main(String args[]) throws Exception {
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    52
        Unsafe unsafe = Utils.getUnsafe();
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    53
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    54
        Class klass = unsafe.defineAnonymousClass(NestedUnsafe.class, klassbuf, new Object[0]);
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    55
        unsafe.ensureClassInitialized(klass);
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    56
        Class[] cArgs = new Class[2];
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    57
        cArgs[0] = String.class;
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    58
        cArgs[1] = String.class;
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    59
        try {
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    60
            klass.getMethod("concat", cArgs).invoke(null, "AA", "BB");
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    61
        } catch (Throwable ex) {
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    62
            throw new RuntimeException("Exception: " + ex.toString());
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    63
        }
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    64
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    65
        // The anonymous class calls defineAnonymousClass creating a nested anonymous class.
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    66
        byte klassbuf2[] = InMemoryJavaCompiler.compile("TestClass2",
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    67
            "import jdk.internal.misc.Unsafe; " +
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    68
            "public class TestClass2 { " +
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    69
            "    public static void doit() throws Throwable { " +
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    70
            "        Unsafe unsafe = jdk.internal.misc.Unsafe.getUnsafe(); " +
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    71
            "        Class klass2 = unsafe.defineAnonymousClass(TestClass2.class, NestedUnsafe.klassbuf, new Object[0]); " +
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    72
            "        unsafe.ensureClassInitialized(klass2); " +
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    73
            "        Class[] dArgs = new Class[2]; " +
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    74
            "        dArgs[0] = String.class; " +
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    75
            "        dArgs[1] = String.class; " +
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    76
            "        try { " +
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    77
            "            klass2.getMethod(\"concat\", dArgs).invoke(null, \"CC\", \"DD\"); " +
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    78
            "        } catch (Throwable ex) { " +
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    79
            "            throw new RuntimeException(\"Exception: \" + ex.toString()); " +
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    80
            "        } " +
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    81
            "} } ",
40623
ea81d1ecdb26 8160851: Remove old launcher module-related options
mchung
parents: 40385
diff changeset
    82
            "--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED");
40385
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    83
        Class klass2 = unsafe.defineAnonymousClass(NestedUnsafe.class, klassbuf2, new Object[0]);
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    84
        try {
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    85
            klass2.getMethod("doit").invoke(null);
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    86
        } catch (Throwable ex) {
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    87
            throw new RuntimeException("Exception: " + ex.toString());
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    88
        }
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    89
    }
a7011a835634 8030221: Checking for anonymous class should check for NULL as well as potential nesting
hseigel
parents:
diff changeset
    90
}