< prev index next >

src/jdk.unsupported/share/classes/sun/misc/Unsafe.java

Print this page

 627      * and base.
 628      *
 629      * <p>As of 1.4.1, offsets for fields are represented as long values,
 630      * although the Sun JVM does not use the most significant 32 bits.
 631      * However, JVM implementations which store static fields at absolute
 632      * addresses can use long offsets and null base pointers to express
 633      * the field locations in a form usable by {@link #getInt(Object,long)}.
 634      * Therefore, code which will be ported to such JVMs on 64-bit platforms
 635      * must preserve all bits of static field offsets.
 636      * @see #getInt(Object, long)
 637      */
 638     @ForceInline
 639     public long objectFieldOffset(Field f) {
 640         if (f == null) {
 641             throw new NullPointerException();
 642         }
 643         Class<?> declaringClass = f.getDeclaringClass();
 644         if (declaringClass.isHidden()) {
 645             throw new UnsupportedOperationException("can't get field offset on a hidden class: " + f);
 646         }



 647         if (declaringClass.isRecord()) {
 648             throw new UnsupportedOperationException("can't get field offset on a record class: " + f);
 649         }
 650         return theInternalUnsafe.objectFieldOffset(f);
 651     }
 652 
 653     /**
 654      * Reports the location of a given static field, in conjunction with {@link
 655      * #staticFieldBase}.
 656      * <p>Do not expect to perform any sort of arithmetic on this offset;
 657      * it is just a cookie which is passed to the unsafe heap memory accessors.
 658      *
 659      * <p>Any given field will always have the same offset, and no two distinct
 660      * fields of the same class will ever have the same offset.
 661      *
 662      * <p>As of 1.4.1, offsets for fields are represented as long values,
 663      * although the Sun JVM does not use the most significant 32 bits.
 664      * It is hard to imagine a JVM technology which needs more than
 665      * a few bits to encode an offset within a non-array object,
 666      * However, for consistency with other methods in this class,
 667      * this method reports its result as a long value.
 668      * @see #getInt(Object, long)
 669      */
 670     @ForceInline
 671     public long staticFieldOffset(Field f) {
 672         if (f == null) {
 673             throw new NullPointerException();
 674         }
 675         Class<?> declaringClass = f.getDeclaringClass();
 676         if (declaringClass.isHidden()) {
 677             throw new UnsupportedOperationException("can't get field offset on a hidden class: " + f);
 678         }



 679         if (declaringClass.isRecord()) {
 680             throw new UnsupportedOperationException("can't get field offset on a record class: " + f);
 681         }
 682         return theInternalUnsafe.staticFieldOffset(f);
 683     }
 684 
 685     /**
 686      * Reports the location of a given static field, in conjunction with {@link
 687      * #staticFieldOffset}.
 688      * <p>Fetch the base "Object", if any, with which static fields of the
 689      * given class can be accessed via methods like {@link #getInt(Object,
 690      * long)}.  This value may be null.  This value may refer to an object
 691      * which is a "cookie", not guaranteed to be a real Object, and it should
 692      * not be used in any way except as argument to the get and put routines in
 693      * this class.
 694      */
 695     @ForceInline
 696     public Object staticFieldBase(Field f) {
 697         if (f == null) {
 698             throw new NullPointerException();
 699         }
 700         Class<?> declaringClass = f.getDeclaringClass();
 701         if (declaringClass.isHidden()) {
 702             throw new UnsupportedOperationException("can't get base address on a hidden class: " + f);
 703         }



 704         if (declaringClass.isRecord()) {
 705             throw new UnsupportedOperationException("can't get base address on a record class: " + f);
 706         }
 707         return theInternalUnsafe.staticFieldBase(f);
 708     }
 709 
 710     /**
 711      * Detects if the given class may need to be initialized. This is often
 712      * needed in conjunction with obtaining the static field base of a
 713      * class.
 714      *
 715      * @deprecated No replacement API for this method.  As multiple threads
 716      * may be trying to initialize the same class or interface at the same time.
 717      * The only reliable result returned by this method is {@code false}
 718      * indicating that the given class has been initialized.  Instead, simply
 719      * call {@link java.lang.invoke.MethodHandles.Lookup#ensureInitialized(Class)}
 720      * that does nothing if the given class has already been initialized.
 721      * This method is subject to removal in a future version of JDK.
 722      *
 723      * @return false only if a call to {@code ensureClassInitialized} would have no effect

 627      * and base.
 628      *
 629      * <p>As of 1.4.1, offsets for fields are represented as long values,
 630      * although the Sun JVM does not use the most significant 32 bits.
 631      * However, JVM implementations which store static fields at absolute
 632      * addresses can use long offsets and null base pointers to express
 633      * the field locations in a form usable by {@link #getInt(Object,long)}.
 634      * Therefore, code which will be ported to such JVMs on 64-bit platforms
 635      * must preserve all bits of static field offsets.
 636      * @see #getInt(Object, long)
 637      */
 638     @ForceInline
 639     public long objectFieldOffset(Field f) {
 640         if (f == null) {
 641             throw new NullPointerException();
 642         }
 643         Class<?> declaringClass = f.getDeclaringClass();
 644         if (declaringClass.isHidden()) {
 645             throw new UnsupportedOperationException("can't get field offset on a hidden class: " + f);
 646         }
 647         if (f.getDeclaringClass().isPrimitiveClass()) {
 648             throw new UnsupportedOperationException("can't get field offset on an inline class: " + f);
 649         }
 650         if (declaringClass.isRecord()) {
 651             throw new UnsupportedOperationException("can't get field offset on a record class: " + f);
 652         }
 653         return theInternalUnsafe.objectFieldOffset(f);
 654     }
 655 
 656     /**
 657      * Reports the location of a given static field, in conjunction with {@link
 658      * #staticFieldBase}.
 659      * <p>Do not expect to perform any sort of arithmetic on this offset;
 660      * it is just a cookie which is passed to the unsafe heap memory accessors.
 661      *
 662      * <p>Any given field will always have the same offset, and no two distinct
 663      * fields of the same class will ever have the same offset.
 664      *
 665      * <p>As of 1.4.1, offsets for fields are represented as long values,
 666      * although the Sun JVM does not use the most significant 32 bits.
 667      * It is hard to imagine a JVM technology which needs more than
 668      * a few bits to encode an offset within a non-array object,
 669      * However, for consistency with other methods in this class,
 670      * this method reports its result as a long value.
 671      * @see #getInt(Object, long)
 672      */
 673     @ForceInline
 674     public long staticFieldOffset(Field f) {
 675         if (f == null) {
 676             throw new NullPointerException();
 677         }
 678         Class<?> declaringClass = f.getDeclaringClass();
 679         if (declaringClass.isHidden()) {
 680             throw new UnsupportedOperationException("can't get field offset on a hidden class: " + f);
 681         }
 682         if (f.getDeclaringClass().isPrimitiveClass()) {
 683             throw new UnsupportedOperationException("can't get static field offset on an inline class: " + f);
 684         }
 685         if (declaringClass.isRecord()) {
 686             throw new UnsupportedOperationException("can't get field offset on a record class: " + f);
 687         }
 688         return theInternalUnsafe.staticFieldOffset(f);
 689     }
 690 
 691     /**
 692      * Reports the location of a given static field, in conjunction with {@link
 693      * #staticFieldOffset}.
 694      * <p>Fetch the base "Object", if any, with which static fields of the
 695      * given class can be accessed via methods like {@link #getInt(Object,
 696      * long)}.  This value may be null.  This value may refer to an object
 697      * which is a "cookie", not guaranteed to be a real Object, and it should
 698      * not be used in any way except as argument to the get and put routines in
 699      * this class.
 700      */
 701     @ForceInline
 702     public Object staticFieldBase(Field f) {
 703         if (f == null) {
 704             throw new NullPointerException();
 705         }
 706         Class<?> declaringClass = f.getDeclaringClass();
 707         if (declaringClass.isHidden()) {
 708             throw new UnsupportedOperationException("can't get base address on a hidden class: " + f);
 709         }
 710         if (f.getDeclaringClass().isPrimitiveClass()) {
 711             throw new UnsupportedOperationException("can't get base address on an inline class: " + f);
 712         }
 713         if (declaringClass.isRecord()) {
 714             throw new UnsupportedOperationException("can't get base address on a record class: " + f);
 715         }
 716         return theInternalUnsafe.staticFieldBase(f);
 717     }
 718 
 719     /**
 720      * Detects if the given class may need to be initialized. This is often
 721      * needed in conjunction with obtaining the static field base of a
 722      * class.
 723      *
 724      * @deprecated No replacement API for this method.  As multiple threads
 725      * may be trying to initialize the same class or interface at the same time.
 726      * The only reliable result returned by this method is {@code false}
 727      * indicating that the given class has been initialized.  Instead, simply
 728      * call {@link java.lang.invoke.MethodHandles.Lookup#ensureInitialized(Class)}
 729      * that does nothing if the given class has already been initialized.
 730      * This method is subject to removal in a future version of JDK.
 731      *
 732      * @return false only if a call to {@code ensureClassInitialized} would have no effect
< prev index next >