aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/utils/junit/src/main/java/org/onlab/junit/TestUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'framework/src/onos/utils/junit/src/main/java/org/onlab/junit/TestUtils.java')
-rw-r--r--framework/src/onos/utils/junit/src/main/java/org/onlab/junit/TestUtils.java37
1 files changed, 25 insertions, 12 deletions
diff --git a/framework/src/onos/utils/junit/src/main/java/org/onlab/junit/TestUtils.java b/framework/src/onos/utils/junit/src/main/java/org/onlab/junit/TestUtils.java
index 1afc4948..686a9a59 100644
--- a/framework/src/onos/utils/junit/src/main/java/org/onlab/junit/TestUtils.java
+++ b/framework/src/onos/utils/junit/src/main/java/org/onlab/junit/TestUtils.java
@@ -64,27 +64,40 @@ public final class TestUtils {
/**
* Gets the field, bypassing scope restriction.
*
- * @param subject Object where the field belongs
+ * @param subject Object where the field belongs
* @param fieldName name of the field to get
+ * @param <T> subject type
+ * @param <U> fieldO value type
* @return value of the field.
- * @param <T> subject type
- * @param <U> field value type
* @throws TestUtilsException if there are reflection errors while getting
- * the field
+ * the field
*/
public static <T, U> U getField(T subject, String fieldName)
throws TestUtilsException {
try {
+ NoSuchFieldException exception = null;
@SuppressWarnings("unchecked")
- Class<T> clazz = (Class<T>) subject.getClass();
- Field field = clazz.getDeclaredField(fieldName);
- field.setAccessible(true);
+ Class clazz = subject.getClass();
+ while (clazz != null) {
+ try {
+ Field field = clazz.getDeclaredField(fieldName);
+ field.setAccessible(true);
- @SuppressWarnings("unchecked")
- U result = (U) field.get(subject);
- return result;
- } catch (NoSuchFieldException | SecurityException |
- IllegalArgumentException | IllegalAccessException e) {
+ @SuppressWarnings("unchecked")
+ U result = (U) field.get(subject);
+ return result;
+ } catch (NoSuchFieldException e) {
+ exception = e;
+ if (clazz == clazz.getSuperclass()) {
+ break;
+ }
+ clazz = clazz.getSuperclass();
+ }
+ }
+ throw new TestUtilsException("Field not found. " + fieldName, exception);
+
+ } catch (SecurityException |
+ IllegalArgumentException | IllegalAccessException e) {
throw new TestUtilsException("getField failed", e);
}
}