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