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