< prev index next >

src/java.base/share/classes/java/lang/reflect/Field.java

Print this page




 150         if (this.root != null)
 151             throw new IllegalArgumentException("Can not copy a non-root Field");
 152 
 153         Field res = new Field(clazz, name, type, modifiers, slot, signature, annotations);
 154         res.root = this;
 155         // Might as well eagerly propagate this if already present
 156         res.fieldAccessor = fieldAccessor;
 157         res.overrideFieldAccessor = overrideFieldAccessor;
 158 
 159         return res;
 160     }
 161 
 162     /**
 163      * @throws InaccessibleObjectException {@inheritDoc}
 164      * @throws SecurityException {@inheritDoc}
 165      */
 166     @Override
 167     @CallerSensitive
 168     public void setAccessible(boolean flag) {
 169         AccessibleObject.checkPermission();
 170         if (flag) checkCanSetAccessible(Reflection.getCallerClass());







 171         setAccessible0(flag);
 172     }
 173 
 174     @Override
 175     void checkCanSetAccessible(Class<?> caller) {
 176         checkCanSetAccessible(caller, clazz);
 177     }
 178 
 179     /**
 180      * Returns the {@code Class} object representing the class or interface
 181      * that declares the field represented by this {@code Field} object.
 182      */
 183     @Override
 184     public Class<?> getDeclaringClass() {
 185         return clazz;
 186     }
 187 
 188     /**
 189      * Returns the name of the field represented by this {@code Field} object.
 190      */


 756      * @param value the new value for the field of {@code obj}
 757      * being modified
 758      *
 759      * @exception IllegalAccessException    if this {@code Field} object
 760      *              is enforcing Java language access control and the underlying
 761      *              field is either inaccessible or final.
 762      * @exception IllegalArgumentException  if the specified object is not an
 763      *              instance of the class or interface declaring the underlying
 764      *              field (or a subclass or implementor thereof),
 765      *              or if an unwrapping conversion fails.
 766      * @exception NullPointerException      if the specified object is null
 767      *              and the field is an instance field.
 768      * @exception ExceptionInInitializerError if the initialization provoked
 769      *              by this method fails.
 770      */
 771     @CallerSensitive
 772     @ForceInline // to ensure Reflection.getCallerClass optimization
 773     public void set(Object obj, Object value)
 774         throws IllegalArgumentException, IllegalAccessException
 775     {


 776         if (!override) {
 777             Class<?> caller = Reflection.getCallerClass();
 778             checkAccess(caller, obj);
 779         }
 780         getFieldAccessor(obj).set(obj, value);
 781     }
 782 
 783     /**
 784      * Sets the value of a field as a {@code boolean} on the specified object.
 785      * This method is equivalent to
 786      * {@code set(obj, zObj)},
 787      * where {@code zObj} is a {@code Boolean} object and
 788      * {@code zObj.booleanValue() == z}.
 789      *
 790      * @param obj the object whose field should be modified
 791      * @param z   the new value for the field of {@code obj}
 792      * being modified
 793      *
 794      * @exception IllegalAccessException    if this {@code Field} object
 795      *              is enforcing Java language access control and the underlying
 796      *              field is either inaccessible or final.
 797      * @exception IllegalArgumentException  if the specified object is not an
 798      *              instance of the class or interface declaring the underlying
 799      *              field (or a subclass or implementor thereof),
 800      *              or if an unwrapping conversion fails.
 801      * @exception NullPointerException      if the specified object is null
 802      *              and the field is an instance field.
 803      * @exception ExceptionInInitializerError if the initialization provoked
 804      *              by this method fails.
 805      * @see       Field#set
 806      */
 807     @CallerSensitive
 808     @ForceInline // to ensure Reflection.getCallerClass optimization
 809     public void setBoolean(Object obj, boolean z)
 810         throws IllegalArgumentException, IllegalAccessException
 811     {


 812         if (!override) {
 813             Class<?> caller = Reflection.getCallerClass();
 814             checkAccess(caller, obj);
 815         }
 816         getFieldAccessor(obj).setBoolean(obj, z);
 817     }
 818 
 819     /**
 820      * Sets the value of a field as a {@code byte} on the specified object.
 821      * This method is equivalent to
 822      * {@code set(obj, bObj)},
 823      * where {@code bObj} is a {@code Byte} object and
 824      * {@code bObj.byteValue() == b}.
 825      *
 826      * @param obj the object whose field should be modified
 827      * @param b   the new value for the field of {@code obj}
 828      * being modified
 829      *
 830      * @exception IllegalAccessException    if this {@code Field} object
 831      *              is enforcing Java language access control and the underlying
 832      *              field is either inaccessible or final.
 833      * @exception IllegalArgumentException  if the specified object is not an
 834      *              instance of the class or interface declaring the underlying
 835      *              field (or a subclass or implementor thereof),
 836      *              or if an unwrapping conversion fails.
 837      * @exception NullPointerException      if the specified object is null
 838      *              and the field is an instance field.
 839      * @exception ExceptionInInitializerError if the initialization provoked
 840      *              by this method fails.
 841      * @see       Field#set
 842      */
 843     @CallerSensitive
 844     @ForceInline // to ensure Reflection.getCallerClass optimization
 845     public void setByte(Object obj, byte b)
 846         throws IllegalArgumentException, IllegalAccessException
 847     {


 848         if (!override) {
 849             Class<?> caller = Reflection.getCallerClass();
 850             checkAccess(caller, obj);
 851         }
 852         getFieldAccessor(obj).setByte(obj, b);
 853     }
 854 
 855     /**
 856      * Sets the value of a field as a {@code char} on the specified object.
 857      * This method is equivalent to
 858      * {@code set(obj, cObj)},
 859      * where {@code cObj} is a {@code Character} object and
 860      * {@code cObj.charValue() == c}.
 861      *
 862      * @param obj the object whose field should be modified
 863      * @param c   the new value for the field of {@code obj}
 864      * being modified
 865      *
 866      * @exception IllegalAccessException    if this {@code Field} object
 867      *              is enforcing Java language access control and the underlying
 868      *              field is either inaccessible or final.
 869      * @exception IllegalArgumentException  if the specified object is not an
 870      *              instance of the class or interface declaring the underlying
 871      *              field (or a subclass or implementor thereof),
 872      *              or if an unwrapping conversion fails.
 873      * @exception NullPointerException      if the specified object is null
 874      *              and the field is an instance field.
 875      * @exception ExceptionInInitializerError if the initialization provoked
 876      *              by this method fails.
 877      * @see       Field#set
 878      */
 879     @CallerSensitive
 880     @ForceInline // to ensure Reflection.getCallerClass optimization
 881     public void setChar(Object obj, char c)
 882         throws IllegalArgumentException, IllegalAccessException
 883     {


 884         if (!override) {
 885             Class<?> caller = Reflection.getCallerClass();
 886             checkAccess(caller, obj);
 887         }
 888         getFieldAccessor(obj).setChar(obj, c);
 889     }
 890 
 891     /**
 892      * Sets the value of a field as a {@code short} on the specified object.
 893      * This method is equivalent to
 894      * {@code set(obj, sObj)},
 895      * where {@code sObj} is a {@code Short} object and
 896      * {@code sObj.shortValue() == s}.
 897      *
 898      * @param obj the object whose field should be modified
 899      * @param s   the new value for the field of {@code obj}
 900      * being modified
 901      *
 902      * @exception IllegalAccessException    if this {@code Field} object
 903      *              is enforcing Java language access control and the underlying
 904      *              field is either inaccessible or final.
 905      * @exception IllegalArgumentException  if the specified object is not an
 906      *              instance of the class or interface declaring the underlying
 907      *              field (or a subclass or implementor thereof),
 908      *              or if an unwrapping conversion fails.
 909      * @exception NullPointerException      if the specified object is null
 910      *              and the field is an instance field.
 911      * @exception ExceptionInInitializerError if the initialization provoked
 912      *              by this method fails.
 913      * @see       Field#set
 914      */
 915     @CallerSensitive
 916     @ForceInline // to ensure Reflection.getCallerClass optimization
 917     public void setShort(Object obj, short s)
 918         throws IllegalArgumentException, IllegalAccessException
 919     {


 920         if (!override) {
 921             Class<?> caller = Reflection.getCallerClass();
 922             checkAccess(caller, obj);
 923         }
 924         getFieldAccessor(obj).setShort(obj, s);
 925     }
 926 
 927     /**
 928      * Sets the value of a field as an {@code int} on the specified object.
 929      * This method is equivalent to
 930      * {@code set(obj, iObj)},
 931      * where {@code iObj} is an {@code Integer} object and
 932      * {@code iObj.intValue() == i}.
 933      *
 934      * @param obj the object whose field should be modified
 935      * @param i   the new value for the field of {@code obj}
 936      * being modified
 937      *
 938      * @exception IllegalAccessException    if this {@code Field} object
 939      *              is enforcing Java language access control and the underlying
 940      *              field is either inaccessible or final.
 941      * @exception IllegalArgumentException  if the specified object is not an
 942      *              instance of the class or interface declaring the underlying
 943      *              field (or a subclass or implementor thereof),
 944      *              or if an unwrapping conversion fails.
 945      * @exception NullPointerException      if the specified object is null
 946      *              and the field is an instance field.
 947      * @exception ExceptionInInitializerError if the initialization provoked
 948      *              by this method fails.
 949      * @see       Field#set
 950      */
 951     @CallerSensitive
 952     @ForceInline // to ensure Reflection.getCallerClass optimization
 953     public void setInt(Object obj, int i)
 954         throws IllegalArgumentException, IllegalAccessException
 955     {


 956         if (!override) {
 957             Class<?> caller = Reflection.getCallerClass();
 958             checkAccess(caller, obj);
 959         }
 960         getFieldAccessor(obj).setInt(obj, i);
 961     }
 962 
 963     /**
 964      * Sets the value of a field as a {@code long} on the specified object.
 965      * This method is equivalent to
 966      * {@code set(obj, lObj)},
 967      * where {@code lObj} is a {@code Long} object and
 968      * {@code lObj.longValue() == l}.
 969      *
 970      * @param obj the object whose field should be modified
 971      * @param l   the new value for the field of {@code obj}
 972      * being modified
 973      *
 974      * @exception IllegalAccessException    if this {@code Field} object
 975      *              is enforcing Java language access control and the underlying
 976      *              field is either inaccessible or final.
 977      * @exception IllegalArgumentException  if the specified object is not an
 978      *              instance of the class or interface declaring the underlying
 979      *              field (or a subclass or implementor thereof),
 980      *              or if an unwrapping conversion fails.
 981      * @exception NullPointerException      if the specified object is null
 982      *              and the field is an instance field.
 983      * @exception ExceptionInInitializerError if the initialization provoked
 984      *              by this method fails.
 985      * @see       Field#set
 986      */
 987     @CallerSensitive
 988     @ForceInline // to ensure Reflection.getCallerClass optimization
 989     public void setLong(Object obj, long l)
 990         throws IllegalArgumentException, IllegalAccessException
 991     {


 992         if (!override) {
 993             Class<?> caller = Reflection.getCallerClass();
 994             checkAccess(caller, obj);
 995         }
 996         getFieldAccessor(obj).setLong(obj, l);
 997     }
 998 
 999     /**
1000      * Sets the value of a field as a {@code float} on the specified object.
1001      * This method is equivalent to
1002      * {@code set(obj, fObj)},
1003      * where {@code fObj} is a {@code Float} object and
1004      * {@code fObj.floatValue() == f}.
1005      *
1006      * @param obj the object whose field should be modified
1007      * @param f   the new value for the field of {@code obj}
1008      * being modified
1009      *
1010      * @exception IllegalAccessException    if this {@code Field} object
1011      *              is enforcing Java language access control and the underlying
1012      *              field is either inaccessible or final.
1013      * @exception IllegalArgumentException  if the specified object is not an
1014      *              instance of the class or interface declaring the underlying
1015      *              field (or a subclass or implementor thereof),
1016      *              or if an unwrapping conversion fails.
1017      * @exception NullPointerException      if the specified object is null
1018      *              and the field is an instance field.
1019      * @exception ExceptionInInitializerError if the initialization provoked
1020      *              by this method fails.
1021      * @see       Field#set
1022      */
1023     @CallerSensitive
1024     @ForceInline // to ensure Reflection.getCallerClass optimization
1025     public void setFloat(Object obj, float f)
1026         throws IllegalArgumentException, IllegalAccessException
1027     {


1028         if (!override) {
1029             Class<?> caller = Reflection.getCallerClass();
1030             checkAccess(caller, obj);
1031         }
1032         getFieldAccessor(obj).setFloat(obj, f);
1033     }
1034 
1035     /**
1036      * Sets the value of a field as a {@code double} on the specified object.
1037      * This method is equivalent to
1038      * {@code set(obj, dObj)},
1039      * where {@code dObj} is a {@code Double} object and
1040      * {@code dObj.doubleValue() == d}.
1041      *
1042      * @param obj the object whose field should be modified
1043      * @param d   the new value for the field of {@code obj}
1044      * being modified
1045      *
1046      * @exception IllegalAccessException    if this {@code Field} object
1047      *              is enforcing Java language access control and the underlying
1048      *              field is either inaccessible or final.
1049      * @exception IllegalArgumentException  if the specified object is not an
1050      *              instance of the class or interface declaring the underlying
1051      *              field (or a subclass or implementor thereof),
1052      *              or if an unwrapping conversion fails.
1053      * @exception NullPointerException      if the specified object is null
1054      *              and the field is an instance field.
1055      * @exception ExceptionInInitializerError if the initialization provoked
1056      *              by this method fails.
1057      * @see       Field#set
1058      */
1059     @CallerSensitive
1060     @ForceInline // to ensure Reflection.getCallerClass optimization
1061     public void setDouble(Object obj, double d)
1062         throws IllegalArgumentException, IllegalAccessException
1063     {


1064         if (!override) {
1065             Class<?> caller = Reflection.getCallerClass();
1066             checkAccess(caller, obj);
1067         }
1068         getFieldAccessor(obj).setDouble(obj, d);
1069     }
1070 
1071     // check access to field
1072     private void checkAccess(Class<?> caller, Object obj)
1073         throws IllegalAccessException
1074     {
1075         checkAccess(caller, clazz,
1076                     Modifier.isStatic(modifiers) ? null : obj.getClass(),
1077                     modifiers);










1078     }
1079 
1080     // security check is done before calling this method
1081     private FieldAccessor getFieldAccessor(Object obj)
1082         throws IllegalAccessException
1083     {
1084         boolean ov = override;
1085         FieldAccessor a = (ov) ? overrideFieldAccessor : fieldAccessor;
1086         return (a != null) ? a : acquireFieldAccessor(ov);
1087     }
1088 
1089     // NOTE that there is no synchronization used here. It is correct
1090     // (though not efficient) to generate more than one FieldAccessor
1091     // for a given Field. However, avoiding synchronization will
1092     // probably make the implementation more scalable.
1093     private FieldAccessor acquireFieldAccessor(boolean overrideFinalCheck) {
1094         // First check to see if one has been created yet, and take it
1095         // if so
1096         FieldAccessor tmp = null;
1097         if (root != null) tmp = root.getFieldAccessor(overrideFinalCheck);




 150         if (this.root != null)
 151             throw new IllegalArgumentException("Can not copy a non-root Field");
 152 
 153         Field res = new Field(clazz, name, type, modifiers, slot, signature, annotations);
 154         res.root = this;
 155         // Might as well eagerly propagate this if already present
 156         res.fieldAccessor = fieldAccessor;
 157         res.overrideFieldAccessor = overrideFieldAccessor;
 158 
 159         return res;
 160     }
 161 
 162     /**
 163      * @throws InaccessibleObjectException {@inheritDoc}
 164      * @throws SecurityException {@inheritDoc}
 165      */
 166     @Override
 167     @CallerSensitive
 168     public void setAccessible(boolean flag) {
 169         AccessibleObject.checkPermission();
 170 
 171         if (clazz.isInlineClass()) {
 172             throw new InaccessibleObjectException("cannot make a field accessible of inline class "
 173                     + clazz.getName());
 174         }
 175         if (flag) {
 176             checkCanSetAccessible(Reflection.getCallerClass());
 177         }
 178         setAccessible0(flag);
 179     }
 180 
 181     @Override
 182     void checkCanSetAccessible(Class<?> caller) {
 183         checkCanSetAccessible(caller, clazz);
 184     }
 185 
 186     /**
 187      * Returns the {@code Class} object representing the class or interface
 188      * that declares the field represented by this {@code Field} object.
 189      */
 190     @Override
 191     public Class<?> getDeclaringClass() {
 192         return clazz;
 193     }
 194 
 195     /**
 196      * Returns the name of the field represented by this {@code Field} object.
 197      */


 763      * @param value the new value for the field of {@code obj}
 764      * being modified
 765      *
 766      * @exception IllegalAccessException    if this {@code Field} object
 767      *              is enforcing Java language access control and the underlying
 768      *              field is either inaccessible or final.
 769      * @exception IllegalArgumentException  if the specified object is not an
 770      *              instance of the class or interface declaring the underlying
 771      *              field (or a subclass or implementor thereof),
 772      *              or if an unwrapping conversion fails.
 773      * @exception NullPointerException      if the specified object is null
 774      *              and the field is an instance field.
 775      * @exception ExceptionInInitializerError if the initialization provoked
 776      *              by this method fails.
 777      */
 778     @CallerSensitive
 779     @ForceInline // to ensure Reflection.getCallerClass optimization
 780     public void set(Object obj, Object value)
 781         throws IllegalArgumentException, IllegalAccessException
 782     {
 783         ensureNotValueClass();
 784 
 785         if (!override) {
 786             Class<?> caller = Reflection.getCallerClass();
 787             checkAccess(caller, obj);
 788         }
 789         getFieldAccessor(obj).set(obj, value);
 790     }
 791 
 792     /**
 793      * Sets the value of a field as a {@code boolean} on the specified object.
 794      * This method is equivalent to
 795      * {@code set(obj, zObj)},
 796      * where {@code zObj} is a {@code Boolean} object and
 797      * {@code zObj.booleanValue() == z}.
 798      *
 799      * @param obj the object whose field should be modified
 800      * @param z   the new value for the field of {@code obj}
 801      * being modified
 802      *
 803      * @exception IllegalAccessException    if this {@code Field} object
 804      *              is enforcing Java language access control and the underlying
 805      *              field is either inaccessible or final.
 806      * @exception IllegalArgumentException  if the specified object is not an
 807      *              instance of the class or interface declaring the underlying
 808      *              field (or a subclass or implementor thereof),
 809      *              or if an unwrapping conversion fails.
 810      * @exception NullPointerException      if the specified object is null
 811      *              and the field is an instance field.
 812      * @exception ExceptionInInitializerError if the initialization provoked
 813      *              by this method fails.
 814      * @see       Field#set
 815      */
 816     @CallerSensitive
 817     @ForceInline // to ensure Reflection.getCallerClass optimization
 818     public void setBoolean(Object obj, boolean z)
 819         throws IllegalArgumentException, IllegalAccessException
 820     {
 821         ensureNotValueClass();
 822 
 823         if (!override) {
 824             Class<?> caller = Reflection.getCallerClass();
 825             checkAccess(caller, obj);
 826         }
 827         getFieldAccessor(obj).setBoolean(obj, z);
 828     }
 829 
 830     /**
 831      * Sets the value of a field as a {@code byte} on the specified object.
 832      * This method is equivalent to
 833      * {@code set(obj, bObj)},
 834      * where {@code bObj} is a {@code Byte} object and
 835      * {@code bObj.byteValue() == b}.
 836      *
 837      * @param obj the object whose field should be modified
 838      * @param b   the new value for the field of {@code obj}
 839      * being modified
 840      *
 841      * @exception IllegalAccessException    if this {@code Field} object
 842      *              is enforcing Java language access control and the underlying
 843      *              field is either inaccessible or final.
 844      * @exception IllegalArgumentException  if the specified object is not an
 845      *              instance of the class or interface declaring the underlying
 846      *              field (or a subclass or implementor thereof),
 847      *              or if an unwrapping conversion fails.
 848      * @exception NullPointerException      if the specified object is null
 849      *              and the field is an instance field.
 850      * @exception ExceptionInInitializerError if the initialization provoked
 851      *              by this method fails.
 852      * @see       Field#set
 853      */
 854     @CallerSensitive
 855     @ForceInline // to ensure Reflection.getCallerClass optimization
 856     public void setByte(Object obj, byte b)
 857         throws IllegalArgumentException, IllegalAccessException
 858     {
 859         ensureNotValueClass();
 860 
 861         if (!override) {
 862             Class<?> caller = Reflection.getCallerClass();
 863             checkAccess(caller, obj);
 864         }
 865         getFieldAccessor(obj).setByte(obj, b);
 866     }
 867 
 868     /**
 869      * Sets the value of a field as a {@code char} on the specified object.
 870      * This method is equivalent to
 871      * {@code set(obj, cObj)},
 872      * where {@code cObj} is a {@code Character} object and
 873      * {@code cObj.charValue() == c}.
 874      *
 875      * @param obj the object whose field should be modified
 876      * @param c   the new value for the field of {@code obj}
 877      * being modified
 878      *
 879      * @exception IllegalAccessException    if this {@code Field} object
 880      *              is enforcing Java language access control and the underlying
 881      *              field is either inaccessible or final.
 882      * @exception IllegalArgumentException  if the specified object is not an
 883      *              instance of the class or interface declaring the underlying
 884      *              field (or a subclass or implementor thereof),
 885      *              or if an unwrapping conversion fails.
 886      * @exception NullPointerException      if the specified object is null
 887      *              and the field is an instance field.
 888      * @exception ExceptionInInitializerError if the initialization provoked
 889      *              by this method fails.
 890      * @see       Field#set
 891      */
 892     @CallerSensitive
 893     @ForceInline // to ensure Reflection.getCallerClass optimization
 894     public void setChar(Object obj, char c)
 895         throws IllegalArgumentException, IllegalAccessException
 896     {
 897         ensureNotValueClass();
 898 
 899         if (!override) {
 900             Class<?> caller = Reflection.getCallerClass();
 901             checkAccess(caller, obj);
 902         }
 903         getFieldAccessor(obj).setChar(obj, c);
 904     }
 905 
 906     /**
 907      * Sets the value of a field as a {@code short} on the specified object.
 908      * This method is equivalent to
 909      * {@code set(obj, sObj)},
 910      * where {@code sObj} is a {@code Short} object and
 911      * {@code sObj.shortValue() == s}.
 912      *
 913      * @param obj the object whose field should be modified
 914      * @param s   the new value for the field of {@code obj}
 915      * being modified
 916      *
 917      * @exception IllegalAccessException    if this {@code Field} object
 918      *              is enforcing Java language access control and the underlying
 919      *              field is either inaccessible or final.
 920      * @exception IllegalArgumentException  if the specified object is not an
 921      *              instance of the class or interface declaring the underlying
 922      *              field (or a subclass or implementor thereof),
 923      *              or if an unwrapping conversion fails.
 924      * @exception NullPointerException      if the specified object is null
 925      *              and the field is an instance field.
 926      * @exception ExceptionInInitializerError if the initialization provoked
 927      *              by this method fails.
 928      * @see       Field#set
 929      */
 930     @CallerSensitive
 931     @ForceInline // to ensure Reflection.getCallerClass optimization
 932     public void setShort(Object obj, short s)
 933         throws IllegalArgumentException, IllegalAccessException
 934     {
 935         ensureNotValueClass();
 936 
 937         if (!override) {
 938             Class<?> caller = Reflection.getCallerClass();
 939             checkAccess(caller, obj);
 940         }
 941         getFieldAccessor(obj).setShort(obj, s);
 942     }
 943 
 944     /**
 945      * Sets the value of a field as an {@code int} on the specified object.
 946      * This method is equivalent to
 947      * {@code set(obj, iObj)},
 948      * where {@code iObj} is an {@code Integer} object and
 949      * {@code iObj.intValue() == i}.
 950      *
 951      * @param obj the object whose field should be modified
 952      * @param i   the new value for the field of {@code obj}
 953      * being modified
 954      *
 955      * @exception IllegalAccessException    if this {@code Field} object
 956      *              is enforcing Java language access control and the underlying
 957      *              field is either inaccessible or final.
 958      * @exception IllegalArgumentException  if the specified object is not an
 959      *              instance of the class or interface declaring the underlying
 960      *              field (or a subclass or implementor thereof),
 961      *              or if an unwrapping conversion fails.
 962      * @exception NullPointerException      if the specified object is null
 963      *              and the field is an instance field.
 964      * @exception ExceptionInInitializerError if the initialization provoked
 965      *              by this method fails.
 966      * @see       Field#set
 967      */
 968     @CallerSensitive
 969     @ForceInline // to ensure Reflection.getCallerClass optimization
 970     public void setInt(Object obj, int i)
 971         throws IllegalArgumentException, IllegalAccessException
 972     {
 973         ensureNotValueClass();
 974 
 975         if (!override) {
 976             Class<?> caller = Reflection.getCallerClass();
 977             checkAccess(caller, obj);
 978         }
 979         getFieldAccessor(obj).setInt(obj, i);
 980     }
 981 
 982     /**
 983      * Sets the value of a field as a {@code long} on the specified object.
 984      * This method is equivalent to
 985      * {@code set(obj, lObj)},
 986      * where {@code lObj} is a {@code Long} object and
 987      * {@code lObj.longValue() == l}.
 988      *
 989      * @param obj the object whose field should be modified
 990      * @param l   the new value for the field of {@code obj}
 991      * being modified
 992      *
 993      * @exception IllegalAccessException    if this {@code Field} object
 994      *              is enforcing Java language access control and the underlying
 995      *              field is either inaccessible or final.
 996      * @exception IllegalArgumentException  if the specified object is not an
 997      *              instance of the class or interface declaring the underlying
 998      *              field (or a subclass or implementor thereof),
 999      *              or if an unwrapping conversion fails.
1000      * @exception NullPointerException      if the specified object is null
1001      *              and the field is an instance field.
1002      * @exception ExceptionInInitializerError if the initialization provoked
1003      *              by this method fails.
1004      * @see       Field#set
1005      */
1006     @CallerSensitive
1007     @ForceInline // to ensure Reflection.getCallerClass optimization
1008     public void setLong(Object obj, long l)
1009         throws IllegalArgumentException, IllegalAccessException
1010     {
1011         ensureNotValueClass();
1012 
1013         if (!override) {
1014             Class<?> caller = Reflection.getCallerClass();
1015             checkAccess(caller, obj);
1016         }
1017         getFieldAccessor(obj).setLong(obj, l);
1018     }
1019 
1020     /**
1021      * Sets the value of a field as a {@code float} on the specified object.
1022      * This method is equivalent to
1023      * {@code set(obj, fObj)},
1024      * where {@code fObj} is a {@code Float} object and
1025      * {@code fObj.floatValue() == f}.
1026      *
1027      * @param obj the object whose field should be modified
1028      * @param f   the new value for the field of {@code obj}
1029      * being modified
1030      *
1031      * @exception IllegalAccessException    if this {@code Field} object
1032      *              is enforcing Java language access control and the underlying
1033      *              field is either inaccessible or final.
1034      * @exception IllegalArgumentException  if the specified object is not an
1035      *              instance of the class or interface declaring the underlying
1036      *              field (or a subclass or implementor thereof),
1037      *              or if an unwrapping conversion fails.
1038      * @exception NullPointerException      if the specified object is null
1039      *              and the field is an instance field.
1040      * @exception ExceptionInInitializerError if the initialization provoked
1041      *              by this method fails.
1042      * @see       Field#set
1043      */
1044     @CallerSensitive
1045     @ForceInline // to ensure Reflection.getCallerClass optimization
1046     public void setFloat(Object obj, float f)
1047         throws IllegalArgumentException, IllegalAccessException
1048     {
1049         ensureNotValueClass();
1050 
1051         if (!override) {
1052             Class<?> caller = Reflection.getCallerClass();
1053             checkAccess(caller, obj);
1054         }
1055         getFieldAccessor(obj).setFloat(obj, f);
1056     }
1057 
1058     /**
1059      * Sets the value of a field as a {@code double} on the specified object.
1060      * This method is equivalent to
1061      * {@code set(obj, dObj)},
1062      * where {@code dObj} is a {@code Double} object and
1063      * {@code dObj.doubleValue() == d}.
1064      *
1065      * @param obj the object whose field should be modified
1066      * @param d   the new value for the field of {@code obj}
1067      * being modified
1068      *
1069      * @exception IllegalAccessException    if this {@code Field} object
1070      *              is enforcing Java language access control and the underlying
1071      *              field is either inaccessible or final.
1072      * @exception IllegalArgumentException  if the specified object is not an
1073      *              instance of the class or interface declaring the underlying
1074      *              field (or a subclass or implementor thereof),
1075      *              or if an unwrapping conversion fails.
1076      * @exception NullPointerException      if the specified object is null
1077      *              and the field is an instance field.
1078      * @exception ExceptionInInitializerError if the initialization provoked
1079      *              by this method fails.
1080      * @see       Field#set
1081      */
1082     @CallerSensitive
1083     @ForceInline // to ensure Reflection.getCallerClass optimization
1084     public void setDouble(Object obj, double d)
1085         throws IllegalArgumentException, IllegalAccessException
1086     {
1087         ensureNotValueClass();
1088 
1089         if (!override) {
1090             Class<?> caller = Reflection.getCallerClass();
1091             checkAccess(caller, obj);
1092         }
1093         getFieldAccessor(obj).setDouble(obj, d);
1094     }
1095 
1096     // check access to field
1097     private void checkAccess(Class<?> caller, Object obj)
1098         throws IllegalAccessException
1099     {
1100         checkAccess(caller, clazz,
1101                     Modifier.isStatic(modifiers) ? null : obj.getClass(),
1102                     modifiers);
1103     }
1104 
1105     /*
1106      * Ensure the declaring class is not an inline class.
1107      */
1108     private void ensureNotValueClass() throws IllegalAccessException {
1109         if (clazz.isInlineClass()) {
1110             throw new IllegalAccessException("cannot set field \"" + this + "\" of inline class "
1111                 + clazz.getName());
1112         }
1113     }
1114 
1115     // security check is done before calling this method
1116     private FieldAccessor getFieldAccessor(Object obj)
1117         throws IllegalAccessException
1118     {
1119         boolean ov = override;
1120         FieldAccessor a = (ov) ? overrideFieldAccessor : fieldAccessor;
1121         return (a != null) ? a : acquireFieldAccessor(ov);
1122     }
1123 
1124     // NOTE that there is no synchronization used here. It is correct
1125     // (though not efficient) to generate more than one FieldAccessor
1126     // for a given Field. However, avoiding synchronization will
1127     // probably make the implementation more scalable.
1128     private FieldAccessor acquireFieldAccessor(boolean overrideFinalCheck) {
1129         // First check to see if one has been created yet, and take it
1130         // if so
1131         FieldAccessor tmp = null;
1132         if (root != null) tmp = root.getFieldAccessor(overrideFinalCheck);


< prev index next >