hotspot/test/runtime/Unsafe/AllocateInstance.java
author dcubed
Thu, 07 Apr 2016 09:47:48 -0700
changeset 37267 ad8c0e8de29f
parent 35567 4b293ebd9ef9
child 36851 03e2f4d0a421
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28732
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
     1
/*
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
     4
 *
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
     8
 *
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    13
 * accompanied this code).
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    14
 *
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    18
 *
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    21
 * questions.
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    22
 */
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    23
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    24
/*
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    25
 * @test
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    26
 * @summary Verifies the behaviour of Unsafe.allocateInstance
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    27
 * @library /testlibrary
29678
dd2f3932c21e 8075586: Add @modules as needed to the open hotspot tests
ykantser
parents: 28732
diff changeset
    28
 * @modules java.base/sun.misc
dd2f3932c21e 8075586: Add @modules as needed to the open hotspot tests
ykantser
parents: 28732
diff changeset
    29
 *          java.management
28732
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    30
 * @run main AllocateInstance
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    31
 */
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    32
30604
b8d532cb6420 8067013: Rename the com.oracle.java.testlibary package
ykantser
parents: 29678
diff changeset
    33
import jdk.test.lib.*;
28732
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    34
import sun.misc.Unsafe;
30604
b8d532cb6420 8067013: Rename the com.oracle.java.testlibary package
ykantser
parents: 29678
diff changeset
    35
import static jdk.test.lib.Asserts.*;
28732
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    36
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    37
public class AllocateInstance {
35567
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    38
    static final Unsafe UNSAFE = Utils.getUnsafe();
28732
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    39
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    40
    class TestClass {
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    41
        public boolean calledConstructor = false;
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    42
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    43
        public TestClass() {
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    44
            calledConstructor = true;
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    45
        }
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    46
    }
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    47
35567
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    48
    static void testConstructorCall() throws InstantiationException {
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    49
        // allocateInstance() should not result in a call to the constructor
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    50
        TestClass tc = (TestClass)UNSAFE.allocateInstance(TestClass.class);
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    51
        assertFalse(tc.calledConstructor);
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    52
    }
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    53
28732
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    54
    abstract class AbstractClass {
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    55
        public AbstractClass() {}
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    56
    }
35567
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    57
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    58
    static void testAbstractClass() {
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    59
        try {
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    60
            AbstractClass ac = (AbstractClass) UNSAFE.allocateInstance(AbstractClass.class);
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    61
            throw new AssertionError("Should throw InstantiationException for an abstract class");
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    62
        } catch (InstantiationException e) {
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    63
            // Expected
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    64
        }
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    65
    }
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    66
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    67
    interface AnInterface {}
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    68
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    69
    static void testInterface() {
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    70
        try {
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    71
            AnInterface ai = (AnInterface) UNSAFE.allocateInstance(AnInterface.class);
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    72
            throw new AssertionError("Should throw InstantiationException for an interface");
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    73
        } catch (InstantiationException e) {
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    74
            // Expected
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    75
        }
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    76
    }
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    77
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    78
    public static void main(String args[]) throws Exception {
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    79
        for (int i = 0; i < 20_000; i++) {
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    80
            testConstructorCall();
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    81
            testAbstractClass();
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    82
            testInterface();
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    83
        }
4b293ebd9ef9 8140001: _allocateInstance intrinsic does not throw InstantiationException for abstract classes and interfaces
vlivanov
parents: 30604
diff changeset
    84
    }
28732
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    85
}