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