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