< prev index next >

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

Print this page

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

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

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

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

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

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


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

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














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

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

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

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

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

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

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



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




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

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

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

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

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

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

 823      * <p>If the underlying field is final, this {@code Field} object has <em>write</em>
 824      * access if and only if all of the following conditions are true, where {@code D} is
 825      * the field's {@linkplain #getDeclaringClass() declaring class}:
 826      *
 827      * <ul>
 828      * <li>{@link #setAccessible(boolean) setAccessible(true)} has succeeded for this
 829      *     {@code Field} object.</li>
 830      * <li><a href="doc-files/MutationMethods.html">final field mutation is enabled</a>
 831      *     for the caller's module.</li>
 832      * <li> At least one of the following conditions holds:
 833      *     <ol type="a">
 834      *     <li> {@code D} and the caller class are in the same module.</li>
 835      *     <li> The field is {@code public} and {@code D} is {@code public} in a package
 836      *     that the module containing {@code D} exports to at least the caller's module.</li>
 837      *     <li> {@code D} is in a package that is {@linkplain Module#isOpen(String, Module)
 838      *     open} to the caller's module.</li>
 839      *     </ol>
 840      * </li>
 841      * <li>{@code D} is not a {@linkplain Class#isRecord() record class}.</li>
 842      * <li>{@code D} is not a {@linkplain Class#isHidden() hidden class}.</li>
 843      * <li>{@code D} is not a {@linkplain Class#isValue() value class}.</li>
 844      * <li>The field is non-static.</li>
 845      * <li>The field is not a {@linkplain #isStrictInit() strictly-initialized} field. </li>
 846      * </ul>
 847      *
 848      * <p>If any of the above conditions is not met, this method throws an
 849      * {@code IllegalAccessException}.
 850      *
 851      * <p>These conditions are more restrictive than the conditions specified by {@link
 852      * #setAccessible(boolean)} to suppress access checks. In particular, updating a
 853      * module to export or open a package cannot be used to allow <em>write</em> access
 854      * to final fields with the {@code set} methods defined by {@code Field}.
 855      * Condition (b) is not met if the module containing {@code D} has been updated with
 856      * {@linkplain Module#addExports(String, Module) addExports} to export the package to
 857      * the caller's module. Condition (c) is not met if the module containing {@code D}
 858      * has been updated with {@linkplain Module#addOpens(String, Module) addOpens} to open
 859      * the package to the caller's module.
 860      *
 861      * <p>This method may be called by <a href="{@docRoot}/../specs/jni/index.html">
 862      * JNI code</a> with no caller class on the stack. In that case, and when the
 863      * underlying field is final, this {@code Field} object has <em>write</em> access
 864      * if and only if all of the following conditions are true, where {@code D} is the
 865      * field's {@linkplain #getDeclaringClass() declaring class}:
 866      *
 867      * <ul>
 868      * <li>{@code setAccessible(true)} has succeeded for this {@code Field} object.</li>
 869      * <li>final field mutation is enabled for the unnamed module.</li>
 870      * <li>The field is {@code public} and {@code D} is {@code public} in a package that
 871      *     is {@linkplain Module#isExported(String) exported} to all modules.</li>
 872      * <li>{@code D} is not a {@linkplain Class#isRecord() record class}.</li>
 873      * <li>{@code D} is not a {@linkplain Class#isHidden() hidden class}.</li>
 874      * <li>{@code D} is not a {@linkplain Class#isValue() value class}.</li>
 875      * <li>The field is non-static.</li>
 876      * <li>The field is not a {@linkplain #isStrictInit() strictly-initialized} field. </li>
 877      * </ul>
 878      *
 879      * <p>If any of the above conditions is not met, this method throws an
 880      * {@code IllegalAccessException}.
 881      *
 882      * <p> Setting a final field in this way
 883      * is meaningful only during deserialization or reconstruction of
 884      * instances of classes with blank final fields, before they are
 885      * made available for access by other parts of a program. Use in
 886      * any other context may have unpredictable effects, including cases
 887      * in which other parts of a program continue to use the original
 888      * value of this field.
 889      *
 890      * <p>If the underlying field is of a primitive type, an unwrapping
 891      * conversion is attempted to convert the new value to a value of
 892      * a primitive type.  If this attempt fails, the method throws an
 893      * {@code IllegalArgumentException}.
 894      *
 895      * <p>If, after possible unwrapping, the new value cannot be
 896      * converted to the type of the underlying field by an identity or

1367             root.setFieldAccessor(accessor);
1368         }
1369     }
1370 
1371     // Sets the overrideFieldAccessor for this Field object and
1372     // (recursively) its root
1373     private void setOverrideFieldAccessor(FieldAccessor accessor) {
1374         overrideFieldAccessor = accessor;
1375         // Propagate up
1376         Field root = this.root;
1377         if (root != null) {
1378             root.setOverrideFieldAccessor(accessor);
1379         }
1380     }
1381 
1382     @Override
1383     /* package-private */ Field getRoot() {
1384         return root;
1385     }
1386 
1387     private static final int TRUST_FINAL     = 0x0010;
1388     private static final int NULL_RESTRICTED = 0x0020;
1389 
1390     /* package-private */ boolean isTrustedFinal() {
1391         return (flags & TRUST_FINAL) == TRUST_FINAL;
1392     }
1393 
1394     /* package-private */ boolean isNullRestricted() {
1395         return (flags & NULL_RESTRICTED) == NULL_RESTRICTED;
1396     }
1397 
1398     /**
1399      * {@inheritDoc}
1400      *
1401      * @throws NullPointerException {@inheritDoc}
1402      * @since 1.5
1403      */
1404     @Override
1405     public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
1406         Objects.requireNonNull(annotationClass);
1407         return annotationClass.cast(declaredAnnotations().get(annotationClass));
1408     }
1409 
1410     /**
1411      * {@inheritDoc}
1412      *
1413      * @throws NullPointerException {@inheritDoc}
1414      * @since 1.8
1415      */
< prev index next >