From 4e0e592c7dfd4499d4f50aba3ea54caa1fcc9871 Mon Sep 17 00:00:00 2001 From: amckenzie Date: Thu, 13 Dec 2018 09:08:23 +0000 Subject: [PATCH] Make ReflectionUtil handle fields in super classes --- .../utils/core/reflection/ReflectionUtil.java | 37 ++++++++++++++----- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/test-utils-core/src/main/java/uk/gov/justice/services/test/utils/core/reflection/ReflectionUtil.java b/test-utils-core/src/main/java/uk/gov/justice/services/test/utils/core/reflection/ReflectionUtil.java index c6682f4..964dfd1 100644 --- a/test-utils-core/src/main/java/uk/gov/justice/services/test/utils/core/reflection/ReflectionUtil.java +++ b/test-utils-core/src/main/java/uk/gov/justice/services/test/utils/core/reflection/ReflectionUtil.java @@ -75,11 +75,11 @@ public static Optional annotatedMethod(final Class classWithMethod, f */ public static void setField(final Object object, final String fieldName, final Object fieldValue) { + final Field field = getField(object.getClass(), fieldName); try { - final Field field = getField(object.getClass(), fieldName); field.setAccessible(true); field.set(object, fieldValue); - } catch (final IllegalAccessException e) { + } catch (IllegalAccessException e) { throw new ReflectionException(format("Unable to access field '%s' on class %s", fieldName, object.getClass().getName()), e); } } @@ -87,15 +87,23 @@ public static void setField(final Object object, final String fieldName, final O /** * Searches for a field in the given class by reflection * - * @param classWithField - class type - * @param fieldName - name of field in class + * @param aClass - class type + * @param fieldName - name of field in class * @return - field belonging to the given classWithField with the given fieldName */ - public static Field getField(final Class classWithField, final String fieldName) { - return stream(classWithField.getDeclaredFields()) - .filter(f -> f.getName().equals(fieldName)) - .findFirst() - .orElseThrow(() -> new ReflectionException(format("No field named '%s' found on class %s", fieldName, classWithField.getName()))); + @SuppressWarnings("CatchMayIgnoreException") + public static Field getField(final Class aClass, final String fieldName) { + + for (Class currentClass = aClass; currentClass != Object.class; currentClass = currentClass.getSuperclass()) { + if (hasField(fieldName, currentClass)) { + try { + return currentClass.getDeclaredField(fieldName); + } catch (final NoSuchFieldException thisWillNeverHappen) { + } + } + } + + throw new ReflectionException(format("No field named '%s' found on class %s", fieldName, aClass.getName())); } /** @@ -120,4 +128,15 @@ public static T getValueOfField( throw new ReflectionException(format("Unable to access field '%s' on class %s", fieldName, object.getClass().getName()), e); } } + + private static boolean hasField(final String fieldName, final Class aClass) { + final Field[] declaredFields = aClass.getDeclaredFields(); + + if (declaredFields == null) { + return false; + } + + return stream(declaredFields) + .anyMatch(field -> field.getName().equals(fieldName)); + } }