hotspot/test/runtime/Unsafe/FieldOffset.java
author psandoz
Tue, 10 Feb 2015 14:10:43 +0100
changeset 28951 a29d2048e16a
parent 28732 d4b650e48a8f
child 29678 dd2f3932c21e
permissions -rw-r--r--
8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation Reviewed-by: dholmes, kvn, vlivanov
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.fieldOffset
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    27
 * @library /testlibrary
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    28
 * @run main FieldOffset
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    29
 */
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    30
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    31
import java.lang.reflect.Field;
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    32
import com.oracle.java.testlibrary.*;
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    33
import sun.misc.Unsafe;
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    34
import java.lang.reflect.*;
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    35
import static com.oracle.java.testlibrary.Asserts.*;
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 FieldOffset {
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    38
    public static void main(String args[]) throws Exception {
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    39
        Unsafe unsafe = Utils.getUnsafe();
28951
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    40
        Field[] fields = Test.class.getDeclaredFields();
28732
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    41
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    42
        for (int i = 0; i < fields.length; i++) {
28951
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    43
            long offset = unsafe.objectFieldOffset(fields[i]);
28732
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    44
            // Ensure we got a valid offset value back
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    45
            assertNotEquals(offset, unsafe.INVALID_FIELD_OFFSET);
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
            // Make sure the field offset is unique
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    48
            for (int j = 0; j < i; j++) {
28951
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    49
                assertNotEquals(offset, unsafe.objectFieldOffset(fields[j]));
28732
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    50
            }
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    51
        }
28951
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    52
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    53
        fields = StaticTest.class.getDeclaredFields();
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    54
        for (int i = 0; i < fields.length; i++) {
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    55
            long offset = unsafe.staticFieldOffset(fields[i]);
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    56
            // Ensure we got a valid offset value back
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    57
            assertNotEquals(offset, unsafe.INVALID_FIELD_OFFSET);
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    58
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    59
            // Make sure the field offset is unique
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    60
            for (int j = 0; j < i; j++) {
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    61
                assertNotEquals(offset, unsafe.staticFieldOffset(fields[j]));
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    62
            }
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    63
        }
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    64
28732
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    65
    }
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    66
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    67
    class Test {
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    68
        boolean booleanField;
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    69
        byte byteField;
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    70
        char charField;
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    71
        double doubleField;
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    72
        float floatField;
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    73
        int intField;
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    74
        long longField;
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    75
        Object objectField;
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    76
        short shortField;
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    77
    }
28951
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    78
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    79
    static class StaticTest {
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    80
        static boolean booleanField;
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    81
        static byte byteField;
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    82
        static char charField;
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    83
        static double doubleField;
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    84
        static float floatField;
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    85
        static int intField;
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    86
        static long longField;
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    87
        static Object objectField;
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    88
        static short shortField;
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    89
    }
a29d2048e16a 8068975: Remove deprecated methods on sun.misc.Unsafe and clean up native implementation
psandoz
parents: 28732
diff changeset
    90
28732
d4b650e48a8f 8069207: [TESTBUG] Exception thrown for java.lang.NoSuchMethodError: sun.misc.Unsafe.monitorExit
ctornqvi
parents:
diff changeset
    91
}