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