< prev index next >

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

Print this page

   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 jdk.internal.access.SharedSecrets;

  29 import jdk.internal.reflect.CallerSensitive;
  30 import jdk.internal.reflect.FieldAccessor;
  31 import jdk.internal.reflect.Reflection;
  32 import jdk.internal.vm.annotation.ForceInline;
  33 import jdk.internal.vm.annotation.Stable;
  34 import sun.reflect.generics.repository.FieldRepository;
  35 import sun.reflect.generics.factory.CoreReflectionFactory;
  36 import sun.reflect.generics.factory.GenericsFactory;
  37 import sun.reflect.generics.scope.ClassScope;
  38 import java.lang.annotation.Annotation;
  39 import java.util.Map;
  40 import java.util.Set;
  41 import java.util.Objects;
  42 import sun.reflect.annotation.AnnotationParser;
  43 import sun.reflect.annotation.AnnotationSupport;
  44 import sun.reflect.annotation.TypeAnnotation;
  45 import sun.reflect.annotation.TypeAnnotationParser;
  46 
  47 /**
  48  * A {@code Field} provides information about, and dynamic access to, a

  56  * @see Member
  57  * @see java.lang.Class
  58  * @see java.lang.Class#getFields()
  59  * @see java.lang.Class#getField(String)
  60  * @see java.lang.Class#getDeclaredFields()
  61  * @see java.lang.Class#getDeclaredField(String)
  62  *
  63  * @author Kenneth Russell
  64  * @author Nakul Saraiya
  65  * @since 1.1
  66  */
  67 public final
  68 class Field extends AccessibleObject implements Member {
  69     private final Class<?>            clazz;
  70     private final int                 slot;
  71     // This is guaranteed to be interned by the VM in the 1.4
  72     // reflection implementation
  73     private final String              name;
  74     private final Class<?>            type;
  75     private final int                 modifiers;
  76     private final boolean             trustedFinal;
  77     // Generics and annotations support
  78     private final transient String    signature;
  79     // generic info repository; lazily initialized
  80     private transient volatile FieldRepository genericInfo;
  81     private final byte[]              annotations;
  82     // Cached field accessor created without override
  83     @Stable
  84     private FieldAccessor fieldAccessor;
  85     // Cached field accessor created with override
  86     @Stable
  87     private FieldAccessor overrideFieldAccessor;
  88     // For sharing of FieldAccessors. This branching structure is
  89     // currently only two levels deep (i.e., one root Field and
  90     // potentially many Field objects pointing to it.)
  91     //
  92     // If this branching structure would ever contain cycles, deadlocks can
  93     // occur in annotation code.
  94     private Field               root;
  95 
  96     // Generics infrastructure

 109         var genericInfo = this.genericInfo;
 110         // lazily initialize repository if necessary
 111         if (genericInfo == null) {
 112             // create and cache generic info repository
 113             genericInfo = FieldRepository.make(getGenericSignature(),
 114                                                getFactory());
 115             this.genericInfo = genericInfo;
 116         }
 117         return genericInfo; //return cached repository
 118     }
 119 
 120 
 121     /**
 122      * Package-private constructor
 123      */
 124     @SuppressWarnings("deprecation")
 125     Field(Class<?> declaringClass,
 126           String name,
 127           Class<?> type,
 128           int modifiers,
 129           boolean trustedFinal,
 130           int slot,
 131           String signature,
 132           byte[] annotations)
 133     {

 134         this.clazz = declaringClass;
 135         this.name = name;
 136         this.type = type;
 137         this.modifiers = modifiers;
 138         this.trustedFinal = trustedFinal;
 139         this.slot = slot;
 140         this.signature = signature;
 141         this.annotations = annotations;
 142     }
 143 
 144     /**
 145      * Package-private routine (exposed to java.lang.Class via
 146      * ReflectAccess) which returns a copy of this Field. The copy's
 147      * "root" field points to this Field.
 148      */
 149     Field copy() {
 150         // This routine enables sharing of FieldAccessor objects
 151         // among Field objects which refer to the same underlying
 152         // method in the VM. (All of this contortion is only necessary
 153         // because of the "accessibility" bit in AccessibleObject,
 154         // which implicitly requires that new java.lang.reflect
 155         // objects be fabricated for each reflective call on Class
 156         // objects.)
 157         if (this.root != null)
 158             throw new IllegalArgumentException("Can not copy a non-root Field");
 159 
 160         Field res = new Field(clazz, name, type, modifiers, trustedFinal, slot, signature, annotations);
 161         res.root = this;
 162         // Might as well eagerly propagate this if already present
 163         res.fieldAccessor = fieldAccessor;
 164         res.overrideFieldAccessor = overrideFieldAccessor;
 165 
 166         return res;
 167     }
 168 
 169     /**
 170      * @throws InaccessibleObjectException {@inheritDoc}
 171      * @throws SecurityException {@inheritDoc}
 172      */
 173     @Override
 174     @CallerSensitive
 175     public void setAccessible(boolean flag) {
 176         AccessibleObject.checkPermission();
 177         if (flag) checkCanSetAccessible(Reflection.getCallerClass());
 178         setAccessible0(flag);
 179     }
 180 

 199         return name;
 200     }
 201 
 202     /**
 203      * Returns the Java language modifiers for the field represented
 204      * by this {@code Field} object, as an integer. The {@code Modifier} class should
 205      * be used to decode the modifiers.
 206      *
 207      * @see Modifier
 208      * @see #accessFlags()
 209      * @jls 8.3 Field Declarations
 210      * @jls 9.3 Field (Constant) Declarations
 211      */
 212     public int getModifiers() {
 213         return modifiers;
 214     }
 215 
 216     /**
 217      * {@return an unmodifiable set of the {@linkplain AccessFlag
 218      * access flags} for this field, possibly empty}


 219      * @see #getModifiers()
 220      * @jvms 4.5 Fields
 221      * @since 20
 222      */
 223     @Override
 224     public Set<AccessFlag> accessFlags() {
 225         return AccessFlag.maskToAccessFlags(getModifiers(), AccessFlag.Location.FIELD);




 226     }
 227 
 228     /**
 229      * Returns {@code true} if this field represents an element of
 230      * an enumerated class; returns {@code false} otherwise.
 231      *
 232      * @return {@code true} if and only if this field represents an element of
 233      * an enumerated class.
 234      * @since 1.5
 235      * @jls 8.9.1 Enum Constants
 236      */
 237     public boolean isEnumConstant() {
 238         return (getModifiers() & Modifier.ENUM) != 0;
 239     }
 240 
 241     /**
 242      * Returns {@code true} if this field is a synthetic
 243      * field; returns {@code false} otherwise.
 244      *
 245      * @return true if and only if this field is a synthetic

 329      * followed by a period, followed by the name of the field.
 330      * For example:
 331      * <pre>
 332      *    public static final int java.lang.Thread.MIN_PRIORITY
 333      *    private int java.io.FileDescriptor.fd
 334      * </pre>
 335      *
 336      * <p>The modifiers are placed in canonical order as specified by
 337      * "The Java Language Specification".  This is {@code public},
 338      * {@code protected} or {@code private} first, and then other
 339      * modifiers in the following order: {@code static}, {@code final},
 340      * {@code transient}, {@code volatile}.
 341      *
 342      * @return a string describing this {@code Field}
 343      * @jls 8.3.1 Field Modifiers
 344      */
 345     public String toString() {
 346         int mod = getModifiers();
 347         return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
 348             + getType().getTypeName() + " "
 349             + getDeclaringClass().getTypeName() + "."
 350             + getName());
 351     }
 352 
 353     @Override
 354     String toShortString() {
 355         return "field " + getDeclaringClass().getTypeName() + "." + getName();








 356     }
 357 
 358     /**
 359      * Returns a string describing this {@code Field}, including
 360      * its generic type.  The format is the access modifiers for the
 361      * field, if any, followed by the generic field type, followed by
 362      * a space, followed by the fully-qualified name of the class
 363      * declaring the field, followed by a period, followed by the name
 364      * of the field.
 365      *
 366      * <p>The modifiers are placed in canonical order as specified by
 367      * "The Java Language Specification".  This is {@code public},
 368      * {@code protected} or {@code private} first, and then other
 369      * modifiers in the following order: {@code static}, {@code final},
 370      * {@code transient}, {@code volatile}.
 371      *
 372      * @return a string describing this {@code Field}, including
 373      * its generic type
 374      *
 375      * @since 1.5
 376      * @jls 8.3.1 Field Modifiers
 377      */
 378     public String toGenericString() {
 379         int mod = getModifiers();
 380         Type fieldType = getGenericType();
 381         return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
 382             + fieldType.getTypeName() + " "
 383             + getDeclaringClass().getTypeName() + "."
 384             + getName());
 385     }
 386 
 387     /**
 388      * Returns the value of the field represented by this {@code Field}, on
 389      * the specified object. The value is automatically wrapped in an
 390      * object if it has a primitive type.
 391      *
 392      * <p>The underlying field's value is obtained as follows:
 393      *
 394      * <p>If the underlying field is a static field, the {@code obj} argument
 395      * is ignored; it may be null.
 396      *
 397      * <p>Otherwise, the underlying field is an instance field.  If the
 398      * specified {@code obj} argument is null, the method throws a
 399      * {@code NullPointerException}. If the specified object is not an
 400      * instance of the class or interface declaring the underlying
 401      * field, the method throws an {@code IllegalArgumentException}.
 402      *
 403      * <p>If this {@code Field} object is enforcing Java language access control, and

 756      * <p>If the underlying field is static, the {@code obj} argument is
 757      * ignored; it may be null.
 758      *
 759      * <p>Otherwise the underlying field is an instance field.  If the
 760      * specified object argument is null, the method throws a
 761      * {@code NullPointerException}.  If the specified object argument is not
 762      * an instance of the class or interface declaring the underlying
 763      * field, the method throws an {@code IllegalArgumentException}.
 764      *
 765      * <p>If this {@code Field} object is enforcing Java language access control, and
 766      * the underlying field is inaccessible, the method throws an
 767      * {@code IllegalAccessException}.
 768      *
 769      * <p>If the underlying field is final, this {@code Field} object has
 770      * <em>write</em> access if and only if the following conditions are met:
 771      * <ul>
 772      * <li>{@link #setAccessible(boolean) setAccessible(true)} has succeeded for
 773      *     this {@code Field} object;</li>
 774      * <li>the field is non-static; and</li>
 775      * <li>the field's declaring class is not a {@linkplain Class#isHidden()
 776      *     hidden class}; and</li>


 777      * <li>the field's declaring class is not a {@linkplain Class#isRecord()
 778      *     record class}.</li>
 779      * </ul>
 780      * If any of the above checks is not met, this method throws an
 781      * {@code IllegalAccessException}.
 782      *
 783      * <p> Setting a final field in this way
 784      * is meaningful only during deserialization or reconstruction of
 785      * instances of classes with blank final fields, before they are
 786      * made available for access by other parts of a program. Use in
 787      * any other context may have unpredictable effects, including cases
 788      * in which other parts of a program continue to use the original
 789      * value of this field.
 790      *
 791      * <p>If the underlying field is of a primitive type, an unwrapping
 792      * conversion is attempted to convert the new value to a value of
 793      * a primitive type.  If this attempt fails, the method throws an
 794      * {@code IllegalArgumentException}.
 795      *
 796      * <p>If, after possible unwrapping, the new value cannot be

1213             root.setFieldAccessor(accessor);
1214         }
1215     }
1216 
1217     // Sets the overrideFieldAccessor for this Field object and
1218     // (recursively) its root
1219     private void setOverrideFieldAccessor(FieldAccessor accessor) {
1220         overrideFieldAccessor = accessor;
1221         // Propagate up
1222         Field root = this.root;
1223         if (root != null) {
1224             root.setOverrideFieldAccessor(accessor);
1225         }
1226     }
1227 
1228     @Override
1229     /* package-private */ Field getRoot() {
1230         return root;
1231     }
1232 



1233     /* package-private */ boolean isTrustedFinal() {
1234         return trustedFinal;




1235     }
1236 
1237     /**
1238      * {@inheritDoc}
1239      *
1240      * @throws NullPointerException {@inheritDoc}
1241      * @since 1.5
1242      */
1243     @Override
1244     public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
1245         Objects.requireNonNull(annotationClass);
1246         return annotationClass.cast(declaredAnnotations().get(annotationClass));
1247     }
1248 
1249     /**
1250      * {@inheritDoc}
1251      *
1252      * @throws NullPointerException {@inheritDoc}
1253      * @since 1.8
1254      */

   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 jdk.internal.access.SharedSecrets;
  29 import jdk.internal.value.PrimitiveClass;
  30 import jdk.internal.reflect.CallerSensitive;
  31 import jdk.internal.reflect.FieldAccessor;
  32 import jdk.internal.reflect.Reflection;
  33 import jdk.internal.vm.annotation.ForceInline;
  34 import jdk.internal.vm.annotation.Stable;
  35 import sun.reflect.generics.repository.FieldRepository;
  36 import sun.reflect.generics.factory.CoreReflectionFactory;
  37 import sun.reflect.generics.factory.GenericsFactory;
  38 import sun.reflect.generics.scope.ClassScope;
  39 import java.lang.annotation.Annotation;
  40 import java.util.Map;
  41 import java.util.Set;
  42 import java.util.Objects;
  43 import sun.reflect.annotation.AnnotationParser;
  44 import sun.reflect.annotation.AnnotationSupport;
  45 import sun.reflect.annotation.TypeAnnotation;
  46 import sun.reflect.annotation.TypeAnnotationParser;
  47 
  48 /**
  49  * A {@code Field} provides information about, and dynamic access to, a

  57  * @see Member
  58  * @see java.lang.Class
  59  * @see java.lang.Class#getFields()
  60  * @see java.lang.Class#getField(String)
  61  * @see java.lang.Class#getDeclaredFields()
  62  * @see java.lang.Class#getDeclaredField(String)
  63  *
  64  * @author Kenneth Russell
  65  * @author Nakul Saraiya
  66  * @since 1.1
  67  */
  68 public final
  69 class Field extends AccessibleObject implements Member {
  70     private final Class<?>            clazz;
  71     private final int                 slot;
  72     // This is guaranteed to be interned by the VM in the 1.4
  73     // reflection implementation
  74     private final String              name;
  75     private final Class<?>            type;
  76     private final int                 modifiers;
  77     private final int                 flags;
  78     // Generics and annotations support
  79     private final transient String    signature;
  80     // generic info repository; lazily initialized
  81     private transient volatile FieldRepository genericInfo;
  82     private final byte[]              annotations;
  83     // Cached field accessor created without override
  84     @Stable
  85     private FieldAccessor fieldAccessor;
  86     // Cached field accessor created with override
  87     @Stable
  88     private FieldAccessor overrideFieldAccessor;
  89     // For sharing of FieldAccessors. This branching structure is
  90     // currently only two levels deep (i.e., one root Field and
  91     // potentially many Field objects pointing to it.)
  92     //
  93     // If this branching structure would ever contain cycles, deadlocks can
  94     // occur in annotation code.
  95     private Field               root;
  96 
  97     // Generics infrastructure

 110         var genericInfo = this.genericInfo;
 111         // lazily initialize repository if necessary
 112         if (genericInfo == null) {
 113             // create and cache generic info repository
 114             genericInfo = FieldRepository.make(getGenericSignature(),
 115                                                getFactory());
 116             this.genericInfo = genericInfo;
 117         }
 118         return genericInfo; //return cached repository
 119     }
 120 
 121 
 122     /**
 123      * Package-private constructor
 124      */
 125     @SuppressWarnings("deprecation")
 126     Field(Class<?> declaringClass,
 127           String name,
 128           Class<?> type,
 129           int modifiers,
 130           int flags,
 131           int slot,
 132           String signature,
 133           byte[] annotations)
 134     {
 135         assert PrimitiveClass.isPrimaryType(declaringClass);
 136         this.clazz = declaringClass;
 137         this.name = name;
 138         this.type = type;
 139         this.modifiers = modifiers;
 140         this.flags = flags;
 141         this.slot = slot;
 142         this.signature = signature;
 143         this.annotations = annotations;
 144     }
 145 
 146     /**
 147      * Package-private routine (exposed to java.lang.Class via
 148      * ReflectAccess) which returns a copy of this Field. The copy's
 149      * "root" field points to this Field.
 150      */
 151     Field copy() {
 152         // This routine enables sharing of FieldAccessor objects
 153         // among Field objects which refer to the same underlying
 154         // method in the VM. (All of this contortion is only necessary
 155         // because of the "accessibility" bit in AccessibleObject,
 156         // which implicitly requires that new java.lang.reflect
 157         // objects be fabricated for each reflective call on Class
 158         // objects.)
 159         if (this.root != null)
 160             throw new IllegalArgumentException("Can not copy a non-root Field");
 161 
 162         Field res = new Field(clazz, name, type, modifiers, flags, slot, signature, annotations);
 163         res.root = this;
 164         // Might as well eagerly propagate this if already present
 165         res.fieldAccessor = fieldAccessor;
 166         res.overrideFieldAccessor = overrideFieldAccessor;
 167 
 168         return res;
 169     }
 170 
 171     /**
 172      * @throws InaccessibleObjectException {@inheritDoc}
 173      * @throws SecurityException {@inheritDoc}
 174      */
 175     @Override
 176     @CallerSensitive
 177     public void setAccessible(boolean flag) {
 178         AccessibleObject.checkPermission();
 179         if (flag) checkCanSetAccessible(Reflection.getCallerClass());
 180         setAccessible0(flag);
 181     }
 182 

 201         return name;
 202     }
 203 
 204     /**
 205      * Returns the Java language modifiers for the field represented
 206      * by this {@code Field} object, as an integer. The {@code Modifier} class should
 207      * be used to decode the modifiers.
 208      *
 209      * @see Modifier
 210      * @see #accessFlags()
 211      * @jls 8.3 Field Declarations
 212      * @jls 9.3 Field (Constant) Declarations
 213      */
 214     public int getModifiers() {
 215         return modifiers;
 216     }
 217 
 218     /**
 219      * {@return an unmodifiable set of the {@linkplain AccessFlag
 220      * access flags} for this field, possibly empty}
 221      * The {@code AccessFlags} may depend on the class file format version of the class.
 222      *
 223      * @see #getModifiers()
 224      * @jvms 4.5 Fields
 225      * @since 20
 226      */
 227     @Override
 228     public Set<AccessFlag> accessFlags() {
 229         int major = SharedSecrets.getJavaLangAccess().classFileFormatVersion(getDeclaringClass()) & 0xffff;
 230         var cffv = ClassFileFormatVersion.fromMajor(major);
 231         return AccessFlag.maskToAccessFlags(getModifiers(),
 232                 AccessFlag.Location.FIELD,
 233                 cffv);
 234     }
 235 
 236     /**
 237      * Returns {@code true} if this field represents an element of
 238      * an enumerated class; returns {@code false} otherwise.
 239      *
 240      * @return {@code true} if and only if this field represents an element of
 241      * an enumerated class.
 242      * @since 1.5
 243      * @jls 8.9.1 Enum Constants
 244      */
 245     public boolean isEnumConstant() {
 246         return (getModifiers() & Modifier.ENUM) != 0;
 247     }
 248 
 249     /**
 250      * Returns {@code true} if this field is a synthetic
 251      * field; returns {@code false} otherwise.
 252      *
 253      * @return true if and only if this field is a synthetic

 337      * followed by a period, followed by the name of the field.
 338      * For example:
 339      * <pre>
 340      *    public static final int java.lang.Thread.MIN_PRIORITY
 341      *    private int java.io.FileDescriptor.fd
 342      * </pre>
 343      *
 344      * <p>The modifiers are placed in canonical order as specified by
 345      * "The Java Language Specification".  This is {@code public},
 346      * {@code protected} or {@code private} first, and then other
 347      * modifiers in the following order: {@code static}, {@code final},
 348      * {@code transient}, {@code volatile}.
 349      *
 350      * @return a string describing this {@code Field}
 351      * @jls 8.3.1 Field Modifiers
 352      */
 353     public String toString() {
 354         int mod = getModifiers();
 355         return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
 356             + getType().getTypeName() + " "
 357             + getDeclaringClassTypeName() + "."
 358             + getName());
 359     }
 360 
 361     @Override
 362     String toShortString() {
 363         return "field " + getDeclaringClassTypeName() + "." + getName();
 364     }
 365 
 366     String getDeclaringClassTypeName() {
 367         Class<?> c = getDeclaringClass();
 368         if (PrimitiveClass.isPrimitiveClass(c)) {
 369             c = PrimitiveClass.asValueType(c);
 370         }
 371         return c.getTypeName();
 372     }
 373 
 374     /**
 375      * Returns a string describing this {@code Field}, including
 376      * its generic type.  The format is the access modifiers for the
 377      * field, if any, followed by the generic field type, followed by
 378      * a space, followed by the fully-qualified name of the class
 379      * declaring the field, followed by a period, followed by the name
 380      * of the field.
 381      *
 382      * <p>The modifiers are placed in canonical order as specified by
 383      * "The Java Language Specification".  This is {@code public},
 384      * {@code protected} or {@code private} first, and then other
 385      * modifiers in the following order: {@code static}, {@code final},
 386      * {@code transient}, {@code volatile}.
 387      *
 388      * @return a string describing this {@code Field}, including
 389      * its generic type
 390      *
 391      * @since 1.5
 392      * @jls 8.3.1 Field Modifiers
 393      */
 394     public String toGenericString() {
 395         int mod = getModifiers();
 396         Type fieldType = getGenericType();
 397         return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
 398             + fieldType.getTypeName() + " "
 399             + getDeclaringClassTypeName() + "."
 400             + getName());
 401     }
 402 
 403     /**
 404      * Returns the value of the field represented by this {@code Field}, on
 405      * the specified object. The value is automatically wrapped in an
 406      * object if it has a primitive type.
 407      *
 408      * <p>The underlying field's value is obtained as follows:
 409      *
 410      * <p>If the underlying field is a static field, the {@code obj} argument
 411      * is ignored; it may be null.
 412      *
 413      * <p>Otherwise, the underlying field is an instance field.  If the
 414      * specified {@code obj} argument is null, the method throws a
 415      * {@code NullPointerException}. If the specified object is not an
 416      * instance of the class or interface declaring the underlying
 417      * field, the method throws an {@code IllegalArgumentException}.
 418      *
 419      * <p>If this {@code Field} object is enforcing Java language access control, and

 772      * <p>If the underlying field is static, the {@code obj} argument is
 773      * ignored; it may be null.
 774      *
 775      * <p>Otherwise the underlying field is an instance field.  If the
 776      * specified object argument is null, the method throws a
 777      * {@code NullPointerException}.  If the specified object argument is not
 778      * an instance of the class or interface declaring the underlying
 779      * field, the method throws an {@code IllegalArgumentException}.
 780      *
 781      * <p>If this {@code Field} object is enforcing Java language access control, and
 782      * the underlying field is inaccessible, the method throws an
 783      * {@code IllegalAccessException}.
 784      *
 785      * <p>If the underlying field is final, this {@code Field} object has
 786      * <em>write</em> access if and only if the following conditions are met:
 787      * <ul>
 788      * <li>{@link #setAccessible(boolean) setAccessible(true)} has succeeded for
 789      *     this {@code Field} object;</li>
 790      * <li>the field is non-static; and</li>
 791      * <li>the field's declaring class is not a {@linkplain Class#isHidden()
 792      *     hidden class};</li>
 793      * <li>the field's declaring class is not a {@linkplain Class#isValue()
 794      *     value class}; and</li>
 795      * <li>the field's declaring class is not a {@linkplain Class#isRecord()
 796      *     record class}.</li>
 797      * </ul>
 798      * If any of the above checks is not met, this method throws an
 799      * {@code IllegalAccessException}.
 800      *
 801      * <p> Setting a final field in this way
 802      * is meaningful only during deserialization or reconstruction of
 803      * instances of classes with blank final fields, before they are
 804      * made available for access by other parts of a program. Use in
 805      * any other context may have unpredictable effects, including cases
 806      * in which other parts of a program continue to use the original
 807      * value of this field.
 808      *
 809      * <p>If the underlying field is of a primitive type, an unwrapping
 810      * conversion is attempted to convert the new value to a value of
 811      * a primitive type.  If this attempt fails, the method throws an
 812      * {@code IllegalArgumentException}.
 813      *
 814      * <p>If, after possible unwrapping, the new value cannot be

1231             root.setFieldAccessor(accessor);
1232         }
1233     }
1234 
1235     // Sets the overrideFieldAccessor for this Field object and
1236     // (recursively) its root
1237     private void setOverrideFieldAccessor(FieldAccessor accessor) {
1238         overrideFieldAccessor = accessor;
1239         // Propagate up
1240         Field root = this.root;
1241         if (root != null) {
1242             root.setOverrideFieldAccessor(accessor);
1243         }
1244     }
1245 
1246     @Override
1247     /* package-private */ Field getRoot() {
1248         return root;
1249     }
1250 
1251     private static final int TRUST_FINAL     = 0x0010;
1252     private static final int NULL_RESTRICTED = 0x0020;
1253 
1254     /* package-private */ boolean isTrustedFinal() {
1255         return (flags & TRUST_FINAL) == TRUST_FINAL;
1256     }
1257 
1258     /* package-private */ boolean isNullRestricted() {
1259         return (flags & NULL_RESTRICTED) == NULL_RESTRICTED;
1260     }
1261 
1262     /**
1263      * {@inheritDoc}
1264      *
1265      * @throws NullPointerException {@inheritDoc}
1266      * @since 1.5
1267      */
1268     @Override
1269     public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
1270         Objects.requireNonNull(annotationClass);
1271         return annotationClass.cast(declaredAnnotations().get(annotationClass));
1272     }
1273 
1274     /**
1275      * {@inheritDoc}
1276      *
1277      * @throws NullPointerException {@inheritDoc}
1278      * @since 1.8
1279      */
< prev index next >