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