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