Skip to content

Commit

Permalink
In FieldSearchHelperб skip protected or private fields that cannot …
Browse files Browse the repository at this point in the history
…be read by `Unsafe` (#367)


---------

Signed-off-by: Evgeniy Moiseenko <[email protected]>
  • Loading branch information
eupp authored Sep 2, 2024
1 parent d852db6 commit 4175292
Showing 1 changed file with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ internal object FieldSearchHelper {
// so there is no problem that we can receive some fields of the same name and the same type.
for (field in testObject::class.java.allDeclaredFieldWithSuperclasses) {
if (field.type.isPrimitive) continue
val fieldValue = readFieldViaUnsafe(testObject, field, Unsafe::getObject)

// We wrap an unsafe read into `runCatching` to hande `UnsupportedOperationException`,
// which can be thrown, for instance, when attempting to read a field of
// a hidden class (starting from Java 15).
val fieldValue = runCatching {
readFieldViaUnsafe(testObject, field, Unsafe::getObject)
}.getOrNull() ?: continue

if (fieldValue in visitedObjects) continue
visitedObjects += testObject
Expand Down

0 comments on commit 4175292

Please sign in to comment.