3737import static io .microsphere .reflect .AccessibleObjectUtils .trySetAccessible ;
3838import static io .microsphere .reflect .TypeUtils .isObjectClass ;
3939import static io .microsphere .text .FormatUtils .format ;
40- import static io .microsphere .util .Assert .assertNotNull ;
4140import static io .microsphere .util .ClassUtils .getAllInheritedTypes ;
4241import static io .microsphere .util .ObjectUtils .defaultIfNull ;
4342import static java .util .Collections .unmodifiableSet ;
@@ -767,17 +766,16 @@ public static <V> V getFieldValue(boolean forceAccess, @Nonnull Object instance,
767766 *
768767 * @param <V> The type of the field value
769768 * @param instance The object instance from which to retrieve the field value
770- * @param field The {@link Field} object representing the field to retrieve
769+ * @param field The {@link Field} object representing the field to retrieve (nullable)
771770 * @return The field value, or {@code null} if the field is {@code null}
772771 * @throws IllegalStateException if this {@code Field} object is enforcing Java language access control and the
773772 * underlying field is inaccessible
774773 * @throws IllegalArgumentException if the specified object is not an instance of the class or interface declaring
775- * the underlying field (or a subclass or implementor thereof), or if the field
776- * is <code>null</code>.
774+ * the underlying field (or a subclass or implementor thereof)
777775 * @throws NullPointerException if the specified object is null and the field is an instance field.
778776 */
779777 @ Nullable
780- public static <V > V getFieldValue (@ Nullable Object instance , @ Nonnull Field field ) throws IllegalStateException ,
778+ public static <V > V getFieldValue (@ Nullable Object instance , @ Nullable Field field ) throws IllegalStateException ,
781779 IllegalArgumentException , NullPointerException {
782780 return getFieldValue (false , instance , field );
783781 }
@@ -804,19 +802,21 @@ public static <V> V getFieldValue(@Nullable Object instance, @Nonnull Field fiel
804802 * @param <V> The type of the field value
805803 * @param forceAccess Whether to force reflective accessibility when reading the field
806804 * @param instance The object instance from which to retrieve the field value
807- * @param field The {@link Field} object representing the field to retrieve
805+ * @param field The {@link Field} object representing the field to retrieve (nullable)
808806 * @return The value of the field if found and accessible; otherwise, {@code null}
809807 * @throws IllegalStateException if this {@code Field} object is enforcing Java language access control and the
810808 * underlying field is inaccessible.
811809 * @throws IllegalArgumentException if the specified object is not an
812810 * instance of the class or interface declaring the underlying field (or a subclass
813- * or implementor thereof). or if the field is <code>null</code>.
811+ * or implementor thereof).
814812 * @throws NullPointerException if the specified object is null and the field is an instance field.
815813 */
816814 @ Nullable
817- public static <V > V getFieldValue (boolean forceAccess , @ Nullable Object instance , @ Nonnull Field field ) throws
815+ public static <V > V getFieldValue (boolean forceAccess , @ Nullable Object instance , @ Nullable Field field ) throws
818816 IllegalStateException , IllegalArgumentException , NullPointerException {
819- assertNotNull (field , () -> "The 'field' must not be null" );
817+ if (field == null ) {
818+ return null ;
819+ }
820820
821821 V fieldValue = null ;
822822 RuntimeException failure = null ;
@@ -830,7 +830,7 @@ public static <V> V getFieldValue(boolean forceAccess, @Nullable Object instance
830830 failure = new IllegalStateException (e );
831831 } finally {
832832 if (logger .isTraceEnabled ()) {
833- logger .trace ("The value of field[signature : '{}' , forceAccess : {} , trySetAccessible : {} , instance : {}] : {}" ,
833+ logger .trace ("Got the value of field[signature : '{}' , forceAccess : {} , trySetAccessible : {} , instance : {}] : {}" ,
834834 field , forceAccess , trySetAccessible , instance , fieldValue , failure );
835835 }
836836 }
@@ -971,7 +971,7 @@ public static <V> V setFieldValue(boolean forceAccess, @Nullable Object instance
971971 * @throws NullPointerException if the specified object is null and the field is an instance field.
972972 */
973973 @ Nullable
974- public static <V > V setFieldValue (@ Nullable Object instance , @ Nonnull Field field , @ Nullable V value ) throws IllegalStateException , IllegalArgumentException {
974+ public static <V > V setFieldValue (@ Nullable Object instance , @ Nullable Field field , @ Nullable V value ) throws IllegalStateException , IllegalArgumentException {
975975 return setFieldValue (false , instance , field , value );
976976 }
977977
@@ -1009,22 +1009,34 @@ public static <V> V setFieldValue(@Nullable Object instance, @Nonnull Field fiel
10091009 * @throws NullPointerException if the specified object is null and the field is an instance field
10101010 */
10111011 @ Nullable
1012- public static <V > V setFieldValue (boolean forceAccess , @ Nullable Object instance , @ Nonnull Field field , @ Nullable V value )
1012+ public static <V > V setFieldValue (boolean forceAccess , @ Nullable Object instance , @ Nullable Field field , @ Nullable V value )
10131013 throws IllegalStateException , IllegalArgumentException , NullPointerException {
1014- assertNotNull (field , () -> "The 'field' must not be null" );
1014+ if (field == null ) {
1015+ return null ;
1016+ }
10151017
10161018 V previousValue = null ;
1019+ RuntimeException failure = null ;
1020+ boolean trySetAccessible = false ;
10171021 try {
10181022 if (forceAccess ) {
1019- trySetAccessible (field );
1023+ trySetAccessible = trySetAccessible (field );
10201024 }
10211025 previousValue = (V ) field .get (instance );
10221026 if (!Objects .equals (previousValue , value )) {
10231027 field .set (instance , value );
10241028 }
10251029 } catch (IllegalAccessException | IllegalArgumentException e ) {
1026- handleFieldException (e , instance , field );
1027- throw new IllegalArgumentException (e );
1030+ failure = new IllegalArgumentException (e );
1031+ } finally {
1032+ if (logger .isTraceEnabled ()) {
1033+ logger .trace ("Set the value of field[signature : '{}' , forceAccess : {} , trySetAccessible : {} , instance : {}] : {} -> {}" ,
1034+ field , forceAccess , trySetAccessible , instance , previousValue , value , failure );
1035+ }
1036+ }
1037+
1038+ if (failure != null ) {
1039+ throw failure ;
10281040 }
10291041
10301042 return previousValue ;
@@ -1073,12 +1085,6 @@ public static void assertFieldMatchType(Object instance, String fieldName, Class
10731085 }
10741086 }
10751087
1076- static void handleFieldException (Exception e , Object instance , Field field ) {
1077- if (logger .isTraceEnabled ()) {
1078- logger .trace ("The instance[object : {}] can't match the field[{}]" , instance , field , e );
1079- }
1080- }
1081-
10821088 private FieldUtils () {
10831089 }
10841090}
0 commit comments