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