1 /*
   2  * Copyright (c) 1996, 2025, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  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.
  65  *
  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
 107     private GenericsFactory getFactory() {
 108         Class<?> c = getDeclaringClass();
 109         // create scope and factory
 110         return CoreReflectionFactory.make(c, ClassScope.make(c));
 111     }
 112 
 113     // Accessor for generic info repository
 114     private FieldRepository getGenericInfo() {
 115         var genericInfo = this.genericInfo;
 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      * </ul>
 194      * <p>If this reflected object represents a non-static final field in a class that
 195      * is not a record class or hidden class, then enabling access will enable read
 196      * access. Whether write access is allowed or not is checked when attempting to
 197      * {@linkplain #set(Object, Object) set} the field value.
 198      *
 199      * @throws InaccessibleObjectException {@inheritDoc}
 200      */
 201     @Override
 202     @CallerSensitive
 203     public void setAccessible(boolean flag) {
 204         if (flag) checkCanSetAccessible(Reflection.getCallerClass());
 205         setAccessible0(flag);
 206     }
 207 
 208     @Override
 209     void checkCanSetAccessible(Class<?> caller) {
 210         checkCanSetAccessible(caller, clazz);
 211     }
 212 
 213     /**
 214      * Returns the {@code Class} object representing the class or interface
 215      * that declares the field represented by this {@code Field} object.
 216      */
 217     @Override
 218     public Class<?> getDeclaringClass() {
 219         return clazz;
 220     }
 221 
 222     /**
 223      * Returns the name of the field represented by this {@code Field} object.
 224      */
 225     public String getName() {
 226         return name;
 227     }
 228 
 229     /**
 230      * Returns the Java language modifiers for the field represented
 231      * by this {@code Field} object, as an integer. The {@code Modifier} class should
 232      * be used to decode the modifiers.
 233      *
 234      * @see Modifier
 235      * @see #accessFlags()
 236      * @jls 8.3 Field Declarations
 237      * @jls 9.3 Field (Constant) Declarations
 238      */
 239     public int getModifiers() {
 240         return modifiers;
 241     }
 242 
 243     /**
 244      * {@return an unmodifiable set of the {@linkplain AccessFlag
 245      * access flags} for this field, possibly empty}
 246      * The {@code AccessFlags} may depend on the class file format version of the class.
 247      *
 248      * @see #getModifiers()
 249      * @jvms 4.5 Fields
 250      * @since 20
 251      */
 252     @Override
 253     public Set<AccessFlag> accessFlags() {
 254         return AccessFlagSet.ofValidated(PreviewAccessFlags.FIELD_PREVIEW_FLAGS, getModifiers());
 255     }
 256 
 257     /**
 258      * Returns {@code true} if this field represents an element of
 259      * an enumerated class; returns {@code false} otherwise.
 260      *
 261      * @return {@code true} if and only if this field represents an element of
 262      * an enumerated class.
 263      * @since 1.5
 264      * @jls 8.9.1 Enum Constants
 265      */
 266     public boolean isEnumConstant() {
 267         return (getModifiers() & Modifier.ENUM) != 0;
 268     }
 269 
 270     /**
 271      * Returns {@code true} if this field is a synthetic
 272      * field; returns {@code false} otherwise.
 273      *
 274      * @return true if and only if this field is a synthetic
 275      * field as defined by the Java Language Specification.
 276      * @since 1.5
 277      * @see <a
 278      * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java
 279      * programming language and JVM modeling in core reflection</a>
 280      */
 281     public boolean isSynthetic() {
 282         return Modifier.isSynthetic(getModifiers());
 283     }
 284 
 285     /**
 286      * Returns {@code true} if this field is a strictly
 287      * initialized field; returns {@code false} otherwise.
 288      *
 289      * @return true if and only if this field is a strictly
 290      * initialized field as defined by the Java Virtual Machine Specification
 291      * @jvms strict-fields-4.5 Field access and property flags
 292      * @since Valhalla
 293      */
 294     @PreviewFeature(feature = PreviewFeature.Feature.STRICT_FIELDS, reflective = true)
 295     public boolean isStrictInit() {
 296         return accessFlags().contains(AccessFlag.STRICT_INIT);
 297     }
 298 
 299     /**
 300      * Returns a {@code Class} object that identifies the
 301      * declared type for the field represented by this
 302      * {@code Field} object.
 303      *
 304      * @return a {@code Class} object identifying the declared
 305      * type of the field represented by this object
 306      */
 307     public Class<?> getType() {
 308         return type;
 309     }
 310 
 311     /**
 312      * Returns a {@code Type} object that represents the declared type for
 313      * the field represented by this {@code Field} object.
 314      *
 315      * <p>If the declared type of the field is a parameterized type,
 316      * the {@code Type} object returned must accurately reflect the
 317      * actual type arguments used in the source code.
 318      *
 319      * <p>If the type of the underlying field is a type variable or a
 320      * parameterized type, it is created. Otherwise, it is resolved.
 321      *
 322      * @return a {@code Type} object that represents the declared type for
 323      *     the field represented by this {@code Field} object
 324      * @throws GenericSignatureFormatError if the generic field
 325      *     signature does not conform to the format specified in
 326      *     <cite>The Java Virtual Machine Specification</cite>
 327      * @throws TypeNotPresentException if the generic type
 328      *     signature of the underlying field refers to a non-existent
 329      *     class or interface declaration
 330      * @throws MalformedParameterizedTypeException if the generic
 331      *     signature of the underlying field refers to a parameterized type
 332      *     that cannot be instantiated for any reason
 333      * @since 1.5
 334      */
 335     public Type getGenericType() {
 336         if (getGenericSignature() != null)
 337             return getGenericInfo().getGenericType();
 338         else
 339             return getType();
 340     }
 341 
 342 
 343     /**
 344      * Compares this {@code Field} against the specified object.  Returns
 345      * true if the objects are the same.  Two {@code Field} objects are the same if
 346      * they were declared by the same class and have the same name
 347      * and type.
 348      */
 349     public boolean equals(Object obj) {
 350         if (obj instanceof Field other) {
 351             return (getDeclaringClass() == other.getDeclaringClass())
 352                 && (getName() == other.getName())
 353                 && (getType() == other.getType());
 354         }
 355         return false;
 356     }
 357 
 358     /**
 359      * Returns a hashcode for this {@code Field}.  This is computed as the
 360      * exclusive-or of the hashcodes for the underlying field's
 361      * declaring class name and its name.
 362      */
 363     public int hashCode() {
 364         return getDeclaringClass().getName().hashCode() ^ getName().hashCode();
 365     }
 366 
 367     /**
 368      * Returns a string describing this {@code Field}.  The format is
 369      * the access modifiers for the field, if any, followed
 370      * by the field type, followed by a space, followed by
 371      * the fully-qualified name of the class declaring the field,
 372      * followed by a period, followed by the name of the field.
 373      * For example:
 374      * <pre>
 375      *    public static final int java.lang.Thread.MIN_PRIORITY
 376      *    private int java.io.FileDescriptor.fd
 377      * </pre>
 378      *
 379      * <p>The modifiers are placed in canonical order as specified by
 380      * "The Java Language Specification".  This is {@code public},
 381      * {@code protected} or {@code private} first, and then other
 382      * modifiers in the following order: {@code static}, {@code final},
 383      * {@code transient}, {@code volatile}.
 384      *
 385      * @return a string describing this {@code Field}
 386      * @jls 8.3.1 Field Modifiers
 387      */
 388     public String toString() {
 389         int mod = getModifiers() & Modifier.fieldModifiers();
 390         return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
 391             + getType().getTypeName() + " "
 392             + getDeclaringClass().getTypeName() + "."
 393             + getName());
 394     }
 395 
 396     @Override
 397     String toShortString() {
 398         return "field " + getDeclaringClass().getTypeName() + "." + getName();
 399     }
 400 
 401     /**
 402      * Returns a string describing this {@code Field}, including
 403      * its generic type.  The format is the access modifiers for the
 404      * field, if any, followed by the generic field type, followed by
 405      * a space, followed by the fully-qualified name of the class
 406      * declaring the field, followed by a period, followed by the name
 407      * of the field.
 408      *
 409      * <p>The modifiers are placed in canonical order as specified by
 410      * "The Java Language Specification".  This is {@code public},
 411      * {@code protected} or {@code private} first, and then other
 412      * modifiers in the following order: {@code static}, {@code final},
 413      * {@code transient}, {@code volatile}.
 414      *
 415      * @return a string describing this {@code Field}, including
 416      * its generic type
 417      *
 418      * @since 1.5
 419      * @jls 8.3.1 Field Modifiers
 420      */
 421     public String toGenericString() {
 422         int mod = getModifiers() & Modifier.fieldModifiers();
 423         Type fieldType = getGenericType();
 424         return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
 425             + fieldType.getTypeName() + " "
 426             + getDeclaringClass().getTypeName() + "."
 427             + getName());
 428     }
 429 
 430     /**
 431      * Returns the value of the field represented by this {@code Field}, on
 432      * the specified object. The value is automatically wrapped in an
 433      * object if it has a primitive type.
 434      *
 435      * <p>The underlying field's value is obtained as follows:
 436      *
 437      * <p>If the underlying field is a static field, the {@code obj} argument
 438      * is ignored; it may be null.
 439      *
 440      * <p>Otherwise, the underlying field is an instance field.  If the
 441      * specified {@code obj} argument is null, the method throws a
 442      * {@code NullPointerException}. If the specified object is not an
 443      * instance of the class or interface declaring the underlying
 444      * field, the method throws an {@code IllegalArgumentException}.
 445      *
 446      * <p>If this {@code Field} object is enforcing Java language access control, and
 447      * the underlying field is inaccessible, the method throws an
 448      * {@code IllegalAccessException}.
 449      * If the underlying field is static, the class that declared the
 450      * field is initialized if it has not already been initialized.
 451      *
 452      * <p>Otherwise, the value is retrieved from the underlying instance
 453      * or static field.  If the field has a primitive type, the value
 454      * is wrapped in an object before being returned, otherwise it is
 455      * returned as is.
 456      *
 457      * <p>If the field is hidden in the type of {@code obj},
 458      * the field's value is obtained according to the preceding rules.
 459      *
 460      * @param obj object from which the represented field's value is
 461      * to be extracted
 462      * @return the value of the represented field in object
 463      * {@code obj}; primitive values are wrapped in an appropriate
 464      * object before being returned
 465      *
 466      * @throws    IllegalAccessException    if this {@code Field} object
 467      *              is enforcing Java language access control and the underlying
 468      *              field is inaccessible.
 469      * @throws    IllegalArgumentException  if the specified object is not an
 470      *              instance of the class or interface declaring the underlying
 471      *              field (or a subclass or implementor thereof).
 472      * @throws    NullPointerException      if the specified object is null
 473      *              and the field is an instance field.
 474      * @throws    ExceptionInInitializerError if the initialization provoked
 475      *              by this method fails.
 476      */
 477     @CallerSensitive
 478     @ForceInline // to ensure Reflection.getCallerClass optimization
 479     public Object get(Object obj)
 480         throws IllegalArgumentException, IllegalAccessException
 481     {
 482         if (!override) {
 483             Class<?> caller = Reflection.getCallerClass();
 484             checkAccess(caller, obj);
 485             return getFieldAccessor().get(obj);
 486         } else {
 487             return getOverrideFieldAccessor().get(obj);
 488         }
 489     }
 490 
 491     /**
 492      * Gets the value of a static or instance {@code boolean} field.
 493      *
 494      * @param obj the object to extract the {@code boolean} value
 495      * from
 496      * @return the value of the {@code boolean} field
 497      *
 498      * @throws    IllegalAccessException    if this {@code Field} object
 499      *              is enforcing Java language access control and the underlying
 500      *              field is inaccessible.
 501      * @throws    IllegalArgumentException  if the specified object is not
 502      *              an instance of the class or interface declaring the
 503      *              underlying field (or a subclass or implementor
 504      *              thereof), or if the field value cannot be
 505      *              converted to the type {@code boolean} by a
 506      *              widening conversion.
 507      * @throws    NullPointerException      if the specified object is null
 508      *              and the field is an instance field.
 509      * @throws    ExceptionInInitializerError if the initialization provoked
 510      *              by this method fails.
 511      * @see       Field#get
 512      */
 513     @CallerSensitive
 514     @ForceInline // to ensure Reflection.getCallerClass optimization
 515     public boolean getBoolean(Object obj)
 516         throws IllegalArgumentException, IllegalAccessException
 517     {
 518         if (!override) {
 519             Class<?> caller = Reflection.getCallerClass();
 520             checkAccess(caller, obj);
 521             return getFieldAccessor().getBoolean(obj);
 522         } else {
 523             return getOverrideFieldAccessor().getBoolean(obj);
 524         }
 525     }
 526 
 527     /**
 528      * Gets the value of a static or instance {@code byte} field.
 529      *
 530      * @param obj the object to extract the {@code byte} value
 531      * from
 532      * @return the value of the {@code byte} field
 533      *
 534      * @throws    IllegalAccessException    if this {@code Field} object
 535      *              is enforcing Java language access control and the underlying
 536      *              field is inaccessible.
 537      * @throws    IllegalArgumentException  if the specified object is not
 538      *              an instance of the class or interface declaring the
 539      *              underlying field (or a subclass or implementor
 540      *              thereof), or if the field value cannot be
 541      *              converted to the type {@code byte} by a
 542      *              widening conversion.
 543      * @throws    NullPointerException      if the specified object is null
 544      *              and the field is an instance field.
 545      * @throws    ExceptionInInitializerError if the initialization provoked
 546      *              by this method fails.
 547      * @see       Field#get
 548      */
 549     @CallerSensitive
 550     @ForceInline // to ensure Reflection.getCallerClass optimization
 551     public byte getByte(Object obj)
 552         throws IllegalArgumentException, IllegalAccessException
 553     {
 554         if (!override) {
 555             Class<?> caller = Reflection.getCallerClass();
 556             checkAccess(caller, obj);
 557             return getFieldAccessor().getByte(obj);
 558         } else {
 559             return getOverrideFieldAccessor().getByte(obj);
 560         }
 561     }
 562 
 563     /**
 564      * Gets the value of a static or instance field of type
 565      * {@code char} or of another primitive type convertible to
 566      * type {@code char} via a widening conversion.
 567      *
 568      * @param obj the object to extract the {@code char} value
 569      * from
 570      * @return the value of the field converted to type {@code char}
 571      *
 572      * @throws    IllegalAccessException    if this {@code Field} object
 573      *              is enforcing Java language access control and the underlying
 574      *              field is inaccessible.
 575      * @throws    IllegalArgumentException  if the specified object is not
 576      *              an instance of the class or interface declaring the
 577      *              underlying field (or a subclass or implementor
 578      *              thereof), or if the field value cannot be
 579      *              converted to the type {@code char} by a
 580      *              widening conversion.
 581      * @throws    NullPointerException      if the specified object is null
 582      *              and the field is an instance field.
 583      * @throws    ExceptionInInitializerError if the initialization provoked
 584      *              by this method fails.
 585      * @see Field#get
 586      */
 587     @CallerSensitive
 588     @ForceInline // to ensure Reflection.getCallerClass optimization
 589     public char getChar(Object obj)
 590         throws IllegalArgumentException, IllegalAccessException
 591     {
 592         if (!override) {
 593             Class<?> caller = Reflection.getCallerClass();
 594             checkAccess(caller, obj);
 595             return getFieldAccessor().getChar(obj);
 596         } else {
 597             return getOverrideFieldAccessor().getChar(obj);
 598         }
 599     }
 600 
 601     /**
 602      * Gets the value of a static or instance field of type
 603      * {@code short} or of another primitive type convertible to
 604      * type {@code short} via a widening conversion.
 605      *
 606      * @param obj the object to extract the {@code short} value
 607      * from
 608      * @return the value of the field converted to type {@code short}
 609      *
 610      * @throws    IllegalAccessException    if this {@code Field} object
 611      *              is enforcing Java language access control and the underlying
 612      *              field is inaccessible.
 613      * @throws    IllegalArgumentException  if the specified object is not
 614      *              an instance of the class or interface declaring the
 615      *              underlying field (or a subclass or implementor
 616      *              thereof), or if the field value cannot be
 617      *              converted to the type {@code short} by a
 618      *              widening conversion.
 619      * @throws    NullPointerException      if the specified object is null
 620      *              and the field is an instance field.
 621      * @throws    ExceptionInInitializerError if the initialization provoked
 622      *              by this method fails.
 623      * @see       Field#get
 624      */
 625     @CallerSensitive
 626     @ForceInline // to ensure Reflection.getCallerClass optimization
 627     public short getShort(Object obj)
 628         throws IllegalArgumentException, IllegalAccessException
 629     {
 630         if (!override) {
 631             Class<?> caller = Reflection.getCallerClass();
 632             checkAccess(caller, obj);
 633             return getFieldAccessor().getShort(obj);
 634         } else {
 635             return getOverrideFieldAccessor().getShort(obj);
 636         }
 637     }
 638 
 639     /**
 640      * Gets the value of a static or instance field of type
 641      * {@code int} or of another primitive type convertible to
 642      * type {@code int} via a widening conversion.
 643      *
 644      * @param obj the object to extract the {@code int} value
 645      * from
 646      * @return the value of the field converted to type {@code int}
 647      *
 648      * @throws    IllegalAccessException    if this {@code Field} object
 649      *              is enforcing Java language access control and the underlying
 650      *              field is inaccessible.
 651      * @throws    IllegalArgumentException  if the specified object is not
 652      *              an instance of the class or interface declaring the
 653      *              underlying field (or a subclass or implementor
 654      *              thereof), or if the field value cannot be
 655      *              converted to the type {@code int} by a
 656      *              widening conversion.
 657      * @throws    NullPointerException      if the specified object is null
 658      *              and the field is an instance field.
 659      * @throws    ExceptionInInitializerError if the initialization provoked
 660      *              by this method fails.
 661      * @see       Field#get
 662      */
 663     @CallerSensitive
 664     @ForceInline // to ensure Reflection.getCallerClass optimization
 665     public int getInt(Object obj)
 666         throws IllegalArgumentException, IllegalAccessException
 667     {
 668         if (!override) {
 669             Class<?> caller = Reflection.getCallerClass();
 670             checkAccess(caller, obj);
 671             return getFieldAccessor().getInt(obj);
 672         } else {
 673             return getOverrideFieldAccessor().getInt(obj);
 674         }
 675     }
 676 
 677     /**
 678      * Gets the value of a static or instance field of type
 679      * {@code long} or of another primitive type convertible to
 680      * type {@code long} via a widening conversion.
 681      *
 682      * @param obj the object to extract the {@code long} value
 683      * from
 684      * @return the value of the field converted to type {@code long}
 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 long} 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      * @see       Field#get
 700      */
 701     @CallerSensitive
 702     @ForceInline // to ensure Reflection.getCallerClass optimization
 703     public long getLong(Object obj)
 704         throws IllegalArgumentException, IllegalAccessException
 705     {
 706         if (!override) {
 707             Class<?> caller = Reflection.getCallerClass();
 708             checkAccess(caller, obj);
 709             return getFieldAccessor().getLong(obj);
 710         } else {
 711             return getOverrideFieldAccessor().getLong(obj);
 712         }
 713     }
 714 
 715     /**
 716      * Gets the value of a static or instance field of type
 717      * {@code float} or of another primitive type convertible to
 718      * type {@code float} via a widening conversion.
 719      *
 720      * @param obj the object to extract the {@code float} value
 721      * from
 722      * @return the value of the field converted to type {@code float}
 723      *
 724      * @throws    IllegalAccessException    if this {@code Field} object
 725      *              is enforcing Java language access control and the underlying
 726      *              field is inaccessible.
 727      * @throws    IllegalArgumentException  if the specified object is not
 728      *              an instance of the class or interface declaring the
 729      *              underlying field (or a subclass or implementor
 730      *              thereof), or if the field value cannot be
 731      *              converted to the type {@code float} by a
 732      *              widening conversion.
 733      * @throws    NullPointerException      if the specified object is null
 734      *              and the field is an instance field.
 735      * @throws    ExceptionInInitializerError if the initialization provoked
 736      *              by this method fails.
 737      * @see Field#get
 738      */
 739     @CallerSensitive
 740     @ForceInline // to ensure Reflection.getCallerClass optimization
 741     public float getFloat(Object obj)
 742         throws IllegalArgumentException, IllegalAccessException
 743     {
 744         if (!override) {
 745             Class<?> caller = Reflection.getCallerClass();
 746             checkAccess(caller, obj);
 747             return getFieldAccessor().getFloat(obj);
 748         } else {
 749             return getOverrideFieldAccessor().getFloat(obj);
 750         }
 751     }
 752 
 753     /**
 754      * Gets the value of a static or instance field of type
 755      * {@code double} or of another primitive type convertible to
 756      * type {@code double} via a widening conversion.
 757      *
 758      * @param obj the object to extract the {@code double} value
 759      * from
 760      * @return the value of the field converted to type {@code double}
 761      *
 762      * @throws    IllegalAccessException    if this {@code Field} object
 763      *              is enforcing Java language access control and the underlying
 764      *              field is inaccessible.
 765      * @throws    IllegalArgumentException  if the specified object is not
 766      *              an instance of the class or interface declaring the
 767      *              underlying field (or a subclass or implementor
 768      *              thereof), or if the field value cannot be
 769      *              converted to the type {@code double} by a
 770      *              widening conversion.
 771      * @throws    NullPointerException      if the specified object is null
 772      *              and the field is an instance field.
 773      * @throws    ExceptionInInitializerError if the initialization provoked
 774      *              by this method fails.
 775      * @see       Field#get
 776      */
 777     @CallerSensitive
 778     @ForceInline // to ensure Reflection.getCallerClass optimization
 779     public double getDouble(Object obj)
 780         throws IllegalArgumentException, IllegalAccessException
 781     {
 782         if (!override) {
 783             Class<?> caller = Reflection.getCallerClass();
 784             checkAccess(caller, obj);
 785             return getFieldAccessor().getDouble(obj);
 786         } else {
 787             return getOverrideFieldAccessor().getDouble(obj);
 788         }
 789     }
 790 
 791     /**
 792      * Sets the field represented by this {@code Field} object on the
 793      * specified object argument to the specified new value. The new
 794      * value is automatically unwrapped if the underlying field has a
 795      * primitive type.
 796      *
 797      * <p>The operation proceeds as follows:
 798      *
 799      * <p>If the underlying field is static, the {@code obj} argument is
 800      * ignored; it may be null.
 801      *
 802      * <p>Otherwise the underlying field is an instance field.  If the
 803      * specified object argument is null, the method throws a
 804      * {@code NullPointerException}.  If the specified object argument is not
 805      * an instance of the class or interface declaring the underlying
 806      * field, the method throws an {@code IllegalArgumentException}.
 807      *
 808      * <p>If this {@code Field} object is enforcing Java language access control, and
 809      * the underlying field is inaccessible, the method throws an
 810      * {@code IllegalAccessException}.
 811      *
 812      * <p>If the underlying field is final, this {@code Field} object has <em>write</em>
 813      * access if and only if all of the following conditions are true, where {@code D} is
 814      * the field's {@linkplain #getDeclaringClass() declaring class}:
 815      *
 816      * <ul>
 817      * <li>{@link #setAccessible(boolean) setAccessible(true)} has succeeded for this
 818      *     {@code Field} object.</li>
 819      * <li><a href="doc-files/MutationMethods.html">final field mutation is enabled</a>
 820      *     for the caller's module.</li>
 821      * <li> At least one of the following conditions holds:
 822      *     <ol type="a">
 823      *     <li> {@code D} and the caller class are in the same module.</li>
 824      *     <li> The field is {@code public} and {@code D} is {@code public} in a package
 825      *     that the module containing {@code D} exports to at least the caller's module.</li>
 826      *     <li> {@code D} is in a package that is {@linkplain Module#isOpen(String, Module)
 827      *     open} to the caller's module.</li>
 828      *     </ol>
 829      * </li>
 830      * <li>{@code D} is not a {@linkplain Class#isRecord() record class}.</li>
 831      * <li>{@code D} is not a {@linkplain Class#isHidden() hidden class}.</li>
 832      * <li>{@code D} is not a {@linkplain Class#isValue() value class}.</li>
 833      * <li>The field is non-static.</li>
 834      * </ul>
 835      *
 836      * <p>If any of the above conditions is not met, this method throws an
 837      * {@code IllegalAccessException}.
 838      *
 839      * <p>These conditions are more restrictive than the conditions specified by {@link
 840      * #setAccessible(boolean)} to suppress access checks. In particular, updating a
 841      * module to export or open a package cannot be used to allow <em>write</em> access
 842      * to final fields with the {@code set} methods defined by {@code Field}.
 843      * Condition (b) is not met if the module containing {@code D} has been updated with
 844      * {@linkplain Module#addExports(String, Module) addExports} to export the package to
 845      * the caller's module. Condition (c) is not met if the module containing {@code D}
 846      * has been updated with {@linkplain Module#addOpens(String, Module) addOpens} to open
 847      * the package to the caller's module.
 848      *
 849      * <p>This method may be called by <a href="{@docRoot}/../specs/jni/index.html">
 850      * JNI code</a> with no caller class on the stack. In that case, and when the
 851      * underlying field is final, this {@code Field} object has <em>write</em> access
 852      * if and only if all of the following conditions are true, where {@code D} is the
 853      * field's {@linkplain #getDeclaringClass() declaring class}:
 854      *
 855      * <ul>
 856      * <li>{@code setAccessible(true)} has succeeded for this {@code Field} object.</li>
 857      * <li>final field mutation is enabled for the unnamed module.</li>
 858      * <li>The field is {@code public} and {@code D} is {@code public} in a package that
 859      *     is {@linkplain Module#isExported(String) exported} to all modules.</li>
 860      * <li>{@code D} is not a {@linkplain Class#isRecord() record class}.</li>
 861      * <li>{@code D} is not a {@linkplain Class#isHidden() hidden class}.</li>
 862      * <li>The field is non-static.</li>
 863      * </ul>
 864      *
 865      * <p>If any of the above conditions is not met, this method throws an
 866      * {@code IllegalAccessException}.
 867      *
 868      * <p> Setting a final field in this way
 869      * is meaningful only during deserialization or reconstruction of
 870      * instances of classes with blank final fields, before they are
 871      * made available for access by other parts of a program. Use in
 872      * any other context may have unpredictable effects, including cases
 873      * in which other parts of a program continue to use the original
 874      * value of this field.
 875      *
 876      * <p>If the underlying field is of a primitive type, an unwrapping
 877      * conversion is attempted to convert the new value to a value of
 878      * a primitive type.  If this attempt fails, the method throws an
 879      * {@code IllegalArgumentException}.
 880      *
 881      * <p>If, after possible unwrapping, the new value cannot be
 882      * converted to the type of the underlying field by an identity or
 883      * widening conversion, the method throws an
 884      * {@code IllegalArgumentException}.
 885      *
 886      * <p>If the underlying field is static, the class that declared the
 887      * field is initialized if it has not already been initialized.
 888      *
 889      * <p>The field is set to the possibly unwrapped and widened new value.
 890      *
 891      * <p>If the field is hidden in the type of {@code obj},
 892      * the field's value is set according to the preceding rules.
 893      *
 894      * @param obj the object whose field should be modified
 895      * @param value the new value for the field of {@code obj}
 896      * being modified
 897      *
 898      * @throws    IllegalAccessException    if this {@code Field} object
 899      *              is enforcing Java language access control and the underlying
 900      *              field is inaccessible or final;
 901      *              or if this {@code Field} object has no write access.
 902      * @throws    IllegalArgumentException  if the specified object is not an
 903      *              instance of the class or interface declaring the underlying
 904      *              field (or a subclass or implementor thereof),
 905      *              or if an unwrapping conversion fails.
 906      * @throws    NullPointerException      if the specified object is null
 907      *              and the field is an instance field.
 908      * @throws    ExceptionInInitializerError if the initialization provoked
 909      *              by this method fails.
 910      *
 911      * @see <a href="doc-files/MutationMethods.html">Mutation methods</a>
 912      */
 913     @CallerSensitive
 914     @ForceInline // to ensure Reflection.getCallerClass optimization
 915     public void set(Object obj, Object value)
 916         throws IllegalArgumentException, IllegalAccessException
 917     {
 918         if (!override) {
 919             Class<?> caller = Reflection.getCallerClass();
 920             checkAccess(caller, obj);
 921             getFieldAccessor().set(obj, value);
 922             return;
 923         }
 924 
 925         FieldAccessor fa = getOverrideFieldAccessor();
 926         if (!Modifier.isFinal(modifiers)) {
 927             fa.set(obj, value);
 928         } else {
 929             setFinal(Reflection.getCallerClass(), obj, () -> fa.set(obj, value));
 930         }
 931     }
 932 
 933     /**
 934      * Sets the value of a field as a {@code boolean} on the specified object.
 935      * This method is equivalent to
 936      * {@code set(obj, zObj)},
 937      * where {@code zObj} is a {@code Boolean} object and
 938      * {@code zObj.booleanValue() == z}.
 939      *
 940      * @param obj the object whose field should be modified
 941      * @param z   the new value for the field of {@code obj}
 942      * being modified
 943      *
 944      * @throws    IllegalAccessException    if this {@code Field} object
 945      *              is enforcing Java language access control and the underlying
 946      *              field is either inaccessible or final;
 947      *              or if this {@code Field} object has no write access.
 948      * @throws    IllegalArgumentException  if the specified object is not an
 949      *              instance of the class or interface declaring the underlying
 950      *              field (or a subclass or implementor thereof),
 951      *              or if an unwrapping conversion fails.
 952      * @throws    NullPointerException      if the specified object is null
 953      *              and the field is an instance field.
 954      * @throws    ExceptionInInitializerError if the initialization provoked
 955      *              by this method fails.
 956      * @see       Field#set
 957      */
 958     @CallerSensitive
 959     @ForceInline // to ensure Reflection.getCallerClass optimization
 960     public void setBoolean(Object obj, boolean z)
 961         throws IllegalArgumentException, IllegalAccessException
 962     {
 963         if (!override) {
 964             Class<?> caller = Reflection.getCallerClass();
 965             checkAccess(caller, obj);
 966             getFieldAccessor().setBoolean(obj, z);
 967             return;
 968         }
 969 
 970         FieldAccessor fa = getOverrideFieldAccessor();
 971         if (!Modifier.isFinal(modifiers)) {
 972             fa.setBoolean(obj, z);
 973         } else {
 974             setFinal(Reflection.getCallerClass(), obj, () -> fa.setBoolean(obj, z));
 975         }
 976     }
 977 
 978     /**
 979      * Sets the value of a field as a {@code byte} on the specified object.
 980      * This method is equivalent to
 981      * {@code set(obj, bObj)},
 982      * where {@code bObj} is a {@code Byte} object and
 983      * {@code bObj.byteValue() == b}.
 984      *
 985      * @param obj the object whose field should be modified
 986      * @param b   the new value for the field of {@code obj}
 987      * being modified
 988      *
 989      * @throws    IllegalAccessException    if this {@code Field} object
 990      *              is enforcing Java language access control and the underlying
 991      *              field is either inaccessible or final;
 992      *              or if this {@code Field} object has no write access.
 993      * @throws    IllegalArgumentException  if the specified object is not an
 994      *              instance of the class or interface declaring the underlying
 995      *              field (or a subclass or implementor thereof),
 996      *              or if an unwrapping conversion fails.
 997      * @throws    NullPointerException      if the specified object is null
 998      *              and the field is an instance field.
 999      * @throws    ExceptionInInitializerError if the initialization provoked
1000      *              by this method fails.
1001      * @see       Field#set
1002      */
1003     @CallerSensitive
1004     @ForceInline // to ensure Reflection.getCallerClass optimization
1005     public void setByte(Object obj, byte b)
1006         throws IllegalArgumentException, IllegalAccessException
1007     {
1008         if (!override) {
1009             Class<?> caller = Reflection.getCallerClass();
1010             checkAccess(caller, obj);
1011             getFieldAccessor().setByte(obj, b);
1012             return;
1013         }
1014 
1015         FieldAccessor fa = getOverrideFieldAccessor();
1016         if (!Modifier.isFinal(modifiers)) {
1017             fa.setByte(obj, b);
1018         } else {
1019             setFinal(Reflection.getCallerClass(), obj, () -> fa.setByte(obj, b));
1020         }
1021     }
1022 
1023     /**
1024      * Sets the value of a field as a {@code char} on the specified object.
1025      * This method is equivalent to
1026      * {@code set(obj, cObj)},
1027      * where {@code cObj} is a {@code Character} object and
1028      * {@code cObj.charValue() == c}.
1029      *
1030      * @param obj the object whose field should be modified
1031      * @param c   the new value for the field of {@code obj}
1032      * being modified
1033      *
1034      * @throws    IllegalAccessException    if this {@code Field} object
1035      *              is enforcing Java language access control and the underlying
1036      *              field is either inaccessible or final;
1037      *              or if this {@code Field} object has no write access.
1038      * @throws    IllegalArgumentException  if the specified object is not an
1039      *              instance of the class or interface declaring the underlying
1040      *              field (or a subclass or implementor thereof),
1041      *              or if an unwrapping conversion fails.
1042      * @throws    NullPointerException      if the specified object is null
1043      *              and the field is an instance field.
1044      * @throws    ExceptionInInitializerError if the initialization provoked
1045      *              by this method fails.
1046      * @see       Field#set
1047      */
1048     @CallerSensitive
1049     @ForceInline // to ensure Reflection.getCallerClass optimization
1050     public void setChar(Object obj, char c)
1051         throws IllegalArgumentException, IllegalAccessException
1052     {
1053         if (!override) {
1054             Class<?> caller = Reflection.getCallerClass();
1055             checkAccess(caller, obj);
1056             getFieldAccessor().setChar(obj, c);
1057             return;
1058         }
1059 
1060         FieldAccessor fa = getOverrideFieldAccessor();
1061         if (!Modifier.isFinal(modifiers)) {
1062             fa.setChar(obj, c);
1063         } else {
1064             setFinal(Reflection.getCallerClass(), obj, () -> fa.setChar(obj, c));
1065         }
1066     }
1067 
1068     /**
1069      * Sets the value of a field as a {@code short} on the specified object.
1070      * This method is equivalent to
1071      * {@code set(obj, sObj)},
1072      * where {@code sObj} is a {@code Short} object and
1073      * {@code sObj.shortValue() == s}.
1074      *
1075      * @param obj the object whose field should be modified
1076      * @param s   the new value for the field of {@code obj}
1077      * being modified
1078      *
1079      * @throws    IllegalAccessException    if this {@code Field} object
1080      *              is enforcing Java language access control and the underlying
1081      *              field is either inaccessible or final;
1082      *              or if this {@code Field} object has no write access.
1083      * @throws    IllegalArgumentException  if the specified object is not an
1084      *              instance of the class or interface declaring the underlying
1085      *              field (or a subclass or implementor thereof),
1086      *              or if an unwrapping conversion fails.
1087      * @throws    NullPointerException      if the specified object is null
1088      *              and the field is an instance field.
1089      * @throws    ExceptionInInitializerError if the initialization provoked
1090      *              by this method fails.
1091      * @see       Field#set
1092      */
1093     @CallerSensitive
1094     @ForceInline // to ensure Reflection.getCallerClass optimization
1095     public void setShort(Object obj, short s)
1096         throws IllegalArgumentException, IllegalAccessException
1097     {
1098         if (!override) {
1099             Class<?> caller = Reflection.getCallerClass();
1100             checkAccess(caller, obj);
1101             getFieldAccessor().setShort(obj, s);
1102             return;
1103         }
1104 
1105         FieldAccessor fa = getOverrideFieldAccessor();
1106         if (!Modifier.isFinal(modifiers)) {
1107             fa.setShort(obj, s);
1108         } else {
1109             setFinal(Reflection.getCallerClass(), obj, () -> fa.setShort(obj, s));
1110         }
1111     }
1112 
1113     /**
1114      * Sets the value of a field as an {@code int} on the specified object.
1115      * This method is equivalent to
1116      * {@code set(obj, iObj)},
1117      * where {@code iObj} is an {@code Integer} object and
1118      * {@code iObj.intValue() == i}.
1119      *
1120      * @param obj the object whose field should be modified
1121      * @param i   the new value for the field of {@code obj}
1122      * being modified
1123      *
1124      * @throws    IllegalAccessException    if this {@code Field} object
1125      *              is enforcing Java language access control and the underlying
1126      *              field is either inaccessible or final;
1127      *              or if this {@code Field} object has no write access.
1128      * @throws    IllegalArgumentException  if the specified object is not an
1129      *              instance of the class or interface declaring the underlying
1130      *              field (or a subclass or implementor thereof),
1131      *              or if an unwrapping conversion fails.
1132      * @throws    NullPointerException      if the specified object is null
1133      *              and the field is an instance field.
1134      * @throws    ExceptionInInitializerError if the initialization provoked
1135      *              by this method fails.
1136      * @see       Field#set
1137      */
1138     @CallerSensitive
1139     @ForceInline // to ensure Reflection.getCallerClass optimization
1140     public void setInt(Object obj, int i)
1141         throws IllegalArgumentException, IllegalAccessException
1142     {
1143         if (!override) {
1144             Class<?> caller = Reflection.getCallerClass();
1145             checkAccess(caller, obj);
1146             getFieldAccessor().setInt(obj, i);
1147             return;
1148         }
1149 
1150         FieldAccessor fa = getOverrideFieldAccessor();
1151         if (!Modifier.isFinal(modifiers)) {
1152             fa.setInt(obj, i);
1153         } else {
1154             setFinal(Reflection.getCallerClass(), obj, () -> fa.setInt(obj, i));
1155         }
1156     }
1157 
1158     /**
1159      * Sets the value of a field as a {@code long} on the specified object.
1160      * This method is equivalent to
1161      * {@code set(obj, lObj)},
1162      * where {@code lObj} is a {@code Long} object and
1163      * {@code lObj.longValue() == l}.
1164      *
1165      * @param obj the object whose field should be modified
1166      * @param l   the new value for the field of {@code obj}
1167      * being modified
1168      *
1169      * @throws    IllegalAccessException    if this {@code Field} object
1170      *              is enforcing Java language access control and the underlying
1171      *              field is either inaccessible or final;
1172      *              or if this {@code Field} object has no write access.
1173      * @throws    IllegalArgumentException  if the specified object is not an
1174      *              instance of the class or interface declaring the underlying
1175      *              field (or a subclass or implementor thereof),
1176      *              or if an unwrapping conversion fails.
1177      * @throws    NullPointerException      if the specified object is null
1178      *              and the field is an instance field.
1179      * @throws    ExceptionInInitializerError if the initialization provoked
1180      *              by this method fails.
1181      * @see       Field#set
1182      */
1183     @CallerSensitive
1184     @ForceInline // to ensure Reflection.getCallerClass optimization
1185     public void setLong(Object obj, long l)
1186         throws IllegalArgumentException, IllegalAccessException
1187     {
1188         if (!override) {
1189             Class<?> caller = Reflection.getCallerClass();
1190             checkAccess(caller, obj);
1191             getFieldAccessor().setLong(obj, l);
1192             return;
1193         }
1194 
1195         FieldAccessor fa = getOverrideFieldAccessor();
1196         if (!Modifier.isFinal(modifiers)) {
1197             fa.setLong(obj, l);
1198         } else {
1199             setFinal(Reflection.getCallerClass(), obj, () -> fa.setLong(obj, l));
1200         }
1201     }
1202 
1203     /**
1204      * Sets the value of a field as a {@code float} on the specified object.
1205      * This method is equivalent to
1206      * {@code set(obj, fObj)},
1207      * where {@code fObj} is a {@code Float} object and
1208      * {@code fObj.floatValue() == f}.
1209      *
1210      * @param obj the object whose field should be modified
1211      * @param f   the new value for the field of {@code obj}
1212      * being modified
1213      *
1214      * @throws    IllegalAccessException    if this {@code Field} object
1215      *              is enforcing Java language access control and the underlying
1216      *              field is either inaccessible or final;
1217      *              or if this {@code Field} object has no write access.
1218      * @throws    IllegalArgumentException  if the specified object is not an
1219      *              instance of the class or interface declaring the underlying
1220      *              field (or a subclass or implementor thereof),
1221      *              or if an unwrapping conversion fails.
1222      * @throws    NullPointerException      if the specified object is null
1223      *              and the field is an instance field.
1224      * @throws    ExceptionInInitializerError if the initialization provoked
1225      *              by this method fails.
1226      * @see       Field#set
1227      */
1228     @CallerSensitive
1229     @ForceInline // to ensure Reflection.getCallerClass optimization
1230     public void setFloat(Object obj, float f)
1231         throws IllegalArgumentException, IllegalAccessException
1232     {
1233         if (!override) {
1234             Class<?> caller = Reflection.getCallerClass();
1235             checkAccess(caller, obj);
1236             getFieldAccessor().setFloat(obj, f);
1237             return;
1238         }
1239 
1240         FieldAccessor fa = getOverrideFieldAccessor();
1241         if (!Modifier.isFinal(modifiers)) {
1242             fa.setFloat(obj, f);
1243         } else {
1244             setFinal(Reflection.getCallerClass(), obj, () -> fa.setFloat(obj, f));
1245         }
1246     }
1247 
1248     /**
1249      * Sets the value of a field as a {@code double} on the specified object.
1250      * This method is equivalent to
1251      * {@code set(obj, dObj)},
1252      * where {@code dObj} is a {@code Double} object and
1253      * {@code dObj.doubleValue() == d}.
1254      *
1255      * @param obj the object whose field should be modified
1256      * @param d   the new value for the field of {@code obj}
1257      * being modified
1258      *
1259      * @throws    IllegalAccessException    if this {@code Field} object
1260      *              is enforcing Java language access control and the underlying
1261      *              field is either inaccessible or final;
1262      *              or if this {@code Field} object has no write access.
1263      * @throws    IllegalArgumentException  if the specified object is not an
1264      *              instance of the class or interface declaring the underlying
1265      *              field (or a subclass or implementor thereof),
1266      *              or if an unwrapping conversion fails.
1267      * @throws    NullPointerException      if the specified object is null
1268      *              and the field is an instance field.
1269      * @throws    ExceptionInInitializerError if the initialization provoked
1270      *              by this method fails.
1271      * @see       Field#set
1272      */
1273     @CallerSensitive
1274     @ForceInline // to ensure Reflection.getCallerClass optimization
1275     public void setDouble(Object obj, double d)
1276         throws IllegalArgumentException, IllegalAccessException
1277     {
1278         if (!override) {
1279             Class<?> caller = Reflection.getCallerClass();
1280             checkAccess(caller, obj);
1281             getFieldAccessor().setDouble(obj, d);
1282             return;
1283         }
1284 
1285         FieldAccessor fa = getOverrideFieldAccessor();
1286         if (!Modifier.isFinal(modifiers)) {
1287             fa.setDouble(obj, d);
1288         } else {
1289             setFinal(Reflection.getCallerClass(), obj, () -> fa.setDouble(obj, d));
1290         }
1291     }
1292 
1293     // check access to field
1294     private void checkAccess(Class<?> caller, Object obj)
1295         throws IllegalAccessException
1296     {
1297         checkAccess(caller, clazz,
1298                     Modifier.isStatic(modifiers) ? null : obj.getClass(),
1299                     modifiers);
1300     }
1301 
1302     private FieldAccessor getFieldAccessor() {
1303         FieldAccessor a = fieldAccessor;
1304         return (a != null) ? a : acquireFieldAccessor();
1305     }
1306 
1307     private FieldAccessor getOverrideFieldAccessor() {
1308         FieldAccessor a = overrideFieldAccessor;
1309         return (a != null) ? a : acquireOverrideFieldAccessor();
1310     }
1311 
1312     // NOTE that there is no synchronization used here. It is correct
1313     // (though not efficient) to generate more than one FieldAccessor
1314     // for a given Field. However, avoiding synchronization will
1315     // probably make the implementation more scalable.
1316     private FieldAccessor acquireFieldAccessor() {
1317         // First check to see if one has been created yet, and take it
1318         // if so
1319         Field root = this.root;
1320         FieldAccessor tmp = root == null ? null : root.fieldAccessor;
1321         if (tmp != null) {
1322             fieldAccessor = tmp;
1323         } else {
1324             // Otherwise fabricate one and propagate it up to the root
1325             tmp = reflectionFactory.newFieldAccessor(this, false);
1326             setFieldAccessor(tmp);
1327         }
1328         return tmp;
1329     }
1330 
1331     private FieldAccessor acquireOverrideFieldAccessor() {
1332         // First check to see if one has been created yet, and take it
1333         // if so
1334         Field root = this.root;
1335         FieldAccessor tmp = root == null ? null : root.overrideFieldAccessor;
1336         if (tmp != null) {
1337             overrideFieldAccessor = tmp;
1338         } else {
1339             // Otherwise fabricate one and propagate it up to the root
1340             tmp = reflectionFactory.newFieldAccessor(this, true);
1341             setOverrideFieldAccessor(tmp);
1342         }
1343         return tmp;
1344     }
1345 
1346     // Sets the fieldAccessor for this Field object and
1347     // (recursively) its root
1348     private void setFieldAccessor(FieldAccessor accessor) {
1349         fieldAccessor = accessor;
1350         // Propagate up
1351         Field root = this.root;
1352         if (root != null) {
1353             root.setFieldAccessor(accessor);
1354         }
1355     }
1356 
1357     // Sets the overrideFieldAccessor for this Field object and
1358     // (recursively) its root
1359     private void setOverrideFieldAccessor(FieldAccessor accessor) {
1360         overrideFieldAccessor = accessor;
1361         // Propagate up
1362         Field root = this.root;
1363         if (root != null) {
1364             root.setOverrideFieldAccessor(accessor);
1365         }
1366     }
1367 
1368     @Override
1369     /* package-private */ Field getRoot() {
1370         return root;
1371     }
1372 
1373     private static final int TRUST_FINAL     = 0x0010;
1374     private static final int NULL_RESTRICTED = 0x0020;
1375 
1376     /* package-private */ boolean isTrustedFinal() {
1377         return (flags & TRUST_FINAL) == TRUST_FINAL;
1378     }
1379 
1380     /* package-private */ boolean isNullRestricted() {
1381         return (flags & NULL_RESTRICTED) == NULL_RESTRICTED;
1382     }
1383 
1384     /**
1385      * {@inheritDoc}
1386      *
1387      * @throws NullPointerException {@inheritDoc}
1388      * @since 1.5
1389      */
1390     @Override
1391     public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
1392         Objects.requireNonNull(annotationClass);
1393         return annotationClass.cast(declaredAnnotations().get(annotationClass));
1394     }
1395 
1396     /**
1397      * {@inheritDoc}
1398      *
1399      * @throws NullPointerException {@inheritDoc}
1400      * @since 1.8
1401      */
1402     @Override
1403     public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
1404         Objects.requireNonNull(annotationClass);
1405 
1406         return AnnotationSupport.getDirectlyAndIndirectlyPresent(declaredAnnotations(), annotationClass);
1407     }
1408 
1409     /**
1410      * {@inheritDoc}
1411      */
1412     @Override
1413     public Annotation[] getDeclaredAnnotations()  {
1414         return AnnotationParser.toArray(declaredAnnotations());
1415     }
1416 
1417     private transient volatile Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
1418 
1419     private Map<Class<? extends Annotation>, Annotation> declaredAnnotations() {
1420         Map<Class<? extends Annotation>, Annotation> declAnnos;
1421         if ((declAnnos = declaredAnnotations) == null) {
1422             synchronized (this) {
1423                 if ((declAnnos = declaredAnnotations) == null) {
1424                     Field root = this.root;
1425                     if (root != null) {
1426                         declAnnos = root.declaredAnnotations();
1427                     } else {
1428                         declAnnos = AnnotationParser.parseAnnotations(
1429                                 annotations,
1430                                 SharedSecrets.getJavaLangAccess()
1431                                         .getConstantPool(getDeclaringClass()),
1432                                 getDeclaringClass());
1433                     }
1434                     declaredAnnotations = declAnnos;
1435                 }
1436             }
1437         }
1438         return declAnnos;
1439     }
1440 
1441     private native byte[] getTypeAnnotationBytes0();
1442 
1443     /**
1444      * Returns an AnnotatedType object that represents the use of a type to specify
1445      * the declared type of the field represented by this Field.
1446      * @return an object representing the declared type of the field
1447      * represented by this Field
1448      *
1449      * @since 1.8
1450      */
1451     public AnnotatedType getAnnotatedType() {
1452         return TypeAnnotationParser.buildAnnotatedType(getTypeAnnotationBytes0(),
1453                                                        SharedSecrets.getJavaLangAccess().
1454                                                            getConstantPool(getDeclaringClass()),
1455                                                        this,
1456                                                        getDeclaringClass(),
1457                                                        getGenericType(),
1458                                                        TypeAnnotation.TypeAnnotationTarget.FIELD);
1459     }
1460 
1461     /**
1462      * A function that sets a field to a value.
1463      */
1464     @FunctionalInterface
1465     private interface FieldSetter {
1466         void setFieldValue() throws IllegalAccessException;
1467     }
1468 
1469     /**
1470      * Attempts to set a final field.
1471      */
1472     private void setFinal(Class<?> caller, Object obj, FieldSetter setter) throws IllegalAccessException {
1473         if (obj != null && isFinalInstanceInNormalClass()) {
1474             preSetFinal(caller, false);
1475             setter.setFieldValue();
1476             postSetFinal(caller, false);
1477         } else {
1478             // throws IllegalAccessException if static, or field in record or hidden class
1479             setter.setFieldValue();
1480         }
1481     }
1482 
1483     /**
1484      * Return true if this field is a final instance field in a normal class (not a
1485      * record class or hidden class),
1486      */
1487     private boolean isFinalInstanceInNormalClass() {
1488         return Modifier.isFinal(modifiers)
1489                 && !Modifier.isStatic(modifiers)
1490                 && !clazz.isRecord()
1491                 && !clazz.isHidden();
1492     }
1493 
1494     /**
1495      * Check that the caller is allowed to unreflect for mutation a final instance field
1496      * in a normal class.
1497      * @throws IllegalAccessException if not allowed
1498      */
1499     void checkAllowedToUnreflectFinalSetter(Class<?> caller) throws IllegalAccessException {
1500         Objects.requireNonNull(caller);
1501         preSetFinal(caller, true);
1502         postSetFinal(caller, true);
1503     }
1504 
1505     /**
1506      * Invoke before attempting to mutate, or unreflect for mutation, a final instance
1507      * field in a normal class.
1508      * @throws IllegalAccessException if not allowed
1509      */
1510     private void preSetFinal(Class<?> caller, boolean unreflect) throws IllegalAccessException {
1511         assert isFinalInstanceInNormalClass();
1512 
1513         if (caller != null) {
1514             // check if declaring class in package that is open to caller, or public field
1515             // and declaring class is public in package exported to caller
1516             if (!isFinalDeeplyAccessible(caller)) {
1517                 throw new IllegalAccessException(notAccessibleToCallerMessage(caller, unreflect));
1518             }
1519         } else {
1520             // no java caller, only allowed if field is public in exported package
1521             if (!Reflection.verifyPublicMemberAccess(clazz, modifiers)) {
1522                 throw new IllegalAccessException(notAccessibleToNoCallerMessage(unreflect));
1523             }
1524         }
1525 
1526         // check if field mutation is enabled for caller module or illegal final field
1527         // mutation is allowed
1528         var mode = ModuleBootstrap.illegalFinalFieldMutation();
1529         if (mode == ModuleBootstrap.IllegalFinalFieldMutation.DENY
1530                 && !Modules.isFinalMutationEnabled(moduleToCheck(caller))) {
1531             throw new IllegalAccessException(callerNotAllowedToMutateMessage(caller, unreflect));
1532         }
1533     }
1534 
1535     /**
1536      * Invoke after mutating a final instance field, or when unreflecting a final instance
1537      * field for mutation, to print a warning and record a JFR event.
1538      */
1539     private void postSetFinal(Class<?> caller, boolean unreflect) {
1540         assert isFinalInstanceInNormalClass();
1541 
1542         var mode = ModuleBootstrap.illegalFinalFieldMutation();
1543         if (mode == ModuleBootstrap.IllegalFinalFieldMutation.WARN) {
1544             // first mutation prints warning
1545             Module moduleToCheck = moduleToCheck(caller);
1546             if (Modules.tryEnableFinalMutation(moduleToCheck)) {
1547                 String warningMsg = finalFieldMutationWarning(caller, unreflect);
1548                 String targetModule = (caller != null && moduleToCheck.isNamed())
1549                         ? moduleToCheck.getName()
1550                         : "ALL-UNNAMED";
1551                 VM.initialErr().printf("""
1552                         WARNING: %s
1553                         WARNING: Use --enable-final-field-mutation=%s to avoid a warning
1554                         WARNING: Mutating final fields will be blocked in a future release unless final field mutation is enabled
1555                         """, warningMsg, targetModule);
1556             }
1557         } else if (mode == ModuleBootstrap.IllegalFinalFieldMutation.DEBUG) {
1558             // print warning and stack trace
1559             var sb = new StringBuilder(finalFieldMutationWarning(caller, unreflect));
1560             StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE)
1561                     .forEach(sf -> {
1562                         sb.append(System.lineSeparator()).append("\tat " + sf);
1563                     });
1564             VM.initialErr().println(sb);
1565         }
1566 
1567         // record JFR event
1568         FinalFieldMutationEvent.offer(getDeclaringClass(), getName());
1569     }
1570 
1571     /**
1572      * Returns true if this final field is "deeply accessible" to the caller.
1573      * The field is deeply accessible if declaring class is in a package that is open
1574      * to the caller's module, or the field is public in a public class that is exported
1575      * to the caller's module.
1576      *
1577      * Updates to the module of the declaring class at runtime with {@code Module.addExports}
1578      * or {@code Module.addOpens} have no impact on the result of this method.
1579      */
1580     private boolean isFinalDeeplyAccessible(Class<?> caller) {
1581         assert isFinalInstanceInNormalClass();
1582 
1583         // all fields in unnamed modules are deeply accessible
1584         Module declaringModule = clazz.getModule();
1585         if (!declaringModule.isNamed()) return true;
1586 
1587         // all fields in the caller's module are deeply accessible
1588         Module callerModule = caller.getModule();
1589         if (callerModule == declaringModule) return true;
1590 
1591         // public field, public class, package exported to caller's module
1592         String pn = clazz.getPackageName();
1593         if (Modifier.isPublic(modifiers)
1594                 && Modifier.isPublic(clazz.getModifiers())
1595                 && Modules.isStaticallyExported(declaringModule, pn, callerModule)) {
1596             return true;
1597         }
1598 
1599         // package open to caller's module
1600         return Modules.isStaticallyOpened(declaringModule, pn, callerModule);
1601     }
1602 
1603     /**
1604      * Returns the Module to use for access checks with the given caller.
1605      */
1606     private Module moduleToCheck(Class<?> caller) {
1607         if (caller != null) {
1608             return caller.getModule();
1609         } else {
1610             // no java caller, only allowed if field is public in exported package
1611             return ClassLoaders.appClassLoader().getUnnamedModule();
1612         }
1613     }
1614 
1615     /**
1616      * Returns the warning message to print when this final field is mutated by
1617      * the given possibly-null caller.
1618      */
1619     private String finalFieldMutationWarning(Class<?> caller, boolean unreflect) {
1620         assert Modifier.isFinal(modifiers);
1621         String source;
1622         if (caller != null) {
1623             source = caller + " in " + caller.getModule();
1624             CodeSource cs = caller.getProtectionDomain().getCodeSource();
1625             if (cs != null) {
1626                 URL url = cs.getLocation();
1627                 if (url != null) {
1628                     source += " (" + url + ")";
1629                 }
1630             }
1631         } else {
1632             source = "JNI attached thread with no caller frame";
1633         }
1634         return String.format("Final field %s in %s has been %s by %s",
1635                 name,
1636                 clazz,
1637                 (unreflect) ? "unreflected for mutation" : "mutated reflectively",
1638                 source);
1639     }
1640 
1641     /**
1642      * Returns the message for an IllegalAccessException when a final field cannot be
1643      * mutated because the declaring class is in a package that is not "deeply accessible"
1644      * to the caller.
1645      */
1646     private String notAccessibleToCallerMessage(Class<?> caller, boolean unreflect) {
1647         String exportsOrOpens = Modifier.isPublic(modifiers)
1648                 && Modifier.isPublic(clazz.getModifiers()) ? "exports" : "opens";
1649         return String.format("%s, %s does not explicitly \"%s\" package %s to %s",
1650                 cannotSetFieldMessage(caller, unreflect),
1651                 clazz.getModule(),
1652                 exportsOrOpens,
1653                 clazz.getPackageName(),
1654                 caller.getModule());
1655     }
1656 
1657     /**
1658      * Returns the exception message for the IllegalAccessException when this
1659      * final field cannot be mutated because the caller module is not allowed
1660      * to mutate final fields.
1661      */
1662     private String callerNotAllowedToMutateMessage(Class<?> caller, boolean unreflect) {
1663         if (caller != null) {
1664             return String.format("%s, %s is not allowed to mutate final fields",
1665                     cannotSetFieldMessage(caller, unreflect),
1666                     caller.getModule());
1667         } else {
1668             return notAccessibleToNoCallerMessage(unreflect);
1669         }
1670     }
1671 
1672     /**
1673      * Returns the message for an IllegalAccessException when a field is not
1674      * accessible to a JNI attached thread.
1675      */
1676     private String notAccessibleToNoCallerMessage(boolean unreflect) {
1677         return cannotSetFieldMessage("JNI attached thread with no caller frame cannot", unreflect);
1678     }
1679 
1680     /**
1681      * Returns a message to indicate that the caller cannot set/unreflect this final field.
1682      */
1683     private String cannotSetFieldMessage(Class<?> caller, boolean unreflect) {
1684         return cannotSetFieldMessage(caller + " (in " + caller.getModule() + ") cannot", unreflect);
1685     }
1686 
1687     /**
1688      * Returns a message to indicate that a field cannot be set/unreflected.
1689      */
1690     private String cannotSetFieldMessage(String prefix, boolean unreflect) {
1691         if (unreflect) {
1692             return prefix + " unreflect final field " + clazz.getName() + "." + name
1693                     + " (in " + clazz.getModule() + ") for mutation";
1694         } else {
1695             return prefix + " set final field " + clazz.getName() + "." + name
1696                     + " (in " + clazz.getModule() + ")";
1697         }
1698     }
1699 }