@@ -160,7 +160,7 @@ public static void write(Object destination, PropertyDescriptor propertyDescript
160
160
} else {
161
161
Object [] args = new Object [] { value };
162
162
Method writeMethod = propertyDescriptor .getWriteMethod ();
163
- withAccessibleObject (writeMethod , method -> method .invoke (destination , args ), force );
163
+ withAccessibleObject (destination , writeMethod , ( dest , method ) -> method .invoke (dest , args ), force );
164
164
}
165
165
} catch (ReflectiveOperationException | RuntimeException e ) {
166
166
throw new ReflectionRuntimeException ("Failed to write " + getQualifiedPropertyName (destination , propertyDescriptor ), e );
@@ -183,7 +183,7 @@ public static void writeDirectly(Object destination, String propertyName, Object
183
183
public static void writeDirectly (Object destination , Field field , Object value ) {
184
184
Assert .notNull (destination , () -> "Destination must not be null" );
185
185
try {
186
- withAccessibleObject (field , f -> f .set (destination , value ));
186
+ withAccessibleObject (destination , field , ( obj , f ) -> f .set (obj , value ));
187
187
} catch (ReflectiveOperationException e ) {
188
188
throw new ReflectionRuntimeException ("Failed to write " + getQualifiedPropertyName (destination , field ), e );
189
189
}
@@ -216,9 +216,9 @@ public static <T> T readDirectly(Object object, String propertyName) {
216
216
217
217
public static <T > T readDirectly (Object object , Field field ) {
218
218
try {
219
- return withAccessibleObject (field , f -> {
219
+ return withAccessibleObject (object , field , ( obj , f ) -> {
220
220
@ SuppressWarnings ("unchecked" )
221
- T value = (T ) field .get (object );
221
+ T value = (T ) field .get (obj );
222
222
return value ;
223
223
}, true );
224
224
} catch (ReflectiveOperationException e ) {
@@ -241,7 +241,7 @@ public static <T> T read(Object source, PropertyDescriptor propertyDescriptor, b
241
241
}
242
242
} else {
243
243
Method readMethod = propertyDescriptor .getReadMethod ();
244
- result = withAccessibleObject (readMethod , method -> readMethod .invoke (source ), force );
244
+ result = withAccessibleObject (source , readMethod , ( src , method ) -> method .invoke (src ), force );
245
245
}
246
246
} catch (ReflectiveOperationException | RuntimeException e ) {
247
247
throw new ReflectionRuntimeException ("Failed to read " + getQualifiedPropertyName (source , propertyDescriptor ), e );
@@ -449,28 +449,28 @@ public static boolean isNotCollectionType(PropertyDescriptor propertyDescriptor)
449
449
return !isCollectionType (propertyDescriptor );
450
450
}
451
451
452
- private interface AccessibleObjectFunction <T extends AccessibleObject , R > {
453
- R access (T object ) throws ReflectiveOperationException ;
452
+ private interface AccessibleObjectFunction <E , T extends AccessibleObject , R > {
453
+ R access (E object , T accessibleObject ) throws ReflectiveOperationException ;
454
454
}
455
455
456
- private interface AccessibleObjectConsumer <T extends AccessibleObject > {
457
- void access (T object ) throws ReflectiveOperationException ;
456
+ private interface AccessibleObjectConsumer <E , T extends AccessibleObject > {
457
+ void access (E object , T accessibleObject ) throws ReflectiveOperationException ;
458
458
}
459
459
460
- private static <T extends AccessibleObject > void withAccessibleObject (T accessibleObject , AccessibleObjectConsumer <T > accessibleObjectConsumer ) throws ReflectiveOperationException {
461
- withAccessibleObject (accessibleObject , obj -> {
462
- accessibleObjectConsumer .access (obj );
460
+ private static <E , T extends AccessibleObject > void withAccessibleObject (E object , T accessibleObject , AccessibleObjectConsumer <E , T > accessibleObjectConsumer ) throws ReflectiveOperationException {
461
+ withAccessibleObject (object , accessibleObject , ( obj , accObj ) -> {
462
+ accessibleObjectConsumer .access (obj , accObj );
463
463
return null ;
464
464
}, true );
465
465
}
466
466
467
- private static <T extends AccessibleObject , R > R withAccessibleObject (T accessibleObject , AccessibleObjectFunction <T , R > function , boolean force ) throws ReflectiveOperationException {
468
- boolean accessible = accessibleObject .isAccessible ( );
467
+ private static <E , T extends AccessibleObject , R > R withAccessibleObject (E object , T accessibleObject , AccessibleObjectFunction <E , T , R > function , boolean force ) throws ReflectiveOperationException {
468
+ boolean accessible = accessibleObject .canAccess ( object );
469
469
try {
470
470
if (force && !accessible ) {
471
471
accessibleObject .setAccessible (true );
472
472
}
473
- return function .access (accessibleObject );
473
+ return function .access (object , accessibleObject );
474
474
} finally {
475
475
if (force && !accessible ) {
476
476
accessibleObject .setAccessible (false );
0 commit comments