386 * object before being returned 387 * 388 * @exception IllegalAccessException if this {@code Field} object 389 * is enforcing Java language access control and the underlying 390 * field is inaccessible. 391 * @exception IllegalArgumentException if the specified object is not an 392 * instance of the class or interface declaring the underlying 393 * field (or a subclass or implementor thereof). 394 * @exception NullPointerException if the specified object is null 395 * and the field is an instance field. 396 * @exception ExceptionInInitializerError if the initialization provoked 397 * by this method fails. 398 */ 399 @CallerSensitive 400 @ForceInline // to ensure Reflection.getCallerClass optimization 401 public Object get(Object obj) 402 throws IllegalArgumentException, IllegalAccessException 403 { 404 if (!override) { 405 Class<?> caller = Reflection.getCallerClass(); 406 checkAccess(caller, clazz, obj, modifiers); 407 } 408 return getFieldAccessor(obj).get(obj); 409 } 410 411 /** 412 * Gets the value of a static or instance {@code boolean} field. 413 * 414 * @param obj the object to extract the {@code boolean} value 415 * from 416 * @return the value of the {@code boolean} field 417 * 418 * @exception IllegalAccessException if this {@code Field} object 419 * is enforcing Java language access control and the underlying 420 * field is inaccessible. 421 * @exception IllegalArgumentException if the specified object is not 422 * an instance of the class or interface declaring the 423 * underlying field (or a subclass or implementor 424 * thereof), or if the field value cannot be 425 * converted to the type {@code boolean} by a 426 * widening conversion. 427 * @exception NullPointerException if the specified object is null 428 * and the field is an instance field. 429 * @exception ExceptionInInitializerError if the initialization provoked 430 * by this method fails. 431 * @see Field#get 432 */ 433 @CallerSensitive 434 @ForceInline // to ensure Reflection.getCallerClass optimization 435 public boolean getBoolean(Object obj) 436 throws IllegalArgumentException, IllegalAccessException 437 { 438 if (!override) { 439 Class<?> caller = Reflection.getCallerClass(); 440 checkAccess(caller, clazz, obj, modifiers); 441 } 442 return getFieldAccessor(obj).getBoolean(obj); 443 } 444 445 /** 446 * Gets the value of a static or instance {@code byte} field. 447 * 448 * @param obj the object to extract the {@code byte} value 449 * from 450 * @return the value of the {@code byte} field 451 * 452 * @exception IllegalAccessException if this {@code Field} object 453 * is enforcing Java language access control and the underlying 454 * field is inaccessible. 455 * @exception IllegalArgumentException if the specified object is not 456 * an instance of the class or interface declaring the 457 * underlying field (or a subclass or implementor 458 * thereof), or if the field value cannot be 459 * converted to the type {@code byte} by a 460 * widening conversion. 461 * @exception NullPointerException if the specified object is null 462 * and the field is an instance field. 463 * @exception ExceptionInInitializerError if the initialization provoked 464 * by this method fails. 465 * @see Field#get 466 */ 467 @CallerSensitive 468 @ForceInline // to ensure Reflection.getCallerClass optimization 469 public byte getByte(Object obj) 470 throws IllegalArgumentException, IllegalAccessException 471 { 472 if (!override) { 473 Class<?> caller = Reflection.getCallerClass(); 474 checkAccess(caller, clazz, obj, modifiers); 475 } 476 return getFieldAccessor(obj).getByte(obj); 477 } 478 479 /** 480 * Gets the value of a static or instance field of type 481 * {@code char} or of another primitive type convertible to 482 * type {@code char} via a widening conversion. 483 * 484 * @param obj the object to extract the {@code char} value 485 * from 486 * @return the value of the field converted to type {@code char} 487 * 488 * @exception IllegalAccessException if this {@code Field} object 489 * is enforcing Java language access control and the underlying 490 * field is inaccessible. 491 * @exception IllegalArgumentException if the specified object is not 492 * an instance of the class or interface declaring the 493 * underlying field (or a subclass or implementor 494 * thereof), or if the field value cannot be 495 * converted to the type {@code char} by a 496 * widening conversion. 497 * @exception NullPointerException if the specified object is null 498 * and the field is an instance field. 499 * @exception ExceptionInInitializerError if the initialization provoked 500 * by this method fails. 501 * @see Field#get 502 */ 503 @CallerSensitive 504 @ForceInline // to ensure Reflection.getCallerClass optimization 505 public char getChar(Object obj) 506 throws IllegalArgumentException, IllegalAccessException 507 { 508 if (!override) { 509 Class<?> caller = Reflection.getCallerClass(); 510 checkAccess(caller, clazz, obj, modifiers); 511 } 512 return getFieldAccessor(obj).getChar(obj); 513 } 514 515 /** 516 * Gets the value of a static or instance field of type 517 * {@code short} or of another primitive type convertible to 518 * type {@code short} via a widening conversion. 519 * 520 * @param obj the object to extract the {@code short} value 521 * from 522 * @return the value of the field converted to type {@code short} 523 * 524 * @exception IllegalAccessException if this {@code Field} object 525 * is enforcing Java language access control and the underlying 526 * field is inaccessible. 527 * @exception IllegalArgumentException if the specified object is not 528 * an instance of the class or interface declaring the 529 * underlying field (or a subclass or implementor 530 * thereof), or if the field value cannot be 531 * converted to the type {@code short} by a 532 * widening conversion. 533 * @exception NullPointerException if the specified object is null 534 * and the field is an instance field. 535 * @exception ExceptionInInitializerError if the initialization provoked 536 * by this method fails. 537 * @see Field#get 538 */ 539 @CallerSensitive 540 @ForceInline // to ensure Reflection.getCallerClass optimization 541 public short getShort(Object obj) 542 throws IllegalArgumentException, IllegalAccessException 543 { 544 if (!override) { 545 Class<?> caller = Reflection.getCallerClass(); 546 checkAccess(caller, clazz, obj, modifiers); 547 } 548 return getFieldAccessor(obj).getShort(obj); 549 } 550 551 /** 552 * Gets the value of a static or instance field of type 553 * {@code int} or of another primitive type convertible to 554 * type {@code int} via a widening conversion. 555 * 556 * @param obj the object to extract the {@code int} value 557 * from 558 * @return the value of the field converted to type {@code int} 559 * 560 * @exception IllegalAccessException if this {@code Field} object 561 * is enforcing Java language access control and the underlying 562 * field is inaccessible. 563 * @exception IllegalArgumentException if the specified object is not 564 * an instance of the class or interface declaring the 565 * underlying field (or a subclass or implementor 566 * thereof), or if the field value cannot be 567 * converted to the type {@code int} by a 568 * widening conversion. 569 * @exception NullPointerException if the specified object is null 570 * and the field is an instance field. 571 * @exception ExceptionInInitializerError if the initialization provoked 572 * by this method fails. 573 * @see Field#get 574 */ 575 @CallerSensitive 576 @ForceInline // to ensure Reflection.getCallerClass optimization 577 public int getInt(Object obj) 578 throws IllegalArgumentException, IllegalAccessException 579 { 580 if (!override) { 581 Class<?> caller = Reflection.getCallerClass(); 582 checkAccess(caller, clazz, obj, modifiers); 583 } 584 return getFieldAccessor(obj).getInt(obj); 585 } 586 587 /** 588 * Gets the value of a static or instance field of type 589 * {@code long} or of another primitive type convertible to 590 * type {@code long} via a widening conversion. 591 * 592 * @param obj the object to extract the {@code long} value 593 * from 594 * @return the value of the field converted to type {@code long} 595 * 596 * @exception IllegalAccessException if this {@code Field} object 597 * is enforcing Java language access control and the underlying 598 * field is inaccessible. 599 * @exception IllegalArgumentException if the specified object is not 600 * an instance of the class or interface declaring the 601 * underlying field (or a subclass or implementor 602 * thereof), or if the field value cannot be 603 * converted to the type {@code long} by a 604 * widening conversion. 605 * @exception NullPointerException if the specified object is null 606 * and the field is an instance field. 607 * @exception ExceptionInInitializerError if the initialization provoked 608 * by this method fails. 609 * @see Field#get 610 */ 611 @CallerSensitive 612 @ForceInline // to ensure Reflection.getCallerClass optimization 613 public long getLong(Object obj) 614 throws IllegalArgumentException, IllegalAccessException 615 { 616 if (!override) { 617 Class<?> caller = Reflection.getCallerClass(); 618 checkAccess(caller, clazz, obj, modifiers); 619 } 620 return getFieldAccessor(obj).getLong(obj); 621 } 622 623 /** 624 * Gets the value of a static or instance field of type 625 * {@code float} or of another primitive type convertible to 626 * type {@code float} via a widening conversion. 627 * 628 * @param obj the object to extract the {@code float} value 629 * from 630 * @return the value of the field converted to type {@code float} 631 * 632 * @exception IllegalAccessException if this {@code Field} object 633 * is enforcing Java language access control and the underlying 634 * field is inaccessible. 635 * @exception IllegalArgumentException if the specified object is not 636 * an instance of the class or interface declaring the 637 * underlying field (or a subclass or implementor 638 * thereof), or if the field value cannot be 639 * converted to the type {@code float} by a 640 * widening conversion. 641 * @exception NullPointerException if the specified object is null 642 * and the field is an instance field. 643 * @exception ExceptionInInitializerError if the initialization provoked 644 * by this method fails. 645 * @see Field#get 646 */ 647 @CallerSensitive 648 @ForceInline // to ensure Reflection.getCallerClass optimization 649 public float getFloat(Object obj) 650 throws IllegalArgumentException, IllegalAccessException 651 { 652 if (!override) { 653 Class<?> caller = Reflection.getCallerClass(); 654 checkAccess(caller, clazz, obj, modifiers); 655 } 656 return getFieldAccessor(obj).getFloat(obj); 657 } 658 659 /** 660 * Gets the value of a static or instance field of type 661 * {@code double} or of another primitive type convertible to 662 * type {@code double} via a widening conversion. 663 * 664 * @param obj the object to extract the {@code double} value 665 * from 666 * @return the value of the field converted to type {@code double} 667 * 668 * @exception IllegalAccessException if this {@code Field} object 669 * is enforcing Java language access control and the underlying 670 * field is inaccessible. 671 * @exception IllegalArgumentException if the specified object is not 672 * an instance of the class or interface declaring the 673 * underlying field (or a subclass or implementor 674 * thereof), or if the field value cannot be 675 * converted to the type {@code double} by a 676 * widening conversion. 677 * @exception NullPointerException if the specified object is null 678 * and the field is an instance field. 679 * @exception ExceptionInInitializerError if the initialization provoked 680 * by this method fails. 681 * @see Field#get 682 */ 683 @CallerSensitive 684 @ForceInline // to ensure Reflection.getCallerClass optimization 685 public double getDouble(Object obj) 686 throws IllegalArgumentException, IllegalAccessException 687 { 688 if (!override) { 689 Class<?> caller = Reflection.getCallerClass(); 690 checkAccess(caller, clazz, obj, modifiers); 691 } 692 return getFieldAccessor(obj).getDouble(obj); 693 } 694 695 /** 696 * Sets the field represented by this {@code Field} object on the 697 * specified object argument to the specified new value. The new 698 * value is automatically unwrapped if the underlying field has a 699 * primitive type. 700 * 701 * <p>The operation proceeds as follows: 702 * 703 * <p>If the underlying field is static, the {@code obj} argument is 704 * ignored; it may be null. 705 * 706 * <p>Otherwise the underlying field is an instance field. If the 707 * specified object argument is null, the method throws a 708 * {@code NullPointerException}. If the specified object argument is not 709 * an instance of the class or interface declaring the underlying 710 * field, the method throws an {@code IllegalArgumentException}. 748 * 749 * @exception IllegalAccessException if this {@code Field} object 750 * is enforcing Java language access control and the underlying 751 * field is either inaccessible or final. 752 * @exception IllegalArgumentException if the specified object is not an 753 * instance of the class or interface declaring the underlying 754 * field (or a subclass or implementor thereof), 755 * or if an unwrapping conversion fails. 756 * @exception NullPointerException if the specified object is null 757 * and the field is an instance field. 758 * @exception ExceptionInInitializerError if the initialization provoked 759 * by this method fails. 760 */ 761 @CallerSensitive 762 @ForceInline // to ensure Reflection.getCallerClass optimization 763 public void set(Object obj, Object value) 764 throws IllegalArgumentException, IllegalAccessException 765 { 766 if (!override) { 767 Class<?> caller = Reflection.getCallerClass(); 768 checkAccess(caller, clazz, obj, modifiers); 769 } 770 getFieldAccessor(obj).set(obj, value); 771 } 772 773 /** 774 * Sets the value of a field as a {@code boolean} on the specified object. 775 * This method is equivalent to 776 * {@code set(obj, zObj)}, 777 * where {@code zObj} is a {@code Boolean} object and 778 * {@code zObj.booleanValue() == z}. 779 * 780 * @param obj the object whose field should be modified 781 * @param z the new value for the field of {@code obj} 782 * being modified 783 * 784 * @exception IllegalAccessException if this {@code Field} object 785 * is enforcing Java language access control and the underlying 786 * field is either inaccessible or final. 787 * @exception IllegalArgumentException if the specified object is not an 788 * instance of the class or interface declaring the underlying 789 * field (or a subclass or implementor thereof), 790 * or if an unwrapping conversion fails. 791 * @exception NullPointerException if the specified object is null 792 * and the field is an instance field. 793 * @exception ExceptionInInitializerError if the initialization provoked 794 * by this method fails. 795 * @see Field#set 796 */ 797 @CallerSensitive 798 @ForceInline // to ensure Reflection.getCallerClass optimization 799 public void setBoolean(Object obj, boolean z) 800 throws IllegalArgumentException, IllegalAccessException 801 { 802 if (!override) { 803 Class<?> caller = Reflection.getCallerClass(); 804 checkAccess(caller, clazz, obj, modifiers); 805 } 806 getFieldAccessor(obj).setBoolean(obj, z); 807 } 808 809 /** 810 * Sets the value of a field as a {@code byte} on the specified object. 811 * This method is equivalent to 812 * {@code set(obj, bObj)}, 813 * where {@code bObj} is a {@code Byte} object and 814 * {@code bObj.byteValue() == b}. 815 * 816 * @param obj the object whose field should be modified 817 * @param b the new value for the field of {@code obj} 818 * being modified 819 * 820 * @exception IllegalAccessException if this {@code Field} object 821 * is enforcing Java language access control and the underlying 822 * field is either inaccessible or final. 823 * @exception IllegalArgumentException if the specified object is not an 824 * instance of the class or interface declaring the underlying 825 * field (or a subclass or implementor thereof), 826 * or if an unwrapping conversion fails. 827 * @exception NullPointerException if the specified object is null 828 * and the field is an instance field. 829 * @exception ExceptionInInitializerError if the initialization provoked 830 * by this method fails. 831 * @see Field#set 832 */ 833 @CallerSensitive 834 @ForceInline // to ensure Reflection.getCallerClass optimization 835 public void setByte(Object obj, byte b) 836 throws IllegalArgumentException, IllegalAccessException 837 { 838 if (!override) { 839 Class<?> caller = Reflection.getCallerClass(); 840 checkAccess(caller, clazz, obj, modifiers); 841 } 842 getFieldAccessor(obj).setByte(obj, b); 843 } 844 845 /** 846 * Sets the value of a field as a {@code char} on the specified object. 847 * This method is equivalent to 848 * {@code set(obj, cObj)}, 849 * where {@code cObj} is a {@code Character} object and 850 * {@code cObj.charValue() == c}. 851 * 852 * @param obj the object whose field should be modified 853 * @param c the new value for the field of {@code obj} 854 * being modified 855 * 856 * @exception IllegalAccessException if this {@code Field} object 857 * is enforcing Java language access control and the underlying 858 * field is either inaccessible or final. 859 * @exception IllegalArgumentException if the specified object is not an 860 * instance of the class or interface declaring the underlying 861 * field (or a subclass or implementor thereof), 862 * or if an unwrapping conversion fails. 863 * @exception NullPointerException if the specified object is null 864 * and the field is an instance field. 865 * @exception ExceptionInInitializerError if the initialization provoked 866 * by this method fails. 867 * @see Field#set 868 */ 869 @CallerSensitive 870 @ForceInline // to ensure Reflection.getCallerClass optimization 871 public void setChar(Object obj, char c) 872 throws IllegalArgumentException, IllegalAccessException 873 { 874 if (!override) { 875 Class<?> caller = Reflection.getCallerClass(); 876 checkAccess(caller, clazz, obj, modifiers); 877 } 878 getFieldAccessor(obj).setChar(obj, c); 879 } 880 881 /** 882 * Sets the value of a field as a {@code short} on the specified object. 883 * This method is equivalent to 884 * {@code set(obj, sObj)}, 885 * where {@code sObj} is a {@code Short} object and 886 * {@code sObj.shortValue() == s}. 887 * 888 * @param obj the object whose field should be modified 889 * @param s the new value for the field of {@code obj} 890 * being modified 891 * 892 * @exception IllegalAccessException if this {@code Field} object 893 * is enforcing Java language access control and the underlying 894 * field is either inaccessible or final. 895 * @exception IllegalArgumentException if the specified object is not an 896 * instance of the class or interface declaring the underlying 897 * field (or a subclass or implementor thereof), 898 * or if an unwrapping conversion fails. 899 * @exception NullPointerException if the specified object is null 900 * and the field is an instance field. 901 * @exception ExceptionInInitializerError if the initialization provoked 902 * by this method fails. 903 * @see Field#set 904 */ 905 @CallerSensitive 906 @ForceInline // to ensure Reflection.getCallerClass optimization 907 public void setShort(Object obj, short s) 908 throws IllegalArgumentException, IllegalAccessException 909 { 910 if (!override) { 911 Class<?> caller = Reflection.getCallerClass(); 912 checkAccess(caller, clazz, obj, modifiers); 913 } 914 getFieldAccessor(obj).setShort(obj, s); 915 } 916 917 /** 918 * Sets the value of a field as an {@code int} on the specified object. 919 * This method is equivalent to 920 * {@code set(obj, iObj)}, 921 * where {@code iObj} is an {@code Integer} object and 922 * {@code iObj.intValue() == i}. 923 * 924 * @param obj the object whose field should be modified 925 * @param i the new value for the field of {@code obj} 926 * being modified 927 * 928 * @exception IllegalAccessException if this {@code Field} object 929 * is enforcing Java language access control and the underlying 930 * field is either inaccessible or final. 931 * @exception IllegalArgumentException if the specified object is not an 932 * instance of the class or interface declaring the underlying 933 * field (or a subclass or implementor thereof), 934 * or if an unwrapping conversion fails. 935 * @exception NullPointerException if the specified object is null 936 * and the field is an instance field. 937 * @exception ExceptionInInitializerError if the initialization provoked 938 * by this method fails. 939 * @see Field#set 940 */ 941 @CallerSensitive 942 @ForceInline // to ensure Reflection.getCallerClass optimization 943 public void setInt(Object obj, int i) 944 throws IllegalArgumentException, IllegalAccessException 945 { 946 if (!override) { 947 Class<?> caller = Reflection.getCallerClass(); 948 checkAccess(caller, clazz, obj, modifiers); 949 } 950 getFieldAccessor(obj).setInt(obj, i); 951 } 952 953 /** 954 * Sets the value of a field as a {@code long} on the specified object. 955 * This method is equivalent to 956 * {@code set(obj, lObj)}, 957 * where {@code lObj} is a {@code Long} object and 958 * {@code lObj.longValue() == l}. 959 * 960 * @param obj the object whose field should be modified 961 * @param l the new value for the field of {@code obj} 962 * being modified 963 * 964 * @exception IllegalAccessException if this {@code Field} object 965 * is enforcing Java language access control and the underlying 966 * field is either inaccessible or final. 967 * @exception IllegalArgumentException if the specified object is not an 968 * instance of the class or interface declaring the underlying 969 * field (or a subclass or implementor thereof), 970 * or if an unwrapping conversion fails. 971 * @exception NullPointerException if the specified object is null 972 * and the field is an instance field. 973 * @exception ExceptionInInitializerError if the initialization provoked 974 * by this method fails. 975 * @see Field#set 976 */ 977 @CallerSensitive 978 @ForceInline // to ensure Reflection.getCallerClass optimization 979 public void setLong(Object obj, long l) 980 throws IllegalArgumentException, IllegalAccessException 981 { 982 if (!override) { 983 Class<?> caller = Reflection.getCallerClass(); 984 checkAccess(caller, clazz, obj, modifiers); 985 } 986 getFieldAccessor(obj).setLong(obj, l); 987 } 988 989 /** 990 * Sets the value of a field as a {@code float} on the specified object. 991 * This method is equivalent to 992 * {@code set(obj, fObj)}, 993 * where {@code fObj} is a {@code Float} object and 994 * {@code fObj.floatValue() == f}. 995 * 996 * @param obj the object whose field should be modified 997 * @param f the new value for the field of {@code obj} 998 * being modified 999 * 1000 * @exception IllegalAccessException if this {@code Field} object 1001 * is enforcing Java language access control and the underlying 1002 * field is either inaccessible or final. 1003 * @exception IllegalArgumentException if the specified object is not an 1004 * instance of the class or interface declaring the underlying 1005 * field (or a subclass or implementor thereof), 1006 * or if an unwrapping conversion fails. 1007 * @exception NullPointerException if the specified object is null 1008 * and the field is an instance field. 1009 * @exception ExceptionInInitializerError if the initialization provoked 1010 * by this method fails. 1011 * @see Field#set 1012 */ 1013 @CallerSensitive 1014 @ForceInline // to ensure Reflection.getCallerClass optimization 1015 public void setFloat(Object obj, float f) 1016 throws IllegalArgumentException, IllegalAccessException 1017 { 1018 if (!override) { 1019 Class<?> caller = Reflection.getCallerClass(); 1020 checkAccess(caller, clazz, obj, modifiers); 1021 } 1022 getFieldAccessor(obj).setFloat(obj, f); 1023 } 1024 1025 /** 1026 * Sets the value of a field as a {@code double} on the specified object. 1027 * This method is equivalent to 1028 * {@code set(obj, dObj)}, 1029 * where {@code dObj} is a {@code Double} object and 1030 * {@code dObj.doubleValue() == d}. 1031 * 1032 * @param obj the object whose field should be modified 1033 * @param d the new value for the field of {@code obj} 1034 * being modified 1035 * 1036 * @exception IllegalAccessException if this {@code Field} object 1037 * is enforcing Java language access control and the underlying 1038 * field is either inaccessible or final. 1039 * @exception IllegalArgumentException if the specified object is not an 1040 * instance of the class or interface declaring the underlying 1041 * field (or a subclass or implementor thereof), 1042 * or if an unwrapping conversion fails. 1043 * @exception NullPointerException if the specified object is null 1044 * and the field is an instance field. 1045 * @exception ExceptionInInitializerError if the initialization provoked 1046 * by this method fails. 1047 * @see Field#set 1048 */ 1049 @CallerSensitive 1050 @ForceInline // to ensure Reflection.getCallerClass optimization 1051 public void setDouble(Object obj, double d) 1052 throws IllegalArgumentException, IllegalAccessException 1053 { 1054 if (!override) { 1055 Class<?> caller = Reflection.getCallerClass(); 1056 checkAccess(caller, clazz, obj, modifiers); 1057 } 1058 getFieldAccessor(obj).setDouble(obj, d); 1059 } 1060 1061 // security check is done before calling this method 1062 private FieldAccessor getFieldAccessor(Object obj) 1063 throws IllegalAccessException 1064 { 1065 boolean ov = override; 1066 FieldAccessor a = (ov) ? overrideFieldAccessor : fieldAccessor; 1067 return (a != null) ? a : acquireFieldAccessor(ov); 1068 } 1069 1070 // NOTE that there is no synchronization used here. It is correct 1071 // (though not efficient) to generate more than one FieldAccessor 1072 // for a given Field. However, avoiding synchronization will 1073 // probably make the implementation more scalable. 1074 private FieldAccessor acquireFieldAccessor(boolean overrideFinalCheck) { 1075 // First check to see if one has been created yet, and take it 1076 // if so 1077 FieldAccessor tmp = null; 1078 if (root != null) tmp = root.getFieldAccessor(overrideFinalCheck); | 386 * object before being returned 387 * 388 * @exception IllegalAccessException if this {@code Field} object 389 * is enforcing Java language access control and the underlying 390 * field is inaccessible. 391 * @exception IllegalArgumentException if the specified object is not an 392 * instance of the class or interface declaring the underlying 393 * field (or a subclass or implementor thereof). 394 * @exception NullPointerException if the specified object is null 395 * and the field is an instance field. 396 * @exception ExceptionInInitializerError if the initialization provoked 397 * by this method fails. 398 */ 399 @CallerSensitive 400 @ForceInline // to ensure Reflection.getCallerClass optimization 401 public Object get(Object obj) 402 throws IllegalArgumentException, IllegalAccessException 403 { 404 if (!override) { 405 Class<?> caller = Reflection.getCallerClass(); 406 checkAccess(caller, obj); 407 } 408 return getFieldAccessor(obj).get(obj); 409 } 410 411 /** 412 * Gets the value of a static or instance {@code boolean} field. 413 * 414 * @param obj the object to extract the {@code boolean} value 415 * from 416 * @return the value of the {@code boolean} field 417 * 418 * @exception IllegalAccessException if this {@code Field} object 419 * is enforcing Java language access control and the underlying 420 * field is inaccessible. 421 * @exception IllegalArgumentException if the specified object is not 422 * an instance of the class or interface declaring the 423 * underlying field (or a subclass or implementor 424 * thereof), or if the field value cannot be 425 * converted to the type {@code boolean} by a 426 * widening conversion. 427 * @exception NullPointerException if the specified object is null 428 * and the field is an instance field. 429 * @exception ExceptionInInitializerError if the initialization provoked 430 * by this method fails. 431 * @see Field#get 432 */ 433 @CallerSensitive 434 @ForceInline // to ensure Reflection.getCallerClass optimization 435 public boolean getBoolean(Object obj) 436 throws IllegalArgumentException, IllegalAccessException 437 { 438 if (!override) { 439 Class<?> caller = Reflection.getCallerClass(); 440 checkAccess(caller, obj); 441 } 442 return getFieldAccessor(obj).getBoolean(obj); 443 } 444 445 /** 446 * Gets the value of a static or instance {@code byte} field. 447 * 448 * @param obj the object to extract the {@code byte} value 449 * from 450 * @return the value of the {@code byte} field 451 * 452 * @exception IllegalAccessException if this {@code Field} object 453 * is enforcing Java language access control and the underlying 454 * field is inaccessible. 455 * @exception IllegalArgumentException if the specified object is not 456 * an instance of the class or interface declaring the 457 * underlying field (or a subclass or implementor 458 * thereof), or if the field value cannot be 459 * converted to the type {@code byte} by a 460 * widening conversion. 461 * @exception NullPointerException if the specified object is null 462 * and the field is an instance field. 463 * @exception ExceptionInInitializerError if the initialization provoked 464 * by this method fails. 465 * @see Field#get 466 */ 467 @CallerSensitive 468 @ForceInline // to ensure Reflection.getCallerClass optimization 469 public byte getByte(Object obj) 470 throws IllegalArgumentException, IllegalAccessException 471 { 472 if (!override) { 473 Class<?> caller = Reflection.getCallerClass(); 474 checkAccess(caller, obj); 475 } 476 return getFieldAccessor(obj).getByte(obj); 477 } 478 479 /** 480 * Gets the value of a static or instance field of type 481 * {@code char} or of another primitive type convertible to 482 * type {@code char} via a widening conversion. 483 * 484 * @param obj the object to extract the {@code char} value 485 * from 486 * @return the value of the field converted to type {@code char} 487 * 488 * @exception IllegalAccessException if this {@code Field} object 489 * is enforcing Java language access control and the underlying 490 * field is inaccessible. 491 * @exception IllegalArgumentException if the specified object is not 492 * an instance of the class or interface declaring the 493 * underlying field (or a subclass or implementor 494 * thereof), or if the field value cannot be 495 * converted to the type {@code char} by a 496 * widening conversion. 497 * @exception NullPointerException if the specified object is null 498 * and the field is an instance field. 499 * @exception ExceptionInInitializerError if the initialization provoked 500 * by this method fails. 501 * @see Field#get 502 */ 503 @CallerSensitive 504 @ForceInline // to ensure Reflection.getCallerClass optimization 505 public char getChar(Object obj) 506 throws IllegalArgumentException, IllegalAccessException 507 { 508 if (!override) { 509 Class<?> caller = Reflection.getCallerClass(); 510 checkAccess(caller, obj); 511 } 512 return getFieldAccessor(obj).getChar(obj); 513 } 514 515 /** 516 * Gets the value of a static or instance field of type 517 * {@code short} or of another primitive type convertible to 518 * type {@code short} via a widening conversion. 519 * 520 * @param obj the object to extract the {@code short} value 521 * from 522 * @return the value of the field converted to type {@code short} 523 * 524 * @exception IllegalAccessException if this {@code Field} object 525 * is enforcing Java language access control and the underlying 526 * field is inaccessible. 527 * @exception IllegalArgumentException if the specified object is not 528 * an instance of the class or interface declaring the 529 * underlying field (or a subclass or implementor 530 * thereof), or if the field value cannot be 531 * converted to the type {@code short} by a 532 * widening conversion. 533 * @exception NullPointerException if the specified object is null 534 * and the field is an instance field. 535 * @exception ExceptionInInitializerError if the initialization provoked 536 * by this method fails. 537 * @see Field#get 538 */ 539 @CallerSensitive 540 @ForceInline // to ensure Reflection.getCallerClass optimization 541 public short getShort(Object obj) 542 throws IllegalArgumentException, IllegalAccessException 543 { 544 if (!override) { 545 Class<?> caller = Reflection.getCallerClass(); 546 checkAccess(caller, obj); 547 } 548 return getFieldAccessor(obj).getShort(obj); 549 } 550 551 /** 552 * Gets the value of a static or instance field of type 553 * {@code int} or of another primitive type convertible to 554 * type {@code int} via a widening conversion. 555 * 556 * @param obj the object to extract the {@code int} value 557 * from 558 * @return the value of the field converted to type {@code int} 559 * 560 * @exception IllegalAccessException if this {@code Field} object 561 * is enforcing Java language access control and the underlying 562 * field is inaccessible. 563 * @exception IllegalArgumentException if the specified object is not 564 * an instance of the class or interface declaring the 565 * underlying field (or a subclass or implementor 566 * thereof), or if the field value cannot be 567 * converted to the type {@code int} by a 568 * widening conversion. 569 * @exception NullPointerException if the specified object is null 570 * and the field is an instance field. 571 * @exception ExceptionInInitializerError if the initialization provoked 572 * by this method fails. 573 * @see Field#get 574 */ 575 @CallerSensitive 576 @ForceInline // to ensure Reflection.getCallerClass optimization 577 public int getInt(Object obj) 578 throws IllegalArgumentException, IllegalAccessException 579 { 580 if (!override) { 581 Class<?> caller = Reflection.getCallerClass(); 582 checkAccess(caller, obj); 583 } 584 return getFieldAccessor(obj).getInt(obj); 585 } 586 587 /** 588 * Gets the value of a static or instance field of type 589 * {@code long} or of another primitive type convertible to 590 * type {@code long} via a widening conversion. 591 * 592 * @param obj the object to extract the {@code long} value 593 * from 594 * @return the value of the field converted to type {@code long} 595 * 596 * @exception IllegalAccessException if this {@code Field} object 597 * is enforcing Java language access control and the underlying 598 * field is inaccessible. 599 * @exception IllegalArgumentException if the specified object is not 600 * an instance of the class or interface declaring the 601 * underlying field (or a subclass or implementor 602 * thereof), or if the field value cannot be 603 * converted to the type {@code long} by a 604 * widening conversion. 605 * @exception NullPointerException if the specified object is null 606 * and the field is an instance field. 607 * @exception ExceptionInInitializerError if the initialization provoked 608 * by this method fails. 609 * @see Field#get 610 */ 611 @CallerSensitive 612 @ForceInline // to ensure Reflection.getCallerClass optimization 613 public long getLong(Object obj) 614 throws IllegalArgumentException, IllegalAccessException 615 { 616 if (!override) { 617 Class<?> caller = Reflection.getCallerClass(); 618 checkAccess(caller, obj); 619 } 620 return getFieldAccessor(obj).getLong(obj); 621 } 622 623 /** 624 * Gets the value of a static or instance field of type 625 * {@code float} or of another primitive type convertible to 626 * type {@code float} via a widening conversion. 627 * 628 * @param obj the object to extract the {@code float} value 629 * from 630 * @return the value of the field converted to type {@code float} 631 * 632 * @exception IllegalAccessException if this {@code Field} object 633 * is enforcing Java language access control and the underlying 634 * field is inaccessible. 635 * @exception IllegalArgumentException if the specified object is not 636 * an instance of the class or interface declaring the 637 * underlying field (or a subclass or implementor 638 * thereof), or if the field value cannot be 639 * converted to the type {@code float} by a 640 * widening conversion. 641 * @exception NullPointerException if the specified object is null 642 * and the field is an instance field. 643 * @exception ExceptionInInitializerError if the initialization provoked 644 * by this method fails. 645 * @see Field#get 646 */ 647 @CallerSensitive 648 @ForceInline // to ensure Reflection.getCallerClass optimization 649 public float getFloat(Object obj) 650 throws IllegalArgumentException, IllegalAccessException 651 { 652 if (!override) { 653 Class<?> caller = Reflection.getCallerClass(); 654 checkAccess(caller, obj); 655 } 656 return getFieldAccessor(obj).getFloat(obj); 657 } 658 659 /** 660 * Gets the value of a static or instance field of type 661 * {@code double} or of another primitive type convertible to 662 * type {@code double} via a widening conversion. 663 * 664 * @param obj the object to extract the {@code double} value 665 * from 666 * @return the value of the field converted to type {@code double} 667 * 668 * @exception IllegalAccessException if this {@code Field} object 669 * is enforcing Java language access control and the underlying 670 * field is inaccessible. 671 * @exception IllegalArgumentException if the specified object is not 672 * an instance of the class or interface declaring the 673 * underlying field (or a subclass or implementor 674 * thereof), or if the field value cannot be 675 * converted to the type {@code double} by a 676 * widening conversion. 677 * @exception NullPointerException if the specified object is null 678 * and the field is an instance field. 679 * @exception ExceptionInInitializerError if the initialization provoked 680 * by this method fails. 681 * @see Field#get 682 */ 683 @CallerSensitive 684 @ForceInline // to ensure Reflection.getCallerClass optimization 685 public double getDouble(Object obj) 686 throws IllegalArgumentException, IllegalAccessException 687 { 688 if (!override) { 689 Class<?> caller = Reflection.getCallerClass(); 690 checkAccess(caller, obj); 691 } 692 return getFieldAccessor(obj).getDouble(obj); 693 } 694 695 /** 696 * Sets the field represented by this {@code Field} object on the 697 * specified object argument to the specified new value. The new 698 * value is automatically unwrapped if the underlying field has a 699 * primitive type. 700 * 701 * <p>The operation proceeds as follows: 702 * 703 * <p>If the underlying field is static, the {@code obj} argument is 704 * ignored; it may be null. 705 * 706 * <p>Otherwise the underlying field is an instance field. If the 707 * specified object argument is null, the method throws a 708 * {@code NullPointerException}. If the specified object argument is not 709 * an instance of the class or interface declaring the underlying 710 * field, the method throws an {@code IllegalArgumentException}. 748 * 749 * @exception IllegalAccessException if this {@code Field} object 750 * is enforcing Java language access control and the underlying 751 * field is either inaccessible or final. 752 * @exception IllegalArgumentException if the specified object is not an 753 * instance of the class or interface declaring the underlying 754 * field (or a subclass or implementor thereof), 755 * or if an unwrapping conversion fails. 756 * @exception NullPointerException if the specified object is null 757 * and the field is an instance field. 758 * @exception ExceptionInInitializerError if the initialization provoked 759 * by this method fails. 760 */ 761 @CallerSensitive 762 @ForceInline // to ensure Reflection.getCallerClass optimization 763 public void set(Object obj, Object value) 764 throws IllegalArgumentException, IllegalAccessException 765 { 766 if (!override) { 767 Class<?> caller = Reflection.getCallerClass(); 768 checkAccess(caller, obj); 769 } 770 getFieldAccessor(obj).set(obj, value); 771 } 772 773 /** 774 * Sets the value of a field as a {@code boolean} on the specified object. 775 * This method is equivalent to 776 * {@code set(obj, zObj)}, 777 * where {@code zObj} is a {@code Boolean} object and 778 * {@code zObj.booleanValue() == z}. 779 * 780 * @param obj the object whose field should be modified 781 * @param z the new value for the field of {@code obj} 782 * being modified 783 * 784 * @exception IllegalAccessException if this {@code Field} object 785 * is enforcing Java language access control and the underlying 786 * field is either inaccessible or final. 787 * @exception IllegalArgumentException if the specified object is not an 788 * instance of the class or interface declaring the underlying 789 * field (or a subclass or implementor thereof), 790 * or if an unwrapping conversion fails. 791 * @exception NullPointerException if the specified object is null 792 * and the field is an instance field. 793 * @exception ExceptionInInitializerError if the initialization provoked 794 * by this method fails. 795 * @see Field#set 796 */ 797 @CallerSensitive 798 @ForceInline // to ensure Reflection.getCallerClass optimization 799 public void setBoolean(Object obj, boolean z) 800 throws IllegalArgumentException, IllegalAccessException 801 { 802 if (!override) { 803 Class<?> caller = Reflection.getCallerClass(); 804 checkAccess(caller, obj); 805 } 806 getFieldAccessor(obj).setBoolean(obj, z); 807 } 808 809 /** 810 * Sets the value of a field as a {@code byte} on the specified object. 811 * This method is equivalent to 812 * {@code set(obj, bObj)}, 813 * where {@code bObj} is a {@code Byte} object and 814 * {@code bObj.byteValue() == b}. 815 * 816 * @param obj the object whose field should be modified 817 * @param b the new value for the field of {@code obj} 818 * being modified 819 * 820 * @exception IllegalAccessException if this {@code Field} object 821 * is enforcing Java language access control and the underlying 822 * field is either inaccessible or final. 823 * @exception IllegalArgumentException if the specified object is not an 824 * instance of the class or interface declaring the underlying 825 * field (or a subclass or implementor thereof), 826 * or if an unwrapping conversion fails. 827 * @exception NullPointerException if the specified object is null 828 * and the field is an instance field. 829 * @exception ExceptionInInitializerError if the initialization provoked 830 * by this method fails. 831 * @see Field#set 832 */ 833 @CallerSensitive 834 @ForceInline // to ensure Reflection.getCallerClass optimization 835 public void setByte(Object obj, byte b) 836 throws IllegalArgumentException, IllegalAccessException 837 { 838 if (!override) { 839 Class<?> caller = Reflection.getCallerClass(); 840 checkAccess(caller, obj); 841 } 842 getFieldAccessor(obj).setByte(obj, b); 843 } 844 845 /** 846 * Sets the value of a field as a {@code char} on the specified object. 847 * This method is equivalent to 848 * {@code set(obj, cObj)}, 849 * where {@code cObj} is a {@code Character} object and 850 * {@code cObj.charValue() == c}. 851 * 852 * @param obj the object whose field should be modified 853 * @param c the new value for the field of {@code obj} 854 * being modified 855 * 856 * @exception IllegalAccessException if this {@code Field} object 857 * is enforcing Java language access control and the underlying 858 * field is either inaccessible or final. 859 * @exception IllegalArgumentException if the specified object is not an 860 * instance of the class or interface declaring the underlying 861 * field (or a subclass or implementor thereof), 862 * or if an unwrapping conversion fails. 863 * @exception NullPointerException if the specified object is null 864 * and the field is an instance field. 865 * @exception ExceptionInInitializerError if the initialization provoked 866 * by this method fails. 867 * @see Field#set 868 */ 869 @CallerSensitive 870 @ForceInline // to ensure Reflection.getCallerClass optimization 871 public void setChar(Object obj, char c) 872 throws IllegalArgumentException, IllegalAccessException 873 { 874 if (!override) { 875 Class<?> caller = Reflection.getCallerClass(); 876 checkAccess(caller, obj); 877 } 878 getFieldAccessor(obj).setChar(obj, c); 879 } 880 881 /** 882 * Sets the value of a field as a {@code short} on the specified object. 883 * This method is equivalent to 884 * {@code set(obj, sObj)}, 885 * where {@code sObj} is a {@code Short} object and 886 * {@code sObj.shortValue() == s}. 887 * 888 * @param obj the object whose field should be modified 889 * @param s the new value for the field of {@code obj} 890 * being modified 891 * 892 * @exception IllegalAccessException if this {@code Field} object 893 * is enforcing Java language access control and the underlying 894 * field is either inaccessible or final. 895 * @exception IllegalArgumentException if the specified object is not an 896 * instance of the class or interface declaring the underlying 897 * field (or a subclass or implementor thereof), 898 * or if an unwrapping conversion fails. 899 * @exception NullPointerException if the specified object is null 900 * and the field is an instance field. 901 * @exception ExceptionInInitializerError if the initialization provoked 902 * by this method fails. 903 * @see Field#set 904 */ 905 @CallerSensitive 906 @ForceInline // to ensure Reflection.getCallerClass optimization 907 public void setShort(Object obj, short s) 908 throws IllegalArgumentException, IllegalAccessException 909 { 910 if (!override) { 911 Class<?> caller = Reflection.getCallerClass(); 912 checkAccess(caller, obj); 913 } 914 getFieldAccessor(obj).setShort(obj, s); 915 } 916 917 /** 918 * Sets the value of a field as an {@code int} on the specified object. 919 * This method is equivalent to 920 * {@code set(obj, iObj)}, 921 * where {@code iObj} is an {@code Integer} object and 922 * {@code iObj.intValue() == i}. 923 * 924 * @param obj the object whose field should be modified 925 * @param i the new value for the field of {@code obj} 926 * being modified 927 * 928 * @exception IllegalAccessException if this {@code Field} object 929 * is enforcing Java language access control and the underlying 930 * field is either inaccessible or final. 931 * @exception IllegalArgumentException if the specified object is not an 932 * instance of the class or interface declaring the underlying 933 * field (or a subclass or implementor thereof), 934 * or if an unwrapping conversion fails. 935 * @exception NullPointerException if the specified object is null 936 * and the field is an instance field. 937 * @exception ExceptionInInitializerError if the initialization provoked 938 * by this method fails. 939 * @see Field#set 940 */ 941 @CallerSensitive 942 @ForceInline // to ensure Reflection.getCallerClass optimization 943 public void setInt(Object obj, int i) 944 throws IllegalArgumentException, IllegalAccessException 945 { 946 if (!override) { 947 Class<?> caller = Reflection.getCallerClass(); 948 checkAccess(caller, obj); 949 } 950 getFieldAccessor(obj).setInt(obj, i); 951 } 952 953 /** 954 * Sets the value of a field as a {@code long} on the specified object. 955 * This method is equivalent to 956 * {@code set(obj, lObj)}, 957 * where {@code lObj} is a {@code Long} object and 958 * {@code lObj.longValue() == l}. 959 * 960 * @param obj the object whose field should be modified 961 * @param l the new value for the field of {@code obj} 962 * being modified 963 * 964 * @exception IllegalAccessException if this {@code Field} object 965 * is enforcing Java language access control and the underlying 966 * field is either inaccessible or final. 967 * @exception IllegalArgumentException if the specified object is not an 968 * instance of the class or interface declaring the underlying 969 * field (or a subclass or implementor thereof), 970 * or if an unwrapping conversion fails. 971 * @exception NullPointerException if the specified object is null 972 * and the field is an instance field. 973 * @exception ExceptionInInitializerError if the initialization provoked 974 * by this method fails. 975 * @see Field#set 976 */ 977 @CallerSensitive 978 @ForceInline // to ensure Reflection.getCallerClass optimization 979 public void setLong(Object obj, long l) 980 throws IllegalArgumentException, IllegalAccessException 981 { 982 if (!override) { 983 Class<?> caller = Reflection.getCallerClass(); 984 checkAccess(caller, obj); 985 } 986 getFieldAccessor(obj).setLong(obj, l); 987 } 988 989 /** 990 * Sets the value of a field as a {@code float} on the specified object. 991 * This method is equivalent to 992 * {@code set(obj, fObj)}, 993 * where {@code fObj} is a {@code Float} object and 994 * {@code fObj.floatValue() == f}. 995 * 996 * @param obj the object whose field should be modified 997 * @param f the new value for the field of {@code obj} 998 * being modified 999 * 1000 * @exception IllegalAccessException if this {@code Field} object 1001 * is enforcing Java language access control and the underlying 1002 * field is either inaccessible or final. 1003 * @exception IllegalArgumentException if the specified object is not an 1004 * instance of the class or interface declaring the underlying 1005 * field (or a subclass or implementor thereof), 1006 * or if an unwrapping conversion fails. 1007 * @exception NullPointerException if the specified object is null 1008 * and the field is an instance field. 1009 * @exception ExceptionInInitializerError if the initialization provoked 1010 * by this method fails. 1011 * @see Field#set 1012 */ 1013 @CallerSensitive 1014 @ForceInline // to ensure Reflection.getCallerClass optimization 1015 public void setFloat(Object obj, float f) 1016 throws IllegalArgumentException, IllegalAccessException 1017 { 1018 if (!override) { 1019 Class<?> caller = Reflection.getCallerClass(); 1020 checkAccess(caller, obj); 1021 } 1022 getFieldAccessor(obj).setFloat(obj, f); 1023 } 1024 1025 /** 1026 * Sets the value of a field as a {@code double} on the specified object. 1027 * This method is equivalent to 1028 * {@code set(obj, dObj)}, 1029 * where {@code dObj} is a {@code Double} object and 1030 * {@code dObj.doubleValue() == d}. 1031 * 1032 * @param obj the object whose field should be modified 1033 * @param d the new value for the field of {@code obj} 1034 * being modified 1035 * 1036 * @exception IllegalAccessException if this {@code Field} object 1037 * is enforcing Java language access control and the underlying 1038 * field is either inaccessible or final. 1039 * @exception IllegalArgumentException if the specified object is not an 1040 * instance of the class or interface declaring the underlying 1041 * field (or a subclass or implementor thereof), 1042 * or if an unwrapping conversion fails. 1043 * @exception NullPointerException if the specified object is null 1044 * and the field is an instance field. 1045 * @exception ExceptionInInitializerError if the initialization provoked 1046 * by this method fails. 1047 * @see Field#set 1048 */ 1049 @CallerSensitive 1050 @ForceInline // to ensure Reflection.getCallerClass optimization 1051 public void setDouble(Object obj, double d) 1052 throws IllegalArgumentException, IllegalAccessException 1053 { 1054 if (!override) { 1055 Class<?> caller = Reflection.getCallerClass(); 1056 checkAccess(caller, obj); 1057 } 1058 getFieldAccessor(obj).setDouble(obj, d); 1059 } 1060 1061 // check access to field 1062 private void checkAccess(Class<?> caller, Object obj) 1063 throws IllegalAccessException 1064 { 1065 checkAccess(caller, clazz, 1066 Modifier.isStatic(modifiers) ? null : obj.getClass(), 1067 modifiers); 1068 } 1069 1070 // security check is done before calling this method 1071 private FieldAccessor getFieldAccessor(Object obj) 1072 throws IllegalAccessException 1073 { 1074 boolean ov = override; 1075 FieldAccessor a = (ov) ? overrideFieldAccessor : fieldAccessor; 1076 return (a != null) ? a : acquireFieldAccessor(ov); 1077 } 1078 1079 // NOTE that there is no synchronization used here. It is correct 1080 // (though not efficient) to generate more than one FieldAccessor 1081 // for a given Field. However, avoiding synchronization will 1082 // probably make the implementation more scalable. 1083 private FieldAccessor acquireFieldAccessor(boolean overrideFinalCheck) { 1084 // First check to see if one has been created yet, and take it 1085 // if so 1086 FieldAccessor tmp = null; 1087 if (root != null) tmp = root.getFieldAccessor(overrideFinalCheck); |