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