1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. Oracle designates this 9 * particular file as subject to the "Classpath" exception as provided 10 * by Oracle in the LICENSE file that accompanied this code. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23 * or visit www.oracle.com if you need additional information or have any 24 * questions. 25 */ 26 27 package java.lang.reflect; 28 29 import dalvik.annotation.optimization.FastNative; 30 import sun.reflect.CallerSensitive; 31 import java.lang.annotation.Annotation; 32 import java.util.Objects; 33 import libcore.reflect.AnnotatedElements; 34 import libcore.reflect.GenericSignatureParser; 35 36 37 /** 38 * A {@code Field} provides information about, and dynamic access to, a 39 * single field of a class or an interface. The reflected field may 40 * be a class (static) field or an instance field. 41 * 42 * <p>A {@code Field} permits widening conversions to occur during a get or 43 * set access operation, but throws an {@code IllegalArgumentException} if a 44 * narrowing conversion would occur. 45 * 46 * @see Member 47 * @see java.lang.Class 48 * @see java.lang.Class#getFields() 49 * @see java.lang.Class#getField(String) 50 * @see java.lang.Class#getDeclaredFields() 51 * @see java.lang.Class#getDeclaredField(String) 52 * 53 * @author Kenneth Russell 54 * @author Nakul Saraiya 55 */ 56 public final 57 class Field extends AccessibleObject implements Member { 58 // Android-changed: Extensive modifications made throughout the class for ART. 59 // Android-changed: Many fields and methods removed / modified. 60 // Android-removed: Type annotations runtime code. Not supported on Android. 61 62 private int accessFlags; 63 private Class<?> declaringClass; 64 private int dexFieldIndex; 65 private int offset; 66 private Class<?> type; 67 Field()68 private Field() { 69 } 70 71 /** 72 * Returns the {@code Class} object representing the class or interface 73 * that declares the field represented by this {@code Field} object. 74 */ getDeclaringClass()75 public Class<?> getDeclaringClass() { 76 // Android-changed: Adjust code for different field names. 77 return declaringClass; 78 } 79 80 /** 81 * Returns the name of the field represented by this {@code Field} object. 82 */ getName()83 public String getName() { 84 // Android-changed: getName() implemented differently. 85 if (dexFieldIndex == -1) { 86 // Proxy classes have 1 synthesized static field with no valid dex index. 87 if (!declaringClass.isProxy()) { 88 throw new AssertionError(); 89 } 90 return "throws"; 91 } 92 93 return getNameInternal(); 94 } 95 96 // Android-added: getName() implemented differently. 97 @FastNative getNameInternal()98 private native String getNameInternal(); 99 100 /** 101 * Returns the Java language modifiers for the field represented 102 * by this {@code Field} object, as an integer. The {@code Modifier} class should 103 * be used to decode the modifiers. 104 * 105 * @see Modifier 106 */ getModifiers()107 public int getModifiers() { 108 // Android-changed: Adjust getModifiers() implementation to mask extra bits used on Android. 109 return accessFlags & 0xffff; // mask out bits not used by Java 110 } 111 112 /** 113 * Returns {@code true} if this field represents an element of 114 * an enumerated type; returns {@code false} otherwise. 115 * 116 * @return {@code true} if and only if this field represents an element of 117 * an enumerated type. 118 * @since 1.5 119 */ isEnumConstant()120 public boolean isEnumConstant() { 121 return (getModifiers() & Modifier.ENUM) != 0; 122 } 123 124 /** 125 * Returns {@code true} if this field is a synthetic 126 * field; returns {@code false} otherwise. 127 * 128 * @return true if and only if this field is a synthetic 129 * field as defined by the Java Language Specification. 130 * @since 1.5 131 */ isSynthetic()132 public boolean isSynthetic() { 133 return Modifier.isSynthetic(getModifiers()); 134 } 135 136 /** 137 * Returns a {@code Class} object that identifies the 138 * declared type for the field represented by this 139 * {@code Field} object. 140 * 141 * @return a {@code Class} object identifying the declared 142 * type of the field represented by this object 143 */ getType()144 public Class<?> getType() { 145 return type; 146 } 147 148 /** 149 * Returns a {@code Type} object that represents the declared type for 150 * the field represented by this {@code Field} object. 151 * 152 * <p>If the {@code Type} is a parameterized type, the 153 * {@code Type} object returned must accurately reflect the 154 * actual type parameters used in the source code. 155 * 156 * <p>If the type of the underlying field is a type variable or a 157 * parameterized type, it is created. Otherwise, it is resolved. 158 * 159 * @return a {@code Type} object that represents the declared type for 160 * the field represented by this {@code Field} object 161 * @throws GenericSignatureFormatError if the generic field 162 * signature does not conform to the format specified in 163 * <cite>The Java™ Virtual Machine Specification</cite> 164 * @throws TypeNotPresentException if the generic type 165 * signature of the underlying field refers to a non-existent 166 * type declaration 167 * @throws MalformedParameterizedTypeException if the generic 168 * signature of the underlying field refers to a parameterized type 169 * that cannot be instantiated for any reason 170 * @since 1.5 171 */ getGenericType()172 public Type getGenericType() { 173 // Android-changed: getGenericType() implemented differently. 174 String signatureAttribute = getSignatureAttribute(); 175 ClassLoader cl = declaringClass.getClassLoader(); 176 GenericSignatureParser parser = new GenericSignatureParser(cl); 177 parser.parseForField(declaringClass, signatureAttribute); 178 Type genericType = parser.fieldType; 179 if (genericType == null) { 180 genericType = getType(); 181 } 182 return genericType; 183 } 184 185 // BEGIN Android-added: getGenericType() implemented differently. getSignatureAttribute()186 private String getSignatureAttribute() { 187 String[] annotation = getSignatureAnnotation(); 188 if (annotation == null) { 189 return null; 190 } 191 StringBuilder result = new StringBuilder(); 192 for (String s : annotation) { 193 result.append(s); 194 } 195 return result.toString(); 196 } 197 @FastNative getSignatureAnnotation()198 private native String[] getSignatureAnnotation(); 199 // END Android-added: getGenericType() implemented differently. 200 201 202 /** 203 * Compares this {@code Field} against the specified object. Returns 204 * true if the objects are the same. Two {@code Field} objects are the same if 205 * they were declared by the same class and have the same name 206 * and type. 207 */ equals(Object obj)208 public boolean equals(Object obj) { 209 if (obj != null && obj instanceof Field) { 210 Field other = (Field)obj; 211 return (getDeclaringClass() == other.getDeclaringClass()) 212 && (getName() == other.getName()) 213 && (getType() == other.getType()); 214 } 215 return false; 216 } 217 218 /** 219 * Returns a hashcode for this {@code Field}. This is computed as the 220 * exclusive-or of the hashcodes for the underlying field's 221 * declaring class name and its name. 222 */ hashCode()223 public int hashCode() { 224 return getDeclaringClass().getName().hashCode() ^ getName().hashCode(); 225 } 226 227 /** 228 * Returns a string describing this {@code Field}. The format is 229 * the access modifiers for the field, if any, followed 230 * by the field type, followed by a space, followed by 231 * the fully-qualified name of the class declaring the field, 232 * followed by a period, followed by the name of the field. 233 * For example: 234 * <pre> 235 * public static final int java.lang.Thread.MIN_PRIORITY 236 * private int java.io.FileDescriptor.fd 237 * </pre> 238 * 239 * <p>The modifiers are placed in canonical order as specified by 240 * "The Java Language Specification". This is {@code public}, 241 * {@code protected} or {@code private} first, and then other 242 * modifiers in the following order: {@code static}, {@code final}, 243 * {@code transient}, {@code volatile}. 244 * 245 * @return a string describing this {@code Field} 246 * @jls 8.3.1 Field Modifiers 247 */ toString()248 public String toString() { 249 int mod = getModifiers(); 250 return (((mod == 0) ? "" : (Modifier.toString(mod) + " ")) 251 + getType().getTypeName() + " " 252 + getDeclaringClass().getTypeName() + "." 253 + getName()); 254 } 255 256 /** 257 * Returns a string describing this {@code Field}, including 258 * its generic type. The format is the access modifiers for the 259 * field, if any, followed by the generic field type, followed by 260 * a space, followed by the fully-qualified name of the class 261 * declaring the field, followed by a period, followed by the name 262 * of the field. 263 * 264 * <p>The modifiers are placed in canonical order as specified by 265 * "The Java Language Specification". This is {@code public}, 266 * {@code protected} or {@code private} first, and then other 267 * modifiers in the following order: {@code static}, {@code final}, 268 * {@code transient}, {@code volatile}. 269 * 270 * @return a string describing this {@code Field}, including 271 * its generic type 272 * 273 * @since 1.5 274 * @jls 8.3.1 Field Modifiers 275 */ toGenericString()276 public String toGenericString() { 277 int mod = getModifiers(); 278 Type fieldType = getGenericType(); 279 return (((mod == 0) ? "" : (Modifier.toString(mod) + " ")) 280 + fieldType.getTypeName() + " " 281 + getDeclaringClass().getTypeName() + "." 282 + getName()); 283 } 284 285 /** 286 * Returns the value of the field represented by this {@code Field}, on 287 * the specified object. The value is automatically wrapped in an 288 * object if it has a primitive type. 289 * 290 * <p>The underlying field's value is obtained as follows: 291 * 292 * <p>If the underlying field is a static field, the {@code obj} argument 293 * is ignored; it may be null. 294 * 295 * <p>Otherwise, the underlying field is an instance field. If the 296 * specified {@code obj} argument is null, the method throws a 297 * {@code NullPointerException}. If the specified object is not an 298 * instance of the class or interface declaring the underlying 299 * field, the method throws an {@code IllegalArgumentException}. 300 * 301 * <p>If this {@code Field} object is enforcing Java language access control, and 302 * the underlying field is inaccessible, the method throws an 303 * {@code IllegalAccessException}. 304 * If the underlying field is static, the class that declared the 305 * field is initialized if it has not already been initialized. 306 * 307 * <p>Otherwise, the value is retrieved from the underlying instance 308 * or static field. If the field has a primitive type, the value 309 * is wrapped in an object before being returned, otherwise it is 310 * returned as is. 311 * 312 * <p>If the field is hidden in the type of {@code obj}, 313 * the field's value is obtained according to the preceding rules. 314 * 315 * @param obj object from which the represented field's value is 316 * to be extracted 317 * @return the value of the represented field in object 318 * {@code obj}; primitive values are wrapped in an appropriate 319 * object before being returned 320 * 321 * @exception IllegalAccessException if this {@code Field} object 322 * is enforcing Java language access control and the underlying 323 * field is inaccessible. 324 * @exception IllegalArgumentException if the specified object is not an 325 * instance of the class or interface declaring the underlying 326 * field (or a subclass or implementor thereof). 327 * @exception NullPointerException if the specified object is null 328 * and the field is an instance field. 329 * @exception ExceptionInInitializerError if the initialization provoked 330 * by this method fails. 331 */ 332 @CallerSensitive 333 // Android-changed: get*(Object) implemented natively. 334 @FastNative get(Object obj)335 public native Object get(Object obj) 336 throws IllegalArgumentException, IllegalAccessException; 337 338 /** 339 * Gets the value of a static or instance {@code boolean} field. 340 * 341 * @param obj the object to extract the {@code boolean} value 342 * from 343 * @return the value of the {@code boolean} field 344 * 345 * @exception IllegalAccessException if this {@code Field} object 346 * is enforcing Java language access control and the underlying 347 * field is inaccessible. 348 * @exception IllegalArgumentException if the specified object is not 349 * an instance of the class or interface declaring the 350 * underlying field (or a subclass or implementor 351 * thereof), or if the field value cannot be 352 * converted to the type {@code boolean} by a 353 * widening conversion. 354 * @exception NullPointerException if the specified object is null 355 * and the field is an instance field. 356 * @exception ExceptionInInitializerError if the initialization provoked 357 * by this method fails. 358 * @see Field#get 359 */ 360 @CallerSensitive 361 // Android-changed: get*(Object) implemented natively. 362 @FastNative getBoolean(Object obj)363 public native boolean getBoolean(Object obj) 364 throws IllegalArgumentException, IllegalAccessException; 365 366 /** 367 * Gets the value of a static or instance {@code byte} field. 368 * 369 * @param obj the object to extract the {@code byte} value 370 * from 371 * @return the value of the {@code byte} field 372 * 373 * @exception IllegalAccessException if this {@code Field} object 374 * is enforcing Java language access control and the underlying 375 * field is inaccessible. 376 * @exception IllegalArgumentException if the specified object is not 377 * an instance of the class or interface declaring the 378 * underlying field (or a subclass or implementor 379 * thereof), or if the field value cannot be 380 * converted to the type {@code byte} by a 381 * widening conversion. 382 * @exception NullPointerException if the specified object is null 383 * and the field is an instance field. 384 * @exception ExceptionInInitializerError if the initialization provoked 385 * by this method fails. 386 * @see Field#get 387 */ 388 @CallerSensitive 389 // Android-changed: get*(Object) implemented natively. 390 @FastNative getByte(Object obj)391 public native byte getByte(Object obj) 392 throws IllegalArgumentException, IllegalAccessException; 393 394 /** 395 * Gets the value of a static or instance field of type 396 * {@code char} or of another primitive type convertible to 397 * type {@code char} via a widening conversion. 398 * 399 * @param obj the object to extract the {@code char} value 400 * from 401 * @return the value of the field converted to type {@code char} 402 * 403 * @exception IllegalAccessException if this {@code Field} object 404 * is enforcing Java language access control and the underlying 405 * field is inaccessible. 406 * @exception IllegalArgumentException if the specified object is not 407 * an instance of the class or interface declaring the 408 * underlying field (or a subclass or implementor 409 * thereof), or if the field value cannot be 410 * converted to the type {@code char} by a 411 * widening conversion. 412 * @exception NullPointerException if the specified object is null 413 * and the field is an instance field. 414 * @exception ExceptionInInitializerError if the initialization provoked 415 * by this method fails. 416 * @see Field#get 417 */ 418 @CallerSensitive 419 // Android-changed: get*(Object) implemented natively. 420 @FastNative getChar(Object obj)421 public native char getChar(Object obj) 422 throws IllegalArgumentException, IllegalAccessException; 423 424 /** 425 * Gets the value of a static or instance field of type 426 * {@code short} or of another primitive type convertible to 427 * type {@code short} via a widening conversion. 428 * 429 * @param obj the object to extract the {@code short} value 430 * from 431 * @return the value of the field converted to type {@code short} 432 * 433 * @exception IllegalAccessException if this {@code Field} object 434 * is enforcing Java language access control and the underlying 435 * field is inaccessible. 436 * @exception IllegalArgumentException if the specified object is not 437 * an instance of the class or interface declaring the 438 * underlying field (or a subclass or implementor 439 * thereof), or if the field value cannot be 440 * converted to the type {@code short} by a 441 * widening conversion. 442 * @exception NullPointerException if the specified object is null 443 * and the field is an instance field. 444 * @exception ExceptionInInitializerError if the initialization provoked 445 * by this method fails. 446 * @see Field#get 447 */ 448 @CallerSensitive 449 // Android-changed: get*(Object) implemented natively. 450 @FastNative getShort(Object obj)451 public native short getShort(Object obj) 452 throws IllegalArgumentException, IllegalAccessException; 453 454 /** 455 * Gets the value of a static or instance field of type 456 * {@code int} or of another primitive type convertible to 457 * type {@code int} via a widening conversion. 458 * 459 * @param obj the object to extract the {@code int} value 460 * from 461 * @return the value of the field converted to type {@code int} 462 * 463 * @exception IllegalAccessException if this {@code Field} object 464 * is enforcing Java language access control and the underlying 465 * field is inaccessible. 466 * @exception IllegalArgumentException if the specified object is not 467 * an instance of the class or interface declaring the 468 * underlying field (or a subclass or implementor 469 * thereof), or if the field value cannot be 470 * converted to the type {@code int} by a 471 * widening conversion. 472 * @exception NullPointerException if the specified object is null 473 * and the field is an instance field. 474 * @exception ExceptionInInitializerError if the initialization provoked 475 * by this method fails. 476 * @see Field#get 477 */ 478 @CallerSensitive 479 // Android-changed: get*(Object) implemented natively. 480 @FastNative getInt(Object obj)481 public native int getInt(Object obj) 482 throws IllegalArgumentException, IllegalAccessException; 483 484 /** 485 * Gets the value of a static or instance field of type 486 * {@code long} or of another primitive type convertible to 487 * type {@code long} via a widening conversion. 488 * 489 * @param obj the object to extract the {@code long} value 490 * from 491 * @return the value of the field converted to type {@code long} 492 * 493 * @exception IllegalAccessException if this {@code Field} object 494 * is enforcing Java language access control and the underlying 495 * field is inaccessible. 496 * @exception IllegalArgumentException if the specified object is not 497 * an instance of the class or interface declaring the 498 * underlying field (or a subclass or implementor 499 * thereof), or if the field value cannot be 500 * converted to the type {@code long} by a 501 * widening conversion. 502 * @exception NullPointerException if the specified object is null 503 * and the field is an instance field. 504 * @exception ExceptionInInitializerError if the initialization provoked 505 * by this method fails. 506 * @see Field#get 507 */ 508 @CallerSensitive 509 // Android-changed: get*(Object) implemented natively. 510 @FastNative getLong(Object obj)511 public native long getLong(Object obj) 512 throws IllegalArgumentException, IllegalAccessException; 513 514 /** 515 * Gets the value of a static or instance field of type 516 * {@code float} or of another primitive type convertible to 517 * type {@code float} via a widening conversion. 518 * 519 * @param obj the object to extract the {@code float} value 520 * from 521 * @return the value of the field converted to type {@code float} 522 * 523 * @exception IllegalAccessException if this {@code Field} object 524 * is enforcing Java language access control and the underlying 525 * field is inaccessible. 526 * @exception IllegalArgumentException if the specified object is not 527 * an instance of the class or interface declaring the 528 * underlying field (or a subclass or implementor 529 * thereof), or if the field value cannot be 530 * converted to the type {@code float} by a 531 * widening conversion. 532 * @exception NullPointerException if the specified object is null 533 * and the field is an instance field. 534 * @exception ExceptionInInitializerError if the initialization provoked 535 * by this method fails. 536 * @see Field#get 537 */ 538 @CallerSensitive 539 // Android-changed: get*(Object) implemented natively. 540 @FastNative getFloat(Object obj)541 public native float getFloat(Object obj) 542 throws IllegalArgumentException, IllegalAccessException; 543 544 /** 545 * Gets the value of a static or instance field of type 546 * {@code double} or of another primitive type convertible to 547 * type {@code double} via a widening conversion. 548 * 549 * @param obj the object to extract the {@code double} value 550 * from 551 * @return the value of the field converted to type {@code double} 552 * 553 * @exception IllegalAccessException if this {@code Field} object 554 * is enforcing Java language access control and the underlying 555 * field is inaccessible. 556 * @exception IllegalArgumentException if the specified object is not 557 * an instance of the class or interface declaring the 558 * underlying field (or a subclass or implementor 559 * thereof), or if the field value cannot be 560 * converted to the type {@code double} by a 561 * widening conversion. 562 * @exception NullPointerException if the specified object is null 563 * and the field is an instance field. 564 * @exception ExceptionInInitializerError if the initialization provoked 565 * by this method fails. 566 * @see Field#get 567 */ 568 @CallerSensitive 569 // Android-changed: get*(Object) implemented natively. 570 @FastNative getDouble(Object obj)571 public native double getDouble(Object obj) 572 throws IllegalArgumentException, IllegalAccessException; 573 574 /** 575 * Sets the field represented by this {@code Field} object on the 576 * specified object argument to the specified new value. The new 577 * value is automatically unwrapped if the underlying field has a 578 * primitive type. 579 * 580 * <p>The operation proceeds as follows: 581 * 582 * <p>If the underlying field is static, the {@code obj} argument is 583 * ignored; it may be null. 584 * 585 * <p>Otherwise the underlying field is an instance field. If the 586 * specified object argument is null, the method throws a 587 * {@code NullPointerException}. If the specified object argument is not 588 * an instance of the class or interface declaring the underlying 589 * field, the method throws an {@code IllegalArgumentException}. 590 * 591 * <p>If this {@code Field} object is enforcing Java language access control, and 592 * the underlying field is inaccessible, the method throws an 593 * {@code IllegalAccessException}. 594 * 595 * <p>If the underlying field is final, the method throws an 596 * {@code IllegalAccessException} unless {@code setAccessible(true)} 597 * has succeeded for this {@code Field} object 598 * and the field is non-static. Setting a final field in this way 599 * is meaningful only during deserialization or reconstruction of 600 * instances of classes with blank final fields, before they are 601 * made available for access by other parts of a program. Use in 602 * any other context may have unpredictable effects, including cases 603 * in which other parts of a program continue to use the original 604 * value of this field. 605 * 606 * <p>If the underlying field is of a primitive type, an unwrapping 607 * conversion is attempted to convert the new value to a value of 608 * a primitive type. If this attempt fails, the method throws an 609 * {@code IllegalArgumentException}. 610 * 611 * <p>If, after possible unwrapping, the new value cannot be 612 * converted to the type of the underlying field by an identity or 613 * widening conversion, the method throws an 614 * {@code IllegalArgumentException}. 615 * 616 * <p>If the underlying field is static, the class that declared the 617 * field is initialized if it has not already been initialized. 618 * 619 * <p>The field is set to the possibly unwrapped and widened new value. 620 * 621 * <p>If the field is hidden in the type of {@code obj}, 622 * the field's value is set according to the preceding rules. 623 * 624 * @param obj the object whose field should be modified 625 * @param value the new value for the field of {@code obj} 626 * being modified 627 * 628 * @exception IllegalAccessException if this {@code Field} object 629 * is enforcing Java language access control and the underlying 630 * field is either inaccessible or final. 631 * @exception IllegalArgumentException if the specified object is not an 632 * instance of the class or interface declaring the underlying 633 * field (or a subclass or implementor thereof), 634 * or if an unwrapping conversion fails. 635 * @exception NullPointerException if the specified object is null 636 * and the field is an instance field. 637 * @exception ExceptionInInitializerError if the initialization provoked 638 * by this method fails. 639 */ 640 @CallerSensitive 641 // Android-changed: set*(Object, ...) implemented natively. 642 @FastNative set(Object obj, Object value)643 public native void set(Object obj, Object value) 644 throws IllegalArgumentException, IllegalAccessException; 645 646 /** 647 * Sets the value of a field as a {@code boolean} on the specified object. 648 * This method is equivalent to 649 * {@code set(obj, zObj)}, 650 * where {@code zObj} is a {@code Boolean} object and 651 * {@code zObj.booleanValue() == z}. 652 * 653 * @param obj the object whose field should be modified 654 * @param z the new value for the field of {@code obj} 655 * being modified 656 * 657 * @exception IllegalAccessException if this {@code Field} object 658 * is enforcing Java language access control and the underlying 659 * field is either inaccessible or final. 660 * @exception IllegalArgumentException if the specified object is not an 661 * instance of the class or interface declaring the underlying 662 * field (or a subclass or implementor thereof), 663 * or if an unwrapping conversion fails. 664 * @exception NullPointerException if the specified object is null 665 * and the field is an instance field. 666 * @exception ExceptionInInitializerError if the initialization provoked 667 * by this method fails. 668 * @see Field#set 669 */ 670 @CallerSensitive 671 // Android-changed: set*(Object, ...) implemented natively. 672 @FastNative setBoolean(Object obj, boolean z)673 public native void setBoolean(Object obj, boolean z) 674 throws IllegalArgumentException, IllegalAccessException; 675 676 /** 677 * Sets the value of a field as a {@code byte} on the specified object. 678 * This method is equivalent to 679 * {@code set(obj, bObj)}, 680 * where {@code bObj} is a {@code Byte} object and 681 * {@code bObj.byteValue() == b}. 682 * 683 * @param obj the object whose field should be modified 684 * @param b the new value for the field of {@code obj} 685 * being modified 686 * 687 * @exception IllegalAccessException if this {@code Field} object 688 * is enforcing Java language access control and the underlying 689 * field is either inaccessible or final. 690 * @exception IllegalArgumentException if the specified object is not an 691 * instance of the class or interface declaring the underlying 692 * field (or a subclass or implementor thereof), 693 * or if an unwrapping conversion fails. 694 * @exception NullPointerException if the specified object is null 695 * and the field is an instance field. 696 * @exception ExceptionInInitializerError if the initialization provoked 697 * by this method fails. 698 * @see Field#set 699 */ 700 @CallerSensitive 701 // Android-changed: set*(Object, ...) implemented natively. 702 @FastNative setByte(Object obj, byte b)703 public native void setByte(Object obj, byte b) 704 throws IllegalArgumentException, IllegalAccessException; 705 706 /** 707 * Sets the value of a field as a {@code char} on the specified object. 708 * This method is equivalent to 709 * {@code set(obj, cObj)}, 710 * where {@code cObj} is a {@code Character} object and 711 * {@code cObj.charValue() == c}. 712 * 713 * @param obj the object whose field should be modified 714 * @param c the new value for the field of {@code obj} 715 * being modified 716 * 717 * @exception IllegalAccessException if this {@code Field} object 718 * is enforcing Java language access control and the underlying 719 * field is either inaccessible or final. 720 * @exception IllegalArgumentException if the specified object is not an 721 * instance of the class or interface declaring the underlying 722 * field (or a subclass or implementor thereof), 723 * or if an unwrapping conversion fails. 724 * @exception NullPointerException if the specified object is null 725 * and the field is an instance field. 726 * @exception ExceptionInInitializerError if the initialization provoked 727 * by this method fails. 728 * @see Field#set 729 */ 730 @CallerSensitive 731 // Android-changed: set*(Object, ...) implemented natively. 732 @FastNative setChar(Object obj, char c)733 public native void setChar(Object obj, char c) 734 throws IllegalArgumentException, IllegalAccessException; 735 736 /** 737 * Sets the value of a field as a {@code short} on the specified object. 738 * This method is equivalent to 739 * {@code set(obj, sObj)}, 740 * where {@code sObj} is a {@code Short} object and 741 * {@code sObj.shortValue() == s}. 742 * 743 * @param obj the object whose field should be modified 744 * @param s the new value for the field of {@code obj} 745 * being modified 746 * 747 * @exception IllegalAccessException if this {@code Field} object 748 * is enforcing Java language access control and the underlying 749 * field is either inaccessible or final. 750 * @exception IllegalArgumentException if the specified object is not an 751 * instance of the class or interface declaring the underlying 752 * field (or a subclass or implementor thereof), 753 * or if an unwrapping conversion fails. 754 * @exception NullPointerException if the specified object is null 755 * and the field is an instance field. 756 * @exception ExceptionInInitializerError if the initialization provoked 757 * by this method fails. 758 * @see Field#set 759 */ 760 @CallerSensitive 761 // Android-changed: set*(Object, ...) implemented natively. 762 @FastNative setShort(Object obj, short s)763 public native void setShort(Object obj, short s) 764 throws IllegalArgumentException, IllegalAccessException; 765 766 /** 767 * Sets the value of a field as an {@code int} on the specified object. 768 * This method is equivalent to 769 * {@code set(obj, iObj)}, 770 * where {@code iObj} is a {@code Integer} object and 771 * {@code iObj.intValue() == i}. 772 * 773 * @param obj the object whose field should be modified 774 * @param i the new value for the field of {@code obj} 775 * being modified 776 * 777 * @exception IllegalAccessException if this {@code Field} object 778 * is enforcing Java language access control and the underlying 779 * field is either inaccessible or final. 780 * @exception IllegalArgumentException if the specified object is not an 781 * instance of the class or interface declaring the underlying 782 * field (or a subclass or implementor thereof), 783 * or if an unwrapping conversion fails. 784 * @exception NullPointerException if the specified object is null 785 * and the field is an instance field. 786 * @exception ExceptionInInitializerError if the initialization provoked 787 * by this method fails. 788 * @see Field#set 789 */ 790 @CallerSensitive 791 // Android-changed: set*(Object, ...) implemented natively. 792 @FastNative setInt(Object obj, int i)793 public native void setInt(Object obj, int i) 794 throws IllegalArgumentException, IllegalAccessException; 795 796 /** 797 * Sets the value of a field as a {@code long} on the specified object. 798 * This method is equivalent to 799 * {@code set(obj, lObj)}, 800 * where {@code lObj} is a {@code Long} object and 801 * {@code lObj.longValue() == l}. 802 * 803 * @param obj the object whose field should be modified 804 * @param l the new value for the field of {@code obj} 805 * being modified 806 * 807 * @exception IllegalAccessException if this {@code Field} object 808 * is enforcing Java language access control and the underlying 809 * field is either inaccessible or final. 810 * @exception IllegalArgumentException if the specified object is not an 811 * instance of the class or interface declaring the underlying 812 * field (or a subclass or implementor thereof), 813 * or if an unwrapping conversion fails. 814 * @exception NullPointerException if the specified object is null 815 * and the field is an instance field. 816 * @exception ExceptionInInitializerError if the initialization provoked 817 * by this method fails. 818 * @see Field#set 819 */ 820 @CallerSensitive 821 // Android-changed: set*(Object, ...) implemented natively. 822 @FastNative setLong(Object obj, long l)823 public native void setLong(Object obj, long l) 824 throws IllegalArgumentException, IllegalAccessException; 825 826 /** 827 * Sets the value of a field as a {@code float} on the specified object. 828 * This method is equivalent to 829 * {@code set(obj, fObj)}, 830 * where {@code fObj} is a {@code Float} object and 831 * {@code fObj.floatValue() == f}. 832 * 833 * @param obj the object whose field should be modified 834 * @param f the new value for the field of {@code obj} 835 * being modified 836 * 837 * @exception IllegalAccessException if this {@code Field} object 838 * is enforcing Java language access control and the underlying 839 * field is either inaccessible or final. 840 * @exception IllegalArgumentException if the specified object is not an 841 * instance of the class or interface declaring the underlying 842 * field (or a subclass or implementor thereof), 843 * or if an unwrapping conversion fails. 844 * @exception NullPointerException if the specified object is null 845 * and the field is an instance field. 846 * @exception ExceptionInInitializerError if the initialization provoked 847 * by this method fails. 848 * @see Field#set 849 */ 850 @CallerSensitive 851 // Android-changed: set*(Object, ...) implemented natively. 852 @FastNative setFloat(Object obj, float f)853 public native void setFloat(Object obj, float f) 854 throws IllegalArgumentException, IllegalAccessException; 855 856 /** 857 * Sets the value of a field as a {@code double} on the specified object. 858 * This method is equivalent to 859 * {@code set(obj, dObj)}, 860 * where {@code dObj} is a {@code Double} object and 861 * {@code dObj.doubleValue() == d}. 862 * 863 * @param obj the object whose field should be modified 864 * @param d the new value for the field of {@code obj} 865 * being modified 866 * 867 * @exception IllegalAccessException if this {@code Field} object 868 * is enforcing Java language access control and the underlying 869 * field is either inaccessible or final. 870 * @exception IllegalArgumentException if the specified object is not an 871 * instance of the class or interface declaring the underlying 872 * field (or a subclass or implementor thereof), 873 * or if an unwrapping conversion fails. 874 * @exception NullPointerException if the specified object is null 875 * and the field is an instance field. 876 * @exception ExceptionInInitializerError if the initialization provoked 877 * by this method fails. 878 * @see Field#set 879 */ 880 @CallerSensitive 881 // Android-changed: set*(Object, ...) implemented natively. 882 @FastNative setDouble(Object obj, double d)883 public native void setDouble(Object obj, double d) 884 throws IllegalArgumentException, IllegalAccessException; 885 886 /** 887 * @throws NullPointerException {@inheritDoc} 888 * @since 1.5 889 */ getAnnotation(Class<T> annotationClass)890 public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { 891 Objects.requireNonNull(annotationClass); 892 // Android-changed: getAnnotation(Class) implemented differently. 893 return getAnnotationNative(annotationClass); 894 } 895 // Android-added: getAnnotation(Class) implemented differently. 896 @FastNative getAnnotationNative(Class<A> annotationType)897 private native <A extends Annotation> A getAnnotationNative(Class<A> annotationType); 898 899 /** 900 * {@inheritDoc} 901 * @throws NullPointerException {@inheritDoc} 902 * @since 1.8 903 */ 904 @Override getAnnotationsByType(Class<T> annotationClass)905 public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) { 906 // Android-added: getAnnotationsByType(Class) implemented differently. 907 return AnnotatedElements.getDirectOrIndirectAnnotationsByType(this, annotationClass); 908 } 909 910 // BEGIN Android-added: isAnnotationPresent(Class) overridden in Field. 911 @Override isAnnotationPresent(Class<? extends Annotation> annotationType)912 public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) { 913 if (annotationType == null) { 914 throw new NullPointerException("annotationType == null"); 915 } 916 return isAnnotationPresentNative(annotationType); 917 } 918 @FastNative isAnnotationPresentNative(Class<? extends Annotation> annotationType)919 private native boolean isAnnotationPresentNative(Class<? extends Annotation> annotationType); 920 // END Android-added: isAnnotationPresent(Class) overridden in Field. 921 922 /** 923 * {@inheritDoc} 924 */ 925 @Override 926 @FastNative getDeclaredAnnotations()927 public native Annotation[] getDeclaredAnnotations(); 928 929 // BEGIN Android-added: Methods for use by Android-specific code. 930 /** 931 * Returns the index of this field's ID in its dex file. 932 * 933 * @hide 934 */ getDexFieldIndex()935 public int getDexFieldIndex() { 936 return dexFieldIndex; 937 } 938 939 /** 940 * Returns the offset of the field within an instance, or for static fields, the class. 941 * 942 * @hide 943 */ getOffset()944 public int getOffset() { 945 return offset; 946 } 947 948 /** 949 * @hide - export for use by {@code java.lang.invoke.*} 950 */ 951 @FastNative getArtField()952 public native long getArtField(); 953 // END Android-added: Methods for use by Android-specific code. 954 } 955