1 /*
   2  * Copyright (c) 2015, 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 Verifies the behaviour of Unsafe.fieldOffset
  27  * @library /test/lib
  28  * @modules java.base/jdk.internal.misc
  29  *          java.management
  30  * @run main FieldOffset
  31  */
  32 
  33 import java.lang.reflect.Field;
  34 import jdk.internal.misc.Unsafe;
  35 import java.lang.reflect.*;
  36 import static jdk.test.lib.Asserts.*;
  37 
  38 public class FieldOffset {
  39     public static void main(String args[]) throws Exception {
  40         Unsafe unsafe = Unsafe.getUnsafe();
  41         Field[] fields = Test.class.getDeclaredFields();
  42 
  43         for (int i = 0; i < fields.length; i++) {
  44             long offset = unsafe.objectFieldOffset(fields[i]);
  45             // Ensure we got a valid offset value back
  46             assertNotEquals(offset, unsafe.INVALID_FIELD_OFFSET);
  47 
  48             // Make sure the field offset is unique
  49             for (int j = 0; j < i; j++) {
  50                 assertNotEquals(offset, unsafe.objectFieldOffset(fields[j]));
  51             }
  52         }
  53 
  54         fields = StaticTest.class.getDeclaredFields();
  55         for (int i = 0; i < fields.length; i++) {
  56             long offset = unsafe.staticFieldOffset(fields[i]);
  57             // Ensure we got a valid offset value back
  58             assertNotEquals(offset, unsafe.INVALID_FIELD_OFFSET);
  59 
  60             // Make sure the field offset is unique
  61             for (int j = 0; j < i; j++) {
  62                 assertNotEquals(offset, unsafe.staticFieldOffset(fields[j]));
  63             }
  64         }
  65 
  66     }
  67 
  68     class Test {
  69         boolean booleanField;
  70         byte byteField;
  71         char charField;
  72         double doubleField;
  73         float floatField;
  74         int intField;
  75         long longField;
  76         Object objectField;
  77         short shortField;
  78     }
  79 
  80     static class StaticTest {
  81         static boolean booleanField;
  82         static byte byteField;
  83         static char charField;
  84         static double doubleField;
  85         static float floatField;
  86         static int intField;
  87         static long longField;
  88         static Object objectField;
  89         static short shortField;
  90     }
  91 
  92 }