aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/utils/junit
diff options
context:
space:
mode:
authorAshlee Young <ashlee@onosfw.com>2015-10-09 18:32:44 -0700
committerAshlee Young <ashlee@onosfw.com>2015-10-09 18:32:44 -0700
commit6a07d2d622eaa06953f3353e39c080984076e8de (patch)
treebfb50a2090fce186c2cc545a400c969bf2ea702b /framework/src/onos/utils/junit
parente6d71622143ff9b2421a1abbe8434b954b5b1099 (diff)
Updated master to commit id 6ee8aa3e67ce89908a8c93aa9445c6f71a18f986
Change-Id: I94b055ee2f298daf71e2ec794fd0f2495bd8081f
Diffstat (limited to 'framework/src/onos/utils/junit')
-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);
}
}