From 358503c75ebddfb6a5a63c284d12457a8e3de086 Mon Sep 17 00:00:00 2001 From: John DeRegnaucourt Date: Mon, 9 Oct 2023 09:55:37 -0400 Subject: [PATCH] Only get field when it is accessible --- .../cedarsoftware/util/ReflectionUtils.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/cedarsoftware/util/ReflectionUtils.java b/src/main/java/com/cedarsoftware/util/ReflectionUtils.java index 5493fe09..a2ce9f39 100644 --- a/src/main/java/com/cedarsoftware/util/ReflectionUtils.java +++ b/src/main/java/com/cedarsoftware/util/ReflectionUtils.java @@ -200,18 +200,15 @@ public static void getDeclaredFields(Class c, Collection fields) { for (Field field : local) { - try + if (field.trySetAccessible()) { - field.setAccessible(true); - } - catch (Exception ignored) { } - - int modifiers = field.getModifiers(); - if (!Modifier.isStatic(modifiers) && - !field.getName().startsWith("this$") && - !Modifier.isTransient(modifiers)) - { // speed up: do not count static fields, do not go back up to enclosing object in nested case, do not consider transients - fields.add(field); + int modifiers = field.getModifiers(); + if (!Modifier.isStatic(modifiers) && + !field.getName().startsWith("this$") && + !Modifier.isTransient(modifiers)) + { // speed up: do not count static fields, do not go back up to enclosing object in nested case, do not consider transients + fields.add(field); + } } } }