< prev index next >

src/java.base/share/classes/java/lang/reflect/Field.java

Print this page

  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.reflect;
  27 
  28 import java.lang.annotation.Annotation;
  29 import java.net.URL;
  30 import java.security.CodeSource;
  31 import java.util.Map;
  32 import java.util.Set;
  33 import java.util.Objects;
  34 import jdk.internal.access.SharedSecrets;
  35 import jdk.internal.event.FinalFieldMutationEvent;

  36 import jdk.internal.loader.ClassLoaders;
  37 import jdk.internal.misc.VM;
  38 import jdk.internal.module.ModuleBootstrap;
  39 import jdk.internal.module.Modules;

  40 import jdk.internal.reflect.CallerSensitive;
  41 import jdk.internal.reflect.FieldAccessor;

  42 import jdk.internal.reflect.Reflection;
  43 import jdk.internal.vm.annotation.ForceInline;
  44 import jdk.internal.vm.annotation.Stable;
  45 import sun.reflect.generics.repository.FieldRepository;
  46 import sun.reflect.generics.factory.CoreReflectionFactory;
  47 import sun.reflect.generics.factory.GenericsFactory;
  48 import sun.reflect.generics.scope.ClassScope;
  49 import sun.reflect.annotation.AnnotationParser;
  50 import sun.reflect.annotation.AnnotationSupport;
  51 import sun.reflect.annotation.TypeAnnotation;
  52 import sun.reflect.annotation.TypeAnnotationParser;
  53 
  54 /**
  55  * A {@code Field} provides information about, and dynamic access to, a
  56  * single field of a class or an interface.  The reflected field may
  57  * be a class (static) field or an instance field.
  58  *
  59  * <p>A {@code Field} permits widening conversions to occur during a get or
  60  * set access operation, but throws an {@code IllegalArgumentException} if a
  61  * narrowing conversion would occur.

  63  * @see Member
  64  * @see java.lang.Class
  65  * @see java.lang.Class#getFields()
  66  * @see java.lang.Class#getField(String)
  67  * @see java.lang.Class#getDeclaredFields()
  68  * @see java.lang.Class#getDeclaredField(String)
  69  *
  70  * @author Kenneth Russell
  71  * @author Nakul Saraiya
  72  * @since 1.1
  73  */
  74 public final
  75 class Field extends AccessibleObject implements Member {
  76     private final Class<?>            clazz;
  77     private final int                 slot;
  78     // This is guaranteed to be interned by the VM in the 1.4
  79     // reflection implementation
  80     private final String              name;
  81     private final Class<?>            type;
  82     private final int                 modifiers;
  83     private final boolean             trustedFinal;
  84     // Generics and annotations support
  85     private final transient String    signature;
  86     private final byte[]              annotations;
  87 
  88     /**
  89      * Fields are mutable due to {@link AccessibleObject#setAccessible(boolean)}.
  90      * Thus, we return a new copy of a root each time a field is returned.
  91      * Some lazily initialized immutable states can be stored on root and shared to the copies.
  92      */
  93     private Field root;
  94     private transient volatile FieldRepository genericInfo;
  95     private @Stable FieldAccessor fieldAccessor; // access control enabled
  96     private @Stable FieldAccessor overrideFieldAccessor; // access control suppressed
  97     // End shared states
  98 
  99     // Generics infrastructure
 100 
 101     private String getGenericSignature() {return signature;}
 102 
 103     // Accessor for factory

 113         if (genericInfo == null) {
 114             var root = this.root;
 115             if (root != null) {
 116                 genericInfo = root.getGenericInfo();
 117             } else {
 118                 genericInfo = FieldRepository.make(getGenericSignature(), getFactory());
 119             }
 120             this.genericInfo = genericInfo;
 121         }
 122         return genericInfo;
 123     }
 124 
 125     /**
 126      * Package-private constructor
 127      */
 128     @SuppressWarnings("deprecation")
 129     Field(Class<?> declaringClass,
 130           String name,
 131           Class<?> type,
 132           int modifiers,
 133           boolean trustedFinal,
 134           int slot,
 135           String signature,
 136           byte[] annotations)
 137     {
 138         this.clazz = declaringClass;
 139         this.name = name;
 140         this.type = type;
 141         this.modifiers = modifiers;
 142         this.trustedFinal = trustedFinal;
 143         this.slot = slot;
 144         this.signature = signature;
 145         this.annotations = annotations;
 146     }
 147 
 148     /**
 149      * Package-private routine (exposed to java.lang.Class via
 150      * ReflectAccess) which returns a copy of this Field. The copy's
 151      * "root" field points to this Field.
 152      */
 153     Field copy() {
 154         // This routine enables sharing of FieldAccessor objects
 155         // among Field objects which refer to the same underlying
 156         // method in the VM. (All of this contortion is only necessary
 157         // because of the "accessibility" bit in AccessibleObject,
 158         // which implicitly requires that new java.lang.reflect
 159         // objects be fabricated for each reflective call on Class
 160         // objects.)
 161         if (this.root != null)
 162             throw new IllegalArgumentException("Can not copy a non-root Field");
 163 
 164         Field res = new Field(clazz, name, type, modifiers, trustedFinal, slot, signature, annotations);
 165         res.root = this;
 166         // Might as well eagerly propagate this if already present
 167         res.fieldAccessor = fieldAccessor;
 168         res.overrideFieldAccessor = overrideFieldAccessor;
 169         res.genericInfo = genericInfo;
 170 
 171         return res;
 172     }
 173 
 174     /**
 175      * {@inheritDoc}
 176      *
 177      * <p>If this reflected object represents a non-final field, and this method is
 178      * used to enable access, then both <em>{@linkplain #get(Object) read}</em>
 179      * and <em>{@linkplain #set(Object, Object) write}</em> access to the field
 180      * are enabled.
 181      *
 182      * <p>If this reflected object represents a <em>non-modifiable</em> final field
 183      * then enabling access only enables read access. Any attempt to {@linkplain
 184      * #set(Object, Object) set} the field value throws an {@code
 185      * IllegalAccessException}. The following fields are non-modifiable:
 186      * <ul>
 187      * <li>static final fields declared in any class or interface</li>
 188      * <li>final fields declared in a {@linkplain Class#isRecord() record}</li>
 189      * <li>final fields declared in a {@linkplain Class#isHidden() hidden class}</li>


 190      * </ul>
 191      * <p>If this reflected object represents a non-static final field in a class that
 192      * is not a record class or hidden class, then enabling access will enable read
 193      * access. Whether write access is allowed or not is checked when attempting to
 194      * {@linkplain #set(Object, Object) set} the field value.
 195      *
 196      * @throws InaccessibleObjectException {@inheritDoc}
 197      */
 198     @Override
 199     @CallerSensitive
 200     public void setAccessible(boolean flag) {
 201         if (flag) checkCanSetAccessible(Reflection.getCallerClass());
 202         setAccessible0(flag);
 203     }
 204 
 205     @Override
 206     void checkCanSetAccessible(Class<?> caller) {
 207         checkCanSetAccessible(caller, clazz);
 208     }
 209 
 210     /**
 211      * Returns the {@code Class} object representing the class or interface
 212      * that declares the field represented by this {@code Field} object.
 213      */
 214     @Override

 229      * be used to decode the modifiers.
 230      *
 231      * @see Modifier
 232      * @see #accessFlags()
 233      * @jls 8.3 Field Declarations
 234      * @jls 9.3 Field (Constant) Declarations
 235      */
 236     public int getModifiers() {
 237         return modifiers;
 238     }
 239 
 240     /**
 241      * {@return an unmodifiable set of the {@linkplain AccessFlag
 242      * access flags} for this field, possibly empty}
 243      * @see #getModifiers()
 244      * @jvms 4.5 Fields
 245      * @since 20
 246      */
 247     @Override
 248     public Set<AccessFlag> accessFlags() {
 249         return reflectionFactory.parseAccessFlags(getModifiers(), AccessFlag.Location.FIELD, getDeclaringClass());
 250     }
 251 
 252     /**
 253      * Returns {@code true} if this field represents an element of
 254      * an enumerated class; returns {@code false} otherwise.
 255      *
 256      * @return {@code true} if and only if this field represents an element of
 257      * an enumerated class.
 258      * @since 1.5
 259      * @jls 8.9.1 Enum Constants
 260      */
 261     public boolean isEnumConstant() {
 262         return (getModifiers() & Modifier.ENUM) != 0;
 263     }
 264 
 265     /**
 266      * Returns {@code true} if this field is a synthetic
 267      * field; returns {@code false} otherwise.
 268      *
 269      * @return true if and only if this field is a synthetic
 270      * field as defined by the Java Language Specification.
 271      * @since 1.5
 272      * @see <a
 273      * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
 274      * programming language and JVM modeling in core reflection</a>
 275      */
 276     public boolean isSynthetic() {
 277         return Modifier.isSynthetic(getModifiers());
 278     }
 279 





















 280     /**
 281      * Returns a {@code Class} object that identifies the
 282      * declared type for the field represented by this
 283      * {@code Field} object.
 284      *
 285      * @return a {@code Class} object identifying the declared
 286      * type of the field represented by this object
 287      */
 288     public Class<?> getType() {
 289         return type;
 290     }
 291 
 292     /**
 293      * Returns a {@code Type} object that represents the declared type for
 294      * the field represented by this {@code Field} object.
 295      *
 296      * <p>If the declared type of the field is a parameterized type,
 297      * the {@code Type} object returned must accurately reflect the
 298      * actual type arguments used in the source code.
 299      *

 449      *
 450      * <p>If the field is hidden in the type of {@code obj},
 451      * the field's value is obtained according to the preceding rules.
 452      *
 453      * @param obj object from which the represented field's value is
 454      * to be extracted
 455      * @return the value of the represented field in object
 456      * {@code obj}; primitive values are wrapped in an appropriate
 457      * object before being returned
 458      *
 459      * @throws    IllegalAccessException    if this {@code Field} object
 460      *              is enforcing Java language access control and the underlying
 461      *              field is inaccessible.
 462      * @throws    IllegalArgumentException  if the specified object is not an
 463      *              instance of the class or interface declaring the underlying
 464      *              field (or a subclass or implementor thereof).
 465      * @throws    NullPointerException      if the specified object is null
 466      *              and the field is an instance field.
 467      * @throws    ExceptionInInitializerError if the initialization provoked
 468      *              by this method fails.




 469      */
 470     @CallerSensitive
 471     @ForceInline // to ensure Reflection.getCallerClass optimization
 472     public Object get(Object obj)
 473         throws IllegalArgumentException, IllegalAccessException
 474     {
 475         if (!override) {
 476             Class<?> caller = Reflection.getCallerClass();
 477             checkAccess(caller, obj);
 478             return getFieldAccessor().get(obj);
 479         } else {
 480             return getOverrideFieldAccessor().get(obj);
 481         }
 482     }
 483 
 484     /**
 485      * Gets the value of a static or instance {@code boolean} field.
 486      *
 487      * @param obj the object to extract the {@code boolean} value
 488      * from
 489      * @return the value of the {@code boolean} field
 490      *
 491      * @throws    IllegalAccessException    if this {@code Field} object
 492      *              is enforcing Java language access control and the underlying
 493      *              field is inaccessible.
 494      * @throws    IllegalArgumentException  if the specified object is not
 495      *              an instance of the class or interface declaring the
 496      *              underlying field (or a subclass or implementor
 497      *              thereof), or if the field value cannot be
 498      *              converted to the type {@code boolean} by a
 499      *              widening conversion.
 500      * @throws    NullPointerException      if the specified object is null
 501      *              and the field is an instance field.
 502      * @throws    ExceptionInInitializerError if the initialization provoked
 503      *              by this method fails.




 504      * @see       Field#get
 505      */
 506     @CallerSensitive
 507     @ForceInline // to ensure Reflection.getCallerClass optimization
 508     public boolean getBoolean(Object obj)
 509         throws IllegalArgumentException, IllegalAccessException
 510     {
 511         if (!override) {
 512             Class<?> caller = Reflection.getCallerClass();
 513             checkAccess(caller, obj);
 514             return getFieldAccessor().getBoolean(obj);
 515         } else {
 516             return getOverrideFieldAccessor().getBoolean(obj);
 517         }
 518     }
 519 
 520     /**
 521      * Gets the value of a static or instance {@code byte} field.
 522      *
 523      * @param obj the object to extract the {@code byte} value
 524      * from
 525      * @return the value of the {@code byte} field
 526      *
 527      * @throws    IllegalAccessException    if this {@code Field} object
 528      *              is enforcing Java language access control and the underlying
 529      *              field is inaccessible.
 530      * @throws    IllegalArgumentException  if the specified object is not
 531      *              an instance of the class or interface declaring the
 532      *              underlying field (or a subclass or implementor
 533      *              thereof), or if the field value cannot be
 534      *              converted to the type {@code byte} by a
 535      *              widening conversion.
 536      * @throws    NullPointerException      if the specified object is null
 537      *              and the field is an instance field.
 538      * @throws    ExceptionInInitializerError if the initialization provoked
 539      *              by this method fails.




 540      * @see       Field#get
 541      */
 542     @CallerSensitive
 543     @ForceInline // to ensure Reflection.getCallerClass optimization
 544     public byte getByte(Object obj)
 545         throws IllegalArgumentException, IllegalAccessException
 546     {
 547         if (!override) {
 548             Class<?> caller = Reflection.getCallerClass();
 549             checkAccess(caller, obj);
 550             return getFieldAccessor().getByte(obj);
 551         } else {
 552             return getOverrideFieldAccessor().getByte(obj);
 553         }
 554     }
 555 
 556     /**
 557      * Gets the value of a static or instance field of type
 558      * {@code char} or of another primitive type convertible to
 559      * type {@code char} via a widening conversion.
 560      *
 561      * @param obj the object to extract the {@code char} value
 562      * from
 563      * @return the value of the field converted to type {@code char}
 564      *
 565      * @throws    IllegalAccessException    if this {@code Field} object
 566      *              is enforcing Java language access control and the underlying
 567      *              field is inaccessible.
 568      * @throws    IllegalArgumentException  if the specified object is not
 569      *              an instance of the class or interface declaring the
 570      *              underlying field (or a subclass or implementor
 571      *              thereof), or if the field value cannot be
 572      *              converted to the type {@code char} by a
 573      *              widening conversion.
 574      * @throws    NullPointerException      if the specified object is null
 575      *              and the field is an instance field.
 576      * @throws    ExceptionInInitializerError if the initialization provoked
 577      *              by this method fails.




 578      * @see Field#get
 579      */
 580     @CallerSensitive
 581     @ForceInline // to ensure Reflection.getCallerClass optimization
 582     public char getChar(Object obj)
 583         throws IllegalArgumentException, IllegalAccessException
 584     {
 585         if (!override) {
 586             Class<?> caller = Reflection.getCallerClass();
 587             checkAccess(caller, obj);
 588             return getFieldAccessor().getChar(obj);
 589         } else {
 590             return getOverrideFieldAccessor().getChar(obj);
 591         }
 592     }
 593 
 594     /**
 595      * Gets the value of a static or instance field of type
 596      * {@code short} or of another primitive type convertible to
 597      * type {@code short} via a widening conversion.
 598      *
 599      * @param obj the object to extract the {@code short} value
 600      * from
 601      * @return the value of the field converted to type {@code short}
 602      *
 603      * @throws    IllegalAccessException    if this {@code Field} object
 604      *              is enforcing Java language access control and the underlying
 605      *              field is inaccessible.
 606      * @throws    IllegalArgumentException  if the specified object is not
 607      *              an instance of the class or interface declaring the
 608      *              underlying field (or a subclass or implementor
 609      *              thereof), or if the field value cannot be
 610      *              converted to the type {@code short} by a
 611      *              widening conversion.
 612      * @throws    NullPointerException      if the specified object is null
 613      *              and the field is an instance field.
 614      * @throws    ExceptionInInitializerError if the initialization provoked
 615      *              by this method fails.




 616      * @see       Field#get
 617      */
 618     @CallerSensitive
 619     @ForceInline // to ensure Reflection.getCallerClass optimization
 620     public short getShort(Object obj)
 621         throws IllegalArgumentException, IllegalAccessException
 622     {
 623         if (!override) {
 624             Class<?> caller = Reflection.getCallerClass();
 625             checkAccess(caller, obj);
 626             return getFieldAccessor().getShort(obj);
 627         } else {
 628             return getOverrideFieldAccessor().getShort(obj);
 629         }
 630     }
 631 
 632     /**
 633      * Gets the value of a static or instance field of type
 634      * {@code int} or of another primitive type convertible to
 635      * type {@code int} via a widening conversion.
 636      *
 637      * @param obj the object to extract the {@code int} value
 638      * from
 639      * @return the value of the field converted to type {@code int}
 640      *
 641      * @throws    IllegalAccessException    if this {@code Field} object
 642      *              is enforcing Java language access control and the underlying
 643      *              field is inaccessible.
 644      * @throws    IllegalArgumentException  if the specified object is not
 645      *              an instance of the class or interface declaring the
 646      *              underlying field (or a subclass or implementor
 647      *              thereof), or if the field value cannot be
 648      *              converted to the type {@code int} by a
 649      *              widening conversion.
 650      * @throws    NullPointerException      if the specified object is null
 651      *              and the field is an instance field.
 652      * @throws    ExceptionInInitializerError if the initialization provoked
 653      *              by this method fails.




 654      * @see       Field#get
 655      */
 656     @CallerSensitive
 657     @ForceInline // to ensure Reflection.getCallerClass optimization
 658     public int getInt(Object obj)
 659         throws IllegalArgumentException, IllegalAccessException
 660     {
 661         if (!override) {
 662             Class<?> caller = Reflection.getCallerClass();
 663             checkAccess(caller, obj);
 664             return getFieldAccessor().getInt(obj);
 665         } else {
 666             return getOverrideFieldAccessor().getInt(obj);
 667         }
 668     }
 669 
 670     /**
 671      * Gets the value of a static or instance field of type
 672      * {@code long} or of another primitive type convertible to
 673      * type {@code long} via a widening conversion.
 674      *
 675      * @param obj the object to extract the {@code long} value
 676      * from
 677      * @return the value of the field converted to type {@code long}
 678      *
 679      * @throws    IllegalAccessException    if this {@code Field} object
 680      *              is enforcing Java language access control and the underlying
 681      *              field is inaccessible.
 682      * @throws    IllegalArgumentException  if the specified object is not
 683      *              an instance of the class or interface declaring the
 684      *              underlying field (or a subclass or implementor
 685      *              thereof), or if the field value cannot be
 686      *              converted to the type {@code long} by a
 687      *              widening conversion.
 688      * @throws    NullPointerException      if the specified object is null
 689      *              and the field is an instance field.
 690      * @throws    ExceptionInInitializerError if the initialization provoked
 691      *              by this method fails.




 692      * @see       Field#get
 693      */
 694     @CallerSensitive
 695     @ForceInline // to ensure Reflection.getCallerClass optimization
 696     public long getLong(Object obj)
 697         throws IllegalArgumentException, IllegalAccessException
 698     {
 699         if (!override) {
 700             Class<?> caller = Reflection.getCallerClass();
 701             checkAccess(caller, obj);
 702             return getFieldAccessor().getLong(obj);
 703         } else {
 704             return getOverrideFieldAccessor().getLong(obj);
 705         }
 706     }
 707 
 708     /**
 709      * Gets the value of a static or instance field of type
 710      * {@code float} or of another primitive type convertible to
 711      * type {@code float} via a widening conversion.
 712      *
 713      * @param obj the object to extract the {@code float} value
 714      * from
 715      * @return the value of the field converted to type {@code float}
 716      *
 717      * @throws    IllegalAccessException    if this {@code Field} object
 718      *              is enforcing Java language access control and the underlying
 719      *              field is inaccessible.
 720      * @throws    IllegalArgumentException  if the specified object is not
 721      *              an instance of the class or interface declaring the
 722      *              underlying field (or a subclass or implementor
 723      *              thereof), or if the field value cannot be
 724      *              converted to the type {@code float} by a
 725      *              widening conversion.
 726      * @throws    NullPointerException      if the specified object is null
 727      *              and the field is an instance field.
 728      * @throws    ExceptionInInitializerError if the initialization provoked
 729      *              by this method fails.




 730      * @see Field#get
 731      */
 732     @CallerSensitive
 733     @ForceInline // to ensure Reflection.getCallerClass optimization
 734     public float getFloat(Object obj)
 735         throws IllegalArgumentException, IllegalAccessException
 736     {
 737         if (!override) {
 738             Class<?> caller = Reflection.getCallerClass();
 739             checkAccess(caller, obj);
 740             return getFieldAccessor().getFloat(obj);
 741         } else {
 742             return getOverrideFieldAccessor().getFloat(obj);
 743         }
 744     }
 745 
 746     /**
 747      * Gets the value of a static or instance field of type
 748      * {@code double} or of another primitive type convertible to
 749      * type {@code double} via a widening conversion.
 750      *
 751      * @param obj the object to extract the {@code double} value
 752      * from
 753      * @return the value of the field converted to type {@code double}
 754      *
 755      * @throws    IllegalAccessException    if this {@code Field} object
 756      *              is enforcing Java language access control and the underlying
 757      *              field is inaccessible.
 758      * @throws    IllegalArgumentException  if the specified object is not
 759      *              an instance of the class or interface declaring the
 760      *              underlying field (or a subclass or implementor
 761      *              thereof), or if the field value cannot be
 762      *              converted to the type {@code double} by a
 763      *              widening conversion.
 764      * @throws    NullPointerException      if the specified object is null
 765      *              and the field is an instance field.
 766      * @throws    ExceptionInInitializerError if the initialization provoked
 767      *              by this method fails.




 768      * @see       Field#get
 769      */
 770     @CallerSensitive
 771     @ForceInline // to ensure Reflection.getCallerClass optimization
 772     public double getDouble(Object obj)
 773         throws IllegalArgumentException, IllegalAccessException
 774     {
 775         if (!override) {
 776             Class<?> caller = Reflection.getCallerClass();
 777             checkAccess(caller, obj);
 778             return getFieldAccessor().getDouble(obj);
 779         } else {
 780             return getOverrideFieldAccessor().getDouble(obj);
 781         }
 782     }
 783 
 784     /**
 785      * Sets the field represented by this {@code Field} object on the
 786      * specified object argument to the specified new value. The new
 787      * value is automatically unwrapped if the underlying field has a

 805      * <p>If the underlying field is final, this {@code Field} object has <em>write</em>
 806      * access if and only if all of the following conditions are true, where {@code D} is
 807      * the field's {@linkplain #getDeclaringClass() declaring class}:
 808      *
 809      * <ul>
 810      * <li>{@link #setAccessible(boolean) setAccessible(true)} has succeeded for this
 811      *     {@code Field} object.</li>
 812      * <li><a href="doc-files/MutationMethods.html">final field mutation is enabled</a>
 813      *     for the caller's module.</li>
 814      * <li> At least one of the following conditions holds:
 815      *     <ol type="a">
 816      *     <li> {@code D} and the caller class are in the same module.</li>
 817      *     <li> The field is {@code public} and {@code D} is {@code public} in a package
 818      *     that the module containing {@code D} exports to at least the caller's module.</li>
 819      *     <li> {@code D} is in a package that is {@linkplain Module#isOpen(String, Module)
 820      *     open} to the caller's module.</li>
 821      *     </ol>
 822      * </li>
 823      * <li>{@code D} is not a {@linkplain Class#isRecord() record class}.</li>
 824      * <li>{@code D} is not a {@linkplain Class#isHidden() hidden class}.</li>

 825      * <li>The field is non-static.</li>

 826      * </ul>
 827      *
 828      * <p>If any of the above conditions is not met, this method throws an
 829      * {@code IllegalAccessException}.
 830      *
 831      * <p>These conditions are more restrictive than the conditions specified by {@link
 832      * #setAccessible(boolean)} to suppress access checks. In particular, updating a
 833      * module to export or open a package cannot be used to allow <em>write</em> access
 834      * to final fields with the {@code set} methods defined by {@code Field}.
 835      * Condition (b) is not met if the module containing {@code D} has been updated with
 836      * {@linkplain Module#addExports(String, Module) addExports} to export the package to
 837      * the caller's module. Condition (c) is not met if the module containing {@code D}
 838      * has been updated with {@linkplain Module#addOpens(String, Module) addOpens} to open
 839      * the package to the caller's module.
 840      *
 841      * <p>This method may be called by <a href="{@docRoot}/../specs/jni/index.html">
 842      * JNI code</a> with no caller class on the stack. In that case, and when the
 843      * underlying field is final, this {@code Field} object has <em>write</em> access
 844      * if and only if all of the following conditions are true, where {@code D} is the
 845      * field's {@linkplain #getDeclaringClass() declaring class}:
 846      *
 847      * <ul>
 848      * <li>{@code setAccessible(true)} has succeeded for this {@code Field} object.</li>
 849      * <li>final field mutation is enabled for the unnamed module.</li>
 850      * <li>The field is {@code public} and {@code D} is {@code public} in a package that
 851      *     is {@linkplain Module#isExported(String) exported} to all modules.</li>
 852      * <li>{@code D} is not a {@linkplain Class#isRecord() record class}.</li>
 853      * <li>{@code D} is not a {@linkplain Class#isHidden() hidden class}.</li>

 854      * <li>The field is non-static.</li>

 855      * </ul>
 856      *
 857      * <p>If any of the above conditions is not met, this method throws an
 858      * {@code IllegalAccessException}.
 859      *
 860      * <p> Setting a final field in this way
 861      * is meaningful only during deserialization or reconstruction of
 862      * instances of classes with blank final fields, before they are
 863      * made available for access by other parts of a program. Use in
 864      * any other context may have unpredictable effects, including cases
 865      * in which other parts of a program continue to use the original
 866      * value of this field.
 867      *
 868      * <p>If the underlying field is of a primitive type, an unwrapping
 869      * conversion is attempted to convert the new value to a value of
 870      * a primitive type.  If this attempt fails, the method throws an
 871      * {@code IllegalArgumentException}.
 872      *
 873      * <p>If, after possible unwrapping, the new value cannot be
 874      * converted to the type of the underlying field by an identity or

1345             root.setFieldAccessor(accessor);
1346         }
1347     }
1348 
1349     // Sets the overrideFieldAccessor for this Field object and
1350     // (recursively) its root
1351     private void setOverrideFieldAccessor(FieldAccessor accessor) {
1352         overrideFieldAccessor = accessor;
1353         // Propagate up
1354         Field root = this.root;
1355         if (root != null) {
1356             root.setOverrideFieldAccessor(accessor);
1357         }
1358     }
1359 
1360     @Override
1361     /* package-private */ Field getRoot() {
1362         return root;
1363     }
1364 



1365     /* package-private */ boolean isTrustedFinal() {
1366         return trustedFinal;




1367     }
1368 
1369     /**
1370      * {@inheritDoc}
1371      *
1372      * @throws NullPointerException {@inheritDoc}
1373      * @since 1.5
1374      */
1375     @Override
1376     public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
1377         Objects.requireNonNull(annotationClass);
1378         return annotationClass.cast(declaredAnnotations().get(annotationClass));
1379     }
1380 
1381     /**
1382      * {@inheritDoc}
1383      *
1384      * @throws NullPointerException {@inheritDoc}
1385      * @since 1.8
1386      */

  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.reflect;
  27 
  28 import java.lang.annotation.Annotation;
  29 import java.net.URL;
  30 import java.security.CodeSource;
  31 import java.util.Map;
  32 import java.util.Set;
  33 import java.util.Objects;
  34 import jdk.internal.access.SharedSecrets;
  35 import jdk.internal.event.FinalFieldMutationEvent;
  36 import jdk.internal.javac.PreviewFeature;
  37 import jdk.internal.loader.ClassLoaders;
  38 import jdk.internal.misc.VM;
  39 import jdk.internal.module.ModuleBootstrap;
  40 import jdk.internal.module.Modules;
  41 import jdk.internal.reflect.AccessFlagSet;
  42 import jdk.internal.reflect.CallerSensitive;
  43 import jdk.internal.reflect.FieldAccessor;
  44 import jdk.internal.reflect.PreviewAccessFlags;
  45 import jdk.internal.reflect.Reflection;
  46 import jdk.internal.vm.annotation.ForceInline;
  47 import jdk.internal.vm.annotation.Stable;
  48 import sun.reflect.generics.repository.FieldRepository;
  49 import sun.reflect.generics.factory.CoreReflectionFactory;
  50 import sun.reflect.generics.factory.GenericsFactory;
  51 import sun.reflect.generics.scope.ClassScope;
  52 import sun.reflect.annotation.AnnotationParser;
  53 import sun.reflect.annotation.AnnotationSupport;
  54 import sun.reflect.annotation.TypeAnnotation;
  55 import sun.reflect.annotation.TypeAnnotationParser;
  56 
  57 /**
  58  * A {@code Field} provides information about, and dynamic access to, a
  59  * single field of a class or an interface.  The reflected field may
  60  * be a class (static) field or an instance field.
  61  *
  62  * <p>A {@code Field} permits widening conversions to occur during a get or
  63  * set access operation, but throws an {@code IllegalArgumentException} if a
  64  * narrowing conversion would occur.

  66  * @see Member
  67  * @see java.lang.Class
  68  * @see java.lang.Class#getFields()
  69  * @see java.lang.Class#getField(String)
  70  * @see java.lang.Class#getDeclaredFields()
  71  * @see java.lang.Class#getDeclaredField(String)
  72  *
  73  * @author Kenneth Russell
  74  * @author Nakul Saraiya
  75  * @since 1.1
  76  */
  77 public final
  78 class Field extends AccessibleObject implements Member {
  79     private final Class<?>            clazz;
  80     private final int                 slot;
  81     // This is guaranteed to be interned by the VM in the 1.4
  82     // reflection implementation
  83     private final String              name;
  84     private final Class<?>            type;
  85     private final int                 modifiers;
  86     private final int                 flags;
  87     // Generics and annotations support
  88     private final transient String    signature;
  89     private final byte[]              annotations;
  90 
  91     /**
  92      * Fields are mutable due to {@link AccessibleObject#setAccessible(boolean)}.
  93      * Thus, we return a new copy of a root each time a field is returned.
  94      * Some lazily initialized immutable states can be stored on root and shared to the copies.
  95      */
  96     private Field root;
  97     private transient volatile FieldRepository genericInfo;
  98     private @Stable FieldAccessor fieldAccessor; // access control enabled
  99     private @Stable FieldAccessor overrideFieldAccessor; // access control suppressed
 100     // End shared states
 101 
 102     // Generics infrastructure
 103 
 104     private String getGenericSignature() {return signature;}
 105 
 106     // Accessor for factory

 116         if (genericInfo == null) {
 117             var root = this.root;
 118             if (root != null) {
 119                 genericInfo = root.getGenericInfo();
 120             } else {
 121                 genericInfo = FieldRepository.make(getGenericSignature(), getFactory());
 122             }
 123             this.genericInfo = genericInfo;
 124         }
 125         return genericInfo;
 126     }
 127 
 128     /**
 129      * Package-private constructor
 130      */
 131     @SuppressWarnings("deprecation")
 132     Field(Class<?> declaringClass,
 133           String name,
 134           Class<?> type,
 135           int modifiers,
 136           int flags,
 137           int slot,
 138           String signature,
 139           byte[] annotations)
 140     {
 141         this.clazz = declaringClass;
 142         this.name = name;
 143         this.type = type;
 144         this.modifiers = modifiers;
 145         this.flags = flags;
 146         this.slot = slot;
 147         this.signature = signature;
 148         this.annotations = annotations;
 149     }
 150 
 151     /**
 152      * Package-private routine (exposed to java.lang.Class via
 153      * ReflectAccess) which returns a copy of this Field. The copy's
 154      * "root" field points to this Field.
 155      */
 156     Field copy() {
 157         // This routine enables sharing of FieldAccessor objects
 158         // among Field objects which refer to the same underlying
 159         // method in the VM. (All of this contortion is only necessary
 160         // because of the "accessibility" bit in AccessibleObject,
 161         // which implicitly requires that new java.lang.reflect
 162         // objects be fabricated for each reflective call on Class
 163         // objects.)
 164         if (this.root != null)
 165             throw new IllegalArgumentException("Can not copy a non-root Field");
 166 
 167         Field res = new Field(clazz, name, type, modifiers, flags, slot, signature, annotations);
 168         res.root = this;
 169         // Might as well eagerly propagate this if already present
 170         res.fieldAccessor = fieldAccessor;
 171         res.overrideFieldAccessor = overrideFieldAccessor;
 172         res.genericInfo = genericInfo;
 173 
 174         return res;
 175     }
 176 
 177     /**
 178      * {@inheritDoc}
 179      *
 180      * <p>If this reflected object represents a non-final field, and this method is
 181      * used to enable access, then both <em>{@linkplain #get(Object) read}</em>
 182      * and <em>{@linkplain #set(Object, Object) write}</em> access to the field
 183      * are enabled.
 184      *
 185      * <p>If this reflected object represents a <em>non-modifiable</em> final field
 186      * then enabling access only enables read access. Any attempt to {@linkplain
 187      * #set(Object, Object) set} the field value throws an {@code
 188      * IllegalAccessException}. The following fields are non-modifiable:
 189      * <ul>
 190      * <li>static final fields declared in any class or interface</li>
 191      * <li>final fields declared in a {@linkplain Class#isRecord() record}</li>
 192      * <li>final fields declared in a {@linkplain Class#isHidden() hidden class}</li>
 193      * <li>fields declared in a {@linkplain Class#isValue() value class}</li>
 194      * <li>{@linkplain #isStrictInit() strictly-initialized} final fields</li>
 195      * </ul>
 196      * <p>Final fields that are not covered by this list may be <em>modifiable</em>.
 197      * Enabling access will enable read access. Whether write access is allowed is
 198      * checked when attempting to {@linkplain #set(Object, Object) set} the field value.

 199      *
 200      * @throws InaccessibleObjectException {@inheritDoc}
 201      */
 202     @Override
 203     @CallerSensitive
 204     public void setAccessible(boolean flag) {
 205         if (flag) checkCanSetAccessible(Reflection.getCallerClass());
 206         setAccessible0(flag);
 207     }
 208 
 209     @Override
 210     void checkCanSetAccessible(Class<?> caller) {
 211         checkCanSetAccessible(caller, clazz);
 212     }
 213 
 214     /**
 215      * Returns the {@code Class} object representing the class or interface
 216      * that declares the field represented by this {@code Field} object.
 217      */
 218     @Override

 233      * be used to decode the modifiers.
 234      *
 235      * @see Modifier
 236      * @see #accessFlags()
 237      * @jls 8.3 Field Declarations
 238      * @jls 9.3 Field (Constant) Declarations
 239      */
 240     public int getModifiers() {
 241         return modifiers;
 242     }
 243 
 244     /**
 245      * {@return an unmodifiable set of the {@linkplain AccessFlag
 246      * access flags} for this field, possibly empty}
 247      * @see #getModifiers()
 248      * @jvms 4.5 Fields
 249      * @since 20
 250      */
 251     @Override
 252     public Set<AccessFlag> accessFlags() {
 253         return AccessFlagSet.ofValidated(PreviewAccessFlags.FIELD_PREVIEW_FLAGS, getModifiers());
 254     }
 255 
 256     /**
 257      * Returns {@code true} if this field represents an element of
 258      * an enumerated class; returns {@code false} otherwise.
 259      *
 260      * @return {@code true} if and only if this field represents an element of
 261      * an enumerated class.
 262      * @since 1.5
 263      * @jls 8.9.1 Enum Constants
 264      */
 265     public boolean isEnumConstant() {
 266         return (getModifiers() & Modifier.ENUM) != 0;
 267     }
 268 
 269     /**
 270      * Returns {@code true} if this field is a synthetic
 271      * field; returns {@code false} otherwise.
 272      *
 273      * @return true if and only if this field is a synthetic
 274      * field as defined by the Java Language Specification.
 275      * @since 1.5
 276      * @see <a
 277      * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
 278      * programming language and JVM modeling in core reflection</a>
 279      */
 280     public boolean isSynthetic() {
 281         return Modifier.isSynthetic(getModifiers());
 282     }
 283 
 284     /**
 285      * Returns {@code true} if this field is a strictly-initialized field;
 286      * returns {@code false} otherwise.
 287      *
 288      * <p>This method returns {@code true} if and only if the class or interface
 289      * that declares this field uses preview features and this field is a
 290      * strictly-initialized field. The {@link AccessFlag#STRICT_INIT
 291      * ACC_STRICT_INIT} flag is considered not set for a field declared in a
 292      * class or interface that does not use preview features; consequently,
 293      * this method always returns {@code false} when preview features are disabled.
 294      *
 295      * @return {@code true} if and only if this field is a strictly-initialized
 296      *         field, as defined by the Java Virtual Machine Specification
 297      * @jvms strict-fields-4.5 Field access and property flags
 298      * @since 28
 299      */
 300     @PreviewFeature(feature = PreviewFeature.Feature.STRICT_FIELDS, reflective = true)
 301     public boolean isStrictInit() {
 302         return accessFlags().contains(AccessFlag.STRICT_INIT);
 303     }
 304 
 305     /**
 306      * Returns a {@code Class} object that identifies the
 307      * declared type for the field represented by this
 308      * {@code Field} object.
 309      *
 310      * @return a {@code Class} object identifying the declared
 311      * type of the field represented by this object
 312      */
 313     public Class<?> getType() {
 314         return type;
 315     }
 316 
 317     /**
 318      * Returns a {@code Type} object that represents the declared type for
 319      * the field represented by this {@code Field} object.
 320      *
 321      * <p>If the declared type of the field is a parameterized type,
 322      * the {@code Type} object returned must accurately reflect the
 323      * actual type arguments used in the source code.
 324      *

 474      *
 475      * <p>If the field is hidden in the type of {@code obj},
 476      * the field's value is obtained according to the preceding rules.
 477      *
 478      * @param obj object from which the represented field's value is
 479      * to be extracted
 480      * @return the value of the represented field in object
 481      * {@code obj}; primitive values are wrapped in an appropriate
 482      * object before being returned
 483      *
 484      * @throws    IllegalAccessException    if this {@code Field} object
 485      *              is enforcing Java language access control and the underlying
 486      *              field is inaccessible.
 487      * @throws    IllegalArgumentException  if the specified object is not an
 488      *              instance of the class or interface declaring the underlying
 489      *              field (or a subclass or implementor thereof).
 490      * @throws    NullPointerException      if the specified object is null
 491      *              and the field is an instance field.
 492      * @throws    ExceptionInInitializerError if the initialization provoked
 493      *              by this method fails.
 494      * @throws    IllegalStateException if the current thread is initializing the
 495      *              field's {@linkplain #getDeclaringClass() declaring class} and
 496      *              the field is a {@linkplain #isStrictInit() strictly-initialized}
 497      *              static field that has not been initialized.
 498      */
 499     @CallerSensitive
 500     @ForceInline // to ensure Reflection.getCallerClass optimization
 501     public Object get(Object obj)
 502         throws IllegalArgumentException, IllegalAccessException
 503     {
 504         if (!override) {
 505             Class<?> caller = Reflection.getCallerClass();
 506             checkAccess(caller, obj);
 507             return getFieldAccessor().get(obj);
 508         } else {
 509             return getOverrideFieldAccessor().get(obj);
 510         }
 511     }
 512 
 513     /**
 514      * Gets the value of a static or instance {@code boolean} field.
 515      *
 516      * @param obj the object to extract the {@code boolean} value
 517      * from
 518      * @return the value of the {@code boolean} field
 519      *
 520      * @throws    IllegalAccessException    if this {@code Field} object
 521      *              is enforcing Java language access control and the underlying
 522      *              field is inaccessible.
 523      * @throws    IllegalArgumentException  if the specified object is not
 524      *              an instance of the class or interface declaring the
 525      *              underlying field (or a subclass or implementor
 526      *              thereof), or if the field value cannot be
 527      *              converted to the type {@code boolean} by a
 528      *              widening conversion.
 529      * @throws    NullPointerException      if the specified object is null
 530      *              and the field is an instance field.
 531      * @throws    ExceptionInInitializerError if the initialization provoked
 532      *              by this method fails.
 533      * @throws    IllegalStateException if the current thread is initializing the
 534      *              field's {@linkplain #getDeclaringClass() declaring class} and
 535      *              the field is a {@linkplain #isStrictInit() strictly-initialized}
 536      *              static field that has not been initialized.
 537      * @see       Field#get
 538      */
 539     @CallerSensitive
 540     @ForceInline // to ensure Reflection.getCallerClass optimization
 541     public boolean getBoolean(Object obj)
 542         throws IllegalArgumentException, IllegalAccessException
 543     {
 544         if (!override) {
 545             Class<?> caller = Reflection.getCallerClass();
 546             checkAccess(caller, obj);
 547             return getFieldAccessor().getBoolean(obj);
 548         } else {
 549             return getOverrideFieldAccessor().getBoolean(obj);
 550         }
 551     }
 552 
 553     /**
 554      * Gets the value of a static or instance {@code byte} field.
 555      *
 556      * @param obj the object to extract the {@code byte} value
 557      * from
 558      * @return the value of the {@code byte} field
 559      *
 560      * @throws    IllegalAccessException    if this {@code Field} object
 561      *              is enforcing Java language access control and the underlying
 562      *              field is inaccessible.
 563      * @throws    IllegalArgumentException  if the specified object is not
 564      *              an instance of the class or interface declaring the
 565      *              underlying field (or a subclass or implementor
 566      *              thereof), or if the field value cannot be
 567      *              converted to the type {@code byte} by a
 568      *              widening conversion.
 569      * @throws    NullPointerException      if the specified object is null
 570      *              and the field is an instance field.
 571      * @throws    ExceptionInInitializerError if the initialization provoked
 572      *              by this method fails.
 573      * @throws    IllegalStateException if the current thread is initializing the
 574      *              field's {@linkplain #getDeclaringClass() declaring class} and
 575      *              the field is a {@linkplain #isStrictInit() strictly-initialized}
 576      *              static field that has not been initialized.
 577      * @see       Field#get
 578      */
 579     @CallerSensitive
 580     @ForceInline // to ensure Reflection.getCallerClass optimization
 581     public byte getByte(Object obj)
 582         throws IllegalArgumentException, IllegalAccessException
 583     {
 584         if (!override) {
 585             Class<?> caller = Reflection.getCallerClass();
 586             checkAccess(caller, obj);
 587             return getFieldAccessor().getByte(obj);
 588         } else {
 589             return getOverrideFieldAccessor().getByte(obj);
 590         }
 591     }
 592 
 593     /**
 594      * Gets the value of a static or instance field of type
 595      * {@code char} or of another primitive type convertible to
 596      * type {@code char} via a widening conversion.
 597      *
 598      * @param obj the object to extract the {@code char} value
 599      * from
 600      * @return the value of the field converted to type {@code char}
 601      *
 602      * @throws    IllegalAccessException    if this {@code Field} object
 603      *              is enforcing Java language access control and the underlying
 604      *              field is inaccessible.
 605      * @throws    IllegalArgumentException  if the specified object is not
 606      *              an instance of the class or interface declaring the
 607      *              underlying field (or a subclass or implementor
 608      *              thereof), or if the field value cannot be
 609      *              converted to the type {@code char} by a
 610      *              widening conversion.
 611      * @throws    NullPointerException      if the specified object is null
 612      *              and the field is an instance field.
 613      * @throws    ExceptionInInitializerError if the initialization provoked
 614      *              by this method fails.
 615      * @throws    IllegalStateException if the current thread is initializing the
 616      *              field's {@linkplain #getDeclaringClass() declaring class} and
 617      *              the field is a {@linkplain #isStrictInit() strictly-initialized}
 618      *              static field that has not been initialized.
 619      * @see Field#get
 620      */
 621     @CallerSensitive
 622     @ForceInline // to ensure Reflection.getCallerClass optimization
 623     public char getChar(Object obj)
 624         throws IllegalArgumentException, IllegalAccessException
 625     {
 626         if (!override) {
 627             Class<?> caller = Reflection.getCallerClass();
 628             checkAccess(caller, obj);
 629             return getFieldAccessor().getChar(obj);
 630         } else {
 631             return getOverrideFieldAccessor().getChar(obj);
 632         }
 633     }
 634 
 635     /**
 636      * Gets the value of a static or instance field of type
 637      * {@code short} or of another primitive type convertible to
 638      * type {@code short} via a widening conversion.
 639      *
 640      * @param obj the object to extract the {@code short} value
 641      * from
 642      * @return the value of the field converted to type {@code short}
 643      *
 644      * @throws    IllegalAccessException    if this {@code Field} object
 645      *              is enforcing Java language access control and the underlying
 646      *              field is inaccessible.
 647      * @throws    IllegalArgumentException  if the specified object is not
 648      *              an instance of the class or interface declaring the
 649      *              underlying field (or a subclass or implementor
 650      *              thereof), or if the field value cannot be
 651      *              converted to the type {@code short} by a
 652      *              widening conversion.
 653      * @throws    NullPointerException      if the specified object is null
 654      *              and the field is an instance field.
 655      * @throws    ExceptionInInitializerError if the initialization provoked
 656      *              by this method fails.
 657      * @throws    IllegalStateException if the current thread is initializing the
 658      *              field's {@linkplain #getDeclaringClass() declaring class} and
 659      *              the field is a {@linkplain #isStrictInit() strictly-initialized}
 660      *              static field that has not been initialized.
 661      * @see       Field#get
 662      */
 663     @CallerSensitive
 664     @ForceInline // to ensure Reflection.getCallerClass optimization
 665     public short getShort(Object obj)
 666         throws IllegalArgumentException, IllegalAccessException
 667     {
 668         if (!override) {
 669             Class<?> caller = Reflection.getCallerClass();
 670             checkAccess(caller, obj);
 671             return getFieldAccessor().getShort(obj);
 672         } else {
 673             return getOverrideFieldAccessor().getShort(obj);
 674         }
 675     }
 676 
 677     /**
 678      * Gets the value of a static or instance field of type
 679      * {@code int} or of another primitive type convertible to
 680      * type {@code int} via a widening conversion.
 681      *
 682      * @param obj the object to extract the {@code int} value
 683      * from
 684      * @return the value of the field converted to type {@code int}
 685      *
 686      * @throws    IllegalAccessException    if this {@code Field} object
 687      *              is enforcing Java language access control and the underlying
 688      *              field is inaccessible.
 689      * @throws    IllegalArgumentException  if the specified object is not
 690      *              an instance of the class or interface declaring the
 691      *              underlying field (or a subclass or implementor
 692      *              thereof), or if the field value cannot be
 693      *              converted to the type {@code int} by a
 694      *              widening conversion.
 695      * @throws    NullPointerException      if the specified object is null
 696      *              and the field is an instance field.
 697      * @throws    ExceptionInInitializerError if the initialization provoked
 698      *              by this method fails.
 699      * @throws    IllegalStateException if the current thread is initializing the
 700      *              field's {@linkplain #getDeclaringClass() declaring class} and
 701      *              the field is a {@linkplain #isStrictInit() strictly-initialized}
 702      *              static field that has not been initialized.
 703      * @see       Field#get
 704      */
 705     @CallerSensitive
 706     @ForceInline // to ensure Reflection.getCallerClass optimization
 707     public int getInt(Object obj)
 708         throws IllegalArgumentException, IllegalAccessException
 709     {
 710         if (!override) {
 711             Class<?> caller = Reflection.getCallerClass();
 712             checkAccess(caller, obj);
 713             return getFieldAccessor().getInt(obj);
 714         } else {
 715             return getOverrideFieldAccessor().getInt(obj);
 716         }
 717     }
 718 
 719     /**
 720      * Gets the value of a static or instance field of type
 721      * {@code long} or of another primitive type convertible to
 722      * type {@code long} via a widening conversion.
 723      *
 724      * @param obj the object to extract the {@code long} value
 725      * from
 726      * @return the value of the field converted to type {@code long}
 727      *
 728      * @throws    IllegalAccessException    if this {@code Field} object
 729      *              is enforcing Java language access control and the underlying
 730      *              field is inaccessible.
 731      * @throws    IllegalArgumentException  if the specified object is not
 732      *              an instance of the class or interface declaring the
 733      *              underlying field (or a subclass or implementor
 734      *              thereof), or if the field value cannot be
 735      *              converted to the type {@code long} by a
 736      *              widening conversion.
 737      * @throws    NullPointerException      if the specified object is null
 738      *              and the field is an instance field.
 739      * @throws    ExceptionInInitializerError if the initialization provoked
 740      *              by this method fails.
 741      * @throws    IllegalStateException if the current thread is initializing the
 742      *              field's {@linkplain #getDeclaringClass() declaring class} and
 743      *              the field is a {@linkplain #isStrictInit() strictly-initialized}
 744      *              static field that has not been initialized.
 745      * @see       Field#get
 746      */
 747     @CallerSensitive
 748     @ForceInline // to ensure Reflection.getCallerClass optimization
 749     public long getLong(Object obj)
 750         throws IllegalArgumentException, IllegalAccessException
 751     {
 752         if (!override) {
 753             Class<?> caller = Reflection.getCallerClass();
 754             checkAccess(caller, obj);
 755             return getFieldAccessor().getLong(obj);
 756         } else {
 757             return getOverrideFieldAccessor().getLong(obj);
 758         }
 759     }
 760 
 761     /**
 762      * Gets the value of a static or instance field of type
 763      * {@code float} or of another primitive type convertible to
 764      * type {@code float} via a widening conversion.
 765      *
 766      * @param obj the object to extract the {@code float} value
 767      * from
 768      * @return the value of the field converted to type {@code float}
 769      *
 770      * @throws    IllegalAccessException    if this {@code Field} object
 771      *              is enforcing Java language access control and the underlying
 772      *              field is inaccessible.
 773      * @throws    IllegalArgumentException  if the specified object is not
 774      *              an instance of the class or interface declaring the
 775      *              underlying field (or a subclass or implementor
 776      *              thereof), or if the field value cannot be
 777      *              converted to the type {@code float} by a
 778      *              widening conversion.
 779      * @throws    NullPointerException      if the specified object is null
 780      *              and the field is an instance field.
 781      * @throws    ExceptionInInitializerError if the initialization provoked
 782      *              by this method fails.
 783      * @throws    IllegalStateException if the current thread is initializing the
 784      *              field's {@linkplain #getDeclaringClass() declaring class} and
 785      *              the field is a {@linkplain #isStrictInit() strictly-initialized}
 786      *              static field that has not been initialized.
 787      * @see Field#get
 788      */
 789     @CallerSensitive
 790     @ForceInline // to ensure Reflection.getCallerClass optimization
 791     public float getFloat(Object obj)
 792         throws IllegalArgumentException, IllegalAccessException
 793     {
 794         if (!override) {
 795             Class<?> caller = Reflection.getCallerClass();
 796             checkAccess(caller, obj);
 797             return getFieldAccessor().getFloat(obj);
 798         } else {
 799             return getOverrideFieldAccessor().getFloat(obj);
 800         }
 801     }
 802 
 803     /**
 804      * Gets the value of a static or instance field of type
 805      * {@code double} or of another primitive type convertible to
 806      * type {@code double} via a widening conversion.
 807      *
 808      * @param obj the object to extract the {@code double} value
 809      * from
 810      * @return the value of the field converted to type {@code double}
 811      *
 812      * @throws    IllegalAccessException    if this {@code Field} object
 813      *              is enforcing Java language access control and the underlying
 814      *              field is inaccessible.
 815      * @throws    IllegalArgumentException  if the specified object is not
 816      *              an instance of the class or interface declaring the
 817      *              underlying field (or a subclass or implementor
 818      *              thereof), or if the field value cannot be
 819      *              converted to the type {@code double} by a
 820      *              widening conversion.
 821      * @throws    NullPointerException      if the specified object is null
 822      *              and the field is an instance field.
 823      * @throws    ExceptionInInitializerError if the initialization provoked
 824      *              by this method fails.
 825      * @throws    IllegalStateException if the current thread is initializing the
 826      *              field's {@linkplain #getDeclaringClass() declaring class} and
 827      *              the field is a {@linkplain #isStrictInit() strictly-initialized}
 828      *              static field that has not been initialized.
 829      * @see       Field#get
 830      */
 831     @CallerSensitive
 832     @ForceInline // to ensure Reflection.getCallerClass optimization
 833     public double getDouble(Object obj)
 834         throws IllegalArgumentException, IllegalAccessException
 835     {
 836         if (!override) {
 837             Class<?> caller = Reflection.getCallerClass();
 838             checkAccess(caller, obj);
 839             return getFieldAccessor().getDouble(obj);
 840         } else {
 841             return getOverrideFieldAccessor().getDouble(obj);
 842         }
 843     }
 844 
 845     /**
 846      * Sets the field represented by this {@code Field} object on the
 847      * specified object argument to the specified new value. The new
 848      * value is automatically unwrapped if the underlying field has a

 866      * <p>If the underlying field is final, this {@code Field} object has <em>write</em>
 867      * access if and only if all of the following conditions are true, where {@code D} is
 868      * the field's {@linkplain #getDeclaringClass() declaring class}:
 869      *
 870      * <ul>
 871      * <li>{@link #setAccessible(boolean) setAccessible(true)} has succeeded for this
 872      *     {@code Field} object.</li>
 873      * <li><a href="doc-files/MutationMethods.html">final field mutation is enabled</a>
 874      *     for the caller's module.</li>
 875      * <li> At least one of the following conditions holds:
 876      *     <ol type="a">
 877      *     <li> {@code D} and the caller class are in the same module.</li>
 878      *     <li> The field is {@code public} and {@code D} is {@code public} in a package
 879      *     that the module containing {@code D} exports to at least the caller's module.</li>
 880      *     <li> {@code D} is in a package that is {@linkplain Module#isOpen(String, Module)
 881      *     open} to the caller's module.</li>
 882      *     </ol>
 883      * </li>
 884      * <li>{@code D} is not a {@linkplain Class#isRecord() record class}.</li>
 885      * <li>{@code D} is not a {@linkplain Class#isHidden() hidden class}.</li>
 886      * <li>{@code D} is not a {@linkplain Class#isValue() value class}.</li>
 887      * <li>The field is non-static.</li>
 888      * <li>The field is not a {@linkplain #isStrictInit() strictly-initialized} field. </li>
 889      * </ul>
 890      *
 891      * <p>If any of the above conditions is not met, this method throws an
 892      * {@code IllegalAccessException}.
 893      *
 894      * <p>These conditions are more restrictive than the conditions specified by {@link
 895      * #setAccessible(boolean)} to suppress access checks. In particular, updating a
 896      * module to export or open a package cannot be used to allow <em>write</em> access
 897      * to final fields with the {@code set} methods defined by {@code Field}.
 898      * Condition (b) is not met if the module containing {@code D} has been updated with
 899      * {@linkplain Module#addExports(String, Module) addExports} to export the package to
 900      * the caller's module. Condition (c) is not met if the module containing {@code D}
 901      * has been updated with {@linkplain Module#addOpens(String, Module) addOpens} to open
 902      * the package to the caller's module.
 903      *
 904      * <p>This method may be called by <a href="{@docRoot}/../specs/jni/index.html">
 905      * JNI code</a> with no caller class on the stack. In that case, and when the
 906      * underlying field is final, this {@code Field} object has <em>write</em> access
 907      * if and only if all of the following conditions are true, where {@code D} is the
 908      * field's {@linkplain #getDeclaringClass() declaring class}:
 909      *
 910      * <ul>
 911      * <li>{@code setAccessible(true)} has succeeded for this {@code Field} object.</li>
 912      * <li>final field mutation is enabled for the unnamed module.</li>
 913      * <li>The field is {@code public} and {@code D} is {@code public} in a package that
 914      *     is {@linkplain Module#isExported(String) exported} to all modules.</li>
 915      * <li>{@code D} is not a {@linkplain Class#isRecord() record class}.</li>
 916      * <li>{@code D} is not a {@linkplain Class#isHidden() hidden class}.</li>
 917      * <li>{@code D} is not a {@linkplain Class#isValue() value class}.</li>
 918      * <li>The field is non-static.</li>
 919      * <li>The field is not a {@linkplain #isStrictInit() strictly-initialized} field. </li>
 920      * </ul>
 921      *
 922      * <p>If any of the above conditions is not met, this method throws an
 923      * {@code IllegalAccessException}.
 924      *
 925      * <p> Setting a final field in this way
 926      * is meaningful only during deserialization or reconstruction of
 927      * instances of classes with blank final fields, before they are
 928      * made available for access by other parts of a program. Use in
 929      * any other context may have unpredictable effects, including cases
 930      * in which other parts of a program continue to use the original
 931      * value of this field.
 932      *
 933      * <p>If the underlying field is of a primitive type, an unwrapping
 934      * conversion is attempted to convert the new value to a value of
 935      * a primitive type.  If this attempt fails, the method throws an
 936      * {@code IllegalArgumentException}.
 937      *
 938      * <p>If, after possible unwrapping, the new value cannot be
 939      * converted to the type of the underlying field by an identity or

1410             root.setFieldAccessor(accessor);
1411         }
1412     }
1413 
1414     // Sets the overrideFieldAccessor for this Field object and
1415     // (recursively) its root
1416     private void setOverrideFieldAccessor(FieldAccessor accessor) {
1417         overrideFieldAccessor = accessor;
1418         // Propagate up
1419         Field root = this.root;
1420         if (root != null) {
1421             root.setOverrideFieldAccessor(accessor);
1422         }
1423     }
1424 
1425     @Override
1426     /* package-private */ Field getRoot() {
1427         return root;
1428     }
1429 
1430     private static final int TRUST_FINAL     = 0x0010;
1431     private static final int NULL_RESTRICTED = 0x0020;
1432 
1433     /* package-private */ boolean isTrustedFinal() {
1434         return (flags & TRUST_FINAL) == TRUST_FINAL;
1435     }
1436 
1437     /* package-private */ boolean isNullRestricted() {
1438         return (flags & NULL_RESTRICTED) == NULL_RESTRICTED;
1439     }
1440 
1441     /**
1442      * {@inheritDoc}
1443      *
1444      * @throws NullPointerException {@inheritDoc}
1445      * @since 1.5
1446      */
1447     @Override
1448     public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
1449         Objects.requireNonNull(annotationClass);
1450         return annotationClass.cast(declaredAnnotations().get(annotationClass));
1451     }
1452 
1453     /**
1454      * {@inheritDoc}
1455      *
1456      * @throws NullPointerException {@inheritDoc}
1457      * @since 1.8
1458      */
< prev index next >