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