1 /* 2 * Copyright (c) 1996, 2023, 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 jdk.internal.access.SharedSecrets; 29 import jdk.internal.reflect.CallerSensitive; 30 import jdk.internal.reflect.FieldAccessor; 31 import jdk.internal.reflect.Reflection; 32 import jdk.internal.vm.annotation.ForceInline; 33 import jdk.internal.vm.annotation.Stable; 34 import sun.reflect.generics.repository.FieldRepository; 35 import sun.reflect.generics.factory.CoreReflectionFactory; 36 import sun.reflect.generics.factory.GenericsFactory; 37 import sun.reflect.generics.scope.ClassScope; 38 import java.lang.annotation.Annotation; 39 import java.util.Map; 40 import java.util.Set; 41 import java.util.Objects; 42 import sun.reflect.annotation.AnnotationParser; 43 import sun.reflect.annotation.AnnotationSupport; 44 import sun.reflect.annotation.TypeAnnotation; 45 import sun.reflect.annotation.TypeAnnotationParser; 46 47 /** 48 * A {@code Field} provides information about, and dynamic access to, a 49 * single field of a class or an interface. The reflected field may 50 * be a class (static) field or an instance field. 51 * 52 * <p>A {@code Field} permits widening conversions to occur during a get or 53 * set access operation, but throws an {@code IllegalArgumentException} if a 54 * narrowing conversion would occur. 55 * 56 * @see Member 57 * @see java.lang.Class 58 * @see java.lang.Class#getFields() 59 * @see java.lang.Class#getField(String) 60 * @see java.lang.Class#getDeclaredFields() 61 * @see java.lang.Class#getDeclaredField(String) 62 * 63 * @author Kenneth Russell 64 * @author Nakul Saraiya 65 * @since 1.1 66 */ 67 public final 68 class Field extends AccessibleObject implements Member { 69 private final Class<?> clazz; 70 private final int slot; 71 // This is guaranteed to be interned by the VM in the 1.4 72 // reflection implementation 73 private final String name; 74 private final Class<?> type; 75 private final int modifiers; 76 private final boolean trustedFinal; 77 // Generics and annotations support 78 private final transient String signature; 79 // generic info repository; lazily initialized 80 private transient volatile FieldRepository genericInfo; 81 private final byte[] annotations; 82 // Cached field accessor created without override 83 @Stable 84 private FieldAccessor fieldAccessor; 85 // Cached field accessor created with override 86 @Stable 87 private FieldAccessor overrideFieldAccessor; 88 // For sharing of FieldAccessors. This branching structure is 89 // currently only two levels deep (i.e., one root Field and 90 // potentially many Field objects pointing to it.) 91 // 92 // If this branching structure would ever contain cycles, deadlocks can 93 // occur in annotation code. 94 private Field root; 95 96 // Generics infrastructure 97 98 private String getGenericSignature() {return signature;} 99 100 // Accessor for factory 101 private GenericsFactory getFactory() { 102 Class<?> c = getDeclaringClass(); 103 // create scope and factory 104 return CoreReflectionFactory.make(c, ClassScope.make(c)); 105 } 106 107 // Accessor for generic info repository 108 private FieldRepository getGenericInfo() { 109 var genericInfo = this.genericInfo; 110 // lazily initialize repository if necessary 111 if (genericInfo == null) { 112 // create and cache generic info repository 113 genericInfo = FieldRepository.make(getGenericSignature(), 114 getFactory()); 115 this.genericInfo = genericInfo; 116 } 117 return genericInfo; //return cached repository 118 } 119 120 121 /** 122 * Package-private constructor 123 */ 124 @SuppressWarnings("deprecation") 125 Field(Class<?> declaringClass, 126 String name, 127 Class<?> type, 128 int modifiers, 129 boolean trustedFinal, 130 int slot, 131 String signature, 132 byte[] annotations) 133 { 134 this.clazz = declaringClass; 135 this.name = name; 136 this.type = type; 137 this.modifiers = modifiers; 138 this.trustedFinal = trustedFinal; 139 this.slot = slot; 140 this.signature = signature; 141 this.annotations = annotations; 142 } 143 144 /** 145 * Package-private routine (exposed to java.lang.Class via 146 * ReflectAccess) which returns a copy of this Field. The copy's 147 * "root" field points to this Field. 148 */ 149 Field copy() { 150 // This routine enables sharing of FieldAccessor objects 151 // among Field objects which refer to the same underlying 152 // method in the VM. (All of this contortion is only necessary 153 // because of the "accessibility" bit in AccessibleObject, 154 // which implicitly requires that new java.lang.reflect 155 // objects be fabricated for each reflective call on Class 156 // objects.) 157 if (this.root != null) 158 throw new IllegalArgumentException("Can not copy a non-root Field"); 159 160 Field res = new Field(clazz, name, type, modifiers, trustedFinal, slot, signature, annotations); 161 res.root = this; 162 // Might as well eagerly propagate this if already present 163 res.fieldAccessor = fieldAccessor; 164 res.overrideFieldAccessor = overrideFieldAccessor; 165 166 return res; 167 } 168 169 /** 170 * @throws InaccessibleObjectException {@inheritDoc} 171 * @throws SecurityException {@inheritDoc} 172 */ 173 @Override 174 @CallerSensitive 175 public void setAccessible(boolean flag) { 176 AccessibleObject.checkPermission(); 177 if (flag) checkCanSetAccessible(Reflection.getCallerClass()); 178 setAccessible0(flag); 179 } 180 181 @Override 182 void checkCanSetAccessible(Class<?> caller) { 183 checkCanSetAccessible(caller, clazz); 184 } 185 186 /** 187 * Returns the {@code Class} object representing the class or interface 188 * that declares the field represented by this {@code Field} object. 189 */ 190 @Override 191 public Class<?> getDeclaringClass() { 192 return clazz; 193 } 194 195 /** 196 * Returns the name of the field represented by this {@code Field} object. 197 */ 198 public String getName() { 199 return name; 200 } 201 202 /** 203 * Returns the Java language modifiers for the field represented 204 * by this {@code Field} object, as an integer. The {@code Modifier} class should 205 * be used to decode the modifiers. 206 * 207 * @see Modifier 208 * @see #accessFlags() 209 * @jls 8.3 Field Declarations 210 * @jls 9.3 Field (Constant) Declarations 211 */ 212 public int getModifiers() { 213 return modifiers; 214 } 215 216 /** 217 * {@return an unmodifiable set of the {@linkplain AccessFlag 218 * access flags} for this field, possibly empty} 219 * @see #getModifiers() 220 * @jvms 4.5 Fields 221 * @since 20 222 */ 223 @Override 224 public Set<AccessFlag> accessFlags() { 225 return AccessFlag.maskToAccessFlags(getModifiers(), AccessFlag.Location.FIELD); 226 } 227 228 /** 229 * Returns {@code true} if this field represents an element of 230 * an enumerated class; returns {@code false} otherwise. 231 * 232 * @return {@code true} if and only if this field represents an element of 233 * an enumerated class. 234 * @since 1.5 235 * @jls 8.9.1 Enum Constants 236 */ 237 public boolean isEnumConstant() { 238 return (getModifiers() & Modifier.ENUM) != 0; 239 } 240 241 /** 242 * Returns {@code true} if this field is a synthetic 243 * field; returns {@code false} otherwise. 244 * 245 * @return true if and only if this field is a synthetic 246 * field as defined by the Java Language Specification. 247 * @since 1.5 248 * @see <a 249 * href="{@docRoot}/java.base/java/lang/reflect/package-summary.html#LanguageJvmModel">Java 250 * programming language and JVM modeling in core reflection</a> 251 */ 252 public boolean isSynthetic() { 253 return Modifier.isSynthetic(getModifiers()); 254 } 255 256 /** 257 * Returns a {@code Class} object that identifies the 258 * declared type for the field represented by this 259 * {@code Field} object. 260 * 261 * @return a {@code Class} object identifying the declared 262 * type of the field represented by this object 263 */ 264 public Class<?> getType() { 265 return type; 266 } 267 268 /** 269 * Returns a {@code Type} object that represents the declared type for 270 * the field represented by this {@code Field} object. 271 * 272 * <p>If the declared type of the field is a parameterized type, 273 * the {@code Type} object returned must accurately reflect the 274 * actual type arguments used in the source code. 275 * 276 * <p>If the type of the underlying field is a type variable or a 277 * parameterized type, it is created. Otherwise, it is resolved. 278 * 279 * @return a {@code Type} object that represents the declared type for 280 * the field represented by this {@code Field} object 281 * @throws GenericSignatureFormatError if the generic field 282 * signature does not conform to the format specified in 283 * <cite>The Java Virtual Machine Specification</cite> 284 * @throws TypeNotPresentException if the generic type 285 * signature of the underlying field refers to a non-existent 286 * class or interface declaration 287 * @throws MalformedParameterizedTypeException if the generic 288 * signature of the underlying field refers to a parameterized type 289 * that cannot be instantiated for any reason 290 * @since 1.5 291 */ 292 public Type getGenericType() { 293 if (getGenericSignature() != null) 294 return getGenericInfo().getGenericType(); 295 else 296 return getType(); 297 } 298 299 300 /** 301 * Compares this {@code Field} against the specified object. Returns 302 * true if the objects are the same. Two {@code Field} objects are the same if 303 * they were declared by the same class and have the same name 304 * and type. 305 */ 306 public boolean equals(Object obj) { 307 if (obj instanceof Field other) { 308 return (getDeclaringClass() == other.getDeclaringClass()) 309 && (getName() == other.getName()) 310 && (getType() == other.getType()); 311 } 312 return false; 313 } 314 315 /** 316 * Returns a hashcode for this {@code Field}. This is computed as the 317 * exclusive-or of the hashcodes for the underlying field's 318 * declaring class name and its name. 319 */ 320 public int hashCode() { 321 return getDeclaringClass().getName().hashCode() ^ getName().hashCode(); 322 } 323 324 /** 325 * Returns a string describing this {@code Field}. The format is 326 * the access modifiers for the field, if any, followed 327 * by the field type, followed by a space, followed by 328 * the fully-qualified name of the class declaring the field, 329 * followed by a period, followed by the name of the field. 330 * For example: 331 * <pre> 332 * public static final int java.lang.Thread.MIN_PRIORITY 333 * private int java.io.FileDescriptor.fd 334 * </pre> 335 * 336 * <p>The modifiers are placed in canonical order as specified by 337 * "The Java Language Specification". This is {@code public}, 338 * {@code protected} or {@code private} first, and then other 339 * modifiers in the following order: {@code static}, {@code final}, 340 * {@code transient}, {@code volatile}. 341 * 342 * @return a string describing this {@code Field} 343 * @jls 8.3.1 Field Modifiers 344 */ 345 public String toString() { 346 int mod = getModifiers(); 347 return (((mod == 0) ? "" : (Modifier.toString(mod) + " ")) 348 + getType().getTypeName() + " " 349 + getDeclaringClass().getTypeName() + "." 350 + getName()); 351 } 352 353 @Override 354 String toShortString() { 355 return "field " + getDeclaringClass().getTypeName() + "." + getName(); 356 } 357 358 /** 359 * Returns a string describing this {@code Field}, including 360 * its generic type. The format is the access modifiers for the 361 * field, if any, followed by the generic field type, followed by 362 * a space, followed by the fully-qualified name of the class 363 * declaring the field, followed by a period, followed by the name 364 * of the field. 365 * 366 * <p>The modifiers are placed in canonical order as specified by 367 * "The Java Language Specification". This is {@code public}, 368 * {@code protected} or {@code private} first, and then other 369 * modifiers in the following order: {@code static}, {@code final}, 370 * {@code transient}, {@code volatile}. 371 * 372 * @return a string describing this {@code Field}, including 373 * its generic type 374 * 375 * @since 1.5 376 * @jls 8.3.1 Field Modifiers 377 */ 378 public String toGenericString() { 379 int mod = getModifiers(); 380 Type fieldType = getGenericType(); 381 return (((mod == 0) ? "" : (Modifier.toString(mod) + " ")) 382 + fieldType.getTypeName() + " " 383 + getDeclaringClass().getTypeName() + "." 384 + getName()); 385 } 386 387 /** 388 * Returns the value of the field represented by this {@code Field}, on 389 * the specified object. The value is automatically wrapped in an 390 * object if it has a primitive type. 391 * 392 * <p>The underlying field's value is obtained as follows: 393 * 394 * <p>If the underlying field is a static field, the {@code obj} argument 395 * is ignored; it may be null. 396 * 397 * <p>Otherwise, the underlying field is an instance field. If the 398 * specified {@code obj} argument is null, the method throws a 399 * {@code NullPointerException}. If the specified object is not an 400 * instance of the class or interface declaring the underlying 401 * field, the method throws an {@code IllegalArgumentException}. 402 * 403 * <p>If this {@code Field} object is enforcing Java language access control, and 404 * the underlying field is inaccessible, the method throws an 405 * {@code IllegalAccessException}. 406 * If the underlying field is static, the class that declared the 407 * field is initialized if it has not already been initialized. 408 * 409 * <p>Otherwise, the value is retrieved from the underlying instance 410 * or static field. If the field has a primitive type, the value 411 * is wrapped in an object before being returned, otherwise it is 412 * returned as is. 413 * 414 * <p>If the field is hidden in the type of {@code obj}, 415 * the field's value is obtained according to the preceding rules. 416 * 417 * @param obj object from which the represented field's value is 418 * to be extracted 419 * @return the value of the represented field in object 420 * {@code obj}; primitive values are wrapped in an appropriate 421 * object before being returned 422 * 423 * @throws IllegalAccessException if this {@code Field} object 424 * is enforcing Java language access control and the underlying 425 * field is inaccessible. 426 * @throws IllegalArgumentException if the specified object is not an 427 * instance of the class or interface declaring the underlying 428 * field (or a subclass or implementor thereof). 429 * @throws NullPointerException if the specified object is null 430 * and the field is an instance field. 431 * @throws ExceptionInInitializerError if the initialization provoked 432 * by this method fails. 433 */ 434 @CallerSensitive 435 @ForceInline // to ensure Reflection.getCallerClass optimization 436 public Object get(Object obj) 437 throws IllegalArgumentException, IllegalAccessException 438 { 439 if (!override) { 440 Class<?> caller = Reflection.getCallerClass(); 441 checkAccess(caller, obj); 442 return getFieldAccessor().get(obj); 443 } else { 444 return getOverrideFieldAccessor().get(obj); 445 } 446 } 447 448 /** 449 * Gets the value of a static or instance {@code boolean} field. 450 * 451 * @param obj the object to extract the {@code boolean} value 452 * from 453 * @return the value of the {@code boolean} field 454 * 455 * @throws IllegalAccessException if this {@code Field} object 456 * is enforcing Java language access control and the underlying 457 * field is inaccessible. 458 * @throws IllegalArgumentException if the specified object is not 459 * an instance of the class or interface declaring the 460 * underlying field (or a subclass or implementor 461 * thereof), or if the field value cannot be 462 * converted to the type {@code boolean} by a 463 * widening conversion. 464 * @throws NullPointerException if the specified object is null 465 * and the field is an instance field. 466 * @throws ExceptionInInitializerError if the initialization provoked 467 * by this method fails. 468 * @see Field#get 469 */ 470 @CallerSensitive 471 @ForceInline // to ensure Reflection.getCallerClass optimization 472 public boolean getBoolean(Object obj) 473 throws IllegalArgumentException, IllegalAccessException 474 { 475 if (!override) { 476 Class<?> caller = Reflection.getCallerClass(); 477 checkAccess(caller, obj); 478 return getFieldAccessor().getBoolean(obj); 479 } else { 480 return getOverrideFieldAccessor().getBoolean(obj); 481 } 482 } 483 484 /** 485 * Gets the value of a static or instance {@code byte} field. 486 * 487 * @param obj the object to extract the {@code byte} value 488 * from 489 * @return the value of the {@code byte} field 490 * 491 * @throws IllegalAccessException if this {@code Field} object 492 * is enforcing Java language access control and the underlying 493 * field is inaccessible. 494 * @throws IllegalArgumentException if the specified object is not 495 * an instance of the class or interface declaring the 496 * underlying field (or a subclass or implementor 497 * thereof), or if the field value cannot be 498 * converted to the type {@code byte} by a 499 * widening conversion. 500 * @throws NullPointerException if the specified object is null 501 * and the field is an instance field. 502 * @throws ExceptionInInitializerError if the initialization provoked 503 * by this method fails. 504 * @see Field#get 505 */ 506 @CallerSensitive 507 @ForceInline // to ensure Reflection.getCallerClass optimization 508 public byte getByte(Object obj) 509 throws IllegalArgumentException, IllegalAccessException 510 { 511 if (!override) { 512 Class<?> caller = Reflection.getCallerClass(); 513 checkAccess(caller, obj); 514 return getFieldAccessor().getByte(obj); 515 } else { 516 return getOverrideFieldAccessor().getByte(obj); 517 } 518 } 519 520 /** 521 * Gets the value of a static or instance field of type 522 * {@code char} or of another primitive type convertible to 523 * type {@code char} via a widening conversion. 524 * 525 * @param obj the object to extract the {@code char} value 526 * from 527 * @return the value of the field converted to type {@code char} 528 * 529 * @throws IllegalAccessException if this {@code Field} object 530 * is enforcing Java language access control and the underlying 531 * field is inaccessible. 532 * @throws IllegalArgumentException if the specified object is not 533 * an instance of the class or interface declaring the 534 * underlying field (or a subclass or implementor 535 * thereof), or if the field value cannot be 536 * converted to the type {@code char} by a 537 * widening conversion. 538 * @throws NullPointerException if the specified object is null 539 * and the field is an instance field. 540 * @throws ExceptionInInitializerError if the initialization provoked 541 * by this method fails. 542 * @see Field#get 543 */ 544 @CallerSensitive 545 @ForceInline // to ensure Reflection.getCallerClass optimization 546 public char getChar(Object obj) 547 throws IllegalArgumentException, IllegalAccessException 548 { 549 if (!override) { 550 Class<?> caller = Reflection.getCallerClass(); 551 checkAccess(caller, obj); 552 return getFieldAccessor().getChar(obj); 553 } else { 554 return getOverrideFieldAccessor().getChar(obj); 555 } 556 } 557 558 /** 559 * Gets the value of a static or instance field of type 560 * {@code short} or of another primitive type convertible to 561 * type {@code short} via a widening conversion. 562 * 563 * @param obj the object to extract the {@code short} value 564 * from 565 * @return the value of the field converted to type {@code short} 566 * 567 * @throws IllegalAccessException if this {@code Field} object 568 * is enforcing Java language access control and the underlying 569 * field is inaccessible. 570 * @throws IllegalArgumentException if the specified object is not 571 * an instance of the class or interface declaring the 572 * underlying field (or a subclass or implementor 573 * thereof), or if the field value cannot be 574 * converted to the type {@code short} by a 575 * widening conversion. 576 * @throws NullPointerException if the specified object is null 577 * and the field is an instance field. 578 * @throws ExceptionInInitializerError if the initialization provoked 579 * by this method fails. 580 * @see Field#get 581 */ 582 @CallerSensitive 583 @ForceInline // to ensure Reflection.getCallerClass optimization 584 public short getShort(Object obj) 585 throws IllegalArgumentException, IllegalAccessException 586 { 587 if (!override) { 588 Class<?> caller = Reflection.getCallerClass(); 589 checkAccess(caller, obj); 590 return getFieldAccessor().getShort(obj); 591 } else { 592 return getOverrideFieldAccessor().getShort(obj); 593 } 594 } 595 596 /** 597 * Gets the value of a static or instance field of type 598 * {@code int} or of another primitive type convertible to 599 * type {@code int} via a widening conversion. 600 * 601 * @param obj the object to extract the {@code int} value 602 * from 603 * @return the value of the field converted to type {@code int} 604 * 605 * @throws IllegalAccessException if this {@code Field} object 606 * is enforcing Java language access control and the underlying 607 * field is inaccessible. 608 * @throws IllegalArgumentException if the specified object is not 609 * an instance of the class or interface declaring the 610 * underlying field (or a subclass or implementor 611 * thereof), or if the field value cannot be 612 * converted to the type {@code int} by a 613 * widening conversion. 614 * @throws NullPointerException if the specified object is null 615 * and the field is an instance field. 616 * @throws ExceptionInInitializerError if the initialization provoked 617 * by this method fails. 618 * @see Field#get 619 */ 620 @CallerSensitive 621 @ForceInline // to ensure Reflection.getCallerClass optimization 622 public int getInt(Object obj) 623 throws IllegalArgumentException, IllegalAccessException 624 { 625 if (!override) { 626 Class<?> caller = Reflection.getCallerClass(); 627 checkAccess(caller, obj); 628 return getFieldAccessor().getInt(obj); 629 } else { 630 return getOverrideFieldAccessor().getInt(obj); 631 } 632 } 633 634 /** 635 * Gets the value of a static or instance field of type 636 * {@code long} or of another primitive type convertible to 637 * type {@code long} via a widening conversion. 638 * 639 * @param obj the object to extract the {@code long} value 640 * from 641 * @return the value of the field converted to type {@code long} 642 * 643 * @throws IllegalAccessException if this {@code Field} object 644 * is enforcing Java language access control and the underlying 645 * field is inaccessible. 646 * @throws IllegalArgumentException if the specified object is not 647 * an instance of the class or interface declaring the 648 * underlying field (or a subclass or implementor 649 * thereof), or if the field value cannot be 650 * converted to the type {@code long} by a 651 * widening conversion. 652 * @throws NullPointerException if the specified object is null 653 * and the field is an instance field. 654 * @throws ExceptionInInitializerError if the initialization provoked 655 * by this method fails. 656 * @see Field#get 657 */ 658 @CallerSensitive 659 @ForceInline // to ensure Reflection.getCallerClass optimization 660 public long getLong(Object obj) 661 throws IllegalArgumentException, IllegalAccessException 662 { 663 if (!override) { 664 Class<?> caller = Reflection.getCallerClass(); 665 checkAccess(caller, obj); 666 return getFieldAccessor().getLong(obj); 667 } else { 668 return getOverrideFieldAccessor().getLong(obj); 669 } 670 } 671 672 /** 673 * Gets the value of a static or instance field of type 674 * {@code float} or of another primitive type convertible to 675 * type {@code float} via a widening conversion. 676 * 677 * @param obj the object to extract the {@code float} value 678 * from 679 * @return the value of the field converted to type {@code float} 680 * 681 * @throws IllegalAccessException if this {@code Field} object 682 * is enforcing Java language access control and the underlying 683 * field is inaccessible. 684 * @throws IllegalArgumentException if the specified object is not 685 * an instance of the class or interface declaring the 686 * underlying field (or a subclass or implementor 687 * thereof), or if the field value cannot be 688 * converted to the type {@code float} by a 689 * widening conversion. 690 * @throws NullPointerException if the specified object is null 691 * and the field is an instance field. 692 * @throws ExceptionInInitializerError if the initialization provoked 693 * by this method fails. 694 * @see Field#get 695 */ 696 @CallerSensitive 697 @ForceInline // to ensure Reflection.getCallerClass optimization 698 public float getFloat(Object obj) 699 throws IllegalArgumentException, IllegalAccessException 700 { 701 if (!override) { 702 Class<?> caller = Reflection.getCallerClass(); 703 checkAccess(caller, obj); 704 return getFieldAccessor().getFloat(obj); 705 } else { 706 return getOverrideFieldAccessor().getFloat(obj); 707 } 708 } 709 710 /** 711 * Gets the value of a static or instance field of type 712 * {@code double} or of another primitive type convertible to 713 * type {@code double} via a widening conversion. 714 * 715 * @param obj the object to extract the {@code double} value 716 * from 717 * @return the value of the field converted to type {@code double} 718 * 719 * @throws IllegalAccessException if this {@code Field} object 720 * is enforcing Java language access control and the underlying 721 * field is inaccessible. 722 * @throws IllegalArgumentException if the specified object is not 723 * an instance of the class or interface declaring the 724 * underlying field (or a subclass or implementor 725 * thereof), or if the field value cannot be 726 * converted to the type {@code double} by a 727 * widening conversion. 728 * @throws NullPointerException if the specified object is null 729 * and the field is an instance field. 730 * @throws ExceptionInInitializerError if the initialization provoked 731 * by this method fails. 732 * @see Field#get 733 */ 734 @CallerSensitive 735 @ForceInline // to ensure Reflection.getCallerClass optimization 736 public double getDouble(Object obj) 737 throws IllegalArgumentException, IllegalAccessException 738 { 739 if (!override) { 740 Class<?> caller = Reflection.getCallerClass(); 741 checkAccess(caller, obj); 742 return getFieldAccessor().getDouble(obj); 743 } else { 744 return getOverrideFieldAccessor().getDouble(obj); 745 } 746 } 747 748 /** 749 * Sets the field represented by this {@code Field} object on the 750 * specified object argument to the specified new value. The new 751 * value is automatically unwrapped if the underlying field has a 752 * primitive type. 753 * 754 * <p>The operation proceeds as follows: 755 * 756 * <p>If the underlying field is static, the {@code obj} argument is 757 * ignored; it may be null. 758 * 759 * <p>Otherwise the underlying field is an instance field. If the 760 * specified object argument is null, the method throws a 761 * {@code NullPointerException}. If the specified object argument is not 762 * an instance of the class or interface declaring the underlying 763 * field, the method throws an {@code IllegalArgumentException}. 764 * 765 * <p>If this {@code Field} object is enforcing Java language access control, and 766 * the underlying field is inaccessible, the method throws an 767 * {@code IllegalAccessException}. 768 * 769 * <p>If the underlying field is final, this {@code Field} object has 770 * <em>write</em> access if and only if the following conditions are met: 771 * <ul> 772 * <li>{@link #setAccessible(boolean) setAccessible(true)} has succeeded for 773 * this {@code Field} object;</li> 774 * <li>the field is non-static; and</li> 775 * <li>the field's declaring class is not a {@linkplain Class#isHidden() 776 * hidden class}; and</li> 777 * <li>the field's declaring class is not a {@linkplain Class#isRecord() 778 * record class}.</li> 779 * </ul> 780 * If any of the above checks is not met, this method throws an 781 * {@code IllegalAccessException}. 782 * 783 * <p> Setting a final field in this way 784 * is meaningful only during deserialization or reconstruction of 785 * instances of classes with blank final fields, before they are 786 * made available for access by other parts of a program. Use in 787 * any other context may have unpredictable effects, including cases 788 * in which other parts of a program continue to use the original 789 * value of this field. 790 * 791 * <p>If the underlying field is of a primitive type, an unwrapping 792 * conversion is attempted to convert the new value to a value of 793 * a primitive type. If this attempt fails, the method throws an 794 * {@code IllegalArgumentException}. 795 * 796 * <p>If, after possible unwrapping, the new value cannot be 797 * converted to the type of the underlying field by an identity or 798 * widening conversion, the method throws an 799 * {@code IllegalArgumentException}. 800 * 801 * <p>If the underlying field is static, the class that declared the 802 * field is initialized if it has not already been initialized. 803 * 804 * <p>The field is set to the possibly unwrapped and widened new value. 805 * 806 * <p>If the field is hidden in the type of {@code obj}, 807 * the field's value is set according to the preceding rules. 808 * 809 * @param obj the object whose field should be modified 810 * @param value the new value for the field of {@code obj} 811 * being modified 812 * 813 * @throws IllegalAccessException if this {@code Field} object 814 * is enforcing Java language access control and the underlying 815 * field is inaccessible or final; 816 * or if this {@code Field} object has no write access. 817 * @throws IllegalArgumentException if the specified object is not an 818 * instance of the class or interface declaring the underlying 819 * field (or a subclass or implementor thereof), 820 * or if an unwrapping conversion fails. 821 * @throws NullPointerException if the specified object is null 822 * and the field is an instance field. 823 * @throws ExceptionInInitializerError if the initialization provoked 824 * by this method fails. 825 */ 826 @CallerSensitive 827 @ForceInline // to ensure Reflection.getCallerClass optimization 828 public void set(Object obj, Object value) 829 throws IllegalArgumentException, IllegalAccessException 830 { 831 if (!override) { 832 Class<?> caller = Reflection.getCallerClass(); 833 checkAccess(caller, obj); 834 getFieldAccessor().set(obj, value); 835 } else { 836 getOverrideFieldAccessor().set(obj, value); 837 } 838 } 839 840 /** 841 * Sets the value of a field as a {@code boolean} on the specified object. 842 * This method is equivalent to 843 * {@code set(obj, zObj)}, 844 * where {@code zObj} is a {@code Boolean} object and 845 * {@code zObj.booleanValue() == z}. 846 * 847 * @param obj the object whose field should be modified 848 * @param z the new value for the field of {@code obj} 849 * being modified 850 * 851 * @throws IllegalAccessException if this {@code Field} object 852 * is enforcing Java language access control and the underlying 853 * field is either inaccessible or final; 854 * or if this {@code Field} object has no write access. 855 * @throws IllegalArgumentException if the specified object is not an 856 * instance of the class or interface declaring the underlying 857 * field (or a subclass or implementor thereof), 858 * or if an unwrapping conversion fails. 859 * @throws NullPointerException if the specified object is null 860 * and the field is an instance field. 861 * @throws ExceptionInInitializerError if the initialization provoked 862 * by this method fails. 863 * @see Field#set 864 */ 865 @CallerSensitive 866 @ForceInline // to ensure Reflection.getCallerClass optimization 867 public void setBoolean(Object obj, boolean z) 868 throws IllegalArgumentException, IllegalAccessException 869 { 870 if (!override) { 871 Class<?> caller = Reflection.getCallerClass(); 872 checkAccess(caller, obj); 873 getFieldAccessor().setBoolean(obj, z); 874 } else { 875 getOverrideFieldAccessor().setBoolean(obj, z); 876 } 877 } 878 879 /** 880 * Sets the value of a field as a {@code byte} on the specified object. 881 * This method is equivalent to 882 * {@code set(obj, bObj)}, 883 * where {@code bObj} is a {@code Byte} object and 884 * {@code bObj.byteValue() == b}. 885 * 886 * @param obj the object whose field should be modified 887 * @param b the new value for the field of {@code obj} 888 * being modified 889 * 890 * @throws IllegalAccessException if this {@code Field} object 891 * is enforcing Java language access control and the underlying 892 * field is either inaccessible or final; 893 * or if this {@code Field} object has no write access. 894 * @throws IllegalArgumentException if the specified object is not an 895 * instance of the class or interface declaring the underlying 896 * field (or a subclass or implementor thereof), 897 * or if an unwrapping conversion fails. 898 * @throws NullPointerException if the specified object is null 899 * and the field is an instance field. 900 * @throws ExceptionInInitializerError if the initialization provoked 901 * by this method fails. 902 * @see Field#set 903 */ 904 @CallerSensitive 905 @ForceInline // to ensure Reflection.getCallerClass optimization 906 public void setByte(Object obj, byte b) 907 throws IllegalArgumentException, IllegalAccessException 908 { 909 if (!override) { 910 Class<?> caller = Reflection.getCallerClass(); 911 checkAccess(caller, obj); 912 getFieldAccessor().setByte(obj, b); 913 } else { 914 getOverrideFieldAccessor().setByte(obj, b); 915 } 916 } 917 918 /** 919 * Sets the value of a field as a {@code char} on the specified object. 920 * This method is equivalent to 921 * {@code set(obj, cObj)}, 922 * where {@code cObj} is a {@code Character} object and 923 * {@code cObj.charValue() == c}. 924 * 925 * @param obj the object whose field should be modified 926 * @param c the new value for the field of {@code obj} 927 * being modified 928 * 929 * @throws IllegalAccessException if this {@code Field} object 930 * is enforcing Java language access control and the underlying 931 * field is either inaccessible or final; 932 * or if this {@code Field} object has no write access. 933 * @throws IllegalArgumentException if the specified object is not an 934 * instance of the class or interface declaring the underlying 935 * field (or a subclass or implementor thereof), 936 * or if an unwrapping conversion fails. 937 * @throws NullPointerException if the specified object is null 938 * and the field is an instance field. 939 * @throws ExceptionInInitializerError if the initialization provoked 940 * by this method fails. 941 * @see Field#set 942 */ 943 @CallerSensitive 944 @ForceInline // to ensure Reflection.getCallerClass optimization 945 public void setChar(Object obj, char c) 946 throws IllegalArgumentException, IllegalAccessException 947 { 948 if (!override) { 949 Class<?> caller = Reflection.getCallerClass(); 950 checkAccess(caller, obj); 951 getFieldAccessor().setChar(obj, c); 952 } else { 953 getOverrideFieldAccessor().setChar(obj, c); 954 } 955 } 956 957 /** 958 * Sets the value of a field as a {@code short} on the specified object. 959 * This method is equivalent to 960 * {@code set(obj, sObj)}, 961 * where {@code sObj} is a {@code Short} object and 962 * {@code sObj.shortValue() == s}. 963 * 964 * @param obj the object whose field should be modified 965 * @param s the new value for the field of {@code obj} 966 * being modified 967 * 968 * @throws IllegalAccessException if this {@code Field} object 969 * is enforcing Java language access control and the underlying 970 * field is either inaccessible or final; 971 * or if this {@code Field} object has no write access. 972 * @throws IllegalArgumentException if the specified object is not an 973 * instance of the class or interface declaring the underlying 974 * field (or a subclass or implementor thereof), 975 * or if an unwrapping conversion fails. 976 * @throws NullPointerException if the specified object is null 977 * and the field is an instance field. 978 * @throws ExceptionInInitializerError if the initialization provoked 979 * by this method fails. 980 * @see Field#set 981 */ 982 @CallerSensitive 983 @ForceInline // to ensure Reflection.getCallerClass optimization 984 public void setShort(Object obj, short s) 985 throws IllegalArgumentException, IllegalAccessException 986 { 987 if (!override) { 988 Class<?> caller = Reflection.getCallerClass(); 989 checkAccess(caller, obj); 990 getFieldAccessor().setShort(obj, s); 991 } else { 992 getOverrideFieldAccessor().setShort(obj, s); 993 } 994 } 995 996 /** 997 * Sets the value of a field as an {@code int} on the specified object. 998 * This method is equivalent to 999 * {@code set(obj, iObj)}, 1000 * where {@code iObj} is an {@code Integer} object and 1001 * {@code iObj.intValue() == i}. 1002 * 1003 * @param obj the object whose field should be modified 1004 * @param i the new value for the field of {@code obj} 1005 * being modified 1006 * 1007 * @throws IllegalAccessException if this {@code Field} object 1008 * is enforcing Java language access control and the underlying 1009 * field is either inaccessible or final; 1010 * or if this {@code Field} object has no write access. 1011 * @throws IllegalArgumentException if the specified object is not an 1012 * instance of the class or interface declaring the underlying 1013 * field (or a subclass or implementor thereof), 1014 * or if an unwrapping conversion fails. 1015 * @throws NullPointerException if the specified object is null 1016 * and the field is an instance field. 1017 * @throws ExceptionInInitializerError if the initialization provoked 1018 * by this method fails. 1019 * @see Field#set 1020 */ 1021 @CallerSensitive 1022 @ForceInline // to ensure Reflection.getCallerClass optimization 1023 public void setInt(Object obj, int i) 1024 throws IllegalArgumentException, IllegalAccessException 1025 { 1026 if (!override) { 1027 Class<?> caller = Reflection.getCallerClass(); 1028 checkAccess(caller, obj); 1029 getFieldAccessor().setInt(obj, i); 1030 } else { 1031 getOverrideFieldAccessor().setInt(obj, i); 1032 } 1033 } 1034 1035 /** 1036 * Sets the value of a field as a {@code long} on the specified object. 1037 * This method is equivalent to 1038 * {@code set(obj, lObj)}, 1039 * where {@code lObj} is a {@code Long} object and 1040 * {@code lObj.longValue() == l}. 1041 * 1042 * @param obj the object whose field should be modified 1043 * @param l the new value for the field of {@code obj} 1044 * being modified 1045 * 1046 * @throws IllegalAccessException if this {@code Field} object 1047 * is enforcing Java language access control and the underlying 1048 * field is either inaccessible or final; 1049 * or if this {@code Field} object has no write access. 1050 * @throws IllegalArgumentException if the specified object is not an 1051 * instance of the class or interface declaring the underlying 1052 * field (or a subclass or implementor thereof), 1053 * or if an unwrapping conversion fails. 1054 * @throws NullPointerException if the specified object is null 1055 * and the field is an instance field. 1056 * @throws ExceptionInInitializerError if the initialization provoked 1057 * by this method fails. 1058 * @see Field#set 1059 */ 1060 @CallerSensitive 1061 @ForceInline // to ensure Reflection.getCallerClass optimization 1062 public void setLong(Object obj, long l) 1063 throws IllegalArgumentException, IllegalAccessException 1064 { 1065 if (!override) { 1066 Class<?> caller = Reflection.getCallerClass(); 1067 checkAccess(caller, obj); 1068 getFieldAccessor().setLong(obj, l); 1069 } else { 1070 getOverrideFieldAccessor().setLong(obj, l); 1071 } 1072 } 1073 1074 /** 1075 * Sets the value of a field as a {@code float} on the specified object. 1076 * This method is equivalent to 1077 * {@code set(obj, fObj)}, 1078 * where {@code fObj} is a {@code Float} object and 1079 * {@code fObj.floatValue() == f}. 1080 * 1081 * @param obj the object whose field should be modified 1082 * @param f the new value for the field of {@code obj} 1083 * being modified 1084 * 1085 * @throws IllegalAccessException if this {@code Field} object 1086 * is enforcing Java language access control and the underlying 1087 * field is either inaccessible or final; 1088 * or if this {@code Field} object has no write access. 1089 * @throws IllegalArgumentException if the specified object is not an 1090 * instance of the class or interface declaring the underlying 1091 * field (or a subclass or implementor thereof), 1092 * or if an unwrapping conversion fails. 1093 * @throws NullPointerException if the specified object is null 1094 * and the field is an instance field. 1095 * @throws ExceptionInInitializerError if the initialization provoked 1096 * by this method fails. 1097 * @see Field#set 1098 */ 1099 @CallerSensitive 1100 @ForceInline // to ensure Reflection.getCallerClass optimization 1101 public void setFloat(Object obj, float f) 1102 throws IllegalArgumentException, IllegalAccessException 1103 { 1104 if (!override) { 1105 Class<?> caller = Reflection.getCallerClass(); 1106 checkAccess(caller, obj); 1107 getFieldAccessor().setFloat(obj, f); 1108 } else { 1109 getOverrideFieldAccessor().setFloat(obj, f); 1110 } 1111 } 1112 1113 /** 1114 * Sets the value of a field as a {@code double} on the specified object. 1115 * This method is equivalent to 1116 * {@code set(obj, dObj)}, 1117 * where {@code dObj} is a {@code Double} object and 1118 * {@code dObj.doubleValue() == d}. 1119 * 1120 * @param obj the object whose field should be modified 1121 * @param d the new value for the field of {@code obj} 1122 * being modified 1123 * 1124 * @throws IllegalAccessException if this {@code Field} object 1125 * is enforcing Java language access control and the underlying 1126 * field is either inaccessible or final; 1127 * or if this {@code Field} object has no write access. 1128 * @throws IllegalArgumentException if the specified object is not an 1129 * instance of the class or interface declaring the underlying 1130 * field (or a subclass or implementor thereof), 1131 * or if an unwrapping conversion fails. 1132 * @throws NullPointerException if the specified object is null 1133 * and the field is an instance field. 1134 * @throws ExceptionInInitializerError if the initialization provoked 1135 * by this method fails. 1136 * @see Field#set 1137 */ 1138 @CallerSensitive 1139 @ForceInline // to ensure Reflection.getCallerClass optimization 1140 public void setDouble(Object obj, double d) 1141 throws IllegalArgumentException, IllegalAccessException 1142 { 1143 if (!override) { 1144 Class<?> caller = Reflection.getCallerClass(); 1145 checkAccess(caller, obj); 1146 getFieldAccessor().setDouble(obj, d); 1147 } else { 1148 getOverrideFieldAccessor().setDouble(obj, d); 1149 } 1150 } 1151 1152 // check access to field 1153 private void checkAccess(Class<?> caller, Object obj) 1154 throws IllegalAccessException 1155 { 1156 checkAccess(caller, clazz, 1157 Modifier.isStatic(modifiers) ? null : obj.getClass(), 1158 modifiers); 1159 } 1160 1161 // security check is done before calling this method 1162 private FieldAccessor getFieldAccessor() { 1163 FieldAccessor a = fieldAccessor; 1164 return (a != null) ? a : acquireFieldAccessor(); 1165 } 1166 1167 private FieldAccessor getOverrideFieldAccessor() { 1168 FieldAccessor a = overrideFieldAccessor; 1169 return (a != null) ? a : acquireOverrideFieldAccessor(); 1170 } 1171 1172 // NOTE that there is no synchronization used here. It is correct 1173 // (though not efficient) to generate more than one FieldAccessor 1174 // for a given Field. However, avoiding synchronization will 1175 // probably make the implementation more scalable. 1176 private FieldAccessor acquireFieldAccessor() { 1177 // First check to see if one has been created yet, and take it 1178 // if so 1179 Field root = this.root; 1180 FieldAccessor tmp = root == null ? null : root.fieldAccessor; 1181 if (tmp != null) { 1182 fieldAccessor = tmp; 1183 } else { 1184 // Otherwise fabricate one and propagate it up to the root 1185 tmp = reflectionFactory.newFieldAccessor(this, false); 1186 setFieldAccessor(tmp); 1187 } 1188 return tmp; 1189 } 1190 1191 private FieldAccessor acquireOverrideFieldAccessor() { 1192 // First check to see if one has been created yet, and take it 1193 // if so 1194 Field root = this.root; 1195 FieldAccessor tmp = root == null ? null : root.overrideFieldAccessor; 1196 if (tmp != null) { 1197 overrideFieldAccessor = tmp; 1198 } else { 1199 // Otherwise fabricate one and propagate it up to the root 1200 tmp = reflectionFactory.newFieldAccessor(this, true); 1201 setOverrideFieldAccessor(tmp); 1202 } 1203 return tmp; 1204 } 1205 1206 // Sets the fieldAccessor for this Field object and 1207 // (recursively) its root 1208 private void setFieldAccessor(FieldAccessor accessor) { 1209 fieldAccessor = accessor; 1210 // Propagate up 1211 Field root = this.root; 1212 if (root != null) { 1213 root.setFieldAccessor(accessor); 1214 } 1215 } 1216 1217 // Sets the overrideFieldAccessor for this Field object and 1218 // (recursively) its root 1219 private void setOverrideFieldAccessor(FieldAccessor accessor) { 1220 overrideFieldAccessor = accessor; 1221 // Propagate up 1222 Field root = this.root; 1223 if (root != null) { 1224 root.setOverrideFieldAccessor(accessor); 1225 } 1226 } 1227 1228 @Override 1229 /* package-private */ Field getRoot() { 1230 return root; 1231 } 1232 1233 /* package-private */ boolean isTrustedFinal() { 1234 return trustedFinal; 1235 } 1236 1237 /** 1238 * {@inheritDoc} 1239 * 1240 * @throws NullPointerException {@inheritDoc} 1241 * @since 1.5 1242 */ 1243 @Override 1244 public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { 1245 Objects.requireNonNull(annotationClass); 1246 return annotationClass.cast(declaredAnnotations().get(annotationClass)); 1247 } 1248 1249 /** 1250 * {@inheritDoc} 1251 * 1252 * @throws NullPointerException {@inheritDoc} 1253 * @since 1.8 1254 */ 1255 @Override 1256 public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) { 1257 Objects.requireNonNull(annotationClass); 1258 1259 return AnnotationSupport.getDirectlyAndIndirectlyPresent(declaredAnnotations(), annotationClass); 1260 } 1261 1262 /** 1263 * {@inheritDoc} 1264 */ 1265 @Override 1266 public Annotation[] getDeclaredAnnotations() { 1267 return AnnotationParser.toArray(declaredAnnotations()); 1268 } 1269 1270 private transient volatile Map<Class<? extends Annotation>, Annotation> declaredAnnotations; 1271 1272 private Map<Class<? extends Annotation>, Annotation> declaredAnnotations() { 1273 Map<Class<? extends Annotation>, Annotation> declAnnos; 1274 if ((declAnnos = declaredAnnotations) == null) { 1275 synchronized (this) { 1276 if ((declAnnos = declaredAnnotations) == null) { 1277 Field root = this.root; 1278 if (root != null) { 1279 declAnnos = root.declaredAnnotations(); 1280 } else { 1281 declAnnos = AnnotationParser.parseAnnotations( 1282 annotations, 1283 SharedSecrets.getJavaLangAccess() 1284 .getConstantPool(getDeclaringClass()), 1285 getDeclaringClass()); 1286 } 1287 declaredAnnotations = declAnnos; 1288 } 1289 } 1290 } 1291 return declAnnos; 1292 } 1293 1294 private native byte[] getTypeAnnotationBytes0(); 1295 1296 /** 1297 * Returns an AnnotatedType object that represents the use of a type to specify 1298 * the declared type of the field represented by this Field. 1299 * @return an object representing the declared type of the field 1300 * represented by this Field 1301 * 1302 * @since 1.8 1303 */ 1304 public AnnotatedType getAnnotatedType() { 1305 return TypeAnnotationParser.buildAnnotatedType(getTypeAnnotationBytes0(), 1306 SharedSecrets.getJavaLangAccess(). 1307 getConstantPool(getDeclaringClass()), 1308 this, 1309 getDeclaringClass(), 1310 getGenericType(), 1311 TypeAnnotation.TypeAnnotationTarget.FIELD); 1312 } 1313 }