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